mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-16 01:26:16 +00:00
Merge remote-tracking branch 'upstream/main' into aep-audit-f
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 }}
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
Generated
+230
-122
@@ -1089,15 +1089,24 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-audio"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4)",
|
||||
"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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
|
||||
[[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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -1107,12 +1116,12 @@ dependencies = [
|
||||
[[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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
|
||||
[[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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"fixed_decimal",
|
||||
"icu_decimal",
|
||||
@@ -1132,6 +1141,14 @@ dependencies = [
|
||||
"writeable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-media"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-media"
|
||||
version = "0.1.0"
|
||||
@@ -1143,7 +1160,7 @@ dependencies = [
|
||||
[[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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"hayro-syntax",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1152,7 +1169,7 @@ dependencies = [
|
||||
[[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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"dispatch2",
|
||||
"egui",
|
||||
@@ -1163,7 +1180,7 @@ dependencies = [
|
||||
[[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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"craft-fonts-build",
|
||||
"craft-i18n",
|
||||
@@ -1184,7 +1201,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-update"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"dunce",
|
||||
@@ -1194,6 +1211,7 @@ dependencies = [
|
||||
"image",
|
||||
"log",
|
||||
"plist",
|
||||
"rustix 1.1.5",
|
||||
"semver",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -1207,6 +1225,16 @@ dependencies = [
|
||||
"zip 9.1.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-video"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4)",
|
||||
"rayon",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.5.2"
|
||||
@@ -1420,8 +1448,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"cpal",
|
||||
"craft-update",
|
||||
"effectcraft-engine",
|
||||
@@ -1439,6 +1468,7 @@ dependencies = [
|
||||
"rfd",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sysinfo",
|
||||
"winit",
|
||||
"winresource",
|
||||
"zbus",
|
||||
@@ -1446,7 +1476,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-aep"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-effects",
|
||||
@@ -1461,7 +1491,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-automation"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-engine",
|
||||
"effectcraft-host",
|
||||
@@ -1473,16 +1503,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",
|
||||
@@ -1501,7 +1524,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-color"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -1509,7 +1532,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-effects"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"effectcraft-color",
|
||||
@@ -1527,7 +1550,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-engine"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-docpath",
|
||||
@@ -1566,12 +1589,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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4)",
|
||||
"craft-video",
|
||||
"effectcraft-color",
|
||||
"effectcraft-effects",
|
||||
"effectcraft-hevcenc",
|
||||
"effectcraft-keyframe",
|
||||
"effectcraft-media",
|
||||
"effectcraft-opusenc",
|
||||
@@ -1579,25 +1603,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",
|
||||
@@ -1614,7 +1635,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-geom"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -1622,7 +1643,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-gpu"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"effectcraft-color",
|
||||
@@ -1645,7 +1666,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-helper-host"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"effectcraft-effects",
|
||||
@@ -1654,16 +1675,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",
|
||||
@@ -1690,7 +1704,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-interchange"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-keyframe",
|
||||
@@ -1709,7 +1723,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-keyframe"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-geom",
|
||||
@@ -1721,7 +1735,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-lottie"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-docpath",
|
||||
"effectcraft-color",
|
||||
@@ -1739,9 +1753,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-media"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-audio",
|
||||
"craft-docpath",
|
||||
"craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4)",
|
||||
"effectcraft-color",
|
||||
"effectcraft-effects",
|
||||
"effectcraft-model",
|
||||
@@ -1753,12 +1769,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",
|
||||
@@ -1767,12 +1780,13 @@ dependencies = [
|
||||
"log",
|
||||
"rayon",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-model"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-geom",
|
||||
"image",
|
||||
@@ -1785,14 +1799,14 @@ dependencies = [
|
||||
|
||||
[[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",
|
||||
@@ -1804,7 +1818,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-pdf"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-pdf",
|
||||
"effectcraft-raster",
|
||||
@@ -1820,7 +1834,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-plugin"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-effects",
|
||||
"effectcraft-keyframe",
|
||||
@@ -1832,7 +1846,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-project"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"craft-ecproj-format",
|
||||
"effectcraft-color",
|
||||
@@ -1846,14 +1860,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-psd"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"miniz_oxide 0.9.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-raster"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-geom",
|
||||
@@ -1863,7 +1877,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-render"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-color",
|
||||
"effectcraft-effects",
|
||||
@@ -1886,7 +1900,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-sam2"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"candle-core",
|
||||
@@ -1901,7 +1915,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-script"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"boa_engine",
|
||||
"effectcraft-engine",
|
||||
@@ -1919,7 +1933,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-segment"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"miniz_oxide 0.9.1",
|
||||
"ndarray",
|
||||
@@ -1928,19 +1942,20 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-svg"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"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",
|
||||
@@ -1952,7 +1967,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-time"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"proptest",
|
||||
"serde",
|
||||
@@ -1960,7 +1975,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-track"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"effectcraft-raster",
|
||||
"effectcraft-segment",
|
||||
@@ -1972,7 +1987,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "effectcraft-ui-egui"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"chrono",
|
||||
@@ -2011,13 +2026,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",
|
||||
@@ -2482,7 +2493,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]]
|
||||
@@ -2583,16 +2594,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"
|
||||
@@ -2625,7 +2626,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]]
|
||||
@@ -2633,7 +2634,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]]
|
||||
@@ -3069,7 +3070,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]]
|
||||
@@ -3322,7 +3323,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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"hayro-ccitt",
|
||||
]
|
||||
@@ -3336,7 +3337,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=e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4#e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4"
|
||||
dependencies = [
|
||||
"hayro-jbig2",
|
||||
"hayro-jpeg2000",
|
||||
@@ -3669,7 +3670,7 @@ dependencies = [
|
||||
"simd_cesu8",
|
||||
"thiserror 2.0.21",
|
||||
"walkdir",
|
||||
"windows-link",
|
||||
"windows-link 0.2.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3815,7 +3816,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"windows-link",
|
||||
"windows-link 0.2.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4121,6 +4122,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"
|
||||
@@ -4435,6 +4445,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"
|
||||
@@ -4665,7 +4685,7 @@ dependencies = [
|
||||
"libc",
|
||||
"redox_syscall 0.5.18",
|
||||
"smallvec",
|
||||
"windows-link",
|
||||
"windows-link 0.2.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -6072,6 +6092,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"
|
||||
@@ -7225,16 +7259,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]]
|
||||
@@ -7256,6 +7312,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"
|
||||
@@ -7264,9 +7333,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]]
|
||||
@@ -7276,8 +7356,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]]
|
||||
@@ -7302,12 +7382,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"
|
||||
@@ -7315,7 +7411,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]]
|
||||
@@ -7324,9 +7420,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]]
|
||||
@@ -7338,13 +7434,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]]
|
||||
@@ -7353,7 +7467,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]]
|
||||
@@ -7389,7 +7503,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]]
|
||||
@@ -7423,13 +7537,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]]
|
||||
@@ -7712,7 +7835,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
|
||||
|
||||
[[package]]
|
||||
name = "xtask"
|
||||
version = "0.21.0"
|
||||
version = "0.24.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"pkg-config",
|
||||
@@ -8038,18 +8161,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"
|
||||
|
||||
+24
-46
@@ -4,19 +4,19 @@ 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 = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4", version = "=0.1.0", default-features = false, features = ["read"] }
|
||||
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4" }
|
||||
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4" }
|
||||
craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4" }
|
||||
# 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 = "1b34eba41e62138fc2438d2734523b302840b45d", features = ["egui"] }
|
||||
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4", features = ["egui"] }
|
||||
# internal
|
||||
effectcraft-time = { path = "crates/time" }
|
||||
effectcraft-geom = { path = "crates/geom" }
|
||||
@@ -41,7 +41,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 +48,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 +79,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 = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4", default-features = false, features = ["isobmff", "matroska"] }
|
||||
craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4", default-features = false, features = ["aac-decode", "aac-encode", "opus-decode"] }
|
||||
craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4", 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 +112,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 = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4", 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 +132,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 +190,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 = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4", version = "=0.7.2" }
|
||||
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "e7c3dda7f3e339dd2c8d40fc4eb405472be1a1e4", 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 +233,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" }
|
||||
|
||||
@@ -156,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,
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
//! 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)
|
||||
@@ -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 <info|commands|exec|run|props|get|set|render-frame|render|script|mcp> [args] [--json]
|
||||
const USAGE: &str = "usage: effectcraft-cli <info|commands|exec|run|props|get|set|render-frame|render|script|replay|mcp> [args] [--json]
|
||||
info project + engine summary
|
||||
commands [--filter TEXT] [--enabled] list engine commands
|
||||
exec <command-id> [--params JSON] run one engine command (exec --list: list them, as `commands`)
|
||||
@@ -138,6 +139,9 @@ const USAGE: &str = "usage: effectcraft-cli <info|commands|exec|run|props|get|se
|
||||
script FILE.jsx [F.ecproj] | --eval CODE run JavaScript with the After Effects-style object model
|
||||
(app.project, comps, layers, properties…); prints writeLn
|
||||
output and the result; errors exit 1 with file:line:col
|
||||
replay SCRIPT.jsonl [--keep-going] replay a repro script headless (journal.export, crash repros):
|
||||
per-step ok/error and the first failure; exits 1 when a step
|
||||
fails, an expectation is not met or a command panics
|
||||
mcp [--autosave | --bridge PORT] MCP server (JSON-RPC over stdio); --autosave preserves unsaved headless work
|
||||
[--automation-read-root DIR] [--automation-write-root DIR]
|
||||
confine the agent's file access: reads under a read or write root,
|
||||
@@ -222,6 +226,7 @@ const FLAGS: &[&str] = &[
|
||||
"--small",
|
||||
"--adv3d",
|
||||
"--autosave",
|
||||
"--keep-going",
|
||||
"--progress",
|
||||
];
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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::<chrono::Utc>::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<u64>,
|
||||
total: u64,
|
||||
last_at: Option<String>,
|
||||
last_uptime_secs: Option<u64>,
|
||||
}
|
||||
|
||||
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<u64>,
|
||||
started: Instant,
|
||||
started_at: String,
|
||||
detected_at: Option<String>,
|
||||
last_sample: Instant,
|
||||
memory: Memory,
|
||||
// Only Windows can retrieve an unrelated process's ExitStatus through a retained handle.
|
||||
exit: Receiver<Option<std::process::ExitStatus>>,
|
||||
status: Option<std::process::ExitStatus>,
|
||||
alive: Option<bool>,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,797 @@
|
||||
//! 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<std::sync::Mutex<Option<Reporter>>>);
|
||||
|
||||
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<Reporter> {
|
||||
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<Reporter> {
|
||||
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::<u32>().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<Message> {
|
||||
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<bool> {
|
||||
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<String>,
|
||||
) -> io::Result<PathBuf> {
|
||||
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<PathBuf>, 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<PathBuf> {
|
||||
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 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 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 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 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 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 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));
|
||||
}
|
||||
}
|
||||
@@ -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<AtomicU8>,
|
||||
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"]);
|
||||
}
|
||||
}
|
||||
@@ -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<File> {
|
||||
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<Backends>,
|
||||
pub source: BackendSource,
|
||||
/// Switched to OpenGL because the previous launch stopped as its window opened: the message
|
||||
/// to show.
|
||||
pub notice: Option<String>,
|
||||
marker: Option<PathBuf>,
|
||||
/// 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<File>,
|
||||
/// When the window drew its first frame, until the launch settles.
|
||||
first_frame: Option<Instant>,
|
||||
@@ -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();
|
||||
@@ -142,6 +220,7 @@ mod tests {
|
||||
// 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);
|
||||
}
|
||||
@@ -217,4 +300,232 @@ mod tests {
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A fresh settings folder for one test.
|
||||
fn fresh_dir(case: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("ec-launch-guard-{case}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// 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 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 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 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 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 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() {
|
||||
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 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 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() {
|
||||
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 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 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -180,6 +182,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::<Vec<_>>()) {
|
||||
@@ -205,20 +224,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 +282,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 +323,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 +454,7 @@ fn main() -> eframe::Result {
|
||||
window: window::Tracker::new(opened, saved_window, control_port.is_none()),
|
||||
launch,
|
||||
drawn: false,
|
||||
reporter: desktop_reporter,
|
||||
}))
|
||||
}),
|
||||
);
|
||||
@@ -491,6 +548,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 +583,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) {
|
||||
|
||||
@@ -49,7 +49,7 @@ pub use effects::{DefinitionSource, Effect, EffectDefinitions, EffectParam, Laye
|
||||
#[cfg(test)]
|
||||
pub(crate) use effects::Pard;
|
||||
pub use error::AepError;
|
||||
pub use footage::{Footage, SequenceInfo};
|
||||
pub use footage::{Footage, PsdLayer, SequenceInfo};
|
||||
pub use header::{BlendMode, CompHeader, LayerHeader, LayerType, LightType, Quality, Rational, Renderer};
|
||||
pub use index::{Node, NodeWalk, Nodes, TreeIndex};
|
||||
pub use keys::{ExpressionFlags, Interpolation, KeyEase, KeyframeKind, SpatialKey};
|
||||
|
||||
@@ -38,6 +38,19 @@ pub enum Conv {
|
||||
Skip,
|
||||
}
|
||||
|
||||
impl Conv {
|
||||
/// For a number (`Percent`, `Plain`, `Factor`): what a stored value is multiplied by to give
|
||||
/// the value here. `None` for every other conversion.
|
||||
pub fn number_scale(self) -> Option<f64> {
|
||||
match self {
|
||||
Conv::Percent { valid_max } => Some(100.0 / valid_max),
|
||||
Conv::Plain { .. } => Some(1.0),
|
||||
Conv::Factor { factor, .. } => Some(factor),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// After Effects' popup items as the corpus stores them (`pdnm`).
|
||||
/// 368.
|
||||
pub const RAMP_SHAPE: &[&str] = &["Linear Ramp", "Radial Ramp"];
|
||||
@@ -1142,8 +1155,12 @@ pub const GLOW_MIDPOINT_MAX: f64 = 0.990_005_493_164_062_5;
|
||||
|
||||
/// The key a popup item is matched by between the two programs: trimmed, lower case, and
|
||||
/// Set Matte's "Red Channel" ... "Alpha Channel" are EffectCraft's "Red" ... "Alpha" (904 E24).
|
||||
///
|
||||
/// Lower case is Unicode's, not only ASCII's: After Effects stores popup items in its UI
|
||||
/// language, so an item can start with a capital outside ASCII (French "Écran"), and its key must
|
||||
/// be the same as for the same word in lower case.
|
||||
pub fn popup_key(item: &str) -> String {
|
||||
popup_stem(item).to_ascii_lowercase()
|
||||
popup_stem(item).chars().flat_map(char::to_lowercase).collect()
|
||||
}
|
||||
|
||||
/// [`popup_key`] before the lower-casing, borrowed: `item` trimmed, without a final ` channel`
|
||||
@@ -1161,7 +1178,29 @@ const SUFFIX: &[u8] = b" channel";
|
||||
|
||||
/// Whether two popup items have the same [`popup_key`], without building either key.
|
||||
pub(crate) fn popup_keys_match(a: &str, b: &str) -> bool {
|
||||
popup_stem(a).eq_ignore_ascii_case(popup_stem(b))
|
||||
popup_stem(a).chars().flat_map(char::to_lowercase).eq(popup_stem(b).chars().flat_map(char::to_lowercase))
|
||||
}
|
||||
|
||||
/// The English [`popup_key`] a non-English After Effects popup item name stands for, when this
|
||||
/// table knows it; `None` otherwise. EffectCraft's own popup options are English only, so a
|
||||
/// project saved by an After Effects set to another UI language never matches them by name and
|
||||
/// the parameter falls back to its default (observed on a real "Niveaux" project, French After
|
||||
/// Effects 2026 26.0.0x67: Levels' Channel and Clip to Output Black / White). Add an entry only
|
||||
/// from a confirmed After Effects label, never a guess at a whole menu's translation.
|
||||
pub fn popup_alias(key: &str) -> Option<&'static str> {
|
||||
Some(match key {
|
||||
// French: Levels' Channel popup (`ADBE Easy Levels2-0001`). "Alpha" is the same word in
|
||||
// both languages, so it needs no entry.
|
||||
"rvb" => "rgb",
|
||||
"rouge" => "red",
|
||||
"vert" => "green",
|
||||
"bleu" => "blue",
|
||||
// French: Levels' Clip to Output Black / White popups (`-0008`, `-0009`).
|
||||
"activé" => "on",
|
||||
"désactivé" => "off",
|
||||
"désactivé pour la couleur 32 bpc" => "off for 32 bpc color",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// The table row of an After Effects match name.
|
||||
@@ -1188,7 +1227,7 @@ mod tests {
|
||||
|
||||
/// The key as it was built before the borrowed form (the reference for the differential test).
|
||||
fn old_key(item: &str) -> String {
|
||||
let s = item.trim().to_ascii_lowercase();
|
||||
let s = item.trim().to_lowercase();
|
||||
s.strip_suffix(" channel").map(str::to_string).unwrap_or(s)
|
||||
}
|
||||
|
||||
@@ -1206,12 +1245,14 @@ mod tests {
|
||||
" channel",
|
||||
"x channel",
|
||||
"Xchannel",
|
||||
"Ä channel",
|
||||
"Grün Channel",
|
||||
"é",
|
||||
"Ünit channel",
|
||||
"\u{c4} channel",
|
||||
"Gr\u{fc}n Channel",
|
||||
"\u{e9}",
|
||||
"\u{dc}nit channel",
|
||||
"a channel channel",
|
||||
"Luminance",
|
||||
"\u{c9}CRAN",
|
||||
"\u{e9}cran",
|
||||
];
|
||||
for a in items {
|
||||
assert_eq!(popup_key(a), old_key(a), "{a:?}");
|
||||
@@ -1220,4 +1261,13 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A capital outside ASCII has the same key as its lower case; ASCII keys are unchanged.
|
||||
#[test]
|
||||
fn popup_key_lowers_non_ascii_capitals() {
|
||||
assert_eq!(popup_key("\u{c9}CRAN"), popup_key("\u{e9}cran"));
|
||||
assert_eq!(popup_key(" D\u{e9}sactiv\u{e9} "), "d\u{e9}sactiv\u{e9}");
|
||||
assert_eq!(popup_key("Off for 32 bpc Color"), "off for 32 bpc color");
|
||||
assert_eq!(popup_key("Red Channel"), "red");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Item ids and labels, footage items (solids and files), precomp linkage, and the name and size
|
||||
//! of a layer's source.
|
||||
|
||||
use crate::container::bytes::{be_f32, be_f64, be_u32, cstr, le_u32, text};
|
||||
use crate::container::bytes::{be_f32, be_f64, be_i32, be_u32, cstr, le_u32, text, until_nul};
|
||||
use crate::container::header::Rational;
|
||||
use crate::container::index::Node;
|
||||
use crate::container::typed::{Item, ItemKind, Layer, Project};
|
||||
@@ -45,6 +45,59 @@ pub struct Footage {
|
||||
/// True when the alias points at a folder (`"target_is_folder":true`): an image sequence is ONE
|
||||
/// item whose alias is its folder and whose `opti` holds the first file name.
|
||||
pub is_sequence: bool,
|
||||
/// The one layer of a Photoshop file the item shows; `None` for the merged image and for
|
||||
/// any other footage.
|
||||
pub psd_layer: Option<PsdLayer>,
|
||||
}
|
||||
|
||||
/// One layer of a Photoshop file shown by a footage item, from its `opti` (tag `8BPS`,
|
||||
/// docs/aep-format.md 3.3): the layer record's index as a big-endian `i32` at byte 14 (-1 for the
|
||||
/// merged image), the document's sides as little-endian `u32` at 32 and 36, its bounds top, left,
|
||||
/// bottom, right as little-endian `i32` at 78, 82, 86 and 90, and its name (UTF-8, NUL
|
||||
/// terminated) from byte 344.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PsdLayer {
|
||||
/// The layer record's position in the file (bottom of the stack = 0).
|
||||
pub index: u32,
|
||||
pub name: String,
|
||||
/// Top, left, bottom, right in document pixels; all 0 for a layer without pixels (an
|
||||
/// adjustment layer).
|
||||
pub bounds: [i32; 4],
|
||||
/// The document's two sides (bytes 32 and 36; which one is the width is not sampled: the one
|
||||
/// file read has a square document). 0 when not stored.
|
||||
pub document: [u32; 2],
|
||||
}
|
||||
|
||||
/// Most bytes of a Photoshop layer name read.
|
||||
const MAX_NAME: usize = 1024;
|
||||
|
||||
impl PsdLayer {
|
||||
/// The bounds' width and height; `None` when they are empty.
|
||||
pub fn size(&self) -> Option<(u32, u32)> {
|
||||
let [top, left, bottom, right] = self.bounds;
|
||||
let w = u32::try_from(i64::from(right) - i64::from(left)).ok().filter(|w| *w > 0)?;
|
||||
let h = u32::try_from(i64::from(bottom) - i64::from(top)).ok().filter(|h| *h > 0)?;
|
||||
Some((w, h))
|
||||
}
|
||||
|
||||
/// Whether the document is `size` big, its sides in either order.
|
||||
pub fn document_is(&self, size: (u32, u32)) -> bool {
|
||||
let [a, b] = self.document;
|
||||
a > 0 && b > 0 && ((a, b) == size || (b, a) == size)
|
||||
}
|
||||
|
||||
/// The layer an `opti` names; `None` for another tag, the merged image or a cut record.
|
||||
fn read(opti: &[u8]) -> Option<Self> {
|
||||
if opti.get(..4) != Some(&b"8BPS"[..]) {
|
||||
return None;
|
||||
}
|
||||
let index = u32::try_from(be_i32(opti, 14)?).ok()?;
|
||||
let le_i32 = |at| le_u32(opti, at).map(|v| i32::from_le_bytes(v.to_le_bytes()));
|
||||
// (After Effects writes 602 bytes; a longer hostile name is cut.)
|
||||
let name = opti.get(344..).map(|b| text(until_nul(b.get(..MAX_NAME).unwrap_or(b)))).unwrap_or_default();
|
||||
let document = [le_u32(opti, 32).unwrap_or(0), le_u32(opti, 36).unwrap_or(0)];
|
||||
Some(Self { index, name, bounds: [le_i32(78)?, le_i32(82)?, le_i32(86)?, le_i32(90)?], document })
|
||||
}
|
||||
}
|
||||
|
||||
impl Footage {
|
||||
@@ -307,6 +360,7 @@ impl<'f, 'a> Item<'f, 'a> {
|
||||
file_size: be_u32(sspc, 208).unwrap_or(0),
|
||||
audio_sample_rate: be_f64(sspc, 160).filter(|r| r.is_finite() && *r > 0.0),
|
||||
is_sequence,
|
||||
psd_layer: PsdLayer::read(opti),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -342,7 +396,46 @@ impl<'f, 'a> Project<'f, 'a> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::json_string;
|
||||
use super::{PsdLayer, json_string};
|
||||
|
||||
/// A 602-byte Photoshop `opti` naming layer `index` with `bounds` and `name`.
|
||||
fn opti(index: i32, bounds: [i32; 4], name: &[u8]) -> Vec<u8> {
|
||||
let mut b = vec![0u8; 602];
|
||||
b[..4].copy_from_slice(b"8BPS");
|
||||
b[14..18].copy_from_slice(&index.to_be_bytes());
|
||||
for (i, v) in bounds.iter().enumerate() {
|
||||
b[78 + 4 * i..82 + 4 * i].copy_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
b[344..344 + name.len()].copy_from_slice(name);
|
||||
b
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_photoshop_layer_reads_its_index_bounds_and_name() {
|
||||
let l = PsdLayer::read(&opti(7, [749, 800, 764, 848], "tést".as_bytes())).unwrap();
|
||||
assert_eq!((l.index, l.name.as_str(), l.bounds, l.size()), (7, "tést", [749, 800, 764, 848], Some((48, 15))));
|
||||
// The merged image, another tag, a cut record: no layer.
|
||||
assert_eq!(PsdLayer::read(&opti(-1, [0; 4], b"x")), None);
|
||||
let mut other = opti(1, [0; 4], b"x");
|
||||
other[..4].copy_from_slice(b"Soli");
|
||||
assert_eq!(PsdLayer::read(&other), None);
|
||||
assert_eq!(PsdLayer::read(&opti(1, [0; 4], b"x")[..80]), None);
|
||||
// A cut name, and no name at all.
|
||||
assert_eq!(PsdLayer::read(&opti(1, [0; 4], b"x")[..345]).unwrap().name, "x");
|
||||
assert_eq!(PsdLayer::read(&opti(1, [0; 4], b"x")[..300]).unwrap().name, "");
|
||||
// A layer without pixels has no size; extreme bounds do not overflow.
|
||||
assert_eq!(PsdLayer::read(&opti(2, [0; 4], b"")).unwrap().size(), None);
|
||||
assert_eq!(PsdLayer::read(&opti(2, [i32::MIN, i32::MIN, i32::MAX, i32::MAX], b"")).unwrap().size(), Some((u32::MAX, u32::MAX)));
|
||||
assert_eq!(PsdLayer::read(&opti(2, [i32::MAX, 0, i32::MIN, 1], b"")).unwrap().size(), None);
|
||||
// The document's size, its sides in either order; none when not stored.
|
||||
let mut doc = opti(0, [0, 0, 1000, 800], b"");
|
||||
doc[32..36].copy_from_slice(&800u32.to_le_bytes());
|
||||
doc[36..40].copy_from_slice(&1000u32.to_le_bytes());
|
||||
let l = PsdLayer::read(&doc).unwrap();
|
||||
assert_eq!(l.document, [800, 1000]);
|
||||
assert!(l.document_is((800, 1000)) && l.document_is((1000, 800)) && !l.document_is((800, 800)));
|
||||
assert!(!PsdLayer::read(&opti(0, [0; 4], b"")).unwrap().document_is((0, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_a_string_with_escapes() {
|
||||
|
||||
@@ -707,3 +707,99 @@ pub(crate) static FX_DEFS: &[FxDef] = &[
|
||||
p(BUILT_IN, 9, &[], None, None, None, 0x00000000),
|
||||
]),
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::container::effect_map::by_match_name;
|
||||
use effectcraft_project::ParamUi;
|
||||
|
||||
/// Effect parameters whose hard range (`ParamUi::Slider` min..max, which `prop.set` and the
|
||||
/// value editors clamp to) is still narrower than After Effects' recorded valid range, each
|
||||
/// with the open issue that tracks it: (effect, parameter, issue). Remove a row when its
|
||||
/// issue is fixed; the test fails on a row that is no longer needed.
|
||||
#[rustfmt::skip]
|
||||
const KNOWN_NARROWER: &[(&str, &str, u32)] = &[
|
||||
("ec.blur.gaussian", "blurriness", 3460),
|
||||
("ec.blur.fastbox", "radius", 3461),
|
||||
("ec.blur.channel", "redBlurriness", 3462),
|
||||
("ec.blur.channel", "greenBlurriness", 3462),
|
||||
("ec.blur.channel", "blueBlurriness", 3462),
|
||||
("ec.blur.channel", "alphaBlurriness", 3462),
|
||||
("ec.obsolete.gaussianlegacy", "blurriness", 3463),
|
||||
("ec.perspective.dropshadow", "softness", 3464),
|
||||
("ec.blur.compound", "maximumBlur", 3465),
|
||||
("ec.blur.sharpen", "amount", 3466),
|
||||
("ec.blur.radial", "amount", 3467),
|
||||
("ec.blur.ccradial", "amount", 3468),
|
||||
("ec.distort.turbulentdisplace", "amount", 3469),
|
||||
("ec.time.pixelmotionblur", "shutterAngle", 3481),
|
||||
("ec.time.pixelmotionblur", "shutterSamples", 3482),
|
||||
("ec.noise.fractal", "subSettings/subInfluence", 3485),
|
||||
("ec.noise.fractal", "subSettings/subScaling", 3486),
|
||||
("ec.distort.ccpowerpin", "expandTop", 3492),
|
||||
("ec.distort.ccpowerpin", "expandLeft", 3492),
|
||||
("ec.distort.ccpowerpin", "expandRight", 3492),
|
||||
("ec.distort.ccpowerpin", "expandBottom", 3492),
|
||||
];
|
||||
|
||||
/// `a` is at most `b`, allowing for the `f32` the recorded bounds were read from.
|
||||
fn at_most(a: f64, b: f64) -> bool {
|
||||
a <= b + 1e-6 * a.abs().max(b.abs()).max(1.0)
|
||||
}
|
||||
|
||||
/// Every effect parameter the .aep table maps to a recorded After Effects definition accepts
|
||||
/// at least After Effects' valid range, converted to EffectCraft's units the way the importer
|
||||
/// converts values (`Conv::number_scale`). Only `ParamUi::Slider` limits values (`prop.set`,
|
||||
/// the Effect Controls and Timeline editors); other controls take any number. A mapping with
|
||||
/// a declared narrower range (`ParamMap::limit`) must match the effect's range instead.
|
||||
#[test]
|
||||
fn effect_hard_ranges_cover_recorded_after_effects_ranges() {
|
||||
let mut problems = Vec::new();
|
||||
let mut narrower = Vec::new();
|
||||
for fx in FX_DEFS {
|
||||
let Some(map) = by_match_name(fx.match_name) else { continue };
|
||||
let Some(spec) = effectcraft_effects::find(map.effect) else {
|
||||
problems.push(format!("{}: no effect {}", fx.match_name, map.effect));
|
||||
continue;
|
||||
};
|
||||
for pm in map.params {
|
||||
let Some(scale) = pm.conv.number_scale() else { continue };
|
||||
let at = format!("{} {}", map.effect, pm.target);
|
||||
let Some(ps) = spec.params.iter().find(|p| p.id == pm.target) else {
|
||||
problems.push(format!("{at}: not a parameter of the effect"));
|
||||
continue;
|
||||
};
|
||||
// Other controls take any number.
|
||||
let ParamUi::Slider { min, max, .. } = &ps.ui else { continue };
|
||||
let Some(def) = fx.params.get(pm.index) else {
|
||||
problems.push(format!("{at}: no recorded definition at parT {}", pm.index));
|
||||
continue;
|
||||
};
|
||||
let (Some(lo), Some(hi)) = (def.valid_min, def.valid_max) else {
|
||||
problems.push(format!("{at}: no recorded valid range"));
|
||||
continue;
|
||||
};
|
||||
let (lo, hi) = (lo * scale, hi * scale);
|
||||
if let Some(limit) = pm.limit {
|
||||
if limit != (*min, *max) {
|
||||
problems.push(format!("{at}: declared limit {limit:?} is not the effect's range {min}..{max}"));
|
||||
}
|
||||
} else if !(at_most(*min, lo) && at_most(hi, *max)) {
|
||||
narrower.push((map.effect, pm.target, format!("{at}: {min}..{max} excludes After Effects' {lo}..{hi}")));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (effect, param, why) in &narrower {
|
||||
if !KNOWN_NARROWER.iter().any(|(e, p, _)| e == effect && p == param) {
|
||||
problems.push(why.clone());
|
||||
}
|
||||
}
|
||||
for (effect, param, issue) in KNOWN_NARROWER {
|
||||
if !narrower.iter().any(|(e, p, _)| e == effect && p == param) {
|
||||
problems.push(format!("{effect} {param}: covers After Effects' range now (or is not mapped); remove it from KNOWN_NARROWER (#{issue})"));
|
||||
}
|
||||
}
|
||||
assert!(problems.is_empty(), "{}", problems.join("\n"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,7 +281,7 @@ mod tests {
|
||||
let json = serde_json::to_string(&collected()).unwrap();
|
||||
assert_eq!(
|
||||
json,
|
||||
r#"{"exported":{"folders":0,"compositions":0,"solids":0,"layers":2,"solidLayers":0,"nullLayers":0,"adjustmentLayers":0,"properties":0,"keyframes":0,"expressions":0,"masks":0,"precompLayers":0,"effects":0,"footage":0,"footageLayers":0,"textLayers":0,"markers":0,"cameras":0,"lights":0,"shapeLayers":0,"timeRemaps":0,"textAnimators":0,"layerStyles":0},"losses":[{"category":"effects","count":2,"details":["fx 1","fx 2"]},{"category":"rounded_times","count":1,"details":["time 1"]}],"limitations":["limit"]}"#
|
||||
r#"{"exported":{"folders":0,"compositions":0,"solids":0,"layers":2,"solidLayers":0,"nullLayers":0,"adjustmentLayers":0,"properties":0,"keyframes":0,"expressions":0,"masks":0,"precompLayers":0,"effects":0,"footage":0,"footageLayers":0,"textLayers":0,"markers":0,"cameras":0,"lights":0,"shapeLayers":0,"timeRemaps":0,"textAnimators":0,"layerStyles":0},"losses":[{"category":"effects","count":2,"details":["fx 1","fx 2"],"unlisted":0},{"category":"rounded_times","count":1,"details":["time 1"],"unlisted":0}],"limitations":["limit"]}"#
|
||||
);
|
||||
let back: ExportReport = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(back, collected());
|
||||
|
||||
@@ -14,7 +14,8 @@
|
||||
//! missing-footage placeholders). `docs/aep-import.md` has the full list.
|
||||
//!
|
||||
//! What does not arrive is **reported**, never dropped silently: [`ImportReport`] counts other
|
||||
//! effects (by match name) and unimported effect parameters, unusable masks, layer style
|
||||
//! effects (by match name; they arrive as placeholders that render nothing, with their parameters
|
||||
//! for expressions) and unimported effect parameters, unusable masks, layer style
|
||||
//! features, text layer features, shape items, unsupported blend modes and transform
|
||||
//! properties, simplified keyframes and unreadable data per [`LossCategory`]. By default the import
|
||||
//! is refused when anything would be lost ([`ImportOptions::allow_losses`] lifts that); a refusal
|
||||
@@ -30,6 +31,7 @@ mod effects;
|
||||
mod lumetri;
|
||||
mod markers;
|
||||
mod masks;
|
||||
mod placeholder;
|
||||
mod props;
|
||||
mod puppet;
|
||||
mod report;
|
||||
|
||||
@@ -12,7 +12,8 @@ use effectcraft_color::{BlendMode, Label};
|
||||
use effectcraft_keyframe::Value;
|
||||
use effectcraft_project::build::{self, Ids};
|
||||
use effectcraft_project::{
|
||||
AutoOrient, Comp, Footage, FootageKind, ItemId, ItemKind, Layer, LayerId, LayerSource, LightKind, MatteKind, Project, Quality, Solid, TrackMatte,
|
||||
AutoOrient, Comp, Footage, FootageKind, ItemId, ItemKind, Layer, LayerId, LayerSource, LightKind, MatteKind, Project, Quality, Solid, SourceLayer,
|
||||
TrackMatte,
|
||||
};
|
||||
use effectcraft_time::Tick;
|
||||
|
||||
@@ -217,7 +218,17 @@ fn import_footage(project: &mut Project, cx: &mut Ctx<'_>, item: &AepItem<'_, '_
|
||||
let aep_id = item.id();
|
||||
// An item without a stored name shows its file's name, a sequence `shot_[0101-0103].exr`
|
||||
// (126, 603-607).
|
||||
let name = if name.is_empty() { default_footage_name(item, &f).unwrap_or_else(|| "Footage".to_string()) } else { name };
|
||||
// One layer of a Photoshop file is `<layer>/<file>`, as EffectCraft's own Photoshop import
|
||||
// names it (#3087).
|
||||
let name = if name.is_empty() {
|
||||
let file = default_footage_name(item, &f).unwrap_or_else(|| "Footage".to_string());
|
||||
match &f.psd_layer {
|
||||
Some(l) if !l.name.is_empty() => format!("{}/{file}", l.name),
|
||||
_ => file,
|
||||
}
|
||||
} else {
|
||||
name
|
||||
};
|
||||
if f.is_solid {
|
||||
// A null object's placeholder solid is not an item here.
|
||||
if aep_id.is_some_and(|i| null_only.contains(&i)) {
|
||||
@@ -299,6 +310,7 @@ fn import_footage(project: &mut Project, cx: &mut Ctx<'_>, item: &AepItem<'_, '_
|
||||
missing: !present,
|
||||
placed_page,
|
||||
sequence,
|
||||
layer: source_layer(&f, &name, &mut cx.col),
|
||||
..Footage::default()
|
||||
};
|
||||
// The native frame rate and the stored duration of sequences and clips (352, 603, 606).
|
||||
@@ -338,6 +350,34 @@ fn is_placed_page_path(path: &str) -> bool {
|
||||
matches!(ext.as_deref(), Some("pdf" | "ai"))
|
||||
}
|
||||
|
||||
/// The Photoshop layer a footage item shows (#3087): at its own bounds when the item is as big
|
||||
/// as they are (Retain Layer Sizes), else on the document. 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 (the item's size fits both framings; its bounds are used), and an item as big as
|
||||
/// neither (the document is used).
|
||||
fn source_layer(f: &crate::Footage, name: &str, col: &mut Collector) -> Option<SourceLayer> {
|
||||
let l = f.psd_layer.as_ref()?;
|
||||
let Some(size) = l.size() else {
|
||||
col.lose(LossCategory::UnsupportedItems, format!("footage '{name}': a Photoshop layer without pixels (an adjustment layer), the merged image used"));
|
||||
return None;
|
||||
};
|
||||
let layer_size = size == (f.width, f.height);
|
||||
if !layer_size && l.document.iter().all(|side| *side > 0) && !l.document_is((f.width, f.height)) {
|
||||
col.lose(
|
||||
LossCategory::UnsupportedItems,
|
||||
format!("footage '{name}': {} x {} is neither the Photoshop layer's size nor the document's, shown on the document", f.width, f.height),
|
||||
);
|
||||
}
|
||||
let [top, left, ..] = l.bounds;
|
||||
if layer_size && (top, left) != (0, 0) && l.document_is(size) {
|
||||
col.lose(
|
||||
LossCategory::UnsupportedItems,
|
||||
format!("footage '{name}': a Photoshop layer as big as the document but at ({left}, {top}), shown at its own bounds (Retain Layer Sizes assumed)"),
|
||||
);
|
||||
}
|
||||
Some(SourceLayer { index: l.index, name: l.name.clone(), layer_size, ..SourceLayer::default() })
|
||||
}
|
||||
|
||||
/// The name After Effects shows for a footage item that stores none: the file's name, or for a
|
||||
/// sequence the name with the frame range (`shot_[0101-0103].exr`).
|
||||
fn default_footage_name(item: &AepItem<'_, '_>, f: &crate::Footage) -> Option<String> {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Effects: a table maps an After Effects effect (its match name) and its parameters (by their
|
||||
//! index in `parT`) to an EffectCraft effect, a parameter id and a unit conversion. Effects and
|
||||
//! parameters outside the table are reported in [`LossCategory::Effects`] by match name, never
|
||||
//! dropped silently.
|
||||
//! index in `parT`) to an EffectCraft effect, a parameter id and a unit conversion. Parameters
|
||||
//! outside the table are reported in [`LossCategory::Effects`] by match name, never dropped
|
||||
//! silently. An effect outside it arrives as a placeholder holding its parameters (`placeholder.rs`)
|
||||
//! and is reported too.
|
||||
//!
|
||||
//! Every conversion is backed by the corpus (`docs/aep-format.md` 3.7, cases 356-450, 511-516):
|
||||
//!
|
||||
@@ -34,7 +35,7 @@
|
||||
//! import. Hue/Saturation's master values and Levels' values are arbitrary data, decoded by the
|
||||
//! reader (444-446, 512-516).
|
||||
|
||||
use crate::effect_map::{CRYPTOMATTE_OUTPUT, Conv, EFFECTS, EffectMap, Extra, HUE_SATURATION_MASTER, LEVELS_RGB, ParamMap, popup_key, popup_stem};
|
||||
use crate::effect_map::{CRYPTOMATTE_OUTPUT, Conv, EFFECTS, EffectMap, Extra, HUE_SATURATION_MASTER, LEVELS_RGB, ParamMap, popup_alias, popup_key};
|
||||
use crate::{
|
||||
CryptomatteData, CurvesData, DefinitionSource, Effect as AepEffect, EffectDefinitions, EffectParam, ExtractorChannels, HueSaturationData,
|
||||
Layer as AepLayer, LayerRef, LevelsData, MeshData, ParamKind, ParamValue,
|
||||
@@ -82,44 +83,37 @@ pub(crate) fn import_effects<'f, 'a>(
|
||||
continue;
|
||||
}
|
||||
let shown = fx.display_name().unwrap_or_else(|| fx.match_name().to_string());
|
||||
let Some(map) = EFFECTS.iter().find(|m| m.match_name == fx.match_name()) else {
|
||||
col.lose_with(LossCategory::Effects, || format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
|
||||
continue;
|
||||
};
|
||||
let Some(spec) = effectcraft_effects::find(map.effect) else {
|
||||
col.lose_with(LossCategory::Effects, || format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()));
|
||||
continue;
|
||||
};
|
||||
let found = EFFECTS.iter().find(|m| m.match_name == fx.match_name()).and_then(|m| Some((m, effectcraft_effects::find(m.effect)?)));
|
||||
let lost = || format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name());
|
||||
if defs.source(&fx) == DefinitionSource::Missing {
|
||||
col.lose_with(LossCategory::Effects, || {
|
||||
format!("{}: '{}' has the effect '{shown}' ({}) without readable parameter definitions, not imported", cx.comp, cx.layer, fx.match_name())
|
||||
});
|
||||
match found {
|
||||
Some(_) => col.lose_with(LossCategory::Effects, || format!("{} without readable parameter definitions, not imported", lost())),
|
||||
None => col.lose_unmapped_effect(fx.match_name(), lost),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let size = [f64::from(cx.size.0), f64::from(cx.size.1)];
|
||||
let mut group = effectcraft_effects::instantiate(spec, ids, &shown, size);
|
||||
match fx.enabled() {
|
||||
Some(on) => group.enabled = on,
|
||||
None => col.lose_with(LossCategory::DamagedData, || {
|
||||
format!("{}: '{}': the fx switch of '{shown}' is unreadable, imported switched on", cx.comp, cx.layer)
|
||||
}),
|
||||
if found.is_none() && col.placeholder_cost >= crate::import::placeholder::MAX_COST {
|
||||
// The import's placeholder budget is spent: the definitions (shared by many instances)
|
||||
// are not read again. Named like any effect without a counterpart, in a group of its
|
||||
// own (with the kept instances it would read as kept).
|
||||
let key = format!("{} (not kept)", fx.match_name());
|
||||
col.lose_unmapped_effect(&key, || format!("{}, not kept (past the import's placeholder parameters)", lost()));
|
||||
continue;
|
||||
}
|
||||
let params = defs.params(&fx);
|
||||
// Borrowed definitions (another instance's or the project copy) may not name every value
|
||||
// record this instance has: such a record has nothing to import into, so it is reported.
|
||||
if params.first().is_some_and(|p| p.borrowed_definition) {
|
||||
let defined: std::collections::HashSet<&str> = params.iter().map(|p| p.match_name.as_str()).collect();
|
||||
for name in fx.value_record_names().iter().filter(|n| !defined.contains(n.as_str())) {
|
||||
col.lose_with(LossCategory::Effects, || {
|
||||
format!(
|
||||
"{}: '{}' effect '{shown}' ({}): the value record {name} has no parameter definition, not imported",
|
||||
cx.comp,
|
||||
cx.layer,
|
||||
fx.match_name()
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
report_records_without_definition(&fx, ¶ms, cx, &shown, col);
|
||||
let Some((map, spec)) = found else {
|
||||
// No counterpart: kept as a placeholder that renders nothing, for expressions (#3087).
|
||||
let mut group = crate::import::placeholder::placeholder(&fx, ¶ms, cx, &shown, ids, refs, col);
|
||||
set_enabled(&fx, &mut group, cx, &shown, col);
|
||||
col.lose_unmapped_effect(fx.match_name(), || format!("{}, kept without rendering", lost()));
|
||||
col.imported.placeholder_effects = col.imported.placeholder_effects.saturating_add(1);
|
||||
target.children.push(Node::Group(group));
|
||||
continue;
|
||||
};
|
||||
let size = [f64::from(cx.size.0), f64::from(cx.size.1)];
|
||||
let mut group = effectcraft_effects::instantiate(spec, ids, &shown, size);
|
||||
set_enabled(&fx, &mut group, cx, &shown, col);
|
||||
if map.extra == Extra::Lumetri {
|
||||
crate::import::lumetri::import_lumetri(&fx, ¶ms, cx, &shown, ParamOut { group: &mut group, refs: &mut *refs, col: &mut *col });
|
||||
} else if map.extra == Extra::Cryptomatte {
|
||||
@@ -134,6 +128,30 @@ pub(crate) fn import_effects<'f, 'a>(
|
||||
}
|
||||
}
|
||||
|
||||
/// The effect's fx switch.
|
||||
fn set_enabled(fx: &AepEffect<'_, '_>, group: &mut PropGroup, cx: &LayerCtx<'_>, shown: &str, col: &mut Collector) {
|
||||
match fx.enabled() {
|
||||
Some(on) => group.enabled = on,
|
||||
None => {
|
||||
col.lose_with(LossCategory::DamagedData, || format!("{}: '{}': the fx switch of '{shown}' is unreadable, imported switched on", cx.comp, cx.layer))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Borrowed definitions (another instance's or the project copy) may not name every value record
|
||||
/// this instance has: such a record has nothing to import into, so it is reported.
|
||||
fn report_records_without_definition(fx: &AepEffect<'_, '_>, params: &[EffectParam<'_, '_>], cx: &LayerCtx<'_>, shown: &str, col: &mut Collector) {
|
||||
if !params.first().is_some_and(|p| p.borrowed_definition) {
|
||||
return;
|
||||
}
|
||||
let defined: std::collections::HashSet<&str> = params.iter().map(|p| p.match_name.as_str()).collect();
|
||||
for name in fx.value_record_names().iter().filter(|n| !defined.contains(n.as_str())) {
|
||||
col.lose_with(LossCategory::Effects, || {
|
||||
format!("{}: '{}' effect '{shown}' ({}): the value record {name} has no parameter definition, not imported", cx.comp, cx.layer, fx.match_name())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Parameters that never carry a value to import.
|
||||
pub(crate) fn carries_nothing(p: &EffectParam<'_, '_>) -> bool {
|
||||
matches!(p.kind, ParamKind::Button | ParamKind::NoData | ParamKind::GroupStart | ParamKind::GroupEnd) || (p.index == 0 && p.kind == ParamKind::Layer)
|
||||
@@ -241,23 +259,12 @@ pub(crate) fn import_mapped(p: &EffectParam<'_, '_>, m: &ParamMap, cx: &LayerCtx
|
||||
Conv::Checkbox => Kind::EffectBool,
|
||||
// Handled above.
|
||||
Conv::LayerRef { .. } | Conv::Skip => return,
|
||||
Conv::Percent { valid_max } => {
|
||||
Conv::Percent { valid_max } | Conv::Plain { valid_max } | Conv::Factor { valid_max, .. } => {
|
||||
if !checked(valid_max, col) {
|
||||
return;
|
||||
}
|
||||
Kind::EffectNumber(100.0 / valid_max)
|
||||
}
|
||||
Conv::Plain { valid_max } => {
|
||||
if !checked(valid_max, col) {
|
||||
return;
|
||||
}
|
||||
Kind::EffectNumber(1.0)
|
||||
}
|
||||
Conv::Factor { valid_max, factor } => {
|
||||
if !checked(valid_max, col) {
|
||||
return;
|
||||
}
|
||||
Kind::EffectNumber(factor)
|
||||
let Some(scale) = m.conv.number_scale() else { return };
|
||||
Kind::EffectNumber(scale)
|
||||
}
|
||||
};
|
||||
// Grouped parameters are addressed by path (`master/exposure`).
|
||||
@@ -365,7 +372,7 @@ fn report_beyond_limit<'v>(limit: Option<(f64, f64)>, values: impl Iterator<Item
|
||||
/// A layer parameter: None stays unset, the owning layer is itself (`own`) or unset, another layer
|
||||
/// is resolved by [`resolve_layer_refs`] once the composition's layers exist.
|
||||
/// `names` is `(here, label)`, the effect and the parameter for the report.
|
||||
fn import_layer_ref(p: &EffectParam<'_, '_>, target: &str, own: bool, cx: &LayerCtx<'_>, names: (&str, &str), out: ParamOut<'_>) {
|
||||
pub(crate) fn import_layer_ref(p: &EffectParam<'_, '_>, target: &str, own: bool, cx: &LayerCtx<'_>, names: (&str, &str), out: ParamOut<'_>) {
|
||||
let (here, label) = names;
|
||||
let ParamOut { group, refs, col } = out;
|
||||
// After Effects hands the named layer's source to the effect, without its masks and effects
|
||||
@@ -416,14 +423,13 @@ pub(crate) fn resolve_layer_refs(
|
||||
}
|
||||
}
|
||||
|
||||
/// The EffectCraft item of each file item (by name, ignoring case and separators); `None` when
|
||||
/// the parameter here is not a popup.
|
||||
///
|
||||
/// The file's item count is not bounded (its `pdnm`), EffectCraft's options are: each side's key
|
||||
/// is taken once and compared without a copy, so the cost is linear in the file's items.
|
||||
/// The EffectCraft item of each file item (by name, ignoring case and separators, and a known
|
||||
/// non-English name for it, [`popup_alias`]); `None` when the parameter here is not a popup.
|
||||
fn popup_mapping(file_items: &[String], ui: &ParamUi) -> Option<Vec<Option<u32>>> {
|
||||
let ParamUi::Popup { options } = ui else { return None };
|
||||
let options: Vec<&str> = options.iter().map(|o| popup_stem(o)).collect();
|
||||
// The file's item count is not bounded (its `pdnm`), EffectCraft's options are: the options'
|
||||
// keys are built once, and each file item's once, so the cost is linear in the file's items.
|
||||
let options: Vec<String> = options.iter().map(|o| popup_key(o)).collect();
|
||||
Some(
|
||||
file_items
|
||||
.iter()
|
||||
@@ -431,8 +437,12 @@ fn popup_mapping(file_items: &[String], ui: &ParamUi) -> Option<Vec<Option<u32>>
|
||||
if item.trim() == "(-" {
|
||||
return None;
|
||||
}
|
||||
let item = popup_stem(item);
|
||||
options.iter().position(|o| o.eq_ignore_ascii_case(item)).and_then(|i| u32::try_from(i).ok())
|
||||
let key = popup_key(item);
|
||||
options
|
||||
.iter()
|
||||
.position(|o| *o == key)
|
||||
.or_else(|| popup_alias(&key).and_then(|a| options.iter().position(|o| o == a)))
|
||||
.and_then(|i| u32::try_from(i).ok())
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
@@ -807,7 +817,7 @@ fn px(v: f64) -> String {
|
||||
|
||||
/// A decoded value in EffectCraft's units, `None` when it does not fit the kind or is not a valid
|
||||
/// number.
|
||||
fn convert(value: ParamValue, kind: Kind) -> Option<Value> {
|
||||
pub(crate) fn convert(value: ParamValue, kind: Kind) -> Option<Value> {
|
||||
let raw = match (value, kind) {
|
||||
// The reader gives colours as ARGB fractions; `Kind` takes the stored 0..255.
|
||||
(ParamValue::Argb(c), Kind::EffectColor) => c.map(|x| x * 255.0).to_vec(),
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
//! Placeholders: an effect with no EffectCraft counterpart (a third-party plug-in, a pseudo effect
|
||||
//! of expression controls) arrives as an effect group that renders nothing, as a missing plug-in
|
||||
//! does. Its effect id is the After Effects match name, so Find Missing Effects lists it. It holds
|
||||
//! the effect's parameters, flat and named as in After Effects, each with its value, keys and
|
||||
//! expression, so expressions that read them (`effect("Settings")("Speed")`) keep working. It is
|
||||
//! still reported, once per match name.
|
||||
//!
|
||||
//! The values keep After Effects' units, the ones its expressions read: sliders and angles as
|
||||
//! stored, points in layer pixels, colours RGBA 0..1, checkboxes on / off, popups as EffectCraft's
|
||||
//! 0-based items over the file's items in order (expressions read them 1-based, as After Effects
|
||||
//! does). Layer parameters name their layer (their keys and expression are reported). Paths and
|
||||
//! arbitrary data are not kept (reported when they hold something).
|
||||
//!
|
||||
//! The parameters placeholders walk, kept or not, and their popup items (by name length) are
|
||||
//! bounded per import ([`MAX_COST`]): definitions can be shared by many instances, so a small file
|
||||
//! could otherwise ask for very many properties. Once
|
||||
//! the budget is spent, later effects without a counterpart are reported without a placeholder.
|
||||
|
||||
use effectcraft_keyframe::Value;
|
||||
use effectcraft_project::build::Ids;
|
||||
use effectcraft_project::{GroupKind, Node, ParamUi, PropGroup, Property};
|
||||
|
||||
use crate::import::effects::{LayerCtx, ParamOut, PendingLayerRef, carries_nothing, convert, import_layer_ref, param_label};
|
||||
use crate::import::props::{Kind, import_expression, import_keys};
|
||||
use crate::import::report::{Collector, LossCategory};
|
||||
use crate::{Effect as AepEffect, EffectParam, ParamKind, ParamValue};
|
||||
|
||||
/// Most placeholder parameters (kept or not) and popup items one import walks.
|
||||
pub(crate) const MAX_COST: usize = 200_000;
|
||||
|
||||
/// Most items read from one placeholder popup.
|
||||
const MAX_POPUP_ITEMS: usize = 1024;
|
||||
|
||||
/// A popup item costs one more for each of these bytes in its name, so the names kept stay
|
||||
/// bounded too.
|
||||
const NAME_BYTES: usize = 16;
|
||||
|
||||
/// The placeholder of `fx`, with `params` (its parameter definitions) as properties.
|
||||
pub(crate) fn placeholder(
|
||||
fx: &AepEffect<'_, '_>,
|
||||
params: &[EffectParam<'_, '_>],
|
||||
cx: &LayerCtx<'_>,
|
||||
shown: &str,
|
||||
ids: &mut Ids<'_>,
|
||||
refs: &mut Vec<PendingLayerRef>,
|
||||
col: &mut Collector,
|
||||
) -> PropGroup {
|
||||
// The effect id is the match name; the group's match id is one segment of a property path
|
||||
// (a pseudo effect's `Pseudo/0.25` would be cut apart), so match paths find its parameters.
|
||||
let mut group = ids.group(&segment(fx.match_name()), shown);
|
||||
group.kind = GroupKind::Effect { effect: fx.match_name().to_string() };
|
||||
let here = format!("{}: '{}' effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name());
|
||||
for p in params {
|
||||
// Every parameter walked counts, kept or not: a definition shared by many instances is
|
||||
// otherwise walked again for each one.
|
||||
if carries_nothing(p) {
|
||||
col.placeholder_cost = col.placeholder_cost.saturating_add(1).min(MAX_COST);
|
||||
continue;
|
||||
}
|
||||
if col.placeholder_cost >= MAX_COST {
|
||||
col.lose(LossCategory::Effects, format!("{here}: parameters past the import's {MAX_COST} placeholder parameters not kept"));
|
||||
break;
|
||||
}
|
||||
let label = param_label(p);
|
||||
let Some((kind, ui, default)) = shape(p, cx.size) else {
|
||||
col.placeholder_cost = col.placeholder_cost.saturating_add(1);
|
||||
if p.stored().is_some() {
|
||||
col.lose(LossCategory::Effects, format!("{here}: parameter {label} not kept (type {:?})", p.kind));
|
||||
}
|
||||
continue;
|
||||
};
|
||||
let (items, names) = match &ui {
|
||||
ParamUi::Popup { options } => (options.len(), options.iter().map(|o| 1 + o.len() / NAME_BYTES).fold(0, usize::saturating_add)),
|
||||
_ => (0, 0),
|
||||
};
|
||||
if p.kind == ParamKind::Popup && popup_len(p).is_some_and(|n| n > items) {
|
||||
col.lose(LossCategory::Effects, format!("{here}: parameter {label}: the items past the first {MAX_POPUP_ITEMS} not kept"));
|
||||
}
|
||||
let cost = col.placeholder_cost.saturating_add(names).saturating_add(1);
|
||||
if cost > MAX_COST {
|
||||
// The budget is spent: nothing more is built, here or in later effects (definitions
|
||||
// shared by many instances would otherwise still be walked item by item).
|
||||
col.lose(
|
||||
LossCategory::Effects,
|
||||
format!("{here}: parameter {label} and the ones after it not kept (past the import's {MAX_COST} placeholder parameters)"),
|
||||
);
|
||||
col.placeholder_cost = MAX_COST;
|
||||
break;
|
||||
}
|
||||
col.placeholder_cost = cost;
|
||||
let mut prop = Property::new(ids.alloc(), ¶m_id(p), p.name.trim(), default).with_ui(ui);
|
||||
prop.spatial = p.kind == ParamKind::Point;
|
||||
prop.hold_only = matches!(p.kind, ParamKind::Checkbox | ParamKind::Popup | ParamKind::Layer);
|
||||
match kind {
|
||||
Some(kind) => {
|
||||
fill(p, &mut prop, kind, cx, (&here, &label), col);
|
||||
group.children.push(Node::Prop(prop));
|
||||
}
|
||||
// A layer parameter: the layer it names (set once every layer exists, by its uid).
|
||||
None => {
|
||||
let id = format!("@{}", prop.uid);
|
||||
group.children.push(Node::Prop(prop));
|
||||
import_layer_ref(p, &id, true, cx, (&here, &label), ParamOut { group: &mut group, refs: &mut *refs, col: &mut *col });
|
||||
}
|
||||
}
|
||||
}
|
||||
group
|
||||
}
|
||||
|
||||
/// How parameter `p` is held on a layer `size` big: its unit conversion (`None` for a layer
|
||||
/// parameter), its control and its value until one is read. `None` for a type that is not kept.
|
||||
fn shape(p: &EffectParam<'_, '_>, size: (u32, u32)) -> Option<(Option<Kind>, ParamUi, Value)> {
|
||||
let (kind, ui, default) = match p.kind {
|
||||
ParamKind::IntSlider | ParamKind::FixedSlider | ParamKind::FloatSlider => (Kind::EffectNumber(1.0), slider_ui(p), Value::Scalar(0.0)),
|
||||
ParamKind::Angle => (Kind::EffectNumber(1.0), ParamUi::Angle, Value::Scalar(0.0)),
|
||||
ParamKind::Checkbox => (Kind::EffectBool, ParamUi::Checkbox, Value::Bool(false)),
|
||||
ParamKind::Color => (Kind::EffectColor, ParamUi::Color, Value::Color([0.0, 0.0, 0.0, 1.0])),
|
||||
ParamKind::Point => (Kind::EffectPoint(f64::from(size.0), f64::from(size.1)), ParamUi::Point, Value::Vec2([0.0, 0.0])),
|
||||
ParamKind::Layer => return Some((None, ParamUi::Layer, Value::Layer(None))),
|
||||
ParamKind::Popup => {
|
||||
// The file's items, separators (`(-`) included: After Effects counts them.
|
||||
let options: Vec<String> = match &p.items {
|
||||
Some(items) => items.iter().take(MAX_POPUP_ITEMS).cloned().collect(),
|
||||
None => (1..=popup_len(p)?.min(MAX_POPUP_ITEMS)).map(|i| format!("Item {i}")).collect(),
|
||||
};
|
||||
let n = u32::try_from(options.len()).ok().filter(|n| *n > 0)?;
|
||||
(Kind::EffectPopup(n), ParamUi::Popup { options }, Value::Enum(0))
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
Some((Some(kind), ui, default))
|
||||
}
|
||||
|
||||
/// A slider's control: a float slider's valid range and drag range when they are ranges (only a
|
||||
/// float slider's are decoded), else a plain number.
|
||||
fn slider_ui(p: &EffectParam<'_, '_>) -> ParamUi {
|
||||
match (p.valid_min(), p.valid_max(), p.slider_range()) {
|
||||
(Some(min), Some(max), Some((smin, smax))) if [min, max, smin, smax].iter().all(|v| v.is_finite()) && min <= max && smin <= smax => {
|
||||
ParamUi::Slider { min, max, slider_min: smin.clamp(min, max), slider_max: smax.clamp(min, max), decimals: 2 }
|
||||
}
|
||||
_ => ParamUi::Number,
|
||||
}
|
||||
}
|
||||
|
||||
/// How many items a popup has: the file's names, else the count in its definition.
|
||||
fn popup_len(p: &EffectParam<'_, '_>) -> Option<usize> {
|
||||
p.items.as_ref().map(Vec::len).or_else(|| p.popup_item_count().map(usize::from))
|
||||
}
|
||||
|
||||
/// The property id of `p`: its match name when that is one segment of a property path (a
|
||||
/// third-party effect's `CC Cylinder-0003`), else `param<index>` (a pseudo effect's
|
||||
/// `Pseudo/0.25-0001`: `/` and `.` separate path segments). Expressions also find it by its name.
|
||||
fn param_id(p: &EffectParam<'_, '_>) -> String {
|
||||
if !p.match_name.is_empty() && p.match_name.chars().all(plain) { p.match_name.clone() } else { format!("param{}", p.index) }
|
||||
}
|
||||
|
||||
/// Whether `c` can stand in one segment of a property path (`/`, `.`, `#` and `@` mean more).
|
||||
fn plain(c: char) -> bool {
|
||||
c.is_alphanumeric() || matches!(c, ' ' | '-' | '_')
|
||||
}
|
||||
|
||||
/// `name` as one segment of a property path: other characters become `_` (`Pseudo/0.25` is
|
||||
/// `Pseudo_0_25`).
|
||||
fn segment(name: &str) -> String {
|
||||
name.chars().map(|c| if plain(c) { c } else { '_' }).collect()
|
||||
}
|
||||
|
||||
/// Sets `prop` from `p`: its expression, then its keys or its value (the record, else the `pard`
|
||||
/// default). `names` is `(here, label)` for the report.
|
||||
fn fill(p: &EffectParam<'_, '_>, prop: &mut Property, kind: Kind, cx: &LayerCtx<'_>, names: (&str, &str), col: &mut Collector) {
|
||||
let (here, label) = names;
|
||||
if let Some(stored) = p.stored() {
|
||||
import_expression(&stored, prop, col);
|
||||
}
|
||||
if p.is_animated() {
|
||||
let Some(stored) = p.stored() else { return };
|
||||
if kind == Kind::EffectBool {
|
||||
// Keys on a checkbox are not sampled by the corpus.
|
||||
col.lose(LossCategory::Effects, format!("{here}: the keyframes of parameter {label} are not kept (not decoded)"));
|
||||
return;
|
||||
}
|
||||
let before = prop.keys.len();
|
||||
import_keys(&stored, kind, cx.layer, &p.match_name, cx.tm, prop, col);
|
||||
if prop.keys.len() == before {
|
||||
col.lose(LossCategory::DamagedData, format!("{here}: parameter {label} has no readable keyframe"));
|
||||
}
|
||||
return;
|
||||
}
|
||||
let value = match (p.stored(), p.recorded_value()) {
|
||||
(Some(_), Some(v)) => v,
|
||||
(Some(_), None) => {
|
||||
col.lose(LossCategory::DamagedData, format!("{here}: parameter {label} holds an unreadable value"));
|
||||
return;
|
||||
}
|
||||
// A borrowed popup's default slot holds another copy's selection (docs/aep-format.md 3.7).
|
||||
(None, _) if p.kind == ParamKind::Popup && p.borrowed_definition => {
|
||||
col.lose(LossCategory::Effects, format!("{here}: parameter {label}: the default item is not known (shared definition), the first item used"));
|
||||
return;
|
||||
}
|
||||
// A popup's default slot can hold 0, no item (After Effects' save of a project it opened).
|
||||
(None, _) => match p.pard_default() {
|
||||
Some(ParamValue::Index(0)) => {
|
||||
col.lose(LossCategory::Effects, format!("{here}: parameter {label}: the default item is not stored, the first item used"));
|
||||
return;
|
||||
}
|
||||
Some(v) => v,
|
||||
None => return,
|
||||
},
|
||||
};
|
||||
match (convert(value, kind), value) {
|
||||
(Some(v), _) => prop.value = v,
|
||||
// An item of the file that is past the ones kept (reported with the cut): the first item.
|
||||
(None, ParamValue::Index(i)) if usize::try_from(i).is_ok_and(|i| i > MAX_POPUP_ITEMS && popup_len(p).is_some_and(|n| i <= n)) => {
|
||||
col.lose(LossCategory::Effects, format!("{here}: parameter {label}: item {i} is past the items kept, the first item used"));
|
||||
}
|
||||
(None, _) => col.lose(LossCategory::DamagedData, format!("{here}: parameter {label} holds a value that is not valid ({value:?})")),
|
||||
}
|
||||
}
|
||||
@@ -231,15 +231,21 @@ pub(crate) fn import_transform(
|
||||
}
|
||||
import_expression(&node, prop, col);
|
||||
}
|
||||
if let Some(PropNode::Other(_)) = group.get("ADBE Orientation") {
|
||||
if let Some(PropNode::Other(otst)) = group.get("ADBE Orientation") {
|
||||
match (layer.orientation(), target.get_mut("orientation")) {
|
||||
(Some(o), Some(prop)) if !o.animated && !o.has_expression && sane(&Value::Vec3(o.degrees)) => prop.value = Value::Vec3(o.degrees),
|
||||
(Some(o), Some(prop)) if o.animated || sane(&Value::Vec3(o.degrees)) => {
|
||||
if o.animated {
|
||||
import_orientation_keys(layer, layer_name, tm, prop, col);
|
||||
} else {
|
||||
prop.value = Value::Vec3(o.degrees);
|
||||
}
|
||||
// The expression is stored in the `otst`'s `tdbs` as on any property (#3087).
|
||||
if let Some(node) = otst.child_list(b"tdbs").and_then(AepProp::from_chunk) {
|
||||
import_expression(&node, prop, col);
|
||||
}
|
||||
}
|
||||
// A layer without an orientation (an ambient light) loses nothing with the default.
|
||||
(Some(o), None) if !o.animated && !o.has_expression && o.degrees == [0.0; 3] => {}
|
||||
(Some(o), Some(prop)) if o.animated && !o.has_expression => import_orientation_keys(layer, layer_name, tm, prop, col),
|
||||
(Some(o), Some(_)) if o.has_expression => {
|
||||
col.lose_with(LossCategory::TransformProperties, || format!("{layer_name}: ADBE Orientation expression (not decoded)"));
|
||||
}
|
||||
_ => col.lose_with(LossCategory::TransformProperties, || format!("{layer_name}: ADBE Orientation")),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,8 @@ pub enum LossCategory {
|
||||
/// Layers that could not be imported for another reason (unknown layer type, missing or
|
||||
/// unsupported source, precomposition loop).
|
||||
UnsupportedLayers,
|
||||
/// Project items of a kind that is not imported.
|
||||
/// Project items of a kind that is not imported, and items made from a Photoshop layer
|
||||
/// without pixels (they show the merged image).
|
||||
UnsupportedItems,
|
||||
/// Data that could not be read (damaged headers, unreadable keyframes).
|
||||
DamagedData,
|
||||
@@ -100,8 +101,12 @@ pub struct Loss<C = LossCategory> {
|
||||
pub category: C,
|
||||
/// How many occurrences (exact).
|
||||
pub count: usize,
|
||||
/// Up to [`MAX_DETAILS`] example lines.
|
||||
/// Example lines: up to [`MAX_DETAILS`], and for effects with no counterpart one more line per
|
||||
/// effect (with its number of instances).
|
||||
pub details: Vec<String>,
|
||||
/// How many of the `count` occurrences no line of `details` describes.
|
||||
#[serde(default)]
|
||||
pub unlisted: usize,
|
||||
}
|
||||
|
||||
/// The loss bookkeeping the import and the export report share.
|
||||
@@ -109,7 +114,7 @@ impl<C: Copy + Ord> Loss<C> {
|
||||
/// Counts one loss of `category`; `detail` is only called while the category holds fewer than
|
||||
/// [`MAX_DETAILS`] lines.
|
||||
pub(crate) fn record(losses: &mut BTreeMap<C, Self>, category: C, detail: impl FnOnce() -> String) {
|
||||
let loss = losses.entry(category).or_insert_with(|| Loss { category, count: 0, details: Vec::new() });
|
||||
let loss = losses.entry(category).or_insert_with(|| Loss { category, count: 0, details: Vec::new(), unlisted: 0 });
|
||||
loss.count = loss.count.saturating_add(1);
|
||||
if loss.details.len() < MAX_DETAILS {
|
||||
loss.details.push(detail());
|
||||
@@ -186,6 +191,10 @@ pub struct Imported {
|
||||
/// in [`LossCategory::Effects`]).
|
||||
#[serde(default)]
|
||||
pub effects: usize,
|
||||
/// Effects with no counterpart, kept as placeholders that render nothing (their parameters,
|
||||
/// for expressions; each is also in [`LossCategory::Effects`]).
|
||||
#[serde(default)]
|
||||
pub placeholder_effects: usize,
|
||||
/// Layer styles (Drop Shadow, Stroke, ...), one per style on a layer.
|
||||
#[serde(default)]
|
||||
pub layer_styles: usize,
|
||||
@@ -304,8 +313,18 @@ pub(crate) struct Collector {
|
||||
losses: BTreeMap<LossCategory, Loss>,
|
||||
pub missing_footage: Vec<String>,
|
||||
pub missing_fonts: Vec<String>,
|
||||
/// The effects with no counterpart, by match name: the first instance's report line and how
|
||||
/// many instances there were. They get a line each in [`Collector::finish`], so one that
|
||||
/// comes after the example lines are used up is still named (#3365).
|
||||
unmapped_effects: BTreeMap<String, (String, usize)>,
|
||||
/// The properties and popup items the placeholders of this import hold so far (at most
|
||||
/// `placeholder::MAX_COST`).
|
||||
pub placeholder_cost: usize,
|
||||
}
|
||||
|
||||
/// How many different unmapped effects get a line of their own (a file names at most this many).
|
||||
const MAX_UNMAPPED_EFFECTS: usize = 100;
|
||||
|
||||
impl Collector {
|
||||
/// Records one loss.
|
||||
pub fn lose(&mut self, category: LossCategory, detail: impl Into<String>) {
|
||||
@@ -318,7 +337,40 @@ impl Collector {
|
||||
Loss::record(&mut self.losses, category, detail);
|
||||
}
|
||||
|
||||
pub fn finish(self) -> ImportReport {
|
||||
/// Records one instance of an effect with no counterpart. The loss is counted now; its line is
|
||||
/// made in [`Collector::finish`], one per effect with the number of instances, so that many
|
||||
/// other losses cannot hide which effects were dropped.
|
||||
/// `detail` is only called for an effect not seen before.
|
||||
pub fn lose_unmapped_effect(&mut self, match_name: &str, detail: impl FnOnce() -> String) {
|
||||
let loss =
|
||||
self.losses.entry(LossCategory::Effects).or_insert_with(|| Loss { category: LossCategory::Effects, count: 0, details: Vec::new(), unlisted: 0 });
|
||||
loss.count = loss.count.saturating_add(1);
|
||||
if let Some(seen) = self.unmapped_effects.get_mut(match_name) {
|
||||
seen.1 = seen.1.saturating_add(1);
|
||||
} else if self.unmapped_effects.len() < MAX_UNMAPPED_EFFECTS {
|
||||
self.unmapped_effects.insert(match_name.to_string(), (detail(), 1));
|
||||
}
|
||||
// (Past the limit an effect is only counted: its name is not worth a line of its own.)
|
||||
}
|
||||
|
||||
pub fn finish(mut self) -> ImportReport {
|
||||
// Every line describes one occurrence, except an unmapped effect's, which describes all
|
||||
// its instances.
|
||||
for loss in self.losses.values_mut() {
|
||||
loss.unlisted = loss.count.saturating_sub(loss.details.len());
|
||||
}
|
||||
if let Some(loss) = self.losses.get_mut(&LossCategory::Effects) {
|
||||
// The most used effects first.
|
||||
let mut named: Vec<(String, usize)> = std::mem::take(&mut self.unmapped_effects).into_values().collect();
|
||||
named.sort_by_key(|n| std::cmp::Reverse(n.1));
|
||||
let described = named.iter().fold(0usize, |sum, (_, n)| sum.saturating_add(*n));
|
||||
loss.unlisted = loss.unlisted.saturating_sub(described);
|
||||
let lines = named.into_iter().map(|(first, n)| match n {
|
||||
1 => first,
|
||||
_ => format!("{first}, and {} more like it", n - 1),
|
||||
});
|
||||
loss.details.splice(0..0, lines);
|
||||
}
|
||||
ImportReport {
|
||||
imported: self.imported,
|
||||
losses: self.losses.into_values().collect(),
|
||||
@@ -362,7 +414,7 @@ mod tests {
|
||||
let json = serde_json::to_string(&collected()).unwrap();
|
||||
assert_eq!(
|
||||
json,
|
||||
r#"{"imported":{"folders":0,"compositions":0,"solids":0,"footage":0,"layers":2,"solidLayers":0,"nullLayers":0,"adjustmentLayers":0,"footageLayers":0,"precompLayers":0,"textLayers":0,"keyframes":0,"expressions":0,"masks":0,"trackMattes":0,"markers":0,"timeRemaps":0,"shapeLayers":0,"shapeItems":0,"cameras":0,"lights":0,"textAnimators":0,"effects":0,"layerStyles":0},"losses":[{"category":"effects","count":2,"details":["fx 1","fx 2"]},{"category":"masks","count":1,"details":["mask 1"]}],"missingFootage":["a.png"],"missingFonts":[],"limitations":["Frame blending, 3D material options, audio levels, guides are not decoded and not imported."],"aeVersion":{"fileVersion":"unknown","savedBy":"unknown","tested":false}}"#
|
||||
r#"{"imported":{"folders":0,"compositions":0,"solids":0,"footage":0,"layers":2,"solidLayers":0,"nullLayers":0,"adjustmentLayers":0,"footageLayers":0,"precompLayers":0,"textLayers":0,"keyframes":0,"expressions":0,"masks":0,"trackMattes":0,"markers":0,"timeRemaps":0,"shapeLayers":0,"shapeItems":0,"cameras":0,"lights":0,"textAnimators":0,"effects":0,"placeholderEffects":0,"layerStyles":0},"losses":[{"category":"effects","count":2,"details":["fx 1","fx 2"],"unlisted":0},{"category":"masks","count":1,"details":["mask 1"],"unlisted":0}],"missingFootage":["a.png"],"missingFonts":[],"limitations":["Frame blending, 3D material options, audio levels, guides are not decoded and not imported."],"aeVersion":{"fileVersion":"unknown","savedBy":"unknown","tested":false}}"#
|
||||
);
|
||||
let back: ImportReport = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(back, collected());
|
||||
@@ -396,4 +448,61 @@ mod tests {
|
||||
assert_eq!(report.losses[0].details.last().map(String::as_str), Some("mask 19"));
|
||||
assert_eq!(built, MAX_DETAILS);
|
||||
}
|
||||
|
||||
/// #3365: with other losses filling the 20 example lines, an effect with no counterpart still
|
||||
/// gets its own line, with how many instances were lost. The category's count is unchanged.
|
||||
#[test]
|
||||
fn every_unmapped_effect_is_named_however_many_other_losses_there_are() {
|
||||
let mut col = Collector::default();
|
||||
for i in 0..30 {
|
||||
col.lose(LossCategory::Effects, format!("C: 'L{i}': parameter P not imported"));
|
||||
}
|
||||
for i in 0..25 {
|
||||
col.lose_unmapped_effect("CC Cylinder", || format!("C: 'Cyl{i}' has the effect 'CC Cylinder' (CC Cylinder)"));
|
||||
}
|
||||
col.lose_unmapped_effect("Pseudo/", || "C: 'D' has the effect 'Dropdown' (Pseudo/)".to_string());
|
||||
let report = col.finish();
|
||||
let effects = report.losses.iter().find(|l| l.category == LossCategory::Effects).unwrap();
|
||||
assert_eq!(effects.count, 56, "30 parameters, 25 cylinders and 1 dropdown");
|
||||
let named = |what: &str| effects.details.iter().filter(|d| d.contains(what)).collect::<Vec<_>>();
|
||||
assert_eq!(named("(CC Cylinder)").len(), 1, "{:?}", effects.details);
|
||||
assert!(named("(CC Cylinder)")[0].contains("24 more"), "{:?}", named("(CC Cylinder)"));
|
||||
// One instance stays the single line it was.
|
||||
assert_eq!(named("(Pseudo/)"), ["C: 'D' has the effect 'Dropdown' (Pseudo/)"]);
|
||||
assert!(effects.details.len() <= MAX_DETAILS + 2, "the examples stay bounded");
|
||||
// 20 parameter lines, the cylinders' line and the dropdown's describe 46; 10 parameters
|
||||
// have no line.
|
||||
assert_eq!(effects.unlisted, 10);
|
||||
// The most used effect comes first.
|
||||
assert!(effects.details[0].contains("(CC Cylinder)"), "{:?}", effects.details);
|
||||
}
|
||||
|
||||
/// #3365: each category says how many of its occurrences have no example line.
|
||||
#[test]
|
||||
fn a_loss_counts_the_occurrences_without_a_line() {
|
||||
let mut col = Collector::default();
|
||||
for i in 0..27 {
|
||||
col.lose(LossCategory::Masks, format!("mask {i}"));
|
||||
}
|
||||
col.lose(LossCategory::ShapeLayers, "one");
|
||||
let report = col.finish();
|
||||
let unlisted = |c| report.losses.iter().find(|l| l.category == c).map(|l| (l.details.len(), l.unlisted));
|
||||
assert_eq!(unlisted(LossCategory::Masks), Some((MAX_DETAILS, 7)));
|
||||
assert_eq!(unlisted(LossCategory::ShapeLayers), Some((1, 0)));
|
||||
}
|
||||
|
||||
/// A file with more than [`MAX_UNMAPPED_EFFECTS`] different unmapped effects names that many
|
||||
/// and counts the rest, whatever room the example lines have.
|
||||
#[test]
|
||||
fn at_most_the_limit_of_unmapped_effects_are_named() {
|
||||
let mut col = Collector::default();
|
||||
for i in 0..=MAX_UNMAPPED_EFFECTS {
|
||||
col.lose_unmapped_effect(&format!("Effect {i}"), || format!("C: 'L' has the effect 'Effect {i}' (Effect {i})"));
|
||||
}
|
||||
let report = col.finish();
|
||||
let effects = report.losses.iter().find(|l| l.category == LossCategory::Effects).unwrap();
|
||||
assert_eq!(effects.count, MAX_UNMAPPED_EFFECTS + 1, "every instance is counted");
|
||||
assert_eq!(effects.details.len(), MAX_UNMAPPED_EFFECTS, "{} lines", effects.details.len());
|
||||
assert_eq!(effects.unlisted, 1, "the effect past the limit has no line");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,18 +457,22 @@ fn shift_copy_offset(p: &mut effectcraft_project::Property, cx: &mut Cx<'_, '_>)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reports a property whose only sampled value is the default 1.
|
||||
/// Reports a property whose only sampled value is the default 1. A record in the default state
|
||||
/// holds After Effects' default (docs/aep-format.md 3.4, state), so it holds nothing to lose.
|
||||
fn not_default(g: &AepGroup<'_, '_>, match_name: &str, what: &str, cx: &mut Cx<'_, '_>) {
|
||||
if let Some(PropNode::Property(p)) = g.get(match_name)
|
||||
&& stored(&p)
|
||||
&& (p.is_animated() || p.expression().is_some() || p.static_value().and_then(|v| v.first().copied()).is_some_and(|v| v != 1.0))
|
||||
{
|
||||
cx.col.lose(LossCategory::ShapeLayers, format!("{}: {what} not imported", cx.what));
|
||||
}
|
||||
}
|
||||
|
||||
/// Reports a property whose only sampled value is 0.
|
||||
/// Reports a property whose only sampled value is 0 (a record in the default state, as above,
|
||||
/// holds nothing to lose).
|
||||
fn not_zero(g: &AepGroup<'_, '_>, match_name: &str, what: &str, cx: &mut Cx<'_, '_>) {
|
||||
if let Some(PropNode::Property(p)) = g.get(match_name)
|
||||
&& stored(&p)
|
||||
&& (p.is_animated() || p.expression().is_some() || p.static_value().and_then(|v| v.first().copied()).is_some_and(|v| v != 0.0))
|
||||
{
|
||||
cx.col.lose(LossCategory::ShapeLayers, format!("{}: {what} not imported", cx.what));
|
||||
@@ -490,3 +494,41 @@ fn unknown(g: &AepGroup<'_, '_>, known: &[&str], what: &str, cx: &mut Cx<'_, '_>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Chunk;
|
||||
|
||||
/// A group holding `match_name` as a one-component property in state `tdsb` with value `v`.
|
||||
fn group_with(match_name: &str, tdsb: u32, v: f64) -> Chunk<'static> {
|
||||
let mut name = match_name.as_bytes().to_vec();
|
||||
name.resize(40, 0);
|
||||
let mut tdb4 = vec![0u8; 124];
|
||||
tdb4[3] = 1;
|
||||
let tdbs = Chunk::list(
|
||||
*b"tdbs",
|
||||
vec![Chunk::data(*b"tdsb", tdsb.to_be_bytes().to_vec()), Chunk::data(*b"tdb4", tdb4), Chunk::data(*b"cdat", v.to_be_bytes().to_vec())],
|
||||
);
|
||||
Chunk::list(*b"tdgp", vec![Chunk::data(*b"tdmn", name), tdbs])
|
||||
}
|
||||
|
||||
fn reported(match_name: &str, tdsb: u32, v: f64, check: fn(&AepGroup<'_, '_>, &str, &str, &mut Cx<'_, '_>)) -> usize {
|
||||
let chunk = group_with(match_name, tdsb, v);
|
||||
let g = AepGroup::from_chunk(&chunk).unwrap();
|
||||
let tm = TimeMap { timescale: 30, frame_duration: 1, rate: crate::import::time::frame_rate(30, 1).unwrap() };
|
||||
let (mut next, mut col) = (1, Collector::default());
|
||||
check(&g, match_name, "it", &mut Cx { what: "L", tm: &tm, ids: &mut Ids(&mut next), col: &mut col });
|
||||
col.finish().losses.iter().map(|l| l.count).sum()
|
||||
}
|
||||
|
||||
/// #3087: a guide rectangle's Shape Direction recorded in the default state (`tdsb` 3, holding
|
||||
/// 2) is After Effects' default and is no loss; written (`tdsb` 1) with 2, it is one.
|
||||
#[test]
|
||||
fn a_default_state_record_is_no_loss() {
|
||||
assert_eq!(reported("ADBE Vector Shape Direction", 3, 2.0, not_default), 0);
|
||||
assert_eq!(reported("ADBE Vector Shape Direction", 1, 2.0, not_default), 1);
|
||||
assert_eq!(reported("ADBE Vector Grad HiLite Length", 3, 5.0, not_zero), 0);
|
||||
assert_eq!(reported("ADBE Vector Grad HiLite Length", 1, 5.0, not_zero), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ pub use container::{
|
||||
GradientType, HueSaturationData, Interpolation, Item, ItemKind, Justification, KernRun, KeyEase, KeyLayout, KeyList, Keyframe, KeyframeKind, LIST, Layer,
|
||||
LayerHeader, LayerRef, LayerType, LevelsData, LightType, Limits, ListHeader, Marker, Mask, MaskAtom, MaskMode, MatteMode, MeshData, NESTED_IDS,
|
||||
Orientation, OrientationKey, ParagraphRun, ParamKind, ParamValue, PathVertex, PolystarType, Project, ProjectSettings, PropEntry, PropGroup, PropNode,
|
||||
Property, Quality, Rational, Renderer, SequenceInfo, ShapeDetail, ShapeGroup, ShapeItem, ShapePath, SpatialKey, Stroke, StrokeCap, StrokeJoin, StyleRun,
|
||||
TextBox, TextDocument, TextValue, TimeBase, TimeDisplay, TimeRemap, TimeRemapKey, TrackMatte, Tracked, Transform, ValueSource, Walk, WorkingSpace,
|
||||
curve_lut, effect_map, identifier_channel_type, is_aepx, parse, parse_any, parse_text_dict, parse_with_limits, read_aepx, read_aepx_with_limits, style_map,
|
||||
write, write_to,
|
||||
Property, PsdLayer, Quality, Rational, Renderer, SequenceInfo, ShapeDetail, ShapeGroup, ShapeItem, ShapePath, SpatialKey, Stroke, StrokeCap, StrokeJoin,
|
||||
StyleRun, TextBox, TextDocument, TextValue, TimeBase, TimeDisplay, TimeRemap, TimeRemapKey, TrackMatte, Tracked, Transform, ValueSource, Walk,
|
||||
WorkingSpace, curve_lut, effect_map, identifier_channel_type, is_aepx, parse, parse_any, parse_text_dict, parse_with_limits, read_aepx,
|
||||
read_aepx_with_limits, style_map, write, write_to,
|
||||
};
|
||||
|
||||
@@ -23,6 +23,7 @@ mod lumetri;
|
||||
mod markers;
|
||||
mod masks;
|
||||
mod mattes;
|
||||
mod placeholders;
|
||||
mod puppet;
|
||||
mod resaved_defaults;
|
||||
mod robustness;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
use effectcraft_aep::import::{ImportOptions, ImportResult, LossCategory, import};
|
||||
use effectcraft_aep::{Body, Chunk, ParamKind, ParamValue, Project as AepProject, parse, write};
|
||||
use effectcraft_keyframe::Value;
|
||||
use effectcraft_project::{Project, PropGroup};
|
||||
use effectcraft_project::{GroupKind, Project, PropGroup};
|
||||
|
||||
const SYNTHETIC: &[u8] = include_bytes!("../../fixtures/synthetic.aep");
|
||||
|
||||
@@ -274,10 +274,15 @@ pub fn import_bytes(bytes: &[u8], effect: &str) -> (Option<PropGroup>, ImportRes
|
||||
let mut project = Project::default();
|
||||
let none = |_: &str| false;
|
||||
let r = import(&mut project, bytes, &ImportOptions { allow_losses: true, ..ImportOptions::new(&none) }).unwrap();
|
||||
let g = project
|
||||
.comps()
|
||||
.flat_map(|(_, c)| c.layers.iter())
|
||||
.find_map(|l| l.props.sub("effects").and_then(|fx| fx.children.iter().filter_map(|n| n.as_group()).find(|g| g.match_id == effect).cloned()));
|
||||
let g = project.comps().flat_map(|(_, c)| c.layers.iter()).find_map(|l| {
|
||||
l.props.sub("effects").and_then(|fx| {
|
||||
fx.children
|
||||
.iter()
|
||||
.filter_map(|n| n.as_group())
|
||||
.find(|g| g.match_id == effect || matches!(&g.kind, GroupKind::Effect { effect: e } if e == effect))
|
||||
.cloned()
|
||||
})
|
||||
});
|
||||
(g, r)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
//! #3087: an effect with no EffectCraft counterpart (a third-party plug-in, a pseudo effect of
|
||||
//! expression controls) arrives as a placeholder holding its parameters, so expressions that read
|
||||
//! them keep working; it renders nothing and is still reported. Each effect is built into
|
||||
//! `synthetic.aep` (`fx_inject`) in place of its Glow, on the 8 x 8 sequence layer.
|
||||
|
||||
use crate::import::fx_inject::{Default, Fx, bytes_with, import_bytes, losses, other, popup, run, slider, v};
|
||||
use effectcraft_aep::import::LossCategory;
|
||||
use effectcraft_aep::{Body, Chunk, ParamKind as K, parse, write};
|
||||
use effectcraft_keyframe::Value;
|
||||
use effectcraft_project::{GroupKind, ParamUi, PropGroup};
|
||||
|
||||
/// A third-party effect with one parameter of each kept kind, inside a twirl-down group.
|
||||
fn third_party(values: Vec<(usize, f64)>, keys: Vec<(usize, Vec<(i32, f64)>)>) -> Fx {
|
||||
Fx {
|
||||
match_name: "CC Cylinder",
|
||||
name: "CC Cylinder",
|
||||
defs: vec![
|
||||
other(K::GroupStart, "Light", Default::None),
|
||||
slider(K::FloatSlider, "Radius (%)", 30.0, (0.0, 30000.0)),
|
||||
other(K::GroupEnd, "", Default::None),
|
||||
other(K::Angle, "Rotation X", Default::Number(12.5)),
|
||||
other(K::Checkbox, "Wire", Default::Number(1.0)),
|
||||
other(K::Color, "Light Color", Default::Color([255, 51, 102, 204])),
|
||||
other(K::Point, "Position", Default::Point(0.25, 0.5)),
|
||||
popup("Render", 1, &["Full", "(-", "Outside", "Inside"]),
|
||||
slider(K::IntSlider, "Count", 3.0, (0.0, 50.0)),
|
||||
],
|
||||
values,
|
||||
data: vec![],
|
||||
keys,
|
||||
}
|
||||
}
|
||||
|
||||
fn placeholder_of(fx: &Fx) -> (PropGroup, effectcraft_aep::import::ImportResult) {
|
||||
let (g, r) = run(fx, fx.match_name);
|
||||
(g.expect("the placeholder"), r)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_effect_without_a_counterpart_is_kept_with_its_values() {
|
||||
// Radius 37.5 and Render "Outside" (3: the separator counts) stored, the rest at defaults.
|
||||
let (g, r) = placeholder_of(&third_party(vec![(2, 37.5), (8, 3.0)], vec![]));
|
||||
assert_eq!(g.kind, GroupKind::Effect { effect: "CC Cylinder".into() });
|
||||
assert_eq!(g.name, "CC Cylinder");
|
||||
let names: Vec<(&str, &str)> = g.props().map(|p| (p.match_id.as_str(), p.name.as_str())).collect();
|
||||
assert_eq!(
|
||||
names,
|
||||
[
|
||||
("CC Cylinder-0002", "Radius (%)"),
|
||||
("CC Cylinder-0004", "Rotation X"),
|
||||
("CC Cylinder-0005", "Wire"),
|
||||
("CC Cylinder-0006", "Light Color"),
|
||||
("CC Cylinder-0007", "Position"),
|
||||
("CC Cylinder-0008", "Render"),
|
||||
("CC Cylinder-0009", "Count"),
|
||||
],
|
||||
"flat, in order, without the group markers"
|
||||
);
|
||||
assert_eq!(v(&g, "CC Cylinder-0002"), Value::Scalar(37.5));
|
||||
assert_eq!(v(&g, "CC Cylinder-0004"), Value::Scalar(12.5));
|
||||
assert_eq!(v(&g, "CC Cylinder-0005"), Value::Bool(true));
|
||||
let Value::Color(c) = v(&g, "CC Cylinder-0006") else { panic!("a colour") };
|
||||
assert!(c.iter().zip([0.2, 0.4, 0.8, 1.0]).all(|(a, b)| (a - b).abs() < 1e-6), "{c:?}");
|
||||
assert_eq!(v(&g, "CC Cylinder-0007"), Value::Vec2([2.0, 4.0]), "layer pixels");
|
||||
assert_eq!(v(&g, "CC Cylinder-0008"), Value::Enum(2), "item 3 of 4, 0-based");
|
||||
assert_eq!(v(&g, "CC Cylinder-0009"), Value::Scalar(3.0));
|
||||
let render = g.prop("CC Cylinder-0008").unwrap();
|
||||
assert_eq!(render.ui, ParamUi::Popup { options: ["Full", "(-", "Outside", "Inside"].map(String::from).to_vec() });
|
||||
// Still reported, once, and counted apart from the imported effects.
|
||||
let found = losses(&r, LossCategory::Effects, "CC Cylinder");
|
||||
assert!(found.len() == 1 && found[0].ends_with("has the effect 'CC Cylinder' (CC Cylinder), kept without rendering"), "{found:?}");
|
||||
assert_eq!((r.report.imported.placeholder_effects, r.report.imported.effects), (1, 1), "the placeholder and the Levels");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keys_and_expressions_of_a_placeholder_are_kept() {
|
||||
let fx = third_party(vec![(4, 12.5)], vec![(2, vec![(0, 10.0), (25, 20.0)])]);
|
||||
let bytes = with_expression(&bytes_with(&fx), "CC Cylinder-0004", "time * 90");
|
||||
let (g, _) = import_bytes(&bytes, "CC Cylinder");
|
||||
let g = g.unwrap();
|
||||
let radius = g.prop("CC Cylinder-0002").unwrap();
|
||||
assert_eq!(radius.keys.iter().map(|k| k.value.clone()).collect::<Vec<_>>(), [Value::Scalar(10.0), Value::Scalar(20.0)]);
|
||||
let angle = g.prop("CC Cylinder-0004").unwrap();
|
||||
assert_eq!(angle.expr.as_ref().map(|e| (e.text.as_str(), e.enabled)), Some(("time * 90", true)));
|
||||
}
|
||||
|
||||
/// A pseudo effect's parameter match names (`Pseudo/0.25-0001`) are no property path segment:
|
||||
/// its parameters are `param<index>`, found by their names.
|
||||
#[test]
|
||||
fn a_pseudo_effect_is_kept_with_its_parameter_names() {
|
||||
let fx = Fx {
|
||||
match_name: "Pseudo/0.25",
|
||||
name: "Settings",
|
||||
defs: vec![slider(K::FloatSlider, "Speed", 1.0, (0.0, 100.0)), popup("Mode", 2, &["A", "B"])],
|
||||
values: vec![(1, 4.0)],
|
||||
data: vec![],
|
||||
keys: vec![],
|
||||
};
|
||||
let (g, r) = placeholder_of(&fx);
|
||||
assert_eq!(g.prop("param1").map(|p| (p.name.as_str(), p.value.clone())), Some(("Speed", Value::Scalar(4.0))));
|
||||
assert_eq!(g.prop("param2").map(|p| (p.name.as_str(), p.value.clone())), Some(("Mode", Value::Enum(1))));
|
||||
assert_eq!(losses(&r, LossCategory::Effects, "Pseudo/0.25").len(), 1);
|
||||
// The effect id stays the match name (Find Missing Effects, expressions); the group's match id
|
||||
// is one path segment, so a match path (keyframe paste, pick whip) finds its parameters again.
|
||||
assert_eq!(g.kind, GroupKind::Effect { effect: "Pseudo/0.25".into() });
|
||||
let speed = g.prop("param1").map(|p| p.uid);
|
||||
let layer = PropGroup::new(0, "layer", "Layer").with(PropGroup::new(0, "effects", "Effects").with(g));
|
||||
let path = speed.and_then(|u| layer.match_path_of(u));
|
||||
assert_eq!(path.as_deref().and_then(|p| layer.prop(p)).map(|p| p.name.as_str()), Some("Speed"), "{path:?}");
|
||||
}
|
||||
|
||||
/// A float slider keeps its valid range and slider range, so Effect Controls drags and clamps it
|
||||
/// as After Effects does; a range that is not a range stays a plain number.
|
||||
#[test]
|
||||
fn a_float_slider_keeps_its_range() {
|
||||
let fx = Fx {
|
||||
match_name: "Pseudo/r",
|
||||
name: "Rig",
|
||||
defs: vec![slider(K::FloatSlider, "Speed", 1.0, (-10.0, 100.0)), slider(K::FloatSlider, "Odd", 1.0, (5.0, 1.0))],
|
||||
values: vec![],
|
||||
data: vec![],
|
||||
keys: vec![],
|
||||
};
|
||||
let (g, _) = placeholder_of(&fx);
|
||||
let ui = |id: &str| g.prop(id).map(|p| p.ui.clone());
|
||||
assert_eq!(ui("param1"), Some(ParamUi::Slider { min: -10.0, max: 100.0, slider_min: -10.0, slider_max: 100.0, decimals: 2 }));
|
||||
assert_eq!(ui("param2"), Some(ParamUi::Number));
|
||||
}
|
||||
|
||||
/// A popup whose definition is borrowed (the project copy) and has no value record: its default
|
||||
/// slot holds another copy's selection, so the first item is used and that is reported.
|
||||
#[test]
|
||||
fn a_borrowed_popup_without_a_record_takes_the_first_item_and_is_reported() {
|
||||
let bytes = without_instance_definitions(&bytes_with(&third_party(vec![], vec![])));
|
||||
let (g, r) = import_bytes(&bytes, "CC Cylinder");
|
||||
assert_eq!(v(&g.unwrap(), "CC Cylinder-0008"), Value::Enum(0));
|
||||
let found = losses(&r, LossCategory::Effects, "CC Cylinder");
|
||||
assert!(found.iter().any(|d| d.contains("Render") && d.contains("default item is not known")), "{found:?}");
|
||||
}
|
||||
|
||||
/// A popup default slot of 0 (no item; After Effects' save of a project it opened) takes the first
|
||||
/// item and is reported, not read as damaged data.
|
||||
#[test]
|
||||
fn a_popup_default_of_no_item_takes_the_first_item() {
|
||||
let fx = Fx { match_name: "Some Fx", name: "Some", defs: vec![popup("Mode", 0, &["A", "B"])], values: vec![], data: vec![], keys: vec![] };
|
||||
let (g, r) = placeholder_of(&fx);
|
||||
assert_eq!(v(&g, "Some Fx-0001"), Value::Enum(0));
|
||||
let found = losses(&r, LossCategory::Effects, "Some Fx");
|
||||
assert!(found.iter().any(|d| d.contains("default item is not stored")), "{found:?}");
|
||||
assert!(losses(&r, LossCategory::DamagedData, "Some Fx").is_empty());
|
||||
}
|
||||
|
||||
/// A popup past the 1024 items kept: the cut is reported, and a selection among the cut items
|
||||
/// takes the first item and is reported as such, not as damaged data.
|
||||
#[test]
|
||||
fn a_popup_past_the_items_kept_is_reported() {
|
||||
let items: &'static [&'static str] = Box::leak((0..1100).map(|i| &*Box::leak(format!("I{i}").into_boxed_str())).collect::<Vec<_>>().into_boxed_slice());
|
||||
let fx = Fx { match_name: "Long Fx", name: "Long", defs: vec![popup("Pick", 1, items)], values: vec![(1, 1050.0)], data: vec![], keys: vec![] };
|
||||
let (g, r) = placeholder_of(&fx);
|
||||
let pick = g.prop("Long Fx-0001").unwrap();
|
||||
assert_eq!(pick.value, Value::Enum(0));
|
||||
assert!(matches!(&pick.ui, ParamUi::Popup { options } if options.len() == 1024));
|
||||
let found = losses(&r, LossCategory::Effects, "Long Fx");
|
||||
assert!(found.iter().any(|d| d.contains("past the first 1024")), "{found:?}");
|
||||
assert!(found.iter().any(|d| d.contains("item 1050 is past the items kept")), "{found:?}");
|
||||
assert!(losses(&r, LossCategory::DamagedData, "Long Fx").is_empty());
|
||||
}
|
||||
|
||||
/// Many large popups stay within the import's budget: what is beyond is reported, not kept.
|
||||
#[test]
|
||||
fn a_hostile_placeholder_stays_bounded() {
|
||||
let items: &'static [&'static str] = Box::leak((0..1000).map(|i| &*Box::leak(format!("I{i}").into_boxed_str())).collect::<Vec<_>>().into_boxed_slice());
|
||||
let fx = Fx { match_name: "Big Fx", name: "Big", defs: (0..300).map(|_| popup("P", 1, items)).collect(), values: vec![], data: vec![], keys: vec![] };
|
||||
let (g, r) = placeholder_of(&fx);
|
||||
let kept = g.props().count();
|
||||
assert!(kept > 0 && kept < 300, "{kept}");
|
||||
assert!(g.props().map(|p| if let ParamUi::Popup { options } = &p.ui { options.len() + 1 } else { 1 }).sum::<usize>() <= 200_000);
|
||||
assert!(losses(&r, LossCategory::Effects, "Big Fx").iter().any(|d| d.contains("placeholder parameters")));
|
||||
}
|
||||
|
||||
/// Two instances of an effect whose parameters fill the import's budget: the first keeps what fits
|
||||
/// and stops at the first parameter past it; the second is reported without a placeholder (its
|
||||
/// shared definitions are not read again).
|
||||
#[test]
|
||||
fn an_effect_past_the_spent_budget_gets_no_placeholder() {
|
||||
let items: &'static [&'static str] = Box::leak((0..1000).map(|i| &*Box::leak(format!("I{i}").into_boxed_str())).collect::<Vec<_>>().into_boxed_slice());
|
||||
let fx = Fx { match_name: "Big Fx", name: "Big", defs: (0..300).map(|_| popup("P", 1, items)).collect(), values: vec![], data: vec![], keys: vec![] };
|
||||
let bytes = twice(&bytes_with(&fx), "Big Fx");
|
||||
let mut project = effectcraft_project::Project::default();
|
||||
let none = |_: &str| false;
|
||||
let r = effectcraft_aep::import::import(
|
||||
&mut project,
|
||||
&bytes,
|
||||
&effectcraft_aep::import::ImportOptions { allow_losses: true, ..effectcraft_aep::import::ImportOptions::new(&none) },
|
||||
)
|
||||
.unwrap();
|
||||
let placeholders: Vec<usize> = project
|
||||
.comps()
|
||||
.flat_map(|(_, c)| c.layers.iter())
|
||||
.filter_map(|l| l.props.sub("effects"))
|
||||
.flat_map(|fx| fx.children.iter().filter_map(|n| n.as_group()).filter(|g| g.match_id == "Big Fx").map(|g| g.props().count()).collect::<Vec<_>>())
|
||||
.collect();
|
||||
// 199 popups of 1000 items (1001 each) fit in 200 000; the 200th stops the first instance.
|
||||
assert_eq!(placeholders, [199]);
|
||||
let found = losses(&r, LossCategory::Effects, "Big Fx");
|
||||
assert_eq!(found.iter().filter(|d| d.contains("and the ones after it not kept")).count(), 1, "{found:?}");
|
||||
assert!(found.iter().any(|d| d.contains("not kept (past the import's placeholder parameters)")), "{found:?}");
|
||||
assert!(found.iter().any(|d| d.contains("kept without rendering") && !d.contains("more like it")), "the kept instance alone: {found:?}");
|
||||
}
|
||||
|
||||
/// Parameters a placeholder does not keep (markers, arbitrary data) still spend the budget: a
|
||||
/// definition of very many of them, shared by many instances, is not walked again for each one.
|
||||
#[test]
|
||||
fn parameters_not_kept_still_spend_the_budget() {
|
||||
let defs = (0..200_000).map(|i| other(if i % 2 == 0 { K::NoData } else { K::ArbitraryData }, "X", Default::None)).collect();
|
||||
let fx = Fx { match_name: "Odd Fx", name: "Odd", defs, values: vec![], data: vec![], keys: vec![] };
|
||||
let (_, r) = import_bytes(&twice(&bytes_with(&fx), "Odd Fx"), "Odd Fx");
|
||||
let found = losses(&r, LossCategory::Effects, "Odd Fx");
|
||||
assert!(found.iter().any(|d| d.contains("not kept (past the import's placeholder parameters)")), "{found:?}");
|
||||
}
|
||||
|
||||
/// Long popup item names spend the budget by their length, so the names kept stay bounded.
|
||||
#[test]
|
||||
fn long_popup_item_names_spend_the_budget() {
|
||||
let long = "x".repeat(1600);
|
||||
let items: &'static [&'static str] =
|
||||
Box::leak((0..1024).map(|i| &*Box::leak(format!("{long}{i}").into_boxed_str())).collect::<Vec<_>>().into_boxed_slice());
|
||||
let fx = Fx { match_name: "Long Fx", name: "Long", defs: vec![popup("A", 1, items), popup("B", 1, items)], values: vec![], data: vec![], keys: vec![] };
|
||||
let (g, r) = placeholder_of(&fx);
|
||||
assert_eq!(g.props().count(), 1);
|
||||
assert!(losses(&r, LossCategory::Effects, "Long Fx").iter().any(|d| d.contains("placeholder parameters")));
|
||||
}
|
||||
|
||||
/// `bytes` with the layer's instance of `match_name` (its `tdmn` and `sspc`) repeated after it.
|
||||
fn twice(bytes: &[u8], match_name: &str) -> Vec<u8> {
|
||||
let mut file = parse(bytes).unwrap();
|
||||
let mut done = 0;
|
||||
edit(&mut file.chunks, false, &mut |c, in_project_copy| {
|
||||
let Body::List { children, .. } = &mut c.body else { return };
|
||||
if in_project_copy {
|
||||
return;
|
||||
}
|
||||
let Some(at) = children.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(match_name.as_bytes())))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let pair: Vec<Chunk<'_>> = children[at..at + 2].to_vec();
|
||||
children.splice(at + 2..at + 2, pair);
|
||||
done += 1;
|
||||
});
|
||||
assert_eq!(done, 1, "one instance");
|
||||
write(&file).unwrap()
|
||||
}
|
||||
|
||||
/// `bytes` with the expression `text` on the parameter record `param` (enabled: `tdb4` byte 120
|
||||
/// 1, docs/aep-format.md 3.6).
|
||||
fn with_expression(bytes: &[u8], param: &str, text: &str) -> Vec<u8> {
|
||||
let mut file = parse(bytes).unwrap();
|
||||
let mut done = 0;
|
||||
edit(&mut file.chunks, false, &mut |c, _| {
|
||||
let Body::List { children, .. } = &mut c.body else { return };
|
||||
let Some(at) = children.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(param.as_bytes()))) else {
|
||||
return;
|
||||
};
|
||||
let Some(Body::List { children: record, .. }) = children.get_mut(at + 1).map(|k| &mut k.body) else { return };
|
||||
for k in record.iter_mut().filter(|k| k.is(b"tdb4")) {
|
||||
if let Body::Data(d) = &mut k.body
|
||||
&& let Some(b) = d.to_mut().get_mut(120)
|
||||
{
|
||||
*b = 1;
|
||||
}
|
||||
}
|
||||
record.push(Chunk::data(*b"Utf8", text.as_bytes().to_vec()));
|
||||
done += 1;
|
||||
});
|
||||
assert!(done >= 1);
|
||||
write(&file).unwrap()
|
||||
}
|
||||
|
||||
/// `bytes` with the layer's instance of `CC Cylinder` stripped of its own definitions (an empty
|
||||
/// `parT`): it borrows the project copy's (`EfdG`).
|
||||
fn without_instance_definitions(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut file = parse(bytes).unwrap();
|
||||
let mut done = 0;
|
||||
edit(&mut file.chunks, false, &mut |c, in_project_copy| {
|
||||
let Body::List { children, .. } = &mut c.body else { return };
|
||||
if in_project_copy {
|
||||
return;
|
||||
}
|
||||
let Some(at) = children.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(&b"CC Cylinder"[..])))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let Some(sspc) = children.get_mut(at + 1).filter(|k| k.is_list(b"sspc")) else { return };
|
||||
for k in sspc.children_mut() {
|
||||
if let Body::List { list_type, children } = &mut k.body
|
||||
&& list_type == b"parT"
|
||||
{
|
||||
children.clear();
|
||||
done += 1;
|
||||
}
|
||||
}
|
||||
});
|
||||
assert_eq!(done, 1, "one instance");
|
||||
write(&file).unwrap()
|
||||
}
|
||||
|
||||
/// Calls `f` on every chunk with whether it is inside the project's effect definitions (`EfdG`).
|
||||
fn edit(chunks: &mut [Chunk<'_>], in_efdg: bool, f: &mut dyn FnMut(&mut Chunk<'_>, bool)) {
|
||||
for c in chunks {
|
||||
let inside = in_efdg || c.is_list(b"EfdG");
|
||||
f(c, inside);
|
||||
edit(c.children_mut(), inside, f);
|
||||
}
|
||||
}
|
||||
@@ -842,3 +842,187 @@ fn a_default_state_colour_record_holds_the_value() {
|
||||
assert_eq!(colour(&default_state), colour(&written));
|
||||
assert_ne!(colour(&default_state), colour(&unchanged), "the recorded colour, not the default");
|
||||
}
|
||||
|
||||
/// The project with its png item made one layer of a Photoshop file `art.psd`: the `opti` of
|
||||
/// docs/aep-format.md 3.3 (tag `8BPS`, the layer index at 14, a 1000 x 1000 document at 32 and 36,
|
||||
/// its bounds at 78-94, its name at 344) and the item `size` big.
|
||||
fn as_psd_layer(index: i32, bounds: [i32; 4], name: &str, size: (u32, u32)) -> Vec<u8> {
|
||||
let mut opti = vec![0u8; 602];
|
||||
opti[..4].copy_from_slice(b"8BPS");
|
||||
opti[14..18].copy_from_slice(&index.to_be_bytes());
|
||||
opti[32..36].copy_from_slice(&1000u32.to_le_bytes());
|
||||
opti[36..40].copy_from_slice(&1000u32.to_le_bytes());
|
||||
for (i, v) in bounds.iter().enumerate() {
|
||||
opti[78 + 4 * i..82 + 4 * i].copy_from_slice(&v.to_le_bytes());
|
||||
}
|
||||
opti[344..344 + name.len()].copy_from_slice(name.as_bytes());
|
||||
let alias = br#"{"fullpath":"C:\\x\\art.psd","platform":1,"target_is_folder":false}"#.to_vec();
|
||||
mutate(&mut |c| {
|
||||
if !c.is_list(b"Pin ") {
|
||||
return;
|
||||
}
|
||||
let Body::List { children, .. } = &mut c.body else { return };
|
||||
if !children.iter().any(|k| k.is(b"opti") && k.bytes().is_some_and(|d| d.get(..4) == Some(&b"png!"[..]))) {
|
||||
return;
|
||||
}
|
||||
// No sequence naming between the alias and the `opti`.
|
||||
children.retain(|k| k.is(b"sspc") || k.is(b"opti") || k.is_list(b"Als2"));
|
||||
for k in children.iter_mut() {
|
||||
if k.is(b"opti") {
|
||||
k.body = Body::Data(opti.clone().into());
|
||||
} else if k.is(b"sspc") {
|
||||
set_data(k, 30, &size.0.to_be_bytes());
|
||||
set_data(k, 34, &size.1.to_be_bytes());
|
||||
}
|
||||
}
|
||||
visit(children, &mut |k| {
|
||||
if k.is(b"alas") {
|
||||
k.body = Body::Data(alias.clone().into());
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/// #3087: an item made from one layer of a Photoshop file shows that layer, named
|
||||
/// `<layer>/<file>`; it showed the whole document, named after the file. Item as big as the layer:
|
||||
/// Retain Layer Sizes; as big as the document: the layer on the document.
|
||||
#[test]
|
||||
fn a_photoshop_layer_item_shows_its_layer() {
|
||||
for (size, layer_size) in [((48, 15), true), ((1000, 1000), false)] {
|
||||
let (p, r) = run(&as_psd_layer(7, [749, 800, 764, 848], "Tëst", size), true);
|
||||
r.unwrap();
|
||||
let (name, f) = p.items.values().find_map(|i| if let ItemKind::Footage(f) = &i.kind { Some((i.name.clone(), f.clone())) } else { None }).unwrap();
|
||||
assert_eq!(name, "Tëst/art.psd");
|
||||
let l = f.layer.expect("the layer");
|
||||
assert_eq!((l.index, l.name.as_str(), l.layer_size, (f.width, f.height)), (7, "Tëst", layer_size, size));
|
||||
}
|
||||
}
|
||||
|
||||
/// A layer as big as the document but off its corner fits both framings: shown at its own bounds
|
||||
/// and reported. One at the corner, or bigger than the document, reads one way and is not. An item
|
||||
/// as big as neither is shown on the document and reported.
|
||||
#[test]
|
||||
fn a_photoshop_layer_as_big_as_the_document_off_its_corner_is_reported() {
|
||||
let framing = |bounds, size| {
|
||||
let (p, r) = run(&as_psd_layer(3, bounds, "Moved", size), true);
|
||||
let found = losses(&r.unwrap(), LossCategory::UnsupportedItems);
|
||||
let said = |what: &str| found.iter().any(|d| d.contains("'Moved/art.psd'") && d.contains(what));
|
||||
(the_footage(&p).layer.map(|l| l.layer_size), said("as big as the document"), said("neither the Photoshop layer's size"))
|
||||
};
|
||||
assert_eq!(framing([5, 10, 1005, 1010], (1000, 1000)), (Some(true), true, false));
|
||||
assert_eq!(framing([0, 0, 1000, 1000], (1000, 1000)), (Some(true), false, false));
|
||||
assert_eq!(framing([9, -346, 1083, 1086], (1432, 1074)), (Some(true), false, false));
|
||||
assert_eq!(framing([749, 800, 764, 848], (1000, 1000)), (Some(false), false, false));
|
||||
// An item as big as neither the layer nor the document: shown on the document, reported.
|
||||
assert_eq!(framing([5, 10, 1005, 1010], (800, 800)), (Some(false), false, true));
|
||||
}
|
||||
|
||||
/// #3087: a Photoshop layer without pixels (an adjustment layer) keeps the merged image and is
|
||||
/// reported; the merged image itself (index -1) is no layer.
|
||||
#[test]
|
||||
fn a_photoshop_layer_without_pixels_keeps_the_merged_image() {
|
||||
let (p, r) = run(&as_psd_layer(4, [0; 4], "Levels 1", (1000, 1000)), true);
|
||||
let found = losses(&r.unwrap(), LossCategory::UnsupportedItems);
|
||||
assert!(found.iter().any(|d| d.contains("'Levels 1/art.psd'") && d.contains("without pixels")), "{found:?}");
|
||||
assert_eq!(the_footage(&p).layer, None);
|
||||
let (p, _) = run(&as_psd_layer(-1, [0; 4], "", (1000, 1000)), true);
|
||||
assert_eq!(the_footage(&p).layer, None);
|
||||
}
|
||||
|
||||
/// #3087: an expression on Orientation arrives with the stored value, as on any property; it was
|
||||
/// dropped together with the value and reported as "not decoded".
|
||||
#[test]
|
||||
fn an_orientation_expression_is_imported_with_its_value() {
|
||||
let bytes = mutate(&mut |c| {
|
||||
if !c.is_list(b"otst") {
|
||||
return;
|
||||
}
|
||||
for child in c.children_mut() {
|
||||
if child.is_list(b"tdbs") {
|
||||
let Body::List { children, .. } = &mut child.body else { continue };
|
||||
for k in children.iter_mut() {
|
||||
if k.is(b"tdsb") {
|
||||
set_data(k, 0, &1u32.to_be_bytes());
|
||||
} else if k.is(b"tdb4") {
|
||||
set_data(k, 120, &[1]);
|
||||
}
|
||||
}
|
||||
children.push(Chunk::data(*b"Utf8", b"[0, time * 90, 0]".to_vec()));
|
||||
} else if child.is_list(b"otky") {
|
||||
for k in child.children_mut() {
|
||||
let degrees: Vec<u8> = [10.0f64, 20.0, 30.0].iter().flat_map(|d| d.to_be_bytes()).collect();
|
||||
set_data(k, 0, °rees);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
let (p, r) = run(&bytes, true);
|
||||
assert_eq!(losses(&r.unwrap(), LossCategory::TransformProperties), Vec::<String>::new());
|
||||
let mut seen = 0;
|
||||
for item in p.items.values() {
|
||||
let ItemKind::Comp(c) = &item.kind else { continue };
|
||||
for layer in &c.layers {
|
||||
let Some(o) = layer.transform().and_then(|t| t.prop("orientation")).filter(|o| o.expr.is_some()) else { continue };
|
||||
assert_eq!(o.value, effectcraft_keyframe::Value::Vec3([10.0, 20.0, 30.0]), "{}", layer.name);
|
||||
assert_eq!(o.expr.as_ref().map(|e| (e.text.as_str(), e.enabled)), Some(("[0, time * 90, 0]", true)), "{}", layer.name);
|
||||
seen += 1;
|
||||
}
|
||||
}
|
||||
assert!(seen > 0);
|
||||
}
|
||||
|
||||
/// `SYNTHETIC` with Levels' Channel and Clip to Output Black / White popups' item names (`pdnm`)
|
||||
/// rewritten to the French labels a French-language After Effects stores there, keeping the same
|
||||
/// item order so the index each popup is set to is unaffected.
|
||||
fn with_french_levels_popups() -> Vec<u8> {
|
||||
let (mut channel, mut clip) = (0, 0);
|
||||
let bytes = mutate(&mut |c| {
|
||||
if !c.is(b"Utf8") {
|
||||
return;
|
||||
}
|
||||
if c.bytes() == Some(b"RGB|Red|Green|Blue|Alpha".as_slice()) {
|
||||
*c = Chunk::data(*b"Utf8", b"RVB|Rouge|Vert|Bleu|Alpha".to_vec());
|
||||
channel += 1;
|
||||
} else if c.bytes() == Some(b"On|Off|Off for 32 bpc Color".as_slice()) {
|
||||
*c = Chunk::data(*b"Utf8", "Activé|Désactivé|Désactivé pour la couleur 32 bpc".as_bytes().to_vec());
|
||||
clip += 1;
|
||||
}
|
||||
});
|
||||
assert_eq!(channel, 2, "Levels' Channel pdnm");
|
||||
assert_eq!(clip, 4, "Levels' two Clip pdnm (Black and White), each stored twice");
|
||||
bytes
|
||||
}
|
||||
|
||||
/// A popup's item names in the file are matched to EffectCraft's own (English) ones by name
|
||||
/// (`popup_key`): a project saved by a French After Effects stores "RVB", "Activé", "Désactivé
|
||||
/// pour la couleur 32 bpc" for Levels' Channel and Clip to Output Black / White (observed in a
|
||||
/// real "Niveaux" effect, After Effects 2026 26.0.0x67). Before `popup_alias`, none of these
|
||||
/// matched EffectCraft's English-only options, so each came back "has no counterpart, default
|
||||
/// used" and the real selection was silently lost.
|
||||
#[test]
|
||||
fn french_levels_popup_items_are_recognized() {
|
||||
let (unchanged, r1) = run(SYNTHETIC, true);
|
||||
let (french, r2) = run(&with_french_levels_popups(), true);
|
||||
let (r1, r2) = (r1.unwrap(), r2.unwrap());
|
||||
let found = losses(&r2, LossCategory::Effects);
|
||||
assert!(!found.iter().any(|d| d.contains("has no counterpart")), "{found:?}");
|
||||
for id in ["channel", "clipToOutputBlack", "clipToOutputWhite"] {
|
||||
let want: Vec<_> = effect_values(&unchanged).into_iter().filter(|(p, _)| p == id).collect();
|
||||
let got: Vec<_> = effect_values(&french).into_iter().filter(|(p, _)| p == id).collect();
|
||||
assert_eq!(got, want, "{id}");
|
||||
}
|
||||
assert_eq!(r1.report.imported.effects, r2.report.imported.effects);
|
||||
}
|
||||
|
||||
/// `popup_alias` must cover every item of a popup it claims to know, not only the one a first
|
||||
/// report happened to show: Channel's French "Rouge", "Vert", "Bleu" ("Alpha" is the same word in
|
||||
/// both languages) alongside the already-covered "RVB" (`effect_map.rs`, reviewed on #3432).
|
||||
#[test]
|
||||
fn french_levels_channel_aliases_cover_every_item() {
|
||||
use effectcraft_aep::effect_map::popup_alias;
|
||||
for (french, english) in [("rvb", "rgb"), ("rouge", "red"), ("vert", "green"), ("bleu", "blue")] {
|
||||
assert_eq!(popup_alias(french), Some(english), "{french}");
|
||||
}
|
||||
// "Alpha" needs no alias: the same word in both languages.
|
||||
assert_eq!(popup_alias("alpha"), None);
|
||||
}
|
||||
|
||||
@@ -65,6 +65,8 @@ const REFUSED: &[&str] = &[
|
||||
"file.revealInFinder",
|
||||
"layer.openSource",
|
||||
"help.revealLogFile",
|
||||
// Hands the footage file to the platform's default application.
|
||||
"edit.editOriginal",
|
||||
"file.autoSave",
|
||||
// The Plug-in Library downloads code, installs it to load at start-up and reads `file://` catalogs.
|
||||
"library.refresh",
|
||||
@@ -73,8 +75,15 @@ const REFUSED: &[&str] = &[
|
||||
"library.uninstall",
|
||||
"library.loadInstalled",
|
||||
"library.preview",
|
||||
// Watch Folder renders the projects it finds to the outputs they name and writes status
|
||||
// files into the watched folder: neither is a path the agent gives, so neither is checked.
|
||||
"file.watchFolder",
|
||||
"file.watchFolder.poll",
|
||||
];
|
||||
|
||||
/// Most numbered folders `check_render_folder` inspects for one output; more are refused.
|
||||
const MAX_NUMBERED_FOLDERS: usize = 4096;
|
||||
|
||||
/// Commands that read files named by these parameters.
|
||||
const READS: &[(&str, &[&str])] = &[
|
||||
("file.open", &["path"]),
|
||||
@@ -88,7 +97,6 @@ const READS: &[(&str, &[&str])] = &[
|
||||
("file.replaceFootage", &["path"]),
|
||||
("file.relinkFootage", &["folder"]),
|
||||
("file.setProxy", &["path"]),
|
||||
("file.watchFolder", &["folder"]),
|
||||
("anim.applyPreset", &["path"]),
|
||||
("anim.applyRecentPreset", &["path"]),
|
||||
("keys.rpfCameraImport", &["path"]),
|
||||
@@ -102,7 +110,6 @@ const READS: &[(&str, &[&str])] = &[
|
||||
// unchecked.
|
||||
("essential.templateInfo", &["path"]),
|
||||
("file.importProject", &["path"]),
|
||||
("file.watchFolder.poll", &["folder"]),
|
||||
("footage.relink", &["path"]),
|
||||
("layer.newModel", &["path"]),
|
||||
("mediaBrowser.fileInfo", &["path"]),
|
||||
@@ -143,6 +150,8 @@ const WRITES: &[(&str, &[&str])] = &[
|
||||
("shortcuts.export", &["path"]),
|
||||
("renderQueue.add", &["output", "path"]),
|
||||
("file.exportTimeline", &["path"]),
|
||||
("journal.export", &["path"]),
|
||||
("help.exportRepro", &["path"]),
|
||||
// `path` / `output` may also be a file-name template, as for render.preRender.
|
||||
("file.createProxy", &["path", "output"]),
|
||||
];
|
||||
@@ -200,6 +209,80 @@ impl Confinement {
|
||||
if self.roots(access).any(|r| real.starts_with(r)) { Ok(real) } else { Err(denied("it is outside the automation roots")) }
|
||||
}
|
||||
|
||||
/// A render output's folder holds no symbolic link that leads outside the write roots. An
|
||||
/// image sequence is written to numbered names (`out_[#####].png` → `out_00001.png`, and a
|
||||
/// sequence output without `#` gets `_00001` too) that the check of its template never
|
||||
/// resolved, and each frame file is created through whatever link already sits at its name,
|
||||
/// truncating the file it points to. A link that can't be resolved counts as outside: the
|
||||
/// render would create its target. When the frame number is in a folder name, the same goes for
|
||||
/// each existing numbered folder and the folder it holds the frames in.
|
||||
fn check_render_folder(&self, output: &str) -> Result<()> {
|
||||
let real = self.check(output, Access::Write)?;
|
||||
if let Some(dir) = real.parent() {
|
||||
self.check_no_outward_links(dir, output)?;
|
||||
}
|
||||
// The frame number may sit in a folder name (`shots_[##]/f_[#####].png`): the folders the
|
||||
// frames go to are the numbered ones that exist, which the template's own parent is not.
|
||||
let (before, _, after) = effectcraft_engine::project::render_queue::sequence_parts(output);
|
||||
let sep = ['/', '\\'];
|
||||
let Some((dir_tail, rest)) = after.split_once(sep) else { return Ok(()) };
|
||||
let (parent, prefix) = before.rsplit_once(sep).unwrap_or(("", &before));
|
||||
// A parent that doesn't exist has no numbered folders; one that can't be listed might.
|
||||
let scan = if parent.is_empty() { Path::new("/") } else { Path::new(parent) };
|
||||
let entries = match std::fs::read_dir(scan) {
|
||||
Ok(entries) => entries,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => return Err(Error::BadArgs(format!("cannot render to `{output}`: its numbered folders in `{}` can't be checked: {e}", scan.display()))),
|
||||
};
|
||||
let mut seen = 0;
|
||||
for e in entries {
|
||||
let e =
|
||||
e.map_err(|e| Error::BadArgs(format!("cannot render to `{output}`: its numbered folders in `{}` can't be checked: {e}", scan.display())))?;
|
||||
let name = e.file_name().to_string_lossy().into_owned();
|
||||
let Some(digits) = name.strip_prefix(prefix).and_then(|n| n.strip_suffix(dir_tail)) else { continue };
|
||||
if !digits.strip_prefix('-').unwrap_or(digits).bytes().all(|b| b.is_ascii_digit()) || digits.is_empty() {
|
||||
continue;
|
||||
}
|
||||
seen += 1;
|
||||
if seen > MAX_NUMBERED_FOLDERS {
|
||||
return Err(Error::BadArgs(format!("cannot render to `{output}`: more than {MAX_NUMBERED_FOLDERS} numbered folders already exist to check")));
|
||||
}
|
||||
// The numbered folder itself may be a link out; `check` resolves every existing part.
|
||||
let frame_file = e.path().join(rest);
|
||||
let frame_file = frame_file.to_string_lossy();
|
||||
self.check(&frame_file, Access::Write)?;
|
||||
if let Some(dir) = Path::new(&*frame_file).parent().and_then(resolve) {
|
||||
self.check_no_outward_links(&dir, output)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `dir` holds no symbolic link that leads outside the write roots.
|
||||
fn check_no_outward_links(&self, dir: &Path, output: &str) -> Result<()> {
|
||||
// A folder that does not exist yet holds no link; one that can't be listed might.
|
||||
let cannot = |e: std::io::Error| Error::BadArgs(format!("cannot render to `{output}`: its folder `{}` can't be checked for links: {e}", dir.display()));
|
||||
let entries = match std::fs::read_dir(dir) {
|
||||
Ok(entries) => entries,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
Err(e) => return Err(cannot(e)),
|
||||
};
|
||||
for e in entries {
|
||||
let e = e.map_err(cannot)?;
|
||||
if !e.file_type().map_err(cannot)?.is_symlink() {
|
||||
continue;
|
||||
}
|
||||
let link = e.path();
|
||||
if !link.canonicalize().is_ok_and(|r| self.write.iter().any(|w| r.starts_with(w))) {
|
||||
return Err(Error::BadArgs(format!(
|
||||
"cannot render to `{output}`: its folder holds `{}`, a link that leads outside the automation write roots, where a numbered frame could be written",
|
||||
link.display()
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check engine command `id` with `params` before it runs. Returns what must still be looked
|
||||
/// up and passed to [`Confinement::check_lookup`] for commands that write implicitly.
|
||||
pub fn check_command(&self, id: &str, params: &Value) -> Result<Vec<Lookup>> {
|
||||
@@ -219,6 +302,12 @@ impl Confinement {
|
||||
for step in params.get("steps").and_then(Value::as_array).into_iter().flatten() {
|
||||
let Some(inner) = step.get("command").or_else(|| step.get("id")).and_then(Value::as_str) else { continue };
|
||||
let inner_params = step.get("params").cloned().unwrap_or(Value::Null);
|
||||
// A whole `params` given as a `$N` reference becomes an earlier step's result
|
||||
// only as the batch runs, so none of its file paths would have been checked
|
||||
// (`essential.templateInfo` returns a manifest naming any file at all).
|
||||
if !matches!(inner_params, Value::Null | Value::Object(_)) {
|
||||
return Err(Error::BadArgs(format!("`{inner}` in a batch: `params` must be an object while file access is confined")));
|
||||
}
|
||||
let named = keys_of(READS, inner).or_else(|| keys_of(WRITES, inner)).unwrap_or(&[]);
|
||||
let mixed = keys_of(MIXED_WRITES, inner).unwrap_or(&[]);
|
||||
// A step's `$N` reference is resolved only as the batch runs: never for a file.
|
||||
@@ -256,6 +345,9 @@ impl Confinement {
|
||||
Some(s) => {
|
||||
gave_a_file |= v.is_string();
|
||||
self.check(s, access)?;
|
||||
if QUEUES_AN_OUTPUT.contains(&id) || id.starts_with("renderQueue.") {
|
||||
self.check_render_folder(s)?;
|
||||
}
|
||||
}
|
||||
None => return Err(Error::BadArgs(format!("`{id}`: `{key}` must be a path"))),
|
||||
}
|
||||
@@ -280,6 +372,11 @@ impl Confinement {
|
||||
if matches!(id, "file.openRecent" | "file.importRecent" | "anim.applyRecentPreset") && !given {
|
||||
return Err(Error::BadArgs(format!("`{id}`: give `path` while file access is confined")));
|
||||
}
|
||||
// The default repro destination is the settings store, not an automation write root.
|
||||
// Require a single explicit path so missing, null or array parameters cannot bypass it.
|
||||
if id == "help.exportRepro" && params.get("path").and_then(Value::as_str).is_none() {
|
||||
return Err(Error::BadArgs("`help.exportRepro`: give `path` inside an automation write root while file access is confined".into()));
|
||||
}
|
||||
let mut lookups = vec![];
|
||||
// An output the agent did not name is derived from where the project sits: the Default
|
||||
// Output Folder preference when that is set, beside the project file otherwise — and the
|
||||
@@ -314,11 +411,15 @@ impl Confinement {
|
||||
let items = found.get("items").or(Some(found)).and_then(Value::as_array).cloned().unwrap_or_default();
|
||||
for it in items.iter().filter(|it| it.get("render").and_then(Value::as_bool) != Some(false)) {
|
||||
let extra = it.get("extraOutputs").and_then(Value::as_array).into_iter().flatten().map(|o| o.get("outputPath"));
|
||||
for p in std::iter::once(it.get("outputPath")).chain(extra).chain([it.get("logPath")]).flatten() {
|
||||
for p in std::iter::once(it.get("outputPath")).chain(extra).flatten() {
|
||||
if let Some(p) = p.as_str() {
|
||||
self.check(p, Access::Write)?;
|
||||
self.check_render_folder(p)?;
|
||||
}
|
||||
}
|
||||
if let Some(p) = it.get("logPath").and_then(Value::as_str) {
|
||||
self.check(p, Access::Write)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -381,6 +482,10 @@ impl FootageSource for ConfinedFootage {
|
||||
fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option<Arc<Image>> {
|
||||
self.readable(footage).then(|| self.inner.frame_with(item, footage, t, size, float)).flatten()
|
||||
}
|
||||
/// The frame as 8-bit levels, or `None` when the footage lies outside the allowed roots.
|
||||
fn frame8(&self, item: ItemId, footage: &Footage, t: Tick, float: FloatCurve) -> Option<Arc<effectcraft_engine::render::disk_cache::Frame8>> {
|
||||
self.readable(footage).then(|| self.inner.frame8(item, footage, t, float)).flatten()
|
||||
}
|
||||
/// The footage audio from the inner source, or `None` when the footage lies outside the allowed roots.
|
||||
fn audio(&self, item: ItemId, footage: &Footage, t: Tick, frames: usize, rate: u32) -> Option<Vec<f32>> {
|
||||
self.readable(footage).then(|| self.inner.audio(item, footage, t, frames, rate)).flatten()
|
||||
@@ -684,6 +789,16 @@ mod tests {
|
||||
assert!(read(&footage(&inside)).1.is_complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn watch_folder_and_edit_original_are_refused_while_confined() {
|
||||
let dir = std::env::temp_dir();
|
||||
let c = Confinement::new(std::slice::from_ref(&dir), std::slice::from_ref(&dir)).unwrap();
|
||||
for id in ["file.watchFolder", "file.watchFolder.poll", "edit.editOriginal"] {
|
||||
let e = c.check_command(id, &serde_json::json!({"folder": dir.to_string_lossy()})).unwrap_err().to_string();
|
||||
assert!(e.contains("not available while file access is confined"), "{id}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_and_outside_paths_of_newly_listed_commands_are_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-1243-{}", std::process::id()));
|
||||
@@ -746,6 +861,112 @@ mod tests {
|
||||
] {
|
||||
assert!(c.check_command("effect.plugins.install", ¶ms).is_err(), "{params}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_batch_step_whose_params_are_a_reference_is_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-batch-ref-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (read, write) = (base.join("in"), base.join("out"));
|
||||
for d in [&read, &write] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
let c = Confinement::new(std::slice::from_ref(&read), std::slice::from_ref(&write)).unwrap();
|
||||
let info = read.join("t.ectemplate").to_string_lossy().into_owned();
|
||||
// Step 2's params are step 1's result, which can name any `path`.
|
||||
let steps = serde_json::json!({"steps": [
|
||||
{"command": "essential.templateInfo", "params": {"path": info}},
|
||||
{"command": "file.saveAs", "params": "$1"},
|
||||
]});
|
||||
let e = c.check_command("engine.batch", &steps).unwrap_err().to_string();
|
||||
assert!(e.contains("must be an object"), "{e}");
|
||||
for params in [serde_json::json!("$1.manifest"), serde_json::json!(["$1"]), serde_json::json!(7)] {
|
||||
let steps = serde_json::json!({"steps": [{"command": "file.saveAs", "params": params}]});
|
||||
assert!(c.check_command("engine.batch", &steps).is_err(), "{params}");
|
||||
}
|
||||
// Object params (or none) are checked as before.
|
||||
let saved = write.join("p.ecproj").to_string_lossy().into_owned();
|
||||
let steps = serde_json::json!({"steps": [{"command": "file.saveAs", "params": {"path": saved}}, {"command": "edit.undo"}]});
|
||||
assert!(c.check_command("engine.batch", &steps).is_ok());
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_render_folder_with_a_link_out_of_the_write_roots_is_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-seq-link-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (read, write, outside) = (base.join("in"), base.join("out"), base.join("elsewhere"));
|
||||
for d in [&read, &write, &outside] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
let c = Confinement::new(std::slice::from_ref(&read), std::slice::from_ref(&write)).unwrap();
|
||||
let template = write.join("frames_[#####].png").to_string_lossy().into_owned();
|
||||
let add = serde_json::json!({"output": template});
|
||||
let queue = serde_json::json!({"items": [{"outputPath": template}]});
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
assert!(c.check_lookup(&Lookup::RenderOutputs, &queue).is_ok());
|
||||
// A link inside the write root is fine.
|
||||
std::os::unix::fs::symlink(write.join("kept.png"), write.join("frames_00002.png")).unwrap();
|
||||
std::fs::write(write.join("kept.png"), b"x").unwrap();
|
||||
assert!(c.check_lookup(&Lookup::RenderOutputs, &queue).is_ok());
|
||||
// Frame 1's name leads outside: rendering would truncate that file.
|
||||
std::os::unix::fs::symlink(outside.join("victim"), write.join("frames_00001.png")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_err());
|
||||
assert!(c.check_command("render.preRender", &add).is_err());
|
||||
let e = c.check_lookup(&Lookup::RenderOutputs, &queue).unwrap_err().to_string();
|
||||
assert!(e.contains("frames_00001.png"), "{e}");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_render_folder_that_cannot_be_listed_is_refused() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-seq-nolist-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let write = base.join("out");
|
||||
std::fs::create_dir_all(&write).unwrap();
|
||||
let c = Confinement::new(&[], std::slice::from_ref(&write)).unwrap();
|
||||
let template = write.join("frames_[#####].png").to_string_lossy().into_owned();
|
||||
let add = serde_json::json!({"output": template});
|
||||
// Writable and searchable but not listable: a link in it could not be seen.
|
||||
std::fs::set_permissions(&write, std::fs::Permissions::from_mode(0o300)).unwrap();
|
||||
// A user who can list it anyway (root) sees no difference to test.
|
||||
if std::fs::read_dir(&write).is_err() {
|
||||
assert!(c.check_command("renderQueue.add", &add).is_err());
|
||||
}
|
||||
std::fs::set_permissions(&write, std::fs::Permissions::from_mode(0o700)).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn numbered_render_folders_with_links_out_of_the_write_roots_are_refused() {
|
||||
let base = std::env::temp_dir().join(format!("ec-confine-seq-dir-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
let (write, outside) = (base.join("out"), base.join("elsewhere"));
|
||||
for d in [&write, &outside] {
|
||||
std::fs::create_dir_all(d).unwrap();
|
||||
}
|
||||
let c = Confinement::new(&[], std::slice::from_ref(&write)).unwrap();
|
||||
let template = write.join("shot_[###]/f_[#####].png").to_string_lossy().into_owned();
|
||||
let add = serde_json::json!({"output": template});
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok(), "no numbered folder exists yet");
|
||||
// A numbered folder inside the roots holding a link out is refused.
|
||||
std::fs::create_dir(write.join("shot_001")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
std::os::unix::fs::symlink(outside.join("victim"), write.join("shot_001/f_00001.png")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_err());
|
||||
std::fs::remove_file(write.join("shot_001/f_00001.png")).unwrap();
|
||||
// A numbered folder that is itself a link out is refused.
|
||||
std::os::unix::fs::symlink(&outside, write.join("shot_002")).unwrap();
|
||||
assert!(c.check_command("render.preRender", &add).is_err());
|
||||
// Unrelated names are left alone.
|
||||
std::fs::remove_file(write.join("shot_002")).unwrap();
|
||||
std::os::unix::fs::symlink(&outside, write.join("shot_notes")).unwrap();
|
||||
assert!(c.check_command("renderQueue.add", &add).is_ok());
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1865,3 +1865,28 @@ fn automation_roots_check_relinked_footage_again() {
|
||||
assert!(!(px[0] > 200 && px[3] > 200), "the link pointed outside afterwards was read: {px:?}");
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// Exporting a repro must not write to the settings store outside the automation roots.
|
||||
#[test]
|
||||
fn confined_repro_export_requires_an_explicit_writable_path() {
|
||||
use crate::confine::Confinement;
|
||||
use effectcraft_engine::config::{ConfigStore, MemoryConfig};
|
||||
use std::sync::Arc;
|
||||
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let store = Arc::new(MemoryConfig::default());
|
||||
let session = Session { config: Some(store.clone()), ..Default::default() };
|
||||
let mut s = McpServer::new(Backend::headless(session));
|
||||
s.backend().confine(Confinement::new(&[], &[dir.path().to_path_buf()]).unwrap());
|
||||
for params in [json!({}), json!({"path": null}), json!({"path": []}), json!({"path": [dir.path().join("repro.jsonl")]})] {
|
||||
let (content, error) = call(&mut s, "execute_command", json!({"command": "help.exportRepro", "params": params}));
|
||||
assert!(error, "{content:?}");
|
||||
assert!(store.list("Repros").is_empty(), "implicit settings-store writes must be refused");
|
||||
}
|
||||
let path = dir.path().join("repro.jsonl");
|
||||
call_json(&mut s, "execute_command", json!({"command": "help.exportRepro", "params": {"path": path}}));
|
||||
assert!(path.is_file());
|
||||
let journal = call_json(&mut s, "execute_command", json!({"command": "journal.export"}));
|
||||
assert!(journal["script"].is_string(), "in-memory exports need no write root");
|
||||
assert!(store.list("Repros").is_empty());
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
[package]
|
||||
name = "effectcraft-av1enc"
|
||||
description = "Clean-room pure-Rust AV1 encoder (Main profile, key and inter frames) for MP4/WebM export, from the AV1 Bitstream & Decoding Process Specification"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies]
|
||||
filmcraft-av1 = { workspace = true }
|
||||
@@ -1,76 +0,0 @@
|
||||
# effectcraft-av1enc
|
||||
|
||||
Clean-room, pure-Rust AV1 encoder for EffectCraft's MP4 / WebM export (layer L0, `std` only,
|
||||
no `unsafe`, builds for `wasm32-unknown-unknown`).
|
||||
|
||||
Output is decoded **bit-exactly** by libdav1d (`ffmpeg -c:v libdav1d`, checked in
|
||||
`tests/it/ffmpeg_oracle.rs` when ffmpeg is installed) and by FilmCraft's spec-derived
|
||||
`filmcraft-av1` decoder (`tests/it/roundtrip.rs`): every decoded frame equals the encoder's own
|
||||
reconstruction, so prediction never drifts.
|
||||
|
||||
## What is implemented
|
||||
|
||||
- **Stream**: Main profile (`seq_profile` 0), 4:2:0, 8 or 10 bit, BT.709 colour description,
|
||||
limited or full range. Sequence header with one operating point; `seq_level_idx` given or the
|
||||
lowest Annex A level whose MaxPicSize / MaxH/VSize / MaxDisplayRate (and, in bitrate mode, Main
|
||||
tier MaxBitrate) fit. Order hints, film grain, superres, CDEF, loop restoration, intra edge
|
||||
filter, filter intra, inter-intra, masked / distance compound, warped motion and dual filter
|
||||
are disabled in the sequence header.
|
||||
- **Packets**: one temporal unit per frame as stored in MP4 / Matroska samples: OBUs with size
|
||||
fields, no temporal delimiter; key frames start with the sequence header OBU, followed by one
|
||||
`OBU_FRAME` (frame header + tile group). Every frame is shown, in input order.
|
||||
`Encoder::av1c()` returns the `AV1CodecConfigurationRecord` (also the WebM `CodecPrivate`).
|
||||
- **Frames**: a `KEY_FRAME` every `keyint` frames, `INTER_FRAME`s otherwise. Inter frames use
|
||||
slot 0 (the previous reconstruction) for all seven references and refresh slot 0 only, so
|
||||
`LAST_FRAME` is always the previous frame. CDFs adapt per symbol and are saved at the end of
|
||||
every frame; inter frames load them from `LAST_FRAME` (`primary_ref_frame` = 0). One tile,
|
||||
`TX_MODE_LARGEST`, `reduced_tx_set`, no segmentation, no delta q / lf.
|
||||
- **Deblocking**: the normative loop filter (7.14) runs on the reconstruction; the frame level
|
||||
(the same for both luma directions and both chroma planes, no deltas, sharpness 0) is searched
|
||||
per frame for the lowest luma error.
|
||||
- **Tiles**: the multi-symbol arithmetic encoder is the exact inverse of the spec's symbol
|
||||
decoder (8.2), including the exit process padding; CDF adaptation as in 8.2.6.
|
||||
- **Partitions**: 64x64 superblocks, `PARTITION_NONE` / `PARTITION_SPLIT` down to 8x8 chosen by
|
||||
a rate-distortion cost (SSE + λ·estimated bits); blocks crossing the frame edge are split as
|
||||
the syntax requires. 64x64 blocks are only used as inter skip blocks.
|
||||
- **Intra**: DC, V, H, Paeth, smooth, smooth-V, smooth-H for luma and chroma,
|
||||
ranked by SATD with a rate-distortion choice among the best three; the spec's edge preparation
|
||||
and above-right / below-left availability.
|
||||
- **Inter**: single reference (`LAST_FRAME`), `NEWMV` / `NEARESTMV` / `NEARMV` / `GLOBALMV`
|
||||
chosen from the spec's motion vector prediction stack (7.10.2, mirrored exactly); motion search
|
||||
from neighbour / co-located / parent candidates, a whole-sample diamond search and half- then
|
||||
quarter-sample refinement (`allow_high_precision_mv` = 0); the normative 8-tap `EIGHTTAP`
|
||||
prediction for luma and chroma; skip blocks; per-block intra / inter choice.
|
||||
- **Residual**: one transform per plane and block (4x4 ... 32x32): for intra luma `DCT_DCT` or
|
||||
the mode's preferred `ADST` combination (rate-distortion choice over the two best modes), the
|
||||
mode-dependent type for intra chroma, `DCT_DCT` for inter blocks; dead-zone quantisation with the
|
||||
spec's `Dc_Qlookup` / `Ac_Qlookup`; the normative dequantisation and inverse transforms in the
|
||||
reconstruction loop; full coefficient coding (all_zero, tx type, eob_pt, eob_extra,
|
||||
coeff_base_eob, coeff_base, coeff_br, dc_sign, Golomb) with the exact contexts.
|
||||
- **Rate control**: `ConstantQ(base_q_idx)`; `Bitrate { kbps }` adapts `base_q_idx` per frame
|
||||
towards the average target (key frames get a lower quantizer and a larger share of the budget of
|
||||
their group of `keyint` frames, the inter frames the rest; the first key frame is searched in a
|
||||
few passes, a later key frame that misses its budget by more than 19 % is coded again; the
|
||||
deviation from the budget is paid back over 16 frames).
|
||||
|
||||
## Limits
|
||||
|
||||
- One tile: width and height up to 4096 and at most 2304 superblocks (4096x2304 samples).
|
||||
- No CDEF or loop restoration, no compound prediction, no rectangular or 4xN blocks, no
|
||||
directional intra modes other than V / H, no CfL or palette, no transform-size splits, so
|
||||
compression is below mature encoders.
|
||||
- 4:2:0 only (Main profile), no 12 bit.
|
||||
|
||||
## Specification and provenance
|
||||
|
||||
Implemented from the **AV1 Bitstream & Decoding Process Specification, version 1.0.0 with
|
||||
Errata 1** (AOMediaCodec, 2019; the edition FilmCraft's decoder follows) and the **AV1 Codec
|
||||
ISO Media File Format Binding** (v1.2.0) for `av1C`.
|
||||
|
||||
The constant tables (default CDFs, scan orders, quantizer lookup tables, interpolation filter
|
||||
taps, smooth weights, block / transform size tables, ...) and the decoder-side processes the
|
||||
encoder must reproduce exactly (inverse transforms, intra and inter prediction rounding, context
|
||||
derivations, motion vector prediction) are mirrored from FilmCraft's first-party, spec-derived
|
||||
`filmcraft-av1` decoder (MIT OR Apache-2.0), which is also the round-trip oracle. No source of
|
||||
libaom, dav1d, rav1e, SVT-AV1, ffmpeg or any other third-party encoder or decoder was consulted;
|
||||
ffmpeg / libdav1d is used only as an external test oracle.
|
||||
@@ -1,87 +0,0 @@
|
||||
//! MSB-first bit writer for the OBU headers (spec 4.10 `f(n)` descriptors) and `leb128()`.
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct BitWriter {
|
||||
pub buf: Vec<u8>,
|
||||
nbits: usize,
|
||||
}
|
||||
|
||||
impl BitWriter {
|
||||
pub fn new() -> Self {
|
||||
BitWriter::default()
|
||||
}
|
||||
|
||||
/// `f(n)`: writes the low `n` bits of `v`, most significant first.
|
||||
pub fn f(&mut self, n: u32, v: u32) {
|
||||
for i in (0..n).rev() {
|
||||
self.bit((v >> i) & 1 != 0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bit(&mut self, b: bool) {
|
||||
if self.nbits.is_multiple_of(8) {
|
||||
self.buf.push(0);
|
||||
}
|
||||
if b {
|
||||
let last = self.buf.len() - 1;
|
||||
self.buf[last] |= 0x80 >> (self.nbits % 8);
|
||||
}
|
||||
self.nbits += 1;
|
||||
}
|
||||
|
||||
/// `byte_alignment()`: zero bits up to the next byte boundary.
|
||||
pub fn byte_align(&mut self) {
|
||||
while !self.nbits.is_multiple_of(8) {
|
||||
self.bit(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// `trailing_bits()`: a one bit followed by zero bits up to the byte boundary.
|
||||
pub fn trailing_bits(&mut self) {
|
||||
self.bit(true);
|
||||
self.byte_align();
|
||||
}
|
||||
|
||||
pub fn into_bytes(self) -> Vec<u8> {
|
||||
self.buf
|
||||
}
|
||||
}
|
||||
|
||||
/// `leb128()` encoding of an OBU size.
|
||||
pub(crate) fn leb128(mut v: usize, out: &mut Vec<u8>) {
|
||||
loop {
|
||||
let mut b = (v & 0x7f) as u8;
|
||||
v >>= 7;
|
||||
if v != 0 {
|
||||
b |= 0x80;
|
||||
}
|
||||
out.push(b);
|
||||
if v == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An OBU with `obu_has_size_field` = 1 and no extension header.
|
||||
pub(crate) fn obu(obu_type: u8, payload: &[u8], out: &mut Vec<u8>) {
|
||||
out.push((obu_type << 3) | 0x02);
|
||||
leb128(payload.len(), out);
|
||||
out.extend_from_slice(payload);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bits_and_leb128() {
|
||||
let mut w = BitWriter::new();
|
||||
w.f(3, 0b101);
|
||||
w.f(6, 0b110011);
|
||||
w.trailing_bits();
|
||||
assert_eq!(w.into_bytes(), vec![0b1011_1001, 0b1100_0000]);
|
||||
let mut v = Vec::new();
|
||||
leb128(300, &mut v);
|
||||
assert_eq!(v, vec![0xAC, 0x02]);
|
||||
}
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
//! The adaptive CDFs this encoder codes with (the subset of spec 6.8.2 init_non_coeff_cdfs /
|
||||
//! init_coeff_cdfs that its syntax uses). Layout as in filmcraft-av1: each innermost array
|
||||
//! holds N cumulative values (the last is 32768) plus the adaptation counter.
|
||||
|
||||
use crate::tables::{
|
||||
DEFAULT_ANGLE_DELTA_CDF, DEFAULT_COEFF_BASE_CDF, DEFAULT_COEFF_BASE_EOB_CDF, DEFAULT_COEFF_BR_CDF, DEFAULT_DC_SIGN_CDF, DEFAULT_DRL_MODE_CDF,
|
||||
DEFAULT_EOB_EXTRA_CDF, DEFAULT_EOB_PT_16_CDF, DEFAULT_EOB_PT_32_CDF, DEFAULT_EOB_PT_64_CDF, DEFAULT_EOB_PT_128_CDF, DEFAULT_EOB_PT_256_CDF,
|
||||
DEFAULT_EOB_PT_512_CDF, DEFAULT_EOB_PT_1024_CDF, DEFAULT_INTER_TX_TYPE_SET3_CDF, DEFAULT_INTRA_TX_TYPE_SET2_CDF, DEFAULT_IS_INTER_CDF, DEFAULT_MV_BIT_CDF,
|
||||
DEFAULT_MV_CLASS_CDF, DEFAULT_MV_CLASS0_BIT_CDF, DEFAULT_MV_CLASS0_FR_CDF, DEFAULT_MV_FR_CDF, DEFAULT_MV_JOINT_CDF, DEFAULT_MV_SIGN_CDF,
|
||||
DEFAULT_NEW_MV_CDF, DEFAULT_PARTITION_W8_CDF, DEFAULT_PARTITION_W16_CDF, DEFAULT_PARTITION_W32_CDF, DEFAULT_PARTITION_W64_CDF, DEFAULT_REF_MV_CDF,
|
||||
DEFAULT_SINGLE_REF_CDF, DEFAULT_SKIP_CDF, DEFAULT_TXB_SKIP_CDF, DEFAULT_UV_MODE_CFL_ALLOWED_CDF, DEFAULT_UV_MODE_CFL_NOT_ALLOWED_CDF, DEFAULT_Y_MODE_CDF,
|
||||
DEFAULT_ZERO_MV_CDF,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Cdfs {
|
||||
pub y_mode: [[u16; 14]; 4],
|
||||
pub uv_mode_cfl_not_allowed: [[u16; 14]; 13],
|
||||
pub uv_mode_cfl_allowed: [[u16; 15]; 13],
|
||||
pub angle_delta: [[u16; 8]; 8],
|
||||
pub partition_w8: [[u16; 5]; 4],
|
||||
pub partition_w16: [[u16; 11]; 4],
|
||||
pub partition_w32: [[u16; 11]; 4],
|
||||
pub partition_w64: [[u16; 11]; 4],
|
||||
pub skip: [[u16; 3]; 3],
|
||||
pub is_inter: [[u16; 3]; 4],
|
||||
pub single_ref: [[[u16; 3]; 6]; 3],
|
||||
pub new_mv: [[u16; 3]; 6],
|
||||
pub zero_mv: [[u16; 3]; 2],
|
||||
pub ref_mv: [[u16; 3]; 6],
|
||||
pub drl_mode: [[u16; 3]; 3],
|
||||
pub mv_joint: [u16; 5],
|
||||
pub mv_class: [[u16; 12]; 2],
|
||||
pub mv_class0_bit: [[u16; 3]; 2],
|
||||
pub mv_fr: [[u16; 5]; 2],
|
||||
pub mv_class0_fr: [[[u16; 5]; 2]; 2],
|
||||
pub mv_sign: [[u16; 3]; 2],
|
||||
pub mv_bit: [[[u16; 3]; 10]; 2],
|
||||
pub intra_tx_type_set2: [[[u16; 6]; 13]; 3],
|
||||
pub inter_tx_type_set3: [[u16; 3]; 4],
|
||||
pub txb_skip: [[[u16; 3]; 13]; 5],
|
||||
pub eob_pt_16: [[[u16; 6]; 2]; 2],
|
||||
pub eob_pt_32: [[[u16; 7]; 2]; 2],
|
||||
pub eob_pt_64: [[[u16; 8]; 2]; 2],
|
||||
pub eob_pt_128: [[[u16; 9]; 2]; 2],
|
||||
pub eob_pt_256: [[[u16; 10]; 2]; 2],
|
||||
pub eob_pt_512: [[u16; 11]; 2],
|
||||
pub eob_pt_1024: [[u16; 12]; 2],
|
||||
pub eob_extra: [[[[u16; 3]; 9]; 2]; 5],
|
||||
pub dc_sign: [[[u16; 3]; 3]; 2],
|
||||
pub coeff_base_eob: [[[[u16; 4]; 4]; 2]; 5],
|
||||
pub coeff_base: [[[[u16; 5]; 42]; 2]; 5],
|
||||
pub coeff_br: [[[[u16; 5]; 21]; 2]; 5],
|
||||
}
|
||||
|
||||
impl Cdfs {
|
||||
/// init_non_coeff_cdfs( ) followed by init_coeff_cdfs( ) for `base_q_idx`.
|
||||
pub fn new(base_q_idx: u32) -> Box<Cdfs> {
|
||||
let idx = match base_q_idx {
|
||||
0..=20 => 0,
|
||||
21..=60 => 1,
|
||||
61..=120 => 2,
|
||||
_ => 3,
|
||||
};
|
||||
Box::new(Cdfs {
|
||||
y_mode: DEFAULT_Y_MODE_CDF,
|
||||
uv_mode_cfl_not_allowed: DEFAULT_UV_MODE_CFL_NOT_ALLOWED_CDF,
|
||||
uv_mode_cfl_allowed: DEFAULT_UV_MODE_CFL_ALLOWED_CDF,
|
||||
angle_delta: DEFAULT_ANGLE_DELTA_CDF,
|
||||
partition_w8: DEFAULT_PARTITION_W8_CDF,
|
||||
partition_w16: DEFAULT_PARTITION_W16_CDF,
|
||||
partition_w32: DEFAULT_PARTITION_W32_CDF,
|
||||
partition_w64: DEFAULT_PARTITION_W64_CDF,
|
||||
skip: DEFAULT_SKIP_CDF,
|
||||
is_inter: DEFAULT_IS_INTER_CDF,
|
||||
single_ref: DEFAULT_SINGLE_REF_CDF,
|
||||
new_mv: DEFAULT_NEW_MV_CDF,
|
||||
zero_mv: DEFAULT_ZERO_MV_CDF,
|
||||
ref_mv: DEFAULT_REF_MV_CDF,
|
||||
drl_mode: DEFAULT_DRL_MODE_CDF,
|
||||
mv_joint: DEFAULT_MV_JOINT_CDF,
|
||||
mv_class: DEFAULT_MV_CLASS_CDF,
|
||||
mv_class0_bit: [DEFAULT_MV_CLASS0_BIT_CDF; 2],
|
||||
mv_fr: DEFAULT_MV_FR_CDF,
|
||||
mv_class0_fr: DEFAULT_MV_CLASS0_FR_CDF,
|
||||
mv_sign: [DEFAULT_MV_SIGN_CDF; 2],
|
||||
mv_bit: [DEFAULT_MV_BIT_CDF; 2],
|
||||
intra_tx_type_set2: DEFAULT_INTRA_TX_TYPE_SET2_CDF,
|
||||
inter_tx_type_set3: DEFAULT_INTER_TX_TYPE_SET3_CDF,
|
||||
txb_skip: DEFAULT_TXB_SKIP_CDF[idx],
|
||||
eob_pt_16: DEFAULT_EOB_PT_16_CDF[idx],
|
||||
eob_pt_32: DEFAULT_EOB_PT_32_CDF[idx],
|
||||
eob_pt_64: DEFAULT_EOB_PT_64_CDF[idx],
|
||||
eob_pt_128: DEFAULT_EOB_PT_128_CDF[idx],
|
||||
eob_pt_256: DEFAULT_EOB_PT_256_CDF[idx],
|
||||
eob_pt_512: DEFAULT_EOB_PT_512_CDF[idx],
|
||||
eob_pt_1024: DEFAULT_EOB_PT_1024_CDF[idx],
|
||||
eob_extra: DEFAULT_EOB_EXTRA_CDF[idx],
|
||||
dc_sign: DEFAULT_DC_SIGN_CDF[idx],
|
||||
coeff_base_eob: DEFAULT_COEFF_BASE_EOB_CDF[idx],
|
||||
coeff_base: DEFAULT_COEFF_BASE_CDF[idx],
|
||||
coeff_br: DEFAULT_COEFF_BR_CDF[idx],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Cdfs {
|
||||
/// The counter reset of load_cdfs( ): the last entry of every CDF array is set to 0.
|
||||
pub fn clear_counts(&mut self) {
|
||||
macro_rules! clr {
|
||||
($($f:ident),*) => { $( self.$f.clear_count(); )* };
|
||||
}
|
||||
clr!(
|
||||
y_mode,
|
||||
uv_mode_cfl_not_allowed,
|
||||
uv_mode_cfl_allowed,
|
||||
angle_delta,
|
||||
partition_w8,
|
||||
partition_w16,
|
||||
partition_w32,
|
||||
partition_w64,
|
||||
skip,
|
||||
is_inter,
|
||||
single_ref,
|
||||
new_mv,
|
||||
zero_mv,
|
||||
ref_mv,
|
||||
drl_mode,
|
||||
mv_joint,
|
||||
mv_class,
|
||||
mv_class0_bit,
|
||||
mv_fr,
|
||||
mv_class0_fr,
|
||||
mv_sign,
|
||||
mv_bit,
|
||||
intra_tx_type_set2,
|
||||
inter_tx_type_set3,
|
||||
txb_skip,
|
||||
eob_pt_16,
|
||||
eob_pt_32,
|
||||
eob_pt_64,
|
||||
eob_pt_128,
|
||||
eob_pt_256,
|
||||
eob_pt_512,
|
||||
eob_pt_1024,
|
||||
eob_extra,
|
||||
dc_sign,
|
||||
coeff_base_eob,
|
||||
coeff_base,
|
||||
coeff_br
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Zeroes the adaptation counter (last element) of every innermost CDF array.
|
||||
trait ClearCount {
|
||||
fn clear_count(&mut self);
|
||||
}
|
||||
|
||||
impl<const N: usize> ClearCount for [u16; N] {
|
||||
fn clear_count(&mut self) {
|
||||
self[N - 1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ClearCount, const M: usize> ClearCount for [T; M] {
|
||||
fn clear_count(&mut self) {
|
||||
for e in self.iter_mut() {
|
||||
e.clear_count();
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,670 +0,0 @@
|
||||
//! Clean-room pure-Rust AV1 encoder (Main profile, 4:2:0, 8 and 10 bit), implemented from the
|
||||
//! AV1 Bitstream & Decoding Process Specification v1.0.0 with Errata 1.
|
||||
//!
|
||||
//! Key frames and single-reference inter frames (LAST_FRAME = the previous frame), 64x64
|
||||
//! superblocks split down to 8x8 by rate-distortion decisions, intra modes DC / V / H / Paeth /
|
||||
//! smooth, full-sample motion search with the spec's motion vector prediction, DCT / ADST
|
||||
//! transforms up to 32x32, one tile per frame, in-loop filters off. See the crate README.
|
||||
//!
|
||||
//! The public API below is the contract `crates/export` codes against; keep it stable.
|
||||
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
|
||||
mod bitw;
|
||||
mod cdf;
|
||||
mod decide;
|
||||
mod loopfilter;
|
||||
mod pred;
|
||||
mod symbol;
|
||||
mod tables;
|
||||
mod transform;
|
||||
mod writer;
|
||||
|
||||
use bitw::{BitWriter, obu};
|
||||
use decide::{Decider, FrameGeom, RefFrame};
|
||||
use loopfilter::{LfInfo, loop_filter};
|
||||
use pred::Plane;
|
||||
use tables::AC_QLOOKUP;
|
||||
use writer::TileWriter;
|
||||
|
||||
/// Rate control.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum RateControl {
|
||||
/// Constant quantizer index (`base_q_idx`, 1–255; lower is better).
|
||||
ConstantQ(u8),
|
||||
/// Average bitrate target in kbit/s (frame-level quantizer adaptation).
|
||||
Bitrate { kbps: u32 },
|
||||
}
|
||||
|
||||
/// Encoder settings. Main profile (seq_profile 0), 4:2:0.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct EncoderConfig {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub fps_num: u32,
|
||||
pub fps_den: u32,
|
||||
/// 8 or 10.
|
||||
pub bit_depth: u8,
|
||||
/// `seq_level_idx` (e.g. 8 = level 4.0); `None` picks the lowest level that fits.
|
||||
pub level_idx: Option<u8>,
|
||||
pub rate: RateControl,
|
||||
/// Frames between key frames (1 = all key frames).
|
||||
pub keyint: u32,
|
||||
/// BT.709 limited range is signalled unless this is set.
|
||||
pub full_range: bool,
|
||||
/// The colour description of `color_config` (ITU-T H.273 code points); BT.709 by default.
|
||||
pub color_primaries: u8,
|
||||
pub transfer_characteristics: u8,
|
||||
pub matrix_coefficients: u8,
|
||||
}
|
||||
|
||||
impl EncoderConfig {
|
||||
pub fn new(width: u32, height: u32, fps_num: u32, fps_den: u32) -> Self {
|
||||
EncoderConfig {
|
||||
width,
|
||||
height,
|
||||
fps_num,
|
||||
fps_den,
|
||||
bit_depth: 8,
|
||||
level_idx: None,
|
||||
rate: RateControl::ConstantQ(100),
|
||||
keyint: 60,
|
||||
full_range: false,
|
||||
color_primaries: 1,
|
||||
transfer_characteristics: 1,
|
||||
matrix_coefficients: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One input picture, 4:2:0, samples at the configured bit depth.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Frame<'a> {
|
||||
pub y: &'a [u16],
|
||||
pub u: &'a [u16],
|
||||
pub v: &'a [u16],
|
||||
pub y_stride: usize,
|
||||
pub uv_stride: usize,
|
||||
}
|
||||
|
||||
/// One temporal unit as stored in MP4 / WebM samples: OBUs with `obu_has_size_field` = 1 and
|
||||
/// no temporal delimiter (key frames start with the sequence header OBU).
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Packet {
|
||||
pub data: Vec<u8>,
|
||||
pub keyframe: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum Error {
|
||||
InvalidConfig(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Error::InvalidConfig(s) => write!(f, "invalid AV1 encoder config: {s}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
const OBU_SEQUENCE_HEADER: u8 = 1;
|
||||
const OBU_FRAME: u8 = 6;
|
||||
|
||||
/// Annex A level limits: (seq_level_idx, MaxPicSize, MaxHSize, MaxVSize, MaxDisplayRate,
|
||||
/// MaxBitrate (Main tier, Mbit/s)).
|
||||
const LEVELS: [(u8, u64, u32, u32, u64, u64); 14] = [
|
||||
(0, 147_456, 2048, 1152, 4_423_680, 1_500_000),
|
||||
(1, 278_784, 2816, 1584, 8_363_520, 3_000_000),
|
||||
(4, 665_856, 4352, 2448, 19_975_680, 6_000_000),
|
||||
(5, 1_065_024, 5504, 3096, 31_950_720, 10_000_000),
|
||||
(8, 2_359_296, 6144, 3456, 70_778_880, 12_000_000),
|
||||
(9, 2_359_296, 6144, 3456, 141_557_760, 20_000_000),
|
||||
(12, 8_912_896, 8192, 4352, 267_386_880, 30_000_000),
|
||||
(13, 8_912_896, 8192, 4352, 534_773_760, 40_000_000),
|
||||
(14, 8_912_896, 8192, 4352, 1_069_547_520, 60_000_000),
|
||||
(15, 8_912_896, 8192, 4352, 1_069_547_520, 60_000_000),
|
||||
(16, 35_651_584, 16384, 8704, 1_069_547_520, 60_000_000),
|
||||
(17, 35_651_584, 16384, 8704, 2_139_095_040, 100_000_000),
|
||||
(18, 35_651_584, 16384, 8704, 4_278_190_080, 160_000_000),
|
||||
(19, 35_651_584, 16384, 8704, 4_278_190_080, 160_000_000),
|
||||
];
|
||||
|
||||
/// The lowest level whose limits fit the stream (31 = no level limits when none does).
|
||||
fn pick_level(cfg: &EncoderConfig) -> u8 {
|
||||
let pic = cfg.width as u64 * cfg.height as u64;
|
||||
let rate = pic * cfg.fps_num as u64 / cfg.fps_den.max(1) as u64;
|
||||
let bitrate = match cfg.rate {
|
||||
RateControl::Bitrate { kbps } => kbps as u64 * 1000,
|
||||
RateControl::ConstantQ(_) => 0,
|
||||
};
|
||||
for &(idx, max_pic, max_h, max_v, max_rate, max_br) in LEVELS.iter() {
|
||||
if pic <= max_pic && cfg.width <= max_h && cfg.height <= max_v && rate <= max_rate && bitrate <= max_br {
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
31
|
||||
}
|
||||
|
||||
/// Frame-level rate control state for `RateControl::Bitrate`.
|
||||
struct RateState {
|
||||
/// Bits per frame at the target rate.
|
||||
frame_bits: f64,
|
||||
/// Quantizer index for inter frames (continuous).
|
||||
q: f64,
|
||||
/// Key frame quantizer index minus the inter frame one.
|
||||
key_off: f64,
|
||||
/// Quantizer index steps per halving of the frame size, measured from consecutive inter
|
||||
/// frames (the size/quantizer curve is much steeper than the default on noisy content).
|
||||
slope: f64,
|
||||
/// Quantizer index and size (in bits) of the previous inter frame.
|
||||
prev_inter: Option<(f64, f64)>,
|
||||
/// Accumulated (spent - budget) bits.
|
||||
debt: f64,
|
||||
}
|
||||
|
||||
/// Quantizer index steps per halving of the frame size before any measurement.
|
||||
const RC_SLOPE: f64 = 28.0;
|
||||
/// Fraction of the quantizer step the size error asks for that is applied per frame.
|
||||
const RC_GAIN: f64 = 0.8;
|
||||
/// Frames over which an accumulated deviation from the budget is paid back.
|
||||
const RC_PAYBACK: f64 = 16.0;
|
||||
|
||||
impl RateState {
|
||||
/// The nominal bit budget and the starting quantizer index of a key or inter frame. A key
|
||||
/// frame gets a larger share, and the inter frames of the group get the rest, so that a whole
|
||||
/// group of `keyint` frames adds up to `keyint * frame_bits`.
|
||||
fn frame_budget(&self, keyint: u32, key: bool) -> (f64, f64) {
|
||||
let keyint = keyint.max(1) as f64;
|
||||
let boost = (1.0 + (keyint.clamp(1.0, 60.0) - 1.0).sqrt() * 0.75).min(0.5 * keyint + 0.5);
|
||||
if key {
|
||||
(self.frame_bits * boost, (self.q + self.key_off).clamp(1.0, 255.0))
|
||||
} else if keyint > 1.0 {
|
||||
(self.frame_bits * (keyint - boost) / (keyint - 1.0), self.q)
|
||||
} else {
|
||||
(self.frame_bits, self.q)
|
||||
}
|
||||
}
|
||||
|
||||
/// Adapts the quantizers to the size of the frame just coded at `q_used` against `target`.
|
||||
fn update(&mut self, key: bool, first: bool, q_used: f64, bits: f64, target: f64) {
|
||||
let err = (bits / target).log2().clamp(-1.5, 1.5);
|
||||
if first && key {
|
||||
// four passes ran: the key frame quantizer is known, the inter one follows from the offset
|
||||
self.q = (q_used - self.key_off).clamp(1.0, 255.0);
|
||||
} else if key {
|
||||
self.key_off = q_used - self.q + RC_GAIN * self.slope * err;
|
||||
} else {
|
||||
if let Some((pq, pbits)) = self.prev_inter {
|
||||
let (dq, dr) = (q_used - pq, (pbits / bits).log2());
|
||||
if dq.abs() >= 3.0 && dr.abs() >= 0.15 && dq * dr > 0.0 {
|
||||
self.slope = (0.6 * self.slope + 0.4 * (dq / dr)).clamp(3.0, 45.0);
|
||||
}
|
||||
}
|
||||
self.prev_inter = Some((q_used, bits));
|
||||
self.q = (q_used + RC_GAIN * self.slope * err).clamp(1.0, 255.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Most passes on the first frame, and how far from the target (octaves, 3 %) its size may be
|
||||
/// before it is accepted.
|
||||
const RC_FIRST_PASSES: usize = 4;
|
||||
const RC_FIRST_TOLERANCE: f64 = 0.04;
|
||||
/// Quantizer index steps per halving of the size, for the first correction of the first frame
|
||||
/// (the starting quantizer comes from the bits per sample; the first pass shows how far off it
|
||||
/// is, and from there on the passes are placed by `next_probe_q`).
|
||||
const RC_FIRST_SLOPE: f64 = 30.0;
|
||||
/// The same for a key frame after the first: coded again when its size is more than 0.25 octaves
|
||||
/// (19 %) off the target.
|
||||
const RC_KEY_PASSES: usize = 3;
|
||||
const RC_KEY_TOLERANCE: f64 = 0.25;
|
||||
/// Octaves of frame size per octave of quantizer step assumed for the first correction of a frame.
|
||||
const RC_PROBE_SLOPE: f64 = 1.0;
|
||||
/// Largest change of the quantizer step (octaves) between two passes on one frame.
|
||||
const RC_PROBE_MAX_STEP: f64 = 2.0;
|
||||
|
||||
/// The quantizer index whose step size is closest to `2^x`.
|
||||
fn q_for_step(bit_depth: u8, x: f64) -> u8 {
|
||||
let table = &AC_QLOOKUP[((bit_depth - 8) / 2) as usize];
|
||||
let mut best = 1;
|
||||
for q in 1..=255usize {
|
||||
if ((table[q] as f64).log2() - x).abs() < ((table[best] as f64).log2() - x).abs() {
|
||||
best = q;
|
||||
}
|
||||
}
|
||||
best as u8
|
||||
}
|
||||
|
||||
/// The next quantizer to try for a frame, from the `(quantizer, log2(size / target))`
|
||||
/// pairs coded so far. The size follows the quantizer step (not the index, which is linear in the
|
||||
/// step only below about 60) in a near log-log line: false position between the closest pair of
|
||||
/// passes that enclose the target, otherwise a step from the latest one with the slope of the
|
||||
/// last two. `None` when no untried quantizer lies between the enclosing passes.
|
||||
fn next_probe_q(bit_depth: u8, tried: &[(u8, f64)]) -> Option<u8> {
|
||||
let table = &AC_QLOOKUP[((bit_depth - 8) / 2) as usize];
|
||||
let x = |q: u8| (table[q as usize] as f64).log2();
|
||||
let over = tried.iter().filter(|t| t.1 > 0.0).max_by_key(|t| t.0);
|
||||
let under = tried.iter().filter(|t| t.1 <= 0.0).min_by_key(|t| t.0);
|
||||
if let (Some(&(q_lo, e_lo)), Some(&(q_hi, e_hi))) = (over, under) {
|
||||
if q_hi - q_lo < 2 {
|
||||
return None;
|
||||
}
|
||||
let next = x(q_lo) + (x(q_hi) - x(q_lo)) * e_lo / (e_lo - e_hi);
|
||||
return Some(q_for_step(bit_depth, next).clamp(q_lo + 1, q_hi - 1));
|
||||
}
|
||||
let &(q, err) = tried.last()?;
|
||||
let mut slope = RC_PROBE_SLOPE;
|
||||
if let [.., (q0, e0), (q1, e1)] = tried
|
||||
&& (x(*q1) - x(*q0)).abs() > 0.05
|
||||
&& (e0 - e1) / (x(*q1) - x(*q0)) > 0.0
|
||||
{
|
||||
slope = ((e0 - e1) / (x(*q1) - x(*q0))).clamp(0.25, 3.0);
|
||||
}
|
||||
Some(q_for_step(bit_depth, x(q) + (err / slope).clamp(-RC_PROBE_MAX_STEP, RC_PROBE_MAX_STEP)))
|
||||
}
|
||||
|
||||
/// AV1 encoder. Frames come out in input order (no hidden/reordered frames).
|
||||
pub struct Encoder {
|
||||
cfg: EncoderConfig,
|
||||
geom: FrameGeom,
|
||||
level: u8,
|
||||
seq_obu: Vec<u8>,
|
||||
frame_num: u64,
|
||||
refr: Option<RefFrame>,
|
||||
rc: Option<RateState>,
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
pub fn new(cfg: EncoderConfig) -> Result<Encoder, Error> {
|
||||
if cfg.width == 0 || cfg.height == 0 {
|
||||
return Err(Error::InvalidConfig("empty frame".into()));
|
||||
}
|
||||
if cfg.width > 4096 || cfg.height > 4096 {
|
||||
return Err(Error::InvalidConfig(format!("{}x{} exceeds the single-tile limit (4096 samples per side)", cfg.width, cfg.height)));
|
||||
}
|
||||
if cfg.bit_depth != 8 && cfg.bit_depth != 10 {
|
||||
return Err(Error::InvalidConfig(format!("bit depth {} (8 or 10 supported)", cfg.bit_depth)));
|
||||
}
|
||||
if cfg.fps_num == 0 || cfg.fps_den == 0 {
|
||||
return Err(Error::InvalidConfig("frame rate".into()));
|
||||
}
|
||||
let geom = FrameGeom::new(cfg.width as usize, cfg.height as usize);
|
||||
if geom.sb_cols * geom.sb_rows > 2304 {
|
||||
return Err(Error::InvalidConfig(format!("{}x{} exceeds the single-tile area limit (MAX_TILE_AREA)", cfg.width, cfg.height)));
|
||||
}
|
||||
if let RateControl::Bitrate { kbps } = cfg.rate
|
||||
&& kbps == 0
|
||||
{
|
||||
return Err(Error::InvalidConfig("zero bitrate".into()));
|
||||
}
|
||||
let level = cfg.level_idx.unwrap_or_else(|| pick_level(&cfg));
|
||||
if level > 31 {
|
||||
return Err(Error::InvalidConfig(format!("seq_level_idx {level}")));
|
||||
}
|
||||
let rc = match cfg.rate {
|
||||
RateControl::ConstantQ(_) => None,
|
||||
RateControl::Bitrate { kbps } => {
|
||||
let frame_bits = kbps as f64 * 1000.0 * cfg.fps_den as f64 / cfg.fps_num as f64;
|
||||
// initial guess from the bits per sample
|
||||
let bpp = frame_bits / (cfg.width as f64 * cfg.height as f64);
|
||||
let q = (110.0 - 40.0 * (bpp / 0.1).log2()).clamp(20.0, 230.0);
|
||||
Some(RateState { frame_bits, q, key_off: -24.0, slope: RC_SLOPE, prev_inter: None, debt: 0.0 })
|
||||
}
|
||||
};
|
||||
let mut enc = Encoder { cfg, geom, level, seq_obu: Vec::new(), frame_num: 0, refr: None, rc };
|
||||
let payload = enc.sequence_header_payload();
|
||||
obu(OBU_SEQUENCE_HEADER, &payload, &mut enc.seq_obu);
|
||||
Ok(enc)
|
||||
}
|
||||
|
||||
pub fn config(&self) -> &EncoderConfig {
|
||||
&self.cfg
|
||||
}
|
||||
|
||||
/// The sequence header OBU (with size field).
|
||||
pub fn sequence_header_obu(&self) -> Vec<u8> {
|
||||
self.seq_obu.clone()
|
||||
}
|
||||
|
||||
/// AV1CodecConfigurationRecord (`av1C` payload, also the WebM CodecPrivate).
|
||||
pub fn av1c(&self) -> Vec<u8> {
|
||||
let high = (self.cfg.bit_depth > 8) as u8;
|
||||
let mut v = vec![
|
||||
0x81, // marker (1), version (1)
|
||||
self.level & 0x1f, // seq_profile 0, seq_level_idx_0
|
||||
(high << 6) | (1 << 3) | (1 << 2), // tier 0, high_bitdepth, twelve_bit 0, mono 0, ss_x 1, ss_y 1, csp 0
|
||||
0, // no initial_presentation_delay
|
||||
];
|
||||
v.extend_from_slice(&self.seq_obu);
|
||||
v
|
||||
}
|
||||
|
||||
/// sequence_header_obu( ) payload (5.5) including trailing bits.
|
||||
fn sequence_header_payload(&self) -> Vec<u8> {
|
||||
let c = &self.cfg;
|
||||
let mut w = BitWriter::new();
|
||||
w.f(3, 0); // seq_profile
|
||||
w.f(1, 0); // still_picture
|
||||
w.f(1, 0); // reduced_still_picture_header
|
||||
w.f(1, 0); // timing_info_present_flag
|
||||
w.f(1, 0); // initial_display_delay_present_flag
|
||||
w.f(5, 0); // operating_points_cnt_minus_1
|
||||
w.f(12, 0); // operating_point_idc[ 0 ]
|
||||
w.f(5, self.level as u32); // seq_level_idx[ 0 ]
|
||||
if self.level > 7 {
|
||||
w.f(1, 0); // seq_tier[ 0 ]
|
||||
}
|
||||
let wbits = 32 - (c.width - 1).max(1).leading_zeros();
|
||||
let hbits = 32 - (c.height - 1).max(1).leading_zeros();
|
||||
w.f(4, wbits - 1);
|
||||
w.f(4, hbits - 1);
|
||||
w.f(wbits, c.width - 1);
|
||||
w.f(hbits, c.height - 1);
|
||||
w.f(1, 0); // frame_id_numbers_present_flag
|
||||
w.f(1, 0); // use_128x128_superblock
|
||||
w.f(1, 0); // enable_filter_intra
|
||||
w.f(1, 0); // enable_intra_edge_filter
|
||||
w.f(1, 0); // enable_interintra_compound
|
||||
w.f(1, 0); // enable_masked_compound
|
||||
w.f(1, 0); // enable_warped_motion
|
||||
w.f(1, 0); // enable_dual_filter
|
||||
w.f(1, 0); // enable_order_hint
|
||||
w.f(1, 0); // seq_choose_screen_content_tools
|
||||
w.f(1, 0); // seq_force_screen_content_tools
|
||||
w.f(1, 0); // enable_superres
|
||||
w.f(1, 0); // enable_cdef
|
||||
w.f(1, 0); // enable_restoration
|
||||
// color_config( )
|
||||
w.f(1, (c.bit_depth > 8) as u32); // high_bitdepth
|
||||
w.f(1, 0); // mono_chrome
|
||||
w.f(1, 1); // color_description_present_flag
|
||||
w.f(8, c.color_primaries as u32); // color_primaries
|
||||
w.f(8, c.transfer_characteristics as u32); // transfer_characteristics
|
||||
w.f(8, c.matrix_coefficients as u32); // matrix_coefficients
|
||||
w.f(1, c.full_range as u32); // color_range
|
||||
w.f(2, 0); // chroma_sample_position: unknown
|
||||
w.f(1, 0); // separate_uv_delta_q
|
||||
w.f(1, 0); // film_grain_params_present
|
||||
w.trailing_bits();
|
||||
w.into_bytes()
|
||||
}
|
||||
|
||||
/// uncompressed_header( ) (5.9) for a shown KEY_FRAME or an INTER_FRAME referencing slot 0.
|
||||
fn frame_header(&self, w: &mut BitWriter, key: bool, qidx: u8, lf_level: u32) {
|
||||
w.f(1, 0); // show_existing_frame
|
||||
w.f(2, if key { 0 } else { 1 }); // frame_type
|
||||
w.f(1, 1); // show_frame
|
||||
if !key {
|
||||
w.f(1, 0); // error_resilient_mode
|
||||
}
|
||||
w.f(1, 0); // disable_cdf_update
|
||||
w.f(1, 0); // frame_size_override_flag
|
||||
if !key {
|
||||
w.f(3, 0); // primary_ref_frame: LAST_FRAME's saved CDFs
|
||||
w.f(8, 1); // refresh_frame_flags: slot 0 becomes LAST for the next frame
|
||||
for _ in 0..7 {
|
||||
w.f(3, 0); // ref_frame_idx[ i ]
|
||||
}
|
||||
w.f(1, 0); // render_and_frame_size_different
|
||||
w.f(1, 0); // allow_high_precision_mv
|
||||
w.f(1, 0); // is_filter_switchable
|
||||
w.f(2, 0); // interpolation_filter = EIGHTTAP
|
||||
w.f(1, 0); // is_motion_mode_switchable
|
||||
} else {
|
||||
w.f(1, 0); // render_and_frame_size_different
|
||||
}
|
||||
w.f(1, 0); // disable_frame_end_update_cdf
|
||||
// tile_info( ): uniform spacing, one tile
|
||||
let g = &self.geom;
|
||||
let tile_log2 = |blk: usize, target: usize| {
|
||||
let mut k = 0;
|
||||
while (blk << k) < target {
|
||||
k += 1;
|
||||
}
|
||||
k
|
||||
};
|
||||
w.f(1, 1); // uniform_tile_spacing_flag
|
||||
if tile_log2(1, g.sb_cols.min(64)) > 0 {
|
||||
w.f(1, 0); // increment_tile_cols_log2
|
||||
}
|
||||
if tile_log2(1, g.sb_rows.min(64)) > 0 {
|
||||
w.f(1, 0); // increment_tile_rows_log2
|
||||
}
|
||||
// quantization_params( )
|
||||
w.f(8, qidx as u32);
|
||||
w.f(1, 0); // DeltaQYDc
|
||||
w.f(1, 0); // DeltaQUDc
|
||||
w.f(1, 0); // DeltaQUAc
|
||||
w.f(1, 0); // using_qmatrix
|
||||
w.f(1, 0); // segmentation_enabled
|
||||
w.f(1, 0); // delta_q_present (base_q_idx > 0)
|
||||
// loop_filter_params( ): the same level for every plane and direction
|
||||
w.f(6, lf_level);
|
||||
w.f(6, lf_level);
|
||||
if lf_level != 0 {
|
||||
w.f(6, lf_level);
|
||||
w.f(6, lf_level);
|
||||
}
|
||||
w.f(3, 0); // loop_filter_sharpness
|
||||
w.f(1, 0); // loop_filter_delta_enabled
|
||||
w.f(1, 0); // tx_mode_select (TX_MODE_LARGEST)
|
||||
if !key {
|
||||
w.f(1, 0); // reference_select
|
||||
}
|
||||
w.f(1, 1); // reduced_tx_set
|
||||
if !key {
|
||||
for _ in 0..7 {
|
||||
w.f(1, 0); // is_global
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Copies a picture into superblock-padded planes (edge replication).
|
||||
fn load_source(&self, frame: &Frame) -> [Plane; 3] {
|
||||
let maxv = (1u16 << self.cfg.bit_depth) - 1;
|
||||
std::array::from_fn(|p| {
|
||||
let (vw, vh) = self.geom.visible(p);
|
||||
let (pw, ph) = self.geom.padded(p);
|
||||
let (data, stride) = match p {
|
||||
0 => (frame.y, frame.y_stride),
|
||||
1 => (frame.u, frame.uv_stride),
|
||||
_ => (frame.v, frame.uv_stride),
|
||||
};
|
||||
assert!(stride >= vw && data.len() >= stride * (vh - 1) + vw, "plane {p} is too small for {vw}x{vh}");
|
||||
let mut pl = Plane::new(pw, ph);
|
||||
for y in 0..ph {
|
||||
let sy = y.min(vh - 1);
|
||||
let src = &data[sy * stride..sy * stride + vw];
|
||||
let row = &mut pl.data[y * pw..y * pw + pw];
|
||||
for (x, d) in row.iter_mut().enumerate() {
|
||||
*d = src[x.min(vw - 1)].min(maxv);
|
||||
}
|
||||
}
|
||||
pl
|
||||
})
|
||||
}
|
||||
|
||||
/// Codes one frame at `qidx`; returns the frame OBU and the reconstruction.
|
||||
fn encode_frame(&self, src: &[Plane; 3], key: bool, qidx: u8) -> (Vec<u8>, RefFrame) {
|
||||
let g = &self.geom;
|
||||
let refr = if key { None } else { self.refr.as_ref() };
|
||||
let mut dec = Decider::new(g, self.cfg.bit_depth as u32, qidx, key, src, refr);
|
||||
let mut tw = TileWriter::new(g, key, qidx, refr.map(|r| &*r.cdfs));
|
||||
for sbr in 0..g.sb_rows {
|
||||
tw.start_sb_row();
|
||||
for sbc in 0..g.sb_cols {
|
||||
let node = dec.superblock(sbr * 16, sbc * 16);
|
||||
tw.superblock(sbr * 16, sbc * 16, &node);
|
||||
}
|
||||
}
|
||||
let lf_level = self.pick_filter_level(&dec.rec, &tw.lf_info(), src, qidx);
|
||||
loop_filter(&mut dec.rec, &tw.lf_info(), [lf_level; 4], self.cfg.bit_depth as u32, false, 1);
|
||||
let (tile, cdfs) = tw.finish();
|
||||
let mut w = BitWriter::new();
|
||||
self.frame_header(&mut w, key, qidx, lf_level);
|
||||
w.byte_align();
|
||||
// tile_group_obu( ): a single tile, no tile_start_and_end_present_flag
|
||||
let mut payload = w.into_bytes();
|
||||
payload.extend_from_slice(&tile);
|
||||
let mut out = Vec::with_capacity(payload.len() + 8);
|
||||
obu(OBU_FRAME, &payload, &mut out);
|
||||
let rf = RefFrame { planes: dec.rec, mvs: dec.mvs, lf_level, cdfs };
|
||||
(out, rf)
|
||||
}
|
||||
|
||||
/// The loop filter level minimising the luma error (searched around a guess from the
|
||||
/// quantizer, on the luma of a subset of the superblock rows).
|
||||
fn pick_filter_level(&self, rec: &[Plane; 3], info: &LfInfo, src: &[Plane; 3], qidx: u8) -> u32 {
|
||||
let (w, h) = (self.geom.width, self.geom.height);
|
||||
let sb_row_step = if self.geom.sb_rows >= 9 { 3 } else { 1 };
|
||||
let sse = |pl: &Plane| -> u64 {
|
||||
let mut s = 0u64;
|
||||
for y in 0..h {
|
||||
if !(y >> 6).is_multiple_of(sb_row_step) {
|
||||
continue;
|
||||
}
|
||||
for (a, b) in pl.row(y)[..w].iter().zip(&src[0].row(y)[..w]) {
|
||||
let d = *a as i64 - *b as i64;
|
||||
s += (d * d) as u64;
|
||||
}
|
||||
}
|
||||
s
|
||||
};
|
||||
let bd = self.cfg.bit_depth as u32;
|
||||
let mut cache: Vec<(u32, u64)> = vec![(0, sse(&rec[0]))];
|
||||
// one working copy of the luma plane for all candidate levels
|
||||
let mut planes = [rec[0].clone(), Plane::new(0, 0), Plane::new(0, 0)];
|
||||
let mut eval = |lvl: u32| -> u64 {
|
||||
if let Some(&(_, e)) = cache.iter().find(|(l, _)| *l == lvl) {
|
||||
return e;
|
||||
}
|
||||
planes[0].data.copy_from_slice(&rec[0].data);
|
||||
loop_filter(&mut planes, info, [lvl; 4], bd, true, sb_row_step);
|
||||
let e = sse(&planes[0]);
|
||||
cache.push((lvl, e));
|
||||
e
|
||||
};
|
||||
let guess = ((qidx as u32 * 3) / 16).clamp(2, 50);
|
||||
let mut best = (0u32, eval(0));
|
||||
let mut step = (guess / 2).max(1);
|
||||
let mut center = guess;
|
||||
let e = eval(center);
|
||||
if e < best.1 {
|
||||
best = (center, e);
|
||||
}
|
||||
while step >= 2 {
|
||||
for lvl in [center.saturating_sub(step), (center + step).min(63)] {
|
||||
let e = eval(lvl);
|
||||
if e < best.1 {
|
||||
best = (lvl, e);
|
||||
}
|
||||
}
|
||||
center = best.0.max(1);
|
||||
step /= 2;
|
||||
}
|
||||
best.0
|
||||
}
|
||||
|
||||
/// Encodes one frame.
|
||||
pub fn encode(&mut self, frame: &Frame) -> Packet {
|
||||
let key = self.frame_num.is_multiple_of(self.cfg.keyint.max(1) as u64);
|
||||
let src = self.load_source(frame);
|
||||
let (frame_obu, rf) = match self.cfg.rate {
|
||||
RateControl::ConstantQ(q) => self.encode_frame(&src, key, q.max(1)),
|
||||
RateControl::Bitrate { .. } => match self.rc.take() {
|
||||
Some(mut rc) => {
|
||||
let out = self.encode_rate_controlled(&src, key, &mut rc);
|
||||
self.rc = Some(rc);
|
||||
out
|
||||
}
|
||||
// The state is created with the encoder for bitrate mode; without it, encode at a
|
||||
// middle quantizer rather than fail.
|
||||
None => self.encode_frame(&src, key, 128),
|
||||
},
|
||||
};
|
||||
self.refr = Some(rf);
|
||||
self.frame_num += 1;
|
||||
let mut data = Vec::with_capacity(frame_obu.len() + self.seq_obu.len());
|
||||
if key {
|
||||
data.extend_from_slice(&self.seq_obu);
|
||||
}
|
||||
data.extend_from_slice(&frame_obu);
|
||||
Packet { data, keyframe: key }
|
||||
}
|
||||
|
||||
fn encode_rate_controlled(&mut self, src: &[Plane; 3], key: bool, rc: &mut RateState) -> (Vec<u8>, RefFrame) {
|
||||
let first = self.frame_num == 0;
|
||||
let (budget, q_start) = rc.frame_budget(self.cfg.keyint, key);
|
||||
// pay back the accumulated deviation over the next RC_PAYBACK frames
|
||||
let target = (budget - rc.debt / RC_PAYBACK).clamp(budget * 0.35, budget * 2.0);
|
||||
let mut q = q_start;
|
||||
let mut best: Option<(Vec<u8>, RefFrame, f64, f64)> = None;
|
||||
// The first frame is coded up to four times to find its quantizer (it fixes the
|
||||
// quantizers of everything after it), a later key frame up to three times (it is a large
|
||||
// part of the budget of its group). A frame is coded again only while its size is far from
|
||||
// the target and an untried quantizer is left to try; other frames are coded once.
|
||||
let (max_passes, tolerance) = if first {
|
||||
(RC_FIRST_PASSES, RC_FIRST_TOLERANCE)
|
||||
} else if key {
|
||||
(RC_KEY_PASSES, RC_KEY_TOLERANCE)
|
||||
} else {
|
||||
(1, 0.0)
|
||||
};
|
||||
let mut tried: Vec<(u8, f64)> = Vec::new();
|
||||
for _ in 0..max_passes {
|
||||
let qi = q.round().clamp(1.0, 255.0) as u8;
|
||||
if tried.iter().any(|t| t.0 == qi) {
|
||||
break;
|
||||
}
|
||||
let (obu, rf) = self.encode_frame(src, key, qi);
|
||||
let bits = obu.len() as f64 * 8.0;
|
||||
let err = (bits / target).log2();
|
||||
tried.push((qi, err));
|
||||
if best.as_ref().is_none_or(|b| err.abs() < (b.2 / target).log2().abs()) {
|
||||
best = Some((obu, rf, bits, qi as f64));
|
||||
}
|
||||
if err.abs() <= tolerance {
|
||||
break;
|
||||
}
|
||||
let next = if first && tried.len() == 1 {
|
||||
Some((q + RC_FIRST_SLOPE * err).clamp(1.0, 255.0))
|
||||
} else {
|
||||
next_probe_q(self.cfg.bit_depth, &tried).map(f64::from)
|
||||
};
|
||||
match next {
|
||||
Some(next) => q = next,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
// At least one pass ran, so `best` is set.
|
||||
let Some((obu, rf, bits, q_used)) = best else { return self.encode_frame(src, key, q.round().clamp(1.0, 255.0) as u8) };
|
||||
rc.debt += bits - budget;
|
||||
rc.update(key, first, q_used, bits, target);
|
||||
(obu, rf)
|
||||
}
|
||||
|
||||
/// The loop filter level of the last encoded frame. For tests.
|
||||
#[doc(hidden)]
|
||||
pub fn last_loop_filter_level(&self) -> Option<u32> {
|
||||
self.refr.as_ref().map(|r| r.lf_level)
|
||||
}
|
||||
|
||||
/// The reconstruction of the last encoded frame (visible area, planes Y, U, V), equal to
|
||||
/// what a decoder outputs. For tests.
|
||||
#[doc(hidden)]
|
||||
pub fn last_reconstruction(&self) -> Option<[Vec<u16>; 3]> {
|
||||
let rf = self.refr.as_ref()?;
|
||||
Some(std::array::from_fn(|p| {
|
||||
let (vw, vh) = self.geom.visible(p);
|
||||
let pl = &rf.planes[p];
|
||||
let mut v = Vec::with_capacity(vw * vh);
|
||||
for y in 0..vh {
|
||||
v.extend_from_slice(&pl.row(y)[..vw]);
|
||||
}
|
||||
v
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -1,209 +0,0 @@
|
||||
//! Deblocking loop filter (spec 7.14), mirrored from the decoder process so that the encoder's
|
||||
//! references equal a decoder's. Segmentation and loop filter deltas are off and the sharpness
|
||||
//! is 0 in the frames this encoder writes.
|
||||
|
||||
use crate::pred::Plane;
|
||||
use crate::tables::{MAX_LOOP_FILTER, NUM_4X4_BLOCKS_HIGH, NUM_4X4_BLOCKS_WIDE, SUBSAMPLED_SIZE, TX_HEIGHT, TX_WIDTH};
|
||||
|
||||
/// Mode info the loop filter reads (per 4x4 luma unit, row stride `mi_cols`), and the
|
||||
/// transform size per 4x4 unit of each plane (`LoopfilterTxSizes`, row stride `mi_cols + 32`).
|
||||
pub(crate) struct LfInfo<'a> {
|
||||
pub mi_cols: usize,
|
||||
pub mi_rows: usize,
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub mi_size: &'a [u8],
|
||||
pub skip: &'a [bool],
|
||||
pub is_inter: &'a [bool],
|
||||
pub tx: [&'a [u8]; 3],
|
||||
}
|
||||
|
||||
/// Applies the loop filter with `loop_filter_level[ 0..4 ]` to `planes`. For level search
|
||||
/// (`luma_only`), only plane 0 and only every `sb_row_step`-th superblock row are filtered.
|
||||
pub(crate) fn loop_filter(planes: &mut [Plane; 3], info: &LfInfo, level: [u32; 4], bit_depth: u32, luma_only: bool, sb_row_step: usize) {
|
||||
if level[0] == 0 && level[1] == 0 {
|
||||
return;
|
||||
}
|
||||
let nplanes = if luma_only { 1 } else { 3 };
|
||||
for (plane, pl) in planes.iter_mut().enumerate().take(nplanes) {
|
||||
if plane > 0 && level[1 + plane] == 0 {
|
||||
continue;
|
||||
}
|
||||
for pass in 0..2 {
|
||||
let step = if plane == 0 { 1 } else { 2 };
|
||||
let mut row = 0;
|
||||
while row < info.mi_rows {
|
||||
if !(row >> 4).is_multiple_of(sb_row_step) {
|
||||
row += step;
|
||||
continue;
|
||||
}
|
||||
let mut col = 0;
|
||||
while col < info.mi_cols {
|
||||
edge(pl, info, level, bit_depth, plane, pass, (row, col));
|
||||
col += step;
|
||||
}
|
||||
row += step;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn edge(pl: &mut Plane, info: &LfInfo, level: [u32; 4], bd: u32, plane: usize, pass: usize, position: (usize, usize)) {
|
||||
let (row, col) = position;
|
||||
let s = (plane > 0) as usize;
|
||||
let (dx, dy) = if pass == 0 { (1usize, 0usize) } else { (0, 1) };
|
||||
let x = col * 4;
|
||||
let y = row * 4;
|
||||
let row = row | s;
|
||||
let col = col | s;
|
||||
let on_screen = !(x >= info.width || y >= info.height || (pass == 0 && x == 0) || (pass == 1 && y == 0));
|
||||
if !on_screen {
|
||||
return;
|
||||
}
|
||||
let xp = x >> s;
|
||||
let yp = y >> s;
|
||||
let prev_row = row - (dy << s);
|
||||
let prev_col = col - (dx << s);
|
||||
let i = row * info.mi_cols + col;
|
||||
let mi_size = info.mi_size[i] as usize;
|
||||
let tx_stride = info.mi_cols + 32;
|
||||
let tx_sz = info.tx[plane][(row >> s) * tx_stride + (col >> s)] as usize;
|
||||
let plane_size = SUBSAMPLED_SIZE[mi_size][s][s] as usize;
|
||||
let skip = info.skip[i];
|
||||
let is_intra = !info.is_inter[i];
|
||||
let prev_tx_sz = info.tx[plane][(prev_row >> s) * tx_stride + (prev_col >> s)] as usize;
|
||||
let is_block_edge = if pass == 0 {
|
||||
xp.is_multiple_of(4 * NUM_4X4_BLOCKS_WIDE[plane_size] as usize)
|
||||
} else {
|
||||
yp.is_multiple_of(4 * NUM_4X4_BLOCKS_HIGH[plane_size] as usize)
|
||||
};
|
||||
let is_tx_edge = if pass == 0 { xp.is_multiple_of(TX_WIDTH[tx_sz] as usize) } else { yp.is_multiple_of(TX_HEIGHT[tx_sz] as usize) };
|
||||
let apply_filter = is_tx_edge && (is_block_edge || !skip || is_intra);
|
||||
let base_size = if pass == 0 { TX_WIDTH[prev_tx_sz].min(TX_WIDTH[tx_sz]) } else { TX_HEIGHT[prev_tx_sz].min(TX_HEIGHT[tx_sz]) } as usize;
|
||||
let filter_size = if plane == 0 { 16.min(base_size) } else { 8.min(base_size) };
|
||||
// 7.14.4 with no deltas, segmentation or sharpness: the same strength for every block
|
||||
let lvl = level[if plane == 0 { pass } else { plane + 1 }].min(MAX_LOOP_FILTER as u32) as i32;
|
||||
if !apply_filter || lvl == 0 {
|
||||
return;
|
||||
}
|
||||
let limit = lvl.max(1);
|
||||
let blimit = 2 * (lvl + 2) + limit;
|
||||
let thresh = lvl >> 4;
|
||||
let strength = FilterStrength { limit, blimit, thresh };
|
||||
for k in 0..4 {
|
||||
sample_filter(pl, (xp + dy * k, yp + dx * k), plane, &strength, (dx as isize, dy as isize), filter_size, bd);
|
||||
}
|
||||
}
|
||||
|
||||
/// Thresholds derived from the loop-filter level (spec 7.14.4).
|
||||
struct FilterStrength {
|
||||
limit: i32,
|
||||
blimit: i32,
|
||||
thresh: i32,
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_filter(pl: &mut Plane, position: (usize, usize), plane: usize, strength: &FilterStrength, direction: (isize, isize), filter_size: usize, bd: u32) {
|
||||
let (x, y) = position;
|
||||
let (dx, dy) = direction;
|
||||
let FilterStrength { limit, blimit, thresh } = *strength;
|
||||
let stride = pl.stride as isize;
|
||||
let step = dy * stride + dx;
|
||||
let base = y as isize * stride + x as isize;
|
||||
let reach: isize = if filter_size >= 16 { 7 } else { 4 };
|
||||
let mut v = [0i32; 16];
|
||||
for k in -reach..reach {
|
||||
v[(k + 8) as usize] = pl.data[(base + k * step) as usize] as i32;
|
||||
}
|
||||
let at = |k: isize| -> i32 { v[(k + 8) as usize] };
|
||||
let (q0, q1, q2, q3) = (at(0), at(1), at(2), at(3));
|
||||
let (p0, p1, p2, p3) = (at(-1), at(-2), at(-3), at(-4));
|
||||
let d = &mut pl.data;
|
||||
let sh = bd - 8;
|
||||
let thresh_bd = thresh << sh;
|
||||
let hev = (p1 - p0).abs() > thresh_bd || (q1 - q0).abs() > thresh_bd;
|
||||
let filter_len = if filter_size == 4 {
|
||||
4
|
||||
} else if plane != 0 {
|
||||
6
|
||||
} else if filter_size == 8 {
|
||||
8
|
||||
} else {
|
||||
16
|
||||
};
|
||||
let limit_bd = limit << sh;
|
||||
let blimit_bd = blimit << sh;
|
||||
let mut mask = (p1 - p0).abs() > limit_bd || (q1 - q0).abs() > limit_bd || (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 > blimit_bd;
|
||||
if filter_len >= 6 {
|
||||
mask |= (p2 - p1).abs() > limit_bd || (q2 - q1).abs() > limit_bd;
|
||||
}
|
||||
if filter_len >= 8 {
|
||||
mask |= (p3 - p2).abs() > limit_bd || (q3 - q2).abs() > limit_bd;
|
||||
}
|
||||
if mask {
|
||||
return;
|
||||
}
|
||||
let t_bd = 1 << sh;
|
||||
let flat = if filter_size >= 8 {
|
||||
let mut m = (p1 - p0).abs() > t_bd || (q1 - q0).abs() > t_bd || (p2 - p0).abs() > t_bd || (q2 - q0).abs() > t_bd;
|
||||
if filter_len >= 8 {
|
||||
m |= (p3 - p0).abs() > t_bd || (q3 - q0).abs() > t_bd;
|
||||
}
|
||||
!m
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let flat2 = if filter_size >= 16 {
|
||||
let (q4, q5, q6, p4, p5, p6) = (at(4), at(5), at(6), at(-5), at(-6), at(-7));
|
||||
!((p6 - p0).abs() > t_bd
|
||||
|| (q6 - q0).abs() > t_bd
|
||||
|| (p5 - p0).abs() > t_bd
|
||||
|| (q5 - q0).abs() > t_bd
|
||||
|| (p4 - p0).abs() > t_bd
|
||||
|| (q4 - q0).abs() > t_bd)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if filter_size == 4 || !flat {
|
||||
// narrow filter (7.14.6.3)
|
||||
let half = 1i32 << (bd - 1);
|
||||
let c = |v: i32| v.clamp(-half, half - 1);
|
||||
let off = 0x80 << sh;
|
||||
let (ps1, ps0, qs0, qs1) = (p1 - off, p0 - off, q0 - off, q1 - off);
|
||||
let mut filter = if hev { c(ps1 - qs1) } else { 0 };
|
||||
filter = c(filter + 3 * (qs0 - ps0));
|
||||
let filter1 = c(filter + 4) >> 3;
|
||||
let filter2 = c(filter + 3) >> 3;
|
||||
d[base as usize] = (c(qs0 - filter1) + off) as u16;
|
||||
d[(base - step) as usize] = (c(ps0 + filter2) + off) as u16;
|
||||
if !hev {
|
||||
let f = (filter1 + 1) >> 1;
|
||||
d[(base + step) as usize] = (c(qs1 - f) + off) as u16;
|
||||
d[(base - 2 * step) as usize] = (c(ps1 + f) + off) as u16;
|
||||
}
|
||||
} else {
|
||||
// wide filter (7.14.6.4)
|
||||
let log2 = if filter_size == 8 || !flat2 { 3 } else { 4 };
|
||||
let n: isize = if log2 == 4 {
|
||||
6
|
||||
} else if plane == 0 {
|
||||
3
|
||||
} else {
|
||||
2
|
||||
};
|
||||
let n2: isize = if log2 == 3 && plane == 0 { 0 } else { 1 };
|
||||
let mut f = [0i32; 12];
|
||||
for i in -n..n {
|
||||
let mut t = 0;
|
||||
for j in -n..=n {
|
||||
let p = (i + j).clamp(-(n + 1), n);
|
||||
let tap = if j.abs() <= n2 { 2 } else { 1 };
|
||||
t += at(p) * tap;
|
||||
}
|
||||
f[(i + n) as usize] = (t + (1 << (log2 - 1))) >> log2;
|
||||
}
|
||||
for i in -n..n {
|
||||
d[(base + i * step) as usize] = f[(i + n) as usize] as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,445 +0,0 @@
|
||||
//! Prediction processes, mirrored from the spec (7.11.2 intra prediction for the non-directional
|
||||
//! modes plus V_PRED / H_PRED without edge filtering, 7.11.3 block inter prediction without
|
||||
//! reference scaling) so that the encoder predicts exactly like a decoder.
|
||||
|
||||
use crate::tables::{
|
||||
DC_PRED, EIGHTTAP, H_PRED, PAETH_PRED, SM_WEIGHTS_TX_4X4, SM_WEIGHTS_TX_8X8, SM_WEIGHTS_TX_16X16, SM_WEIGHTS_TX_32X32, SMOOTH_H_PRED, SMOOTH_PRED,
|
||||
SMOOTH_V_PRED, SUBPEL_FILTERS, V_PRED,
|
||||
};
|
||||
use crate::transform::round2;
|
||||
|
||||
/// A plane buffer (padded to whole superblocks).
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Plane {
|
||||
pub data: Vec<u16>,
|
||||
pub stride: usize,
|
||||
}
|
||||
|
||||
impl Plane {
|
||||
pub fn new(w: usize, h: usize) -> Plane {
|
||||
Plane { data: vec![0; w * h], stride: w }
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn at(&self, x: usize, y: usize) -> u16 {
|
||||
self.data[y * self.stride + x]
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn row(&self, y: usize) -> &[u16] {
|
||||
&self.data[y * self.stride..(y + 1) * self.stride]
|
||||
}
|
||||
}
|
||||
|
||||
/// The intra modes this encoder uses (values are the spec's y / uv mode numbers).
|
||||
pub(crate) const INTRA_MODES_USED: [usize; 7] = [DC_PRED, V_PRED, H_PRED, PAETH_PRED, SMOOTH_PRED, SMOOTH_V_PRED, SMOOTH_H_PRED];
|
||||
|
||||
/// Inputs of the intra prediction process.
|
||||
pub(crate) struct IntraEdge {
|
||||
pub x: usize,
|
||||
pub y: usize,
|
||||
pub have_left: bool,
|
||||
pub have_above: bool,
|
||||
pub have_above_right: bool,
|
||||
pub have_below_left: bool,
|
||||
pub log2: u32,
|
||||
/// ((MiCols * MI_SIZE) >> subX) - 1 and the same for rows.
|
||||
pub max_x: i32,
|
||||
pub max_y: i32,
|
||||
pub bit_depth: u32,
|
||||
}
|
||||
|
||||
/// Edge samples: `above[0..2n]`, `left[0..2n]` and the top-left corner.
|
||||
pub(crate) struct Edges {
|
||||
above: [i32; 64],
|
||||
left: [i32; 64],
|
||||
corner: i32,
|
||||
}
|
||||
|
||||
pub(crate) fn edges(pl: &Plane, e: &IntraEdge) -> Edges {
|
||||
let w = 1usize << e.log2;
|
||||
let h = w;
|
||||
let bd = e.bit_depth;
|
||||
let (x, y) = (e.x, e.y);
|
||||
let n = w + h;
|
||||
let mut above = [0i32; 64];
|
||||
let mut left = [0i32; 64];
|
||||
if !e.have_above && e.have_left {
|
||||
let v = pl.at(x - 1, y) as i32;
|
||||
above[..n].iter_mut().for_each(|a| *a = v);
|
||||
} else if !e.have_above && !e.have_left {
|
||||
let v = (1 << (bd - 1)) - 1;
|
||||
above[..n].iter_mut().for_each(|a| *a = v);
|
||||
} else {
|
||||
let above_limit = e.max_x.min(x as i32 + if e.have_above_right { 2 * w as i32 } else { w as i32 } - 1);
|
||||
let row = pl.row(y - 1);
|
||||
for (i, a) in above[..n].iter_mut().enumerate() {
|
||||
*a = row[above_limit.min(x as i32 + i as i32) as usize] as i32;
|
||||
}
|
||||
}
|
||||
if !e.have_left && e.have_above {
|
||||
let v = pl.at(x, y - 1) as i32;
|
||||
left[..n].iter_mut().for_each(|a| *a = v);
|
||||
} else if !e.have_left && !e.have_above {
|
||||
let v = (1 << (bd - 1)) + 1;
|
||||
left[..n].iter_mut().for_each(|a| *a = v);
|
||||
} else {
|
||||
let left_limit = e.max_y.min(y as i32 + if e.have_below_left { 2 * h as i32 } else { h as i32 } - 1);
|
||||
for (i, l) in left[..n].iter_mut().enumerate() {
|
||||
*l = pl.at(x - 1, left_limit.min(y as i32 + i as i32) as usize) as i32;
|
||||
}
|
||||
}
|
||||
let corner = if e.have_above && e.have_left {
|
||||
pl.at(x - 1, y - 1) as i32
|
||||
} else if e.have_above {
|
||||
pl.at(x, y - 1) as i32
|
||||
} else if e.have_left {
|
||||
pl.at(x - 1, y) as i32
|
||||
} else {
|
||||
1 << (bd - 1)
|
||||
};
|
||||
Edges { above, left, corner }
|
||||
}
|
||||
|
||||
fn sm_weights(log2: u32) -> &'static [u8] {
|
||||
match log2 {
|
||||
2 => &SM_WEIGHTS_TX_4X4,
|
||||
3 => &SM_WEIGHTS_TX_8X8,
|
||||
4 => &SM_WEIGHTS_TX_16X16,
|
||||
_ => &SM_WEIGHTS_TX_32X32,
|
||||
}
|
||||
}
|
||||
|
||||
/// Predicts a square `2^log2` block with `mode` into `pred` (row stride = block width).
|
||||
pub(crate) fn predict_intra(ed: &Edges, e: &IntraEdge, mode: usize, pred: &mut [u16]) {
|
||||
let log2 = e.log2;
|
||||
let w = 1usize << log2;
|
||||
let h = w;
|
||||
let bd = e.bit_depth;
|
||||
let (above, left) = (&ed.above, &ed.left);
|
||||
match mode {
|
||||
DC_PRED => {
|
||||
let v = if e.have_left && e.have_above {
|
||||
let sum: i32 = left[..h].iter().sum::<i32>() + above[..w].iter().sum::<i32>() + ((w + h) >> 1) as i32;
|
||||
sum / (w + h) as i32
|
||||
} else if e.have_left {
|
||||
(left[..h].iter().sum::<i32>() + (h >> 1) as i32) >> log2
|
||||
} else if e.have_above {
|
||||
(above[..w].iter().sum::<i32>() + (w >> 1) as i32) >> log2
|
||||
} else {
|
||||
1 << (bd - 1)
|
||||
};
|
||||
let v = v.clamp(0, (1 << bd) - 1) as u16;
|
||||
pred[..w * h].iter_mut().for_each(|p| *p = v);
|
||||
}
|
||||
V_PRED => {
|
||||
for i in 0..h {
|
||||
for j in 0..w {
|
||||
pred[i * w + j] = above[j] as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
H_PRED => {
|
||||
for i in 0..h {
|
||||
pred[i * w..i * w + w].iter_mut().for_each(|p| *p = left[i] as u16);
|
||||
}
|
||||
}
|
||||
PAETH_PRED => {
|
||||
let tl = ed.corner;
|
||||
for i in 0..h {
|
||||
for j in 0..w {
|
||||
let a = above[j];
|
||||
let l = left[i];
|
||||
let base = a + l - tl;
|
||||
let p_left = (base - l).abs();
|
||||
let p_top = (base - a).abs();
|
||||
let p_tl = (base - tl).abs();
|
||||
pred[i * w + j] = if p_left <= p_top && p_left <= p_tl {
|
||||
l as u16
|
||||
} else if p_top <= p_tl {
|
||||
a as u16
|
||||
} else {
|
||||
tl as u16
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let wx = sm_weights(log2);
|
||||
let wy = wx;
|
||||
for i in 0..h {
|
||||
for j in 0..w {
|
||||
let v = if mode == SMOOTH_PRED {
|
||||
let s = wy[i] as i32 * above[j] + (256 - wy[i] as i32) * left[h - 1] + wx[j] as i32 * left[i] + (256 - wx[j] as i32) * above[w - 1];
|
||||
round2(s, 9)
|
||||
} else if mode == SMOOTH_V_PRED {
|
||||
round2(wy[i] as i32 * above[j] + (256 - wy[i] as i32) * left[h - 1], 8)
|
||||
} else {
|
||||
round2(wx[j] as i32 * left[i] + (256 - wx[j] as i32) * above[w - 1], 8)
|
||||
};
|
||||
pred[i * w + j] = v as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Block inter prediction (7.11.3.3 motion vector scaling without scaling, 7.11.3.4 block
|
||||
/// inter prediction with the EIGHTTAP filter, single reference so InterRound1 = 11 and the
|
||||
/// final rounding is the identity) of the `w x h` block at plane position (x, y) displaced by
|
||||
/// `mv` (1/8 luma sample units, [row, col]). `last_x` / `last_y` are the largest sample
|
||||
/// coordinates of the reference plane; the result goes to `pred` (row stride `w`).
|
||||
pub(crate) struct InterBlock {
|
||||
pub last_x: i32,
|
||||
pub last_y: i32,
|
||||
pub x: usize,
|
||||
pub y: usize,
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub mv: [i32; 2],
|
||||
pub subsampling: usize,
|
||||
pub bit_depth: u32,
|
||||
}
|
||||
|
||||
pub(crate) fn predict_inter(refp: &Plane, block: &InterBlock, pred: &mut [u16]) {
|
||||
let InterBlock { last_x, last_y, x, y, width: w, height: h, mv, subsampling: ss, bit_depth } = *block;
|
||||
// positions in 1/16 sample units ((startX >> 6) & 15 is the filter phase)
|
||||
let px = ((x as i32) << 4) + ((2 * mv[1]) >> ss);
|
||||
let py = ((y as i32) << 4) + ((2 * mv[0]) >> ss);
|
||||
let (fx, fy) = ((px & 15) as usize, (py & 15) as usize);
|
||||
let (ix, iy) = (px >> 4, py >> 4);
|
||||
let max = (1i32 << bit_depth) - 1;
|
||||
if fx == 0 && fy == 0 {
|
||||
// the filters' phase-0 kernel is a unit impulse: a clamped copy
|
||||
for r in 0..h {
|
||||
let ry = (iy + r as i32).clamp(0, last_y) as usize;
|
||||
let row = refp.row(ry);
|
||||
let out = &mut pred[r * w..r * w + w];
|
||||
let x0 = ix;
|
||||
if x0 >= 0 && x0 + w as i32 - 1 <= last_x {
|
||||
out.copy_from_slice(&row[x0 as usize..x0 as usize + w]);
|
||||
} else {
|
||||
for (c, o) in out.iter_mut().enumerate() {
|
||||
*o = row[(x0 + c as i32).clamp(0, last_x) as usize];
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
let filt_h = if w <= 4 { 4 } else { EIGHTTAP };
|
||||
let filt_v = if h <= 4 { 4 } else { EIGHTTAP };
|
||||
let kh = &SUBPEL_FILTERS[filt_h][fx];
|
||||
let kv = &SUBPEL_FILTERS[filt_v][fy];
|
||||
let bx = ix - 3;
|
||||
let x_inside = bx >= 0 && bx + w as i32 + 7 <= last_x + 1;
|
||||
// horizontal pass of one reference row into `out` (Round2( sum, InterRound0 = 3 ))
|
||||
let hfilter = |ry: usize, out: &mut [i32]| {
|
||||
let row = refp.row(ry);
|
||||
if x_inside {
|
||||
let seg = &row[bx as usize..bx as usize + w + 7];
|
||||
for (c, o) in out.iter_mut().enumerate() {
|
||||
let mut s = 0i32;
|
||||
for t in 0..8 {
|
||||
s += kh[t] as i32 * seg[c + t] as i32;
|
||||
}
|
||||
*o = round2(s, 3);
|
||||
}
|
||||
} else {
|
||||
for (c, o) in out.iter_mut().enumerate() {
|
||||
let mut s = 0i32;
|
||||
for t in 0..8 {
|
||||
s += kh[t] as i32 * row[(bx + c as i32 + t as i32).clamp(0, last_x) as usize] as i32;
|
||||
}
|
||||
*o = round2(s, 3);
|
||||
}
|
||||
}
|
||||
};
|
||||
let mut inter = [0i32; (64 + 7) * 64];
|
||||
if fy == 0 {
|
||||
// the vertical kernel is 128 at the centre: Round2( 128 * v, 11 ) = Round2( v, 4 )
|
||||
for r in 0..h {
|
||||
let ry = (iy + r as i32).clamp(0, last_y) as usize;
|
||||
hfilter(ry, &mut inter[..w]);
|
||||
for (p, &v) in pred[r * w..r * w + w].iter_mut().zip(&inter[..w]) {
|
||||
*p = round2(v, 4).clamp(0, max) as u16;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if fx == 0 {
|
||||
// the horizontal pass is exactly 16 * sample: Round2( 16 * sum, 11 ) = Round2( sum, 7 )
|
||||
for r in 0..h {
|
||||
let rows: [&[u16]; 8] = std::array::from_fn(|t| refp.row((iy + r as i32 + t as i32 - 3).clamp(0, last_y) as usize));
|
||||
for c in 0..w {
|
||||
let xx = if x_inside { (ix + c as i32) as usize } else { (ix + c as i32).clamp(0, last_x) as usize };
|
||||
let mut s = 0i32;
|
||||
for t in 0..8 {
|
||||
s += kv[t] as i32 * rows[t][xx] as i32;
|
||||
}
|
||||
pred[r * w + c] = round2(s, 7).clamp(0, max) as u16;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
let inter_h = h + 7;
|
||||
for r in 0..inter_h {
|
||||
let ry = (iy + r as i32 - 3).clamp(0, last_y) as usize;
|
||||
hfilter(ry, &mut inter[r * w..r * w + w]);
|
||||
}
|
||||
for r in 0..h {
|
||||
for c in 0..w {
|
||||
let mut s = 0i32;
|
||||
for t in 0..8 {
|
||||
s += kv[t] as i32 * inter[(r + t) * w + c];
|
||||
}
|
||||
pred[r * w + c] = round2(s, 11).clamp(0, max) as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The visible `w x h` area of a reference plane interpolated at the sub-sample phase
|
||||
/// (`fx`, `fy`) (1/16 units) with the EIGHTTAP filter, as `predict_inter` computes it for
|
||||
/// blocks wider and taller than 4. The reference for `half_planes`, which the encoder uses.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn subpel_plane(refp: &Plane, w: usize, h: usize, fx: usize, fy: usize, bit_depth: u32) -> Vec<u16> {
|
||||
let kh = &SUBPEL_FILTERS[EIGHTTAP][fx];
|
||||
let kv = &SUBPEL_FILTERS[EIGHTTAP][fy];
|
||||
let max = (1i32 << bit_depth) - 1;
|
||||
let (lx, ly) = (w as i32 - 1, h as i32 - 1);
|
||||
let mut inter = vec![0i32; w * h];
|
||||
for y in 0..h {
|
||||
let row = refp.row(y);
|
||||
for x in 0..w {
|
||||
let mut s = 0i32;
|
||||
for t in 0..8 {
|
||||
s += kh[t] as i32 * row[(x as i32 + t as i32 - 3).clamp(0, lx) as usize] as i32;
|
||||
}
|
||||
inter[y * w + x] = round2(s, 3);
|
||||
}
|
||||
}
|
||||
let mut out = vec![0u16; w * h];
|
||||
for y in 0..h {
|
||||
for x in 0..w {
|
||||
let mut s = 0i32;
|
||||
for t in 0..8 {
|
||||
s += kv[t] as i32 * inter[(y as i32 + t as i32 - 3).clamp(0, ly) as usize * w + x];
|
||||
}
|
||||
out[y * w + x] = round2(s, 11).clamp(0, max) as u16;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The three half-sample planes (8, 0), (0, 8), (8, 8) of `subpel_plane` in one pass: the
|
||||
/// horizontal half-sample pass is shared by (8, 0) and (8, 8), and the phase-0 kernels reduce to
|
||||
/// exact shifts (Round2( 128 * v, 11 ) = Round2( v, 4 ), Round2( 16 * s, 11 ) = Round2( s, 7 )),
|
||||
/// so the result equals three `subpel_plane` calls bit for bit.
|
||||
pub(crate) fn half_planes(refp: &Plane, w: usize, h: usize, bit_depth: u32) -> [Vec<u16>; 3] {
|
||||
let k = &SUBPEL_FILTERS[EIGHTTAP][8];
|
||||
let max = (1i32 << bit_depth) - 1;
|
||||
let (lx, ly) = (w as i32 - 1, h as i32 - 1);
|
||||
// horizontal pass at phase 8: Round2( sum, 3 )
|
||||
let mut hp = vec![0i32; w * h];
|
||||
for y in 0..h {
|
||||
let row = &refp.row(y)[..w];
|
||||
let out = &mut hp[y * w..y * w + w];
|
||||
let tap = |x: i32| -> i32 {
|
||||
let mut s = 0i32;
|
||||
for t in 0..8 {
|
||||
s += k[t] as i32 * row[(x + t as i32 - 3).clamp(0, lx) as usize] as i32;
|
||||
}
|
||||
round2(s, 3)
|
||||
};
|
||||
if w >= 8 {
|
||||
for x in 0..3 {
|
||||
out[x] = tap(x as i32);
|
||||
}
|
||||
for (x, o) in out.iter_mut().enumerate().take(w - 4).skip(3) {
|
||||
let seg = &row[x - 3..x + 5];
|
||||
let mut s = 0i32;
|
||||
for t in 0..8 {
|
||||
s += k[t] as i32 * seg[t] as i32;
|
||||
}
|
||||
*o = round2(s, 3);
|
||||
}
|
||||
for x in w - 4..w {
|
||||
out[x] = tap(x as i32);
|
||||
}
|
||||
} else {
|
||||
for (x, o) in out.iter_mut().enumerate() {
|
||||
*o = tap(x as i32);
|
||||
}
|
||||
}
|
||||
}
|
||||
let p80: Vec<u16> = hp.iter().map(|&v| round2(v, 4).clamp(0, max) as u16).collect();
|
||||
let mut p08 = vec![0u16; w * h];
|
||||
let mut p88 = vec![0u16; w * h];
|
||||
for y in 0..h {
|
||||
let rows: [usize; 8] = std::array::from_fn(|t| (y as i32 + t as i32 - 3).clamp(0, ly) as usize);
|
||||
let (o08, o88) = (&mut p08[y * w..y * w + w], &mut p88[y * w..y * w + w]);
|
||||
for x in 0..w {
|
||||
let (mut a, mut b) = (0i32, 0i32);
|
||||
for t in 0..8 {
|
||||
a += k[t] as i32 * refp.data[rows[t] * refp.stride + x] as i32;
|
||||
b += k[t] as i32 * hp[rows[t] * w + x];
|
||||
}
|
||||
o08[x] = round2(a, 7).clamp(0, max) as u16;
|
||||
o88[x] = round2(b, 11).clamp(0, max) as u16;
|
||||
}
|
||||
}
|
||||
[p80, p08, p88]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Whole-plane interpolation equals block inter prediction at the same phase.
|
||||
#[test]
|
||||
fn subpel_plane_matches_block_prediction() {
|
||||
let (w, h) = (37, 29);
|
||||
let mut p = Plane::new(64, 64);
|
||||
for (i, v) in p.data.iter_mut().enumerate() {
|
||||
*v = ((i * 7919) % 256) as u16;
|
||||
}
|
||||
for (fx, fy) in [(8, 0), (0, 8), (8, 8), (4, 12)] {
|
||||
let plane = subpel_plane(&p, w, h, fx, fy, 8);
|
||||
for (bx, by) in [(0i32, 0i32), (8, 8), (24, 16), (-5, 20), (30, -3)] {
|
||||
let mut pred = vec![0u16; 64];
|
||||
let mv = [by * 8 + fy as i32 / 2, bx * 8 + fx as i32 / 2];
|
||||
predict_inter(
|
||||
&p,
|
||||
&InterBlock { last_x: w as i32 - 1, last_y: h as i32 - 1, x: 0, y: 0, width: 8, height: 8, mv, subsampling: 0, bit_depth: 8 },
|
||||
&mut pred,
|
||||
);
|
||||
for r in 0..8 {
|
||||
for c in 0..8 {
|
||||
let (x, y) = ((bx + c).clamp(0, w as i32 - 1), (by + r).clamp(0, h as i32 - 1));
|
||||
if bx + c == x && by + r == y {
|
||||
assert_eq!(pred[(r * 8 + c) as usize], plane[y as usize * w + x as usize], "phase ({fx}, {fy}) at ({}, {})", bx + c, by + r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn half_planes_match_subpel_plane() {
|
||||
for (w, h) in [(37usize, 29usize), (5, 3), (1, 1), (64, 64)] {
|
||||
let mut p = Plane::new(64, 64);
|
||||
for (i, v) in p.data.iter_mut().enumerate() {
|
||||
*v = ((i * 7919) % 1024) as u16;
|
||||
}
|
||||
let hp = half_planes(&p, w, h, 10);
|
||||
for (i, (fx, fy)) in [(8, 0), (0, 8), (8, 8)].into_iter().enumerate() {
|
||||
assert_eq!(hp[i], subpel_plane(&p, w, h, fx, fy, 10), "{w}x{h} phase ({fx}, {fy})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phase_zero_filters_are_impulses() {
|
||||
for f in SUBPEL_FILTERS.iter() {
|
||||
assert_eq!(f[0], [0, 0, 0, 128, 0, 0, 0, 0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,267 +0,0 @@
|
||||
//! Multi-symbol arithmetic encoder: the exact inverse of the symbol decoder of spec 8.2.
|
||||
//!
|
||||
//! The decoder keeps `SymbolValue`, the bitwise complement of a 15-bit window into the tile data
|
||||
//! minus the lower bounds of the symbols decoded so far, and selects symbol `k` when
|
||||
//! `cur(k) <= SymbolValue < cur(k - 1)` with
|
||||
//! `cur(k) = ((SymbolRange >> 8) * ((32768 - cdf[k]) >> 6) >> 1) + 4 * (N - k - 1)` and
|
||||
//! `cur(-1) = SymbolRange`. The encoder therefore runs an ordinary carry-propagating range
|
||||
//! coder over the complemented bit stream: coding symbol `k` adds `cur(k)` to `low` and sets the
|
||||
//! range to `cur(k - 1) - cur(k)`; both are renormalised exactly like the decoder's range.
|
||||
//!
|
||||
//! At the end (spec 8.2.4 exit process) the decoder has consumed `S` renormalisation bits plus a
|
||||
//! 15-bit window, and the bits from position `S` on must be a one followed by zeros. In the
|
||||
//! complemented domain that window is `0b011_1111_1111_1111`, so the encoder picks the `S`-bit
|
||||
//! prefix `D` with `D * 2^15 + 2^14 - 1` inside `[low, low + range)`, i.e.
|
||||
//! `D = (low + 2^14) >> 15`, and writes `!D`, a one bit and zero padding.
|
||||
|
||||
pub(crate) struct SymbolWriter {
|
||||
/// Low end of the interval, holding `15 + cnt` bits not yet moved to `out` (plus carries).
|
||||
low: u64,
|
||||
rng: u32,
|
||||
cnt: u32,
|
||||
out: Vec<u8>,
|
||||
pub disable_update: bool,
|
||||
}
|
||||
|
||||
impl SymbolWriter {
|
||||
pub fn new(disable_update: bool) -> Self {
|
||||
SymbolWriter { low: 0, rng: 1 << 15, cnt: 0, out: Vec::with_capacity(1 << 16), disable_update }
|
||||
}
|
||||
|
||||
/// Adds `k` to the bytes already output (carry propagation).
|
||||
fn carry(&mut self, mut k: u64) {
|
||||
let mut i = self.out.len();
|
||||
while k != 0 {
|
||||
i -= 1;
|
||||
let t = self.out[i] as u64 + k;
|
||||
self.out[i] = t as u8;
|
||||
k = t >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn encode(&mut self, cdf: &[u16], n: usize, s: usize) {
|
||||
let r8 = self.rng >> 8;
|
||||
let cur = |k: usize| -> u32 { ((r8 * ((32768 - cdf[k] as u32) >> 6)) >> 1) + 4 * (n - k - 1) as u32 };
|
||||
let hi = if s == 0 { self.rng } else { cur(s - 1) };
|
||||
let lo = cur(s);
|
||||
debug_assert!(hi > lo, "symbol {s} has an empty interval");
|
||||
self.low += lo as u64;
|
||||
self.rng = hi - lo;
|
||||
let d = 15 - (31 - self.rng.leading_zeros());
|
||||
if d == 0 {
|
||||
return;
|
||||
}
|
||||
self.rng <<= d;
|
||||
self.low <<= d;
|
||||
self.cnt += d;
|
||||
while self.cnt >= 8 {
|
||||
let sh = 15 + self.cnt - 8;
|
||||
let c = self.low >> sh;
|
||||
self.low &= (1u64 << sh) - 1;
|
||||
self.cnt -= 8;
|
||||
self.carry(c >> 8);
|
||||
self.out.push((c & 0xff) as u8);
|
||||
}
|
||||
}
|
||||
|
||||
/// Codes `s` with the adaptive CDF `cdf` (N values plus the adaptation counter).
|
||||
#[inline]
|
||||
pub fn symbol(&mut self, s: usize, cdf: &mut [u16]) {
|
||||
let n = cdf.len() - 1;
|
||||
debug_assert!(s < n);
|
||||
self.encode(cdf, n, s);
|
||||
if !self.disable_update {
|
||||
update_cdf(cdf, n, s);
|
||||
}
|
||||
}
|
||||
|
||||
/// Codes `s` with a CDF that is not adapted.
|
||||
pub fn symbol_fixed(&mut self, s: usize, cdf: &[u16]) {
|
||||
let n = cdf.len() - 1;
|
||||
self.encode(cdf, n, s);
|
||||
}
|
||||
|
||||
/// `read_bool()` counterpart.
|
||||
pub fn bool(&mut self, b: bool) {
|
||||
self.encode(&[1 << 14, 1 << 15, 0], 2, b as usize);
|
||||
}
|
||||
|
||||
/// `L(n)` counterpart (most significant bit first).
|
||||
#[cfg(test)]
|
||||
pub fn literal(&mut self, n: u32, v: u32) {
|
||||
for i in (0..n).rev() {
|
||||
self.bool((v >> i) & 1 != 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Finishes the tile: returns its bytes (with the trailing one bit and zero padding).
|
||||
pub fn finish(mut self) -> Vec<u8> {
|
||||
let low = self.low + (1 << 14);
|
||||
let mut tail = low >> 15;
|
||||
self.carry(tail >> self.cnt);
|
||||
tail &= (1u64 << self.cnt) - 1;
|
||||
let mut bytes: Vec<u8> = self.out.iter().map(|b| !b).collect();
|
||||
let n = self.cnt + 1; // 1..=8 bits: !tail then the trailing one bit
|
||||
let v = (((!tail) & ((1u64 << self.cnt) - 1)) << 1) | 1;
|
||||
bytes.push((v << (8 - n)) as u8);
|
||||
bytes
|
||||
}
|
||||
}
|
||||
|
||||
/// CDF adaptation (spec 8.2.6, as in the decoder).
|
||||
#[inline(always)]
|
||||
pub(crate) fn update_cdf(cdf: &mut [u16], n: usize, symbol: usize) {
|
||||
let count = cdf[n];
|
||||
let rate = 3 + (count > 15) as u32 + (count > 31) as u32 + (31 - (n as u32).leading_zeros()).min(2);
|
||||
let mut tmp = 0u32;
|
||||
for i in 0..n - 1 {
|
||||
if i == symbol {
|
||||
tmp = 1 << 15;
|
||||
}
|
||||
let c = cdf[i] as u32;
|
||||
if tmp < c {
|
||||
cdf[i] = (c - ((c - tmp) >> rate)) as u16;
|
||||
} else {
|
||||
cdf[i] = (c + ((tmp - c) >> rate)) as u16;
|
||||
}
|
||||
}
|
||||
cdf[n] += (count < 32) as u16;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The spec's symbol decoder (8.2.2 - 8.2.4), for checking the encoder.
|
||||
struct Dec<'a> {
|
||||
data: &'a [u8],
|
||||
pos: usize,
|
||||
value: u32,
|
||||
range: u32,
|
||||
max_bits: i64,
|
||||
}
|
||||
|
||||
impl<'a> Dec<'a> {
|
||||
fn new(data: &'a [u8]) -> Self {
|
||||
let mut d = Dec { data, pos: 0, value: 0, range: 1 << 15, max_bits: 8 * data.len() as i64 - 15 };
|
||||
let nb = (data.len() * 8).min(15) as u32;
|
||||
let buf = d.bits(nb);
|
||||
d.value = ((1 << 15) - 1) ^ (buf << (15 - nb));
|
||||
d
|
||||
}
|
||||
fn bits(&mut self, n: u32) -> u32 {
|
||||
let mut x = 0;
|
||||
for _ in 0..n {
|
||||
let b = if self.pos < self.data.len() * 8 { (self.data[self.pos >> 3] >> (7 - (self.pos & 7))) & 1 } else { 0 };
|
||||
self.pos += 1;
|
||||
x = (x << 1) | b as u32;
|
||||
}
|
||||
x
|
||||
}
|
||||
fn symbol(&mut self, cdf: &mut [u16], adapt: bool) -> usize {
|
||||
let n = cdf.len() - 1;
|
||||
let mut cur = self.range;
|
||||
let mut s = 0;
|
||||
let mut prev;
|
||||
loop {
|
||||
prev = cur;
|
||||
let f = (1u32 << 15) - cdf[s] as u32;
|
||||
cur = (((self.range >> 8) * (f >> 6)) >> 1) + 4 * (n - s - 1) as u32;
|
||||
if self.value >= cur {
|
||||
break;
|
||||
}
|
||||
s += 1;
|
||||
}
|
||||
self.range = prev - cur;
|
||||
self.value -= cur;
|
||||
let b = 15 - (31 - self.range.leading_zeros());
|
||||
if b > 0 {
|
||||
self.range <<= b;
|
||||
let nb = (b as i64).min(self.max_bits.max(0)) as u32;
|
||||
let nd = self.bits(nb) << (b - nb);
|
||||
self.value = nd ^ (((self.value + 1) << b) - 1);
|
||||
self.max_bits -= b as i64;
|
||||
}
|
||||
if adapt {
|
||||
update_cdf(cdf, n, s);
|
||||
}
|
||||
s
|
||||
}
|
||||
/// Position of the trailing one bit (8.2.4).
|
||||
fn trailing_position(&self) -> i64 {
|
||||
let consumed = (self.pos as i64).min(self.data.len() as i64 * 8);
|
||||
consumed - (15i64).min(self.max_bits + 15)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bools_only() {
|
||||
for len in 1..200 {
|
||||
let bits: Vec<bool> = (0..len).map(|i| (i * 7 + len) % 3 == 0).collect();
|
||||
let mut w = SymbolWriter::new(false);
|
||||
for &b in &bits {
|
||||
w.bool(b);
|
||||
}
|
||||
let data = w.finish();
|
||||
let mut d = Dec::new(&data);
|
||||
for (i, &b) in bits.iter().enumerate() {
|
||||
assert_eq!(d.symbol(&mut [1 << 14, 1 << 15, 0], false) == 1, b, "len {len} bit {i} data {data:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trip_random_symbols() {
|
||||
let mut seed = 0x1234_5678u32;
|
||||
let mut rnd = || {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 17;
|
||||
seed ^= seed << 5;
|
||||
seed
|
||||
};
|
||||
for trial in 0..300 {
|
||||
let count = 1 + (rnd() % 3000) as usize;
|
||||
let mut cdfs_e = vec![[4096u16, 11264, 19328, 32768, 0]; 4];
|
||||
let mut cdfs_d = cdfs_e.clone();
|
||||
let mut syms = Vec::new();
|
||||
let mut w = SymbolWriter::new(false);
|
||||
for _ in 0..count {
|
||||
let kind = rnd() % 3;
|
||||
let ctx = (rnd() % 4) as usize;
|
||||
// skewed symbols so that the range also gets small
|
||||
let s = if rnd() % 4 == 0 { (rnd() % 4) as usize } else { 0 };
|
||||
match kind {
|
||||
0 => w.symbol(s, &mut cdfs_e[ctx]),
|
||||
1 => w.bool(s & 1 == 1),
|
||||
_ => w.literal(5, s as u32 * 7),
|
||||
}
|
||||
syms.push((kind, ctx, s));
|
||||
}
|
||||
let data = w.finish();
|
||||
let mut d = Dec::new(&data);
|
||||
for (idx, &(kind, ctx, s)) in syms.iter().enumerate() {
|
||||
let got = match kind {
|
||||
0 => d.symbol(&mut cdfs_d[ctx], true),
|
||||
1 => d.symbol(&mut [1 << 14, 1 << 15, 0], false),
|
||||
_ => {
|
||||
let mut v = 0;
|
||||
for _ in 0..5 {
|
||||
v = (v << 1) | d.symbol(&mut [1 << 14, 1 << 15, 0], false);
|
||||
}
|
||||
v / 7
|
||||
}
|
||||
};
|
||||
let want = if kind == 1 { s & 1 } else { s };
|
||||
assert_eq!(got, want, "trial {trial} idx {idx} kind {kind} of {}", syms.len());
|
||||
}
|
||||
let tp = d.trailing_position();
|
||||
assert!(tp >= 0 && (tp as usize) < data.len() * 8, "trial {trial}: trailing bit position {tp}");
|
||||
let tp = tp as usize;
|
||||
assert_eq!((data[tp >> 3] >> (7 - (tp & 7))) & 1, 1, "trial {trial}: trailing one bit");
|
||||
for p in tp + 1..data.len() * 8 {
|
||||
assert_eq!((data[p >> 3] >> (7 - (p & 7))) & 1, 0, "trial {trial}: padding");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,575 +0,0 @@
|
||||
//! Transforms. The inverse transforms are the normative ones of spec 7.13 (mirrored from
|
||||
//! filmcraft-av1, so that the encoder reconstructs exactly like a decoder); the forward
|
||||
//! transforms are floating-point matrices scaled to match them (not normative).
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::tables::{ADST_ADST, ADST_DCT, COS128_LOOKUP, DCT_ADST, DCT_DCT, TRANSFORM_ROW_SHIFT, TX_WIDTH_LOG2};
|
||||
|
||||
#[inline(always)]
|
||||
fn round2_64(x: i64, n: u32) -> i32 {
|
||||
if n == 0 { x as i32 } else { ((x + (1i64 << (n - 1))) >> n) as i32 }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) fn round2(x: i32, n: u32) -> i32 {
|
||||
if n == 0 { x } else { (x + (1 << (n - 1))) >> n }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn cos128(angle: i32) -> i64 {
|
||||
let a = (angle & 255) as usize;
|
||||
(match a {
|
||||
0..=64 => COS128_LOOKUP[a] as i32,
|
||||
65..=128 => -(COS128_LOOKUP[128 - a] as i32),
|
||||
129..=192 => -(COS128_LOOKUP[a - 128] as i32),
|
||||
_ => COS128_LOOKUP[256 - a] as i32,
|
||||
}) as i64
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn sin128(angle: i32) -> i64 {
|
||||
cos128(angle - 64)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn brev(num_bits: u32, x: usize) -> usize {
|
||||
let mut t = 0;
|
||||
for i in 0..num_bits {
|
||||
let bit = (x >> i) & 1;
|
||||
t += bit << (num_bits - 1 - i);
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn bf(t: &mut [i32], a: usize, b: usize, angle: i32, flip: bool) {
|
||||
let x = t[a] as i64 * cos128(angle) - t[b] as i64 * sin128(angle);
|
||||
let y = t[a] as i64 * sin128(angle) + t[b] as i64 * cos128(angle);
|
||||
t[a] = round2_64(x, 12);
|
||||
t[b] = round2_64(y, 12);
|
||||
if flip {
|
||||
t.swap(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn hd(t: &mut [i32], a: usize, b: usize, flip: bool, r: u32) {
|
||||
let (a, b) = if flip { (b, a) } else { (a, b) };
|
||||
let x = t[a];
|
||||
let y = t[b];
|
||||
let lo = -(1i32 << (r - 1));
|
||||
let hi = (1i32 << (r - 1)) - 1;
|
||||
t[a] = (x + y).clamp(lo, hi);
|
||||
t[b] = (x - y).clamp(lo, hi);
|
||||
}
|
||||
|
||||
/// Inverse DCT of length 2^N, N = 2..=5 (7.13.2.3), in place. `N` is a const generic so that the
|
||||
/// bit-reversal permutations and loop bounds fold at compile time.
|
||||
#[inline(always)]
|
||||
fn inverse_dct_n<const N: u32>(t: &mut [i32], r: u32) {
|
||||
let n = N;
|
||||
let len = 1usize << n;
|
||||
let mut copy = [0i32; 32];
|
||||
copy[..len].copy_from_slice(&t[..len]);
|
||||
for i in 0..len {
|
||||
t[i] = copy[brev(n, i)];
|
||||
}
|
||||
inverse_dct_initial_stages(t, n, r);
|
||||
inverse_dct_final_stages(t, n, r);
|
||||
}
|
||||
|
||||
/// Rotate the high-frequency branches before the four-point core.
|
||||
#[inline(always)]
|
||||
fn inverse_dct_initial_stages(t: &mut [i32], n: u32, r: u32) {
|
||||
if n >= 5 {
|
||||
for i in 0..8 {
|
||||
bf(t, 16 + i, 31 - i, 6 + ((brev(3, 7 - i) as i32) << 3), false);
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..4 {
|
||||
bf(t, 8 + i, 15 - i, 12 + ((brev(2, 3 - i) as i32) << 4), false);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..8 {
|
||||
hd(t, 16 + 2 * i, 17 + 2 * i, i & 1 == 1, r);
|
||||
}
|
||||
}
|
||||
if n >= 3 {
|
||||
for i in 0..2 {
|
||||
bf(t, 4 + i, 7 - i, 56 - 32 * i as i32, false);
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..4 {
|
||||
hd(t, 8 + 2 * i, 9 + 2 * i, i & 1 == 1, r);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..2 {
|
||||
for j in 0..2 {
|
||||
bf(t, 30 - 4 * i - j, 17 + 4 * i + j, 24 + ((j as i32) << 6) + (((1 - i) as i32) << 5), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluate the four-point core and merge progressively larger branches.
|
||||
#[inline(always)]
|
||||
fn inverse_dct_final_stages(t: &mut [i32], n: u32, r: u32) {
|
||||
for i in 0..2 {
|
||||
bf(t, 2 * i, 2 * i + 1, 32 + 16 * i as i32, i == 0);
|
||||
}
|
||||
if n >= 3 {
|
||||
for i in 0..2 {
|
||||
hd(t, 4 + 2 * i, 5 + 2 * i, i == 1, r);
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..2 {
|
||||
bf(t, 14 - i, 9 + i, 48 + 64 * i as i32, true);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..4 {
|
||||
for j in 0..2 {
|
||||
hd(t, 16 + 4 * i + j, 19 + 4 * i - j, i & 1 == 1, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 0..2 {
|
||||
hd(t, i, 3 - i, false, r);
|
||||
}
|
||||
if n >= 3 {
|
||||
bf(t, 6, 5, 32, true);
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..2 {
|
||||
for j in 0..2 {
|
||||
hd(t, 8 + 4 * i + j, 11 + 4 * i - j, i == 1, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..4 {
|
||||
bf(t, 29 - i, 18 + i, 48 + (i as i32 >> 1) * 64, true);
|
||||
}
|
||||
}
|
||||
if n >= 3 {
|
||||
for i in 0..4 {
|
||||
hd(t, i, 7 - i, false, r);
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..2 {
|
||||
bf(t, 13 - i, 10 + i, 32, true);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..2 {
|
||||
for j in 0..4 {
|
||||
hd(t, 16 + i * 8 + j, 23 + i * 8 - j, i == 1, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
if n >= 4 {
|
||||
for i in 0..8 {
|
||||
hd(t, i, 15 - i, false, r);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..4 {
|
||||
bf(t, 27 - i, 20 + i, 32, true);
|
||||
}
|
||||
}
|
||||
if n >= 5 {
|
||||
for i in 0..16 {
|
||||
hd(t, i, 31 - i, false, r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const SINPI_1_9: i64 = 1321;
|
||||
const SINPI_2_9: i64 = 2482;
|
||||
const SINPI_3_9: i64 = 3344;
|
||||
const SINPI_4_9: i64 = 3803;
|
||||
|
||||
#[inline(always)]
|
||||
fn inverse_adst4(t: &mut [i32]) {
|
||||
let (t0, t1, t2, t3) = (t[0] as i64, t[1] as i64, t[2] as i64, t[3] as i64);
|
||||
let mut s = [SINPI_1_9 * t0, SINPI_2_9 * t0, SINPI_3_9 * t1, SINPI_4_9 * t2, SINPI_1_9 * t2, SINPI_2_9 * t3, SINPI_4_9 * t3];
|
||||
let a7 = t0 - t2;
|
||||
let b7 = a7 + t3;
|
||||
s[0] += s[3];
|
||||
s[1] -= s[4];
|
||||
s[3] = s[2];
|
||||
s[2] = SINPI_3_9 * b7;
|
||||
s[0] += s[5];
|
||||
s[1] -= s[6];
|
||||
let x = [s[0] + s[3], s[1] + s[3], s[2], s[0] + s[1] - s[3]];
|
||||
for i in 0..4 {
|
||||
t[i] = round2_64(x[i], 12);
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn adst_in_permute<const N: u32>(t: &mut [i32]) {
|
||||
let n = N;
|
||||
let n0 = 1usize << n;
|
||||
let mut copy = [0i32; 16];
|
||||
copy[..n0].copy_from_slice(&t[..n0]);
|
||||
for i in 0..n0 {
|
||||
let idx = if i & 1 == 1 { i - 1 } else { n0 - i - 1 };
|
||||
t[i] = copy[idx];
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn adst_out_permute<const N: u32>(t: &mut [i32]) {
|
||||
let n = N;
|
||||
let n0 = 1usize << n;
|
||||
let mut copy = [0i32; 16];
|
||||
copy[..n0].copy_from_slice(&t[..n0]);
|
||||
for i in 0..n0 {
|
||||
let a = (i >> 3) & 1;
|
||||
let b = ((i >> 2) & 1) ^ ((i >> 3) & 1);
|
||||
let c = ((i >> 1) & 1) ^ ((i >> 2) & 1);
|
||||
let d = (i & 1) ^ ((i >> 1) & 1);
|
||||
let idx = ((d << 3) | (c << 2) | (b << 1) | a) >> (4 - n);
|
||||
t[i] = if i & 1 == 1 { -copy[idx] } else { copy[idx] };
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn inverse_adst8(t: &mut [i32], r: u32) {
|
||||
adst_in_permute::<3>(t);
|
||||
for i in 0..4 {
|
||||
bf(t, 2 * i, 2 * i + 1, 60 - 16 * i as i32, true);
|
||||
}
|
||||
for i in 0..4 {
|
||||
hd(t, i, 4 + i, false, r);
|
||||
}
|
||||
for i in 0..2 {
|
||||
bf(t, 4 + 3 * i, 5 + i, 48 - 32 * i as i32, true);
|
||||
}
|
||||
for i in 0..2 {
|
||||
for j in 0..2 {
|
||||
hd(t, 4 * j + i, 2 + 4 * j + i, false, r);
|
||||
}
|
||||
}
|
||||
for i in 0..2 {
|
||||
bf(t, 2 + 4 * i, 3 + 4 * i, 32, true);
|
||||
}
|
||||
adst_out_permute::<3>(t);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn inverse_adst16(t: &mut [i32], r: u32) {
|
||||
adst_in_permute::<4>(t);
|
||||
for i in 0..8 {
|
||||
bf(t, 2 * i, 2 * i + 1, 62 - 8 * i as i32, true);
|
||||
}
|
||||
for i in 0..8 {
|
||||
hd(t, i, 8 + i, false, r);
|
||||
}
|
||||
for i in 0..2 {
|
||||
bf(t, 8 + 2 * i, 9 + 2 * i, 56 - 32 * i as i32, true);
|
||||
bf(t, 13 + 2 * i, 12 + 2 * i, 8 + 32 * i as i32, true);
|
||||
}
|
||||
for i in 0..4 {
|
||||
for j in 0..2 {
|
||||
hd(t, 8 * j + i, 4 + 8 * j + i, false, r);
|
||||
}
|
||||
}
|
||||
for i in 0..2 {
|
||||
for j in 0..2 {
|
||||
bf(t, 4 + 8 * j + 3 * i, 5 + 8 * j + i, 48 - 32 * i as i32, true);
|
||||
}
|
||||
}
|
||||
for i in 0..2 {
|
||||
for j in 0..4 {
|
||||
hd(t, 4 * j + i, 2 + 4 * j + i, false, r);
|
||||
}
|
||||
}
|
||||
for i in 0..4 {
|
||||
bf(t, 2 + 4 * i, 3 + 4 * i, 32, true);
|
||||
}
|
||||
adst_out_permute::<4>(t);
|
||||
}
|
||||
|
||||
/// 1-D transform kind.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub(crate) enum Kind {
|
||||
Dct,
|
||||
Adst,
|
||||
}
|
||||
|
||||
/// (vertical / column kind, horizontal / row kind) of a transform type.
|
||||
pub(crate) fn kinds(tx_type: usize) -> (Kind, Kind) {
|
||||
match tx_type {
|
||||
DCT_DCT => (Kind::Dct, Kind::Dct),
|
||||
ADST_DCT => (Kind::Adst, Kind::Dct),
|
||||
DCT_ADST => (Kind::Dct, Kind::Adst),
|
||||
ADST_ADST => (Kind::Adst, Kind::Adst),
|
||||
// The encoder only chooses the four types above.
|
||||
_ => {
|
||||
debug_assert!(false, "unsupported transform type {tx_type}");
|
||||
(Kind::Dct, Kind::Dct)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn inverse_dct(t: &mut [i32], n: u32, r: u32) {
|
||||
match n {
|
||||
2 => inverse_dct_n::<2>(t, r),
|
||||
3 => inverse_dct_n::<3>(t, r),
|
||||
4 => inverse_dct_n::<4>(t, r),
|
||||
_ => inverse_dct_n::<5>(t, r),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn inverse_1d(t: &mut [i32], kind: Kind, n: u32, r: u32) {
|
||||
match kind {
|
||||
Kind::Dct => inverse_dct(t, n, r),
|
||||
Kind::Adst => match n {
|
||||
2 => inverse_adst4(t),
|
||||
3 => inverse_adst8(t, r),
|
||||
_ => inverse_adst16(t, r),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// 2-D inverse transform (7.13.3) of a square `n x n` block (n = 4..=32) of dequantised
|
||||
/// coefficients (row stride `n`) into `residual` (row stride `n`).
|
||||
pub(crate) fn inverse_2d(dequant: &[i32], residual: &mut [i32], tx_sz: usize, tx_type: usize, bit_depth: u32) {
|
||||
match TX_WIDTH_LOG2[tx_sz] {
|
||||
2 => inverse_2d_n::<2>(dequant, residual, tx_sz, tx_type, bit_depth),
|
||||
3 => inverse_2d_n::<3>(dequant, residual, tx_sz, tx_type, bit_depth),
|
||||
4 => inverse_2d_n::<4>(dequant, residual, tx_sz, tx_type, bit_depth),
|
||||
_ => inverse_2d_n::<5>(dequant, residual, tx_sz, tx_type, bit_depth),
|
||||
}
|
||||
}
|
||||
|
||||
/// `inverse_2d` for a block of width `1 << LOG2`.
|
||||
fn inverse_2d_n<const LOG2: u32>(dequant: &[i32], residual: &mut [i32], tx_sz: usize, tx_type: usize, bit_depth: u32) {
|
||||
let log2 = LOG2;
|
||||
let n = 1usize << log2;
|
||||
let row_shift = TRANSFORM_ROW_SHIFT[tx_sz] as u32;
|
||||
let row_clamp = bit_depth + 8;
|
||||
let col_clamp = (bit_depth + 6).max(16);
|
||||
let (ck, rk) = kinds(tx_type);
|
||||
let lo = -(1i32 << (col_clamp - 1));
|
||||
let hi = (1i32 << (col_clamp - 1)) - 1;
|
||||
let mut t = [0i32; 32];
|
||||
for i in 0..n {
|
||||
let row = &dequant[i * n..i * n + n];
|
||||
if row.iter().all(|&v| v == 0) {
|
||||
residual[i * n..i * n + n].iter_mut().for_each(|v| *v = 0);
|
||||
continue;
|
||||
}
|
||||
t[..n].copy_from_slice(row);
|
||||
inverse_1d(&mut t, rk, log2, row_clamp);
|
||||
for j in 0..n {
|
||||
residual[i * n + j] = round2(t[j], row_shift).clamp(lo, hi);
|
||||
}
|
||||
}
|
||||
for j in 0..n {
|
||||
for i in 0..n {
|
||||
t[i] = residual[i * n + j];
|
||||
}
|
||||
inverse_1d(&mut t, ck, log2, col_clamp);
|
||||
for i in 0..n {
|
||||
residual[i * n + j] = round2(t[i], 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Orthonormal forward basis matrices `[k][i]` (row k = basis function k), sizes 4..=32.
|
||||
struct Bases {
|
||||
dct: [Vec<f32>; 4],
|
||||
adst: [Vec<f32>; 3],
|
||||
/// The same matrices transposed (`[i][k]`).
|
||||
dct_t: [Vec<f32>; 4],
|
||||
adst_t: [Vec<f32>; 3],
|
||||
}
|
||||
|
||||
fn transpose(m: &[f32]) -> Vec<f32> {
|
||||
let n = (m.len() as f64).sqrt() as usize;
|
||||
let mut t = vec![0f32; n * n];
|
||||
for k in 0..n {
|
||||
for i in 0..n {
|
||||
t[i * n + k] = m[k * n + i];
|
||||
}
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
fn bases() -> &'static Bases {
|
||||
static B: OnceLock<Bases> = OnceLock::new();
|
||||
B.get_or_init(|| {
|
||||
let pi = std::f64::consts::PI;
|
||||
let dct = std::array::from_fn(|l| {
|
||||
let n = 4usize << l;
|
||||
let mut m = vec![0f32; n * n];
|
||||
for k in 0..n {
|
||||
let c = if k == 0 { std::f64::consts::FRAC_1_SQRT_2 } else { 1.0 };
|
||||
for i in 0..n {
|
||||
m[k * n + i] = (c * (2.0 / n as f64).sqrt() * (pi * (2 * i + 1) as f64 * k as f64 / (2 * n) as f64).cos()) as f32;
|
||||
}
|
||||
}
|
||||
m
|
||||
});
|
||||
let adst = std::array::from_fn(|l| {
|
||||
let n = 4usize << l;
|
||||
let mut m = vec![0f32; n * n];
|
||||
for k in 0..n {
|
||||
for i in 0..n {
|
||||
m[k * n + i] = if n == 4 {
|
||||
// the AV1 4-point ADST (sin(pi (i + 1)(2k + 1) / 9) basis)
|
||||
(2.0 / 3.0 * (pi * ((i + 1) * (2 * k + 1)) as f64 / 9.0).sin()) as f32
|
||||
} else {
|
||||
((2.0 / n as f64).sqrt() * (pi * ((2 * i + 1) * (2 * k + 1)) as f64 / (4 * n) as f64).sin()) as f32
|
||||
};
|
||||
}
|
||||
}
|
||||
m
|
||||
});
|
||||
let dct_t = std::array::from_fn(|l| transpose(&dct[l]));
|
||||
let adst_t = std::array::from_fn(|l| transpose(&adst[l]));
|
||||
Bases { dct, adst, dct_t, adst_t }
|
||||
})
|
||||
}
|
||||
|
||||
fn basis_t(kind: Kind, n: usize) -> &'static [f32] {
|
||||
let l = n.trailing_zeros() as usize - 2;
|
||||
match kind {
|
||||
Kind::Dct => &bases().dct_t[l],
|
||||
Kind::Adst => &bases().adst_t[l],
|
||||
}
|
||||
}
|
||||
|
||||
fn basis(kind: Kind, n: usize) -> &'static [f32] {
|
||||
let l = n.trailing_zeros() as usize - 2;
|
||||
match kind {
|
||||
Kind::Dct => &bases().dct[l],
|
||||
Kind::Adst => &bases().adst[l],
|
||||
}
|
||||
}
|
||||
|
||||
/// Forward 2-D transform of the `n x n` residual (row stride `n`) into orthonormal-domain
|
||||
/// coefficients scaled by 8 (so that `level = out / q` with the spec's dequantiser).
|
||||
pub(crate) fn forward_2d(residual: &[i32], out: &mut [f32], n: usize, tx_type: usize) {
|
||||
match n {
|
||||
4 => forward_2d_n::<4, 16>(residual, out, tx_type),
|
||||
8 => forward_2d_n::<8, 64>(residual, out, tx_type),
|
||||
16 => forward_2d_n::<16, 256>(residual, out, tx_type),
|
||||
_ => forward_2d_dyn(residual, out, n, tx_type),
|
||||
}
|
||||
}
|
||||
|
||||
/// `forward_2d` for a fixed size, so `tmp` is exactly `NN = N * N` floats (no 4 KiB zeroing).
|
||||
#[inline(always)]
|
||||
fn forward_2d_n<const N: usize, const NN: usize>(residual: &[i32], out: &mut [f32], tx_type: usize) {
|
||||
let n = N;
|
||||
let (ck, rk) = kinds(tx_type);
|
||||
let bc = &basis(ck, n)[..NN];
|
||||
let brt = &basis_t(rk, n)[..NN];
|
||||
let residual = &residual[..NN];
|
||||
let out = &mut out[..NN];
|
||||
let mut tmp = [0f32; NN];
|
||||
for i in 0..n {
|
||||
let t = &mut tmp[i * n..i * n + n];
|
||||
for j in 0..n {
|
||||
let r = residual[i * n + j];
|
||||
if r == 0 {
|
||||
continue;
|
||||
}
|
||||
let r = r as f32;
|
||||
for (o, &b) in t.iter_mut().zip(&brt[j * n..j * n + n]) {
|
||||
*o += r * b;
|
||||
}
|
||||
}
|
||||
}
|
||||
out.iter_mut().for_each(|v| *v = 0.0);
|
||||
for k in 0..n {
|
||||
let o = &mut out[k * n..k * n + n];
|
||||
for i in 0..n {
|
||||
let b = 8.0 * bc[k * n + i];
|
||||
for (v, &t) in o.iter_mut().zip(&tmp[i * n..i * n + n]) {
|
||||
*v += b * t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 32x32 (and any other size): a const-generic 32 version measured slower.
|
||||
fn forward_2d_dyn(residual: &[i32], out: &mut [f32], n: usize, tx_type: usize) {
|
||||
let (ck, rk) = kinds(tx_type);
|
||||
let bc = basis(ck, n);
|
||||
let brt = basis_t(rk, n);
|
||||
// tmp[i][k] = sum_j residual[i][j] * br[k][j] (horizontal; written as row updates so that
|
||||
// the inner loops vectorise)
|
||||
let mut tmp = [0f32; 32 * 32];
|
||||
for i in 0..n {
|
||||
let t = &mut tmp[i * n..i * n + n];
|
||||
for j in 0..n {
|
||||
let r = residual[i * n + j];
|
||||
if r == 0 {
|
||||
continue;
|
||||
}
|
||||
let r = r as f32;
|
||||
for (o, &b) in t.iter_mut().zip(&brt[j * n..j * n + n]) {
|
||||
*o += r * b;
|
||||
}
|
||||
}
|
||||
}
|
||||
// out[k][j] = 8 * sum_i bc[k][i] * tmp[i][j] (vertical)
|
||||
out[..n * n].iter_mut().for_each(|v| *v = 0.0);
|
||||
for k in 0..n {
|
||||
let o = &mut out[k * n..k * n + n];
|
||||
for i in 0..n {
|
||||
let b = 8.0 * bc[k * n + i];
|
||||
for (v, &t) in o.iter_mut().zip(&tmp[i * n..i * n + n]) {
|
||||
*v += b * t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::tables::{TX_4X4, TX_8X8, TX_16X16, TX_32X32};
|
||||
|
||||
/// forward (float) then the normative inverse reproduces the residual: the forward basis
|
||||
/// and scale match the decoder's inverse transforms for every supported size and type.
|
||||
#[test]
|
||||
fn forward_matches_inverse() {
|
||||
let mut seed = 99u32;
|
||||
let mut rnd = || {
|
||||
seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12345);
|
||||
((seed >> 16) % 201) as i32 - 100
|
||||
};
|
||||
for (tx, n) in [(TX_4X4, 4), (TX_8X8, 8), (TX_16X16, 16), (TX_32X32, 32)] {
|
||||
let denom = if tx == TX_32X32 { 2.0 } else { 1.0 };
|
||||
for tx_type in [DCT_DCT, ADST_DCT, DCT_ADST, ADST_ADST] {
|
||||
if n == 32 && tx_type != DCT_DCT {
|
||||
continue;
|
||||
}
|
||||
let res: Vec<i32> = (0..n * n).map(|_| rnd()).collect();
|
||||
let mut coef = vec![0f32; n * n];
|
||||
forward_2d(&res, &mut coef, n, tx_type);
|
||||
// q = 8 / denom keeps full precision: dequant = level * q / denom = coef
|
||||
let deq: Vec<i32> = coef.iter().map(|&c| (c / denom).round() as i32).collect();
|
||||
let mut out = vec![0i32; n * n];
|
||||
inverse_2d(&deq, &mut out, tx, tx_type, 8);
|
||||
let err: i32 = res.iter().zip(&out).map(|(a, b)| (a - b).abs()).max().unwrap();
|
||||
assert!(err <= 1, "tx {tx} type {tx_type}: max error {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +0,0 @@
|
||||
//! The crate's integration tests, one binary (each module is a former `tests/<name>.rs`).
|
||||
|
||||
mod bench;
|
||||
mod common;
|
||||
mod ffmpeg_oracle;
|
||||
mod roundtrip;
|
||||
+50
-8
@@ -325,6 +325,9 @@ pub struct Clut {
|
||||
pub data: Vec<f32>,
|
||||
}
|
||||
|
||||
/// Most interpolating inputs [`Clut::eval`] combines (ICC allows 15).
|
||||
pub const MAX_CLUT_INPUTS: usize = 16;
|
||||
|
||||
impl Clut {
|
||||
/// Multilinear interpolation at `x` (0..1 per input).
|
||||
pub fn eval(&self, x: &[f64]) -> Vec<f64> {
|
||||
@@ -334,26 +337,40 @@ impl Clut {
|
||||
let mut s = self.outputs;
|
||||
for i in (0..n).rev() {
|
||||
stride[i] = s;
|
||||
s *= self.grid[i].max(1);
|
||||
// Saturating: a table with enough axes to overflow cannot hold its data anyway, and a
|
||||
// saturated offset reads nothing (zero) instead of panicking.
|
||||
s = s.saturating_mul(self.grid[i].max(1));
|
||||
}
|
||||
let mut frac = vec![0.0; n];
|
||||
let mut step = vec![0usize; n];
|
||||
for i in 0..n {
|
||||
let g = self.grid[i].max(1);
|
||||
let f = x.get(i).copied().unwrap_or(0.0).clamp(0.0, 1.0) * (g - 1) as f64;
|
||||
let k = (f.floor() as usize).min(g.saturating_sub(2));
|
||||
let mut k = (f.floor() as usize).min(g.saturating_sub(2));
|
||||
frac[i] = if g > 1 { f - k as f64 } else { 0.0 };
|
||||
step[i] = if g > 1 { stride[i] } else { 0 };
|
||||
base += k * stride[i];
|
||||
// The last grid point is a corner of its own, not an interpolation toward the next.
|
||||
if frac[i] >= 1.0 {
|
||||
k += 1;
|
||||
frac[i] = 0.0;
|
||||
}
|
||||
base = base.saturating_add(k.saturating_mul(stride[i]));
|
||||
}
|
||||
let mut out = vec![0.0; self.outputs];
|
||||
for corner in 0..(1usize << n) {
|
||||
// Only the axes that interpolate have two corners: a singleton axis, or one sampled on a
|
||||
// grid point, contributes its lower corner alone. ICC tables have at most 15 inputs; a
|
||||
// table built with more interpolating axes than that evaluates to zero.
|
||||
let active: Vec<usize> = (0..n).filter(|&i| step[i] != 0 && frac[i] != 0.0).collect();
|
||||
if active.len() > MAX_CLUT_INPUTS {
|
||||
return out;
|
||||
}
|
||||
for corner in 0..(1usize << active.len()) {
|
||||
let mut w = 1.0;
|
||||
let mut off = base;
|
||||
for i in 0..n {
|
||||
if corner >> i & 1 == 1 {
|
||||
for (bit, &i) in active.iter().enumerate() {
|
||||
if corner >> bit & 1 == 1 {
|
||||
w *= frac[i];
|
||||
off += step[i];
|
||||
off = off.saturating_add(step[i]);
|
||||
} else {
|
||||
w *= 1.0 - frac[i];
|
||||
}
|
||||
@@ -362,7 +379,7 @@ impl Clut {
|
||||
continue;
|
||||
}
|
||||
for (o, v) in out.iter_mut().enumerate() {
|
||||
*v += w * self.data.get(off + o).copied().unwrap_or(0.0) as f64;
|
||||
*v += w * self.data.get(off.saturating_add(o)).copied().unwrap_or(0.0) as f64;
|
||||
}
|
||||
}
|
||||
out
|
||||
@@ -1052,6 +1069,31 @@ mod tests {
|
||||
assert!(matches!(parse_profile(&write_matrix_profile("m", P709, D65, 2.2)).unwrap(), Profile::Matrix(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clut_corners_count_only_interpolating_axes() {
|
||||
// 64 singleton axes and one interpolating one: two corners, not 2^65.
|
||||
let mut grid = vec![1; 64];
|
||||
grid.push(2);
|
||||
let c = Clut { grid, outputs: 1, data: vec![0.0, 1.0] };
|
||||
let mut x = vec![0.5; 64];
|
||||
x.push(0.25);
|
||||
assert!((c.eval(&x)[0] - 0.25).abs() < 1e-9);
|
||||
// On grid points nothing interpolates.
|
||||
let c = Clut { grid: vec![2; 40], outputs: 1, data: vec![] };
|
||||
assert_eq!(c.eval(&[1.0; 40]), vec![0.0]);
|
||||
// More interpolating axes than ICC allows: zero, at once.
|
||||
assert_eq!(c.eval(&[0.5; 40]), vec![0.0]);
|
||||
// The last grid point of every axis is that table entry, however many axes there are.
|
||||
let mut data = vec![0.0; 1 << 17];
|
||||
data[(1 << 17) - 1] = 0.75;
|
||||
let c = Clut { grid: vec![2; 17], outputs: 1, data };
|
||||
assert!((c.eval(&[1.0; 17])[0] - 0.75).abs() < 1e-9);
|
||||
// Strides that overflow usize (64 two-point axes) read nothing instead of panicking.
|
||||
let c = Clut { grid: vec![2; 64], outputs: 1, data: vec![] };
|
||||
assert_eq!(c.eval(&[1.0; 64]), vec![0.0]);
|
||||
assert_eq!(c.eval(&[0.5; 64]), vec![0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clut_and_lut3d_interpolate() {
|
||||
// A 2-point identity CLUT on 3 inputs is exact for every point.
|
||||
|
||||
@@ -228,8 +228,21 @@ fn compound_blur(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Camera Lens Blur
|
||||
|
||||
/// Half-open horizontal spans (dy, x_left, x_right) of a polygonal / round iris of radius `r`.
|
||||
/// Largest Camera Lens Blur radius in buffer pixels. Blur Radius goes to 500 in the UI; a project
|
||||
/// or expression value far beyond it would otherwise build an iris table of one row per pixel of
|
||||
/// radius (and sum that many rows per output pixel) before any image size check.
|
||||
pub const CAMERA_LENS_MAX_RADIUS: f64 = 2000.0;
|
||||
|
||||
/// Camera Lens Blur's iris radius in buffer pixels: Blur Radius at the buffer's `scale`, capped
|
||||
/// at [`CAMERA_LENS_MAX_RADIUS`]. Shared by the CPU and GPU paths.
|
||||
pub fn camera_lens_radius(ctx: &EffectCtx, scale: f64) -> f64 {
|
||||
(ctx.params.f("blurRadius").max(0.0) * scale).clamp(0.0, CAMERA_LENS_MAX_RADIUS)
|
||||
}
|
||||
|
||||
/// Half-open horizontal spans (dy, x_left, x_right) of a polygonal / round iris of radius `r`
|
||||
/// (capped at [`CAMERA_LENS_MAX_RADIUS`]).
|
||||
fn iris_spans(r: f64, sides: u32, rotation_deg: f64, roundness: f64) -> Vec<(i64, i64, i64)> {
|
||||
let r = if r.is_nan() { 0.0 } else { r.clamp(0.0, CAMERA_LENS_MAX_RADIUS) };
|
||||
let ri = r.ceil() as i64;
|
||||
let rot = rotation_deg.to_radians();
|
||||
let verts: Vec<(f64, f64)> = (0..sides)
|
||||
@@ -292,7 +305,8 @@ pub fn camera_lens_spans(ctx: &EffectCtx, r: f64) -> (Vec<(i64, i64, i64)>, f64)
|
||||
s.2 = (s.2 as f64 * aspect).round() as i64;
|
||||
}
|
||||
}
|
||||
(spans, r * aspect.max(1.0))
|
||||
// Same cap as iris_spans, so the reach matches the spans for unbounded radii.
|
||||
(spans, r.clamp(0.0, CAMERA_LENS_MAX_RADIUS) * aspect.max(1.0))
|
||||
}
|
||||
|
||||
/// Whether Camera Lens Blur needs more than plain iris spans (Diffraction Fringe or a Blur
|
||||
@@ -302,7 +316,7 @@ pub fn camera_lens_plain(ctx: &EffectCtx) -> bool {
|
||||
}
|
||||
|
||||
fn camera_lens_blur(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let r = ctx.params.f("blurRadius").max(0.0) * b.scale;
|
||||
let r = camera_lens_radius(ctx, b.scale);
|
||||
if r < 0.5 {
|
||||
return b;
|
||||
}
|
||||
@@ -910,6 +924,19 @@ mod tests {
|
||||
assert!(out.img.get(15 + o, 10 + d)[0] < 0.9, "bright map blurs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_spans_cap_a_huge_radius() {
|
||||
// A project's Blur Radius far beyond the UI's 500 builds a bounded table.
|
||||
let max_rows = 2 * CAMERA_LENS_MAX_RADIUS as usize + 1;
|
||||
assert!(iris_spans(1e12, 6, 0.0, 0.0).len() <= max_rows);
|
||||
assert!(iris_spans(f64::INFINITY, 0, 0.0, 0.0).len() <= max_rows);
|
||||
assert_eq!(iris_spans(f64::NAN, 0, 0.0, 0.0), vec![(0, 0, 0)]);
|
||||
let params = Params { values: [("blurRadius".into(), num(1e15))].into_iter().collect() };
|
||||
let ctx = EffectCtx { params: ¶ms, time: 0.0, layer_size: [4.0, 4.0], seed: 1, adjustment: false, env: Default::default() };
|
||||
assert_eq!(camera_lens_radius(&ctx, 1.0), CAMERA_LENS_MAX_RADIUS);
|
||||
assert!(camera_lens_spans(&ctx, camera_lens_radius(&ctx, 1.0)).0.len() <= max_rows);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_spans_hexagon_is_symmetric() {
|
||||
let s = iris_spans(10.0, 6, 0.0, 0.0);
|
||||
|
||||
@@ -252,10 +252,18 @@ impl Lv {
|
||||
if clip_w {
|
||||
t = t.min(1.0);
|
||||
}
|
||||
self.ob + (self.ow - self.ob) * crate::color_fx::levels_gamma(t, self.g.max(0.01))
|
||||
let curve = crate::color_fx::levels_gamma(t, self.g.max(0.01)).clamp(-LEVELS_IC_CURVE_LIMIT, LEVELS_IC_CURVE_LIMIT);
|
||||
self.ob + (self.ow - self.ob) * curve
|
||||
}
|
||||
}
|
||||
|
||||
/// How far Levels (Individual Controls)' curve may reach: with gamma down to 0 (rendered as 0.01,
|
||||
/// an exponent of 100) an HDR value a few times Input White overflows `f32`, and an infinite curve
|
||||
/// times an empty output range is NaN. 1e30 times the widest output range (2e4) is still finite
|
||||
/// (`f32::MAX` is 3.4e38), and every curve that was finite before stays as it was below it. The
|
||||
/// GPU (`fxt_lv`) uses the same.
|
||||
const LEVELS_IC_CURVE_LIMIT: f32 = 1e30;
|
||||
|
||||
fn levels_ic(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let lv = LV_IDS.map(|ids| Lv {
|
||||
ib: ctx.params.f(ids[0]) as f32,
|
||||
@@ -978,11 +986,14 @@ fn cc_kernel(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
pub fn specs() -> Vec<EffectSpec> {
|
||||
let mut lv_params = Vec::new();
|
||||
for (ids, names) in LV_IDS.iter().zip(LV_NAMES.iter()) {
|
||||
lv_params.push(p(ids[0], names[0], num(0.0), slider(-1.0, 2.0, 0.0, 1.0, 3)));
|
||||
lv_params.push(p(ids[1], names[1], num(1.0), slider(-1.0, 2.0, 0.0, 1.0, 3)));
|
||||
lv_params.push(p(ids[2], names[2], num(1.0), slider(0.1, 10.0, 0.1, 3.0, 2)));
|
||||
lv_params.push(p(ids[3], names[3], num(0.0), slider(-1.0, 2.0, 0.0, 1.0, 3)));
|
||||
lv_params.push(p(ids[4], names[4], num(1.0), slider(-1.0, 2.0, 0.0, 1.0, 3)));
|
||||
// After Effects' valid ranges (#3487-#3491): levels -10000..10000 and gamma from 0 (the
|
||||
// renderer floors it at 0.01); gamma keeps its wider maximum. The sliders stay 0..1.
|
||||
let level = || slider(-10000.0, 10000.0, 0.0, 1.0, 3);
|
||||
lv_params.push(p(ids[0], names[0], num(0.0), level()));
|
||||
lv_params.push(p(ids[1], names[1], num(1.0), level()));
|
||||
lv_params.push(p(ids[2], names[2], num(1.0), slider(0.0, 10.0, 0.1, 3.0, 2)));
|
||||
lv_params.push(p(ids[3], names[3], num(0.0), level()));
|
||||
lv_params.push(p(ids[4], names[4], num(1.0), level()));
|
||||
}
|
||||
lv_params.push(p("clipToOutputBlack", "Clip To Output Black", Value::Enum(2), popup(&crate::color_fx::LEVELS_CLIP)));
|
||||
lv_params.push(p("clipToOutputWhite", "Clip To Output White", Value::Enum(2), popup(&crate::color_fx::LEVELS_CLIP)));
|
||||
|
||||
@@ -270,6 +270,39 @@ fn levels_ic_clip_popups() {
|
||||
assert!(run("ec.color.levelsic", &img, &off).data[0][0] > 1.5);
|
||||
}
|
||||
|
||||
/// #3487-#3491: over After Effects' valid ranges (levels ±10000, gamma from 0) the output stays
|
||||
/// finite: gamma 0 renders as 0.01, whose curve on an HDR value overflows `f32` unbounded.
|
||||
#[test]
|
||||
fn levels_ic_stays_finite_over_the_whole_range() {
|
||||
let img = Image::filled(2, 2, [3.0, 0.5, -2.0, 1.0]);
|
||||
for (ib, iw, ob, ow) in [
|
||||
(0.0, 1.0, 0.0, 1.0),
|
||||
(0.0, 1.0, 1.0, 1.0),
|
||||
(-10000.0, 10000.0, -10000.0, 10000.0),
|
||||
(10000.0, -10000.0, 10000.0, -10000.0),
|
||||
(0.0, 1e-3, -10000.0, 10000.0),
|
||||
] {
|
||||
let lv = [
|
||||
("rgb/rgbInBlack", num(ib)),
|
||||
("rgb/rgbInWhite", num(iw)),
|
||||
("rgb/rgbGamma", num(0.0)),
|
||||
("rgb/rgbOutBlack", num(ob)),
|
||||
("rgb/rgbOutWhite", num(ow)),
|
||||
("clipToOutputBlack", Value::Enum(0)),
|
||||
("clipToOutputWhite", Value::Enum(0)),
|
||||
];
|
||||
let out = run("ec.color.levelsic", &img, &lv);
|
||||
assert!(out.data.iter().flatten().all(|v| v.is_finite()), "{ib} {iw} {ob} {ow}: {:?}", out.data[0]);
|
||||
}
|
||||
// The bound only stops the overflow: an HDR value that was finite before keeps its level
|
||||
// (gamma 0.1 at 4 x Input White: 4^10).
|
||||
let bright = run("ec.color.levelsic", &Image::filled(1, 1, [4.0, 4.0, 4.0, 1.0]), &[("rgb/rgbGamma", num(0.1)), ("clipToOutputWhite", Value::Enum(0))]);
|
||||
assert_eq!(bright.data[0][0], 4.0f32.powf(10.0));
|
||||
// Gamma 0 is the limit of a small gamma: below Input White the curve goes to 0.
|
||||
let dark = run("ec.color.levelsic", &Image::filled(1, 1, [0.5, 0.5, 0.5, 1.0]), &[("rgb/rgbGamma", num(0.0))]);
|
||||
assert!(dark.data[0][0] < 1e-6, "{:?}", dark.data[0]);
|
||||
}
|
||||
|
||||
/// A host whose other layers and other frames are a fixed image.
|
||||
struct ImgHost(Image);
|
||||
impl crate::EffectHost for ImgHost {
|
||||
|
||||
@@ -809,7 +809,7 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
"Change Color",
|
||||
vec![
|
||||
p("view", "View", Value::Enum(0), popup(&["Corrected Layer", "Color Correction Mask"])),
|
||||
p("hueTransform", "Hue Transform", num(0.0), slider(-360.0, 360.0, -180.0, 180.0, 1)),
|
||||
p("hueTransform", "Hue Transform", num(0.0), slider(-1800.0, 1800.0, -180.0, 180.0, 1)),
|
||||
p("lightnessTransform", "Lightness Transform", num(0.0), bipolar()),
|
||||
p("saturationTransform", "Saturation Transform", num(0.0), bipolar()),
|
||||
p("colorToChange", "Color To Change", col(0.8, 0.2, 0.2), ParamUi::Color),
|
||||
|
||||
@@ -506,8 +506,8 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
p("anchor", "Anchor Point", pt(0.5, 0.5), ParamUi::Point),
|
||||
p("position", "Position", pt(0.5, 0.5), ParamUi::Point),
|
||||
p("uniform", "Uniform Scale", Value::Bool(true), ParamUi::Checkbox),
|
||||
p("scaleHeight", "Scale Height", num(100.0), slider(-10000.0, 10000.0, 0.0, 600.0, 1)),
|
||||
p("scaleWidth", "Scale Width", num(100.0), slider(-10000.0, 10000.0, 0.0, 600.0, 1)),
|
||||
p("scaleHeight", "Scale Height", num(100.0), slider(-30000.0, 30000.0, 0.0, 600.0, 1)),
|
||||
p("scaleWidth", "Scale Width", num(100.0), slider(-30000.0, 30000.0, 0.0, 600.0, 1)),
|
||||
p("skew", "Skew", num(0.0), slider(-70.0, 70.0, -70.0, 70.0, 1)),
|
||||
p("skewAxis", "Skew Axis", num(0.0), ParamUi::Angle),
|
||||
p("rotation", "Rotation", num(0.0), ParamUi::Angle),
|
||||
|
||||
@@ -743,7 +743,7 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
p("evolution", "Evolution", num(0.0), ParamUi::Angle),
|
||||
p("evolutionOptions/cycleEvolution", "Cycle Evolution", Value::Bool(false), ParamUi::Checkbox),
|
||||
p("evolutionOptions/cycle", "Cycle (in Revolutions)", num(1.0), slider(1.0, 1000.0, 1.0, 20.0, 0)),
|
||||
p("evolutionOptions/randomSeed", "Random Seed", num(0.0), slider(0.0, 10000.0, 0.0, 1000.0, 0)),
|
||||
p("evolutionOptions/randomSeed", "Random Seed", num(0.0), slider(0.0, 100000.0, 0.0, 1000.0, 0)),
|
||||
p("pinning", "Pinning", Value::Enum(1), popup(&TURBULENT_PINNING)),
|
||||
p("resizeLayer", "Resize Layer", Value::Bool(false), ParamUi::Checkbox),
|
||||
],
|
||||
|
||||
@@ -0,0 +1,436 @@
|
||||
//! Caches of what effects read from files named by a parameter (a LUT, an OCIO transform file, an
|
||||
//! OCIO config), keyed by the parameter text and checked against the files each result was read
|
||||
//! from (#3493, #3538).
|
||||
//!
|
||||
//! A cached result remembers every file its load read, with the file's size and modification time
|
||||
//! (its stamp) taken before reading, including the files a nested cached load read (a config's
|
||||
//! `FileTransform` LUTs) and files looked for but missing. Each lookup stamps those files again
|
||||
//! (one `metadata` call each) and loads again once one moved, so a file edited, created, deleted or
|
||||
//! repaired on disk is followed. Inline text reads no file and is its own content. An edit that keeps
|
||||
//! both the size and the modification time can't be told apart.
|
||||
//!
|
||||
//! Every file read is also followed by [`check_lut_files`], and [`lut_generation`] moves when one is
|
||||
//! found changed: the viewers and frame caches drop the frames made before.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};
|
||||
use std::time::SystemTime;
|
||||
|
||||
/// A file's size and modification time when it was read (`None`: missing, or a file that could
|
||||
/// not be described). A file whose stamp moved is read again.
|
||||
pub(crate) type FileStamp = Option<(u64, Option<SystemTime>)>;
|
||||
|
||||
/// The files a load read and their stamps.
|
||||
type Files = Vec<(String, FileStamp)>;
|
||||
|
||||
/// At most this many files are followed by [`check_lut_files`]; the least recently read go first.
|
||||
const MAX_FOLLOWED_FILES: usize = 1024;
|
||||
|
||||
/// One cached result and what it was read from.
|
||||
struct Entry<V> {
|
||||
files: Files,
|
||||
value: V,
|
||||
used: u64,
|
||||
/// The result's size estimate and the parameter text, counted against the cache's byte bound.
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
/// Results by parameter text, at most `max_entries` and `max_bytes` of them, the least recently
|
||||
/// used going first.
|
||||
pub(crate) struct SourceCache<V> {
|
||||
map: HashMap<String, Entry<V>>,
|
||||
clock: u64,
|
||||
bytes: usize,
|
||||
max_entries: usize,
|
||||
max_bytes: usize,
|
||||
}
|
||||
|
||||
impl<V> SourceCache<V> {
|
||||
pub(crate) fn new(max_entries: usize, max_bytes: usize) -> Self {
|
||||
SourceCache { map: HashMap::new(), clock: 0, bytes: 0, max_entries, max_bytes }
|
||||
}
|
||||
|
||||
fn remove(&mut self, src: &str) {
|
||||
if let Some(e) = self.map.remove(src) {
|
||||
self.bytes = self.bytes.saturating_sub(e.bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/// Forget the results read from `path`.
|
||||
fn forget_file(&mut self, path: &str) {
|
||||
let stale: Vec<String> = self.map.iter().filter(|(_, e)| e.files.iter().any(|(p, _)| p == path)).map(|(k, _)| k.clone()).collect();
|
||||
for src in stale {
|
||||
self.remove(&src);
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep within the bounds, always the result for `keep`.
|
||||
fn evict(&mut self, keep: &str) {
|
||||
while self.map.len() > self.max_entries || (self.bytes > self.max_bytes && self.map.len() > 1) {
|
||||
let Some(oldest) = self.map.iter().filter(|(k, _)| k.as_str() != keep).min_by_key(|(_, e)| e.used).map(|(k, _)| k.clone()) else { break };
|
||||
self.remove(&oldest);
|
||||
}
|
||||
}
|
||||
|
||||
/// The files the result cached for `src` was read from.
|
||||
pub(crate) fn files(&self, src: &str) -> Option<Vec<String>> {
|
||||
self.map.get(src).map(|e| e.files.iter().map(|(p, _)| p.clone()).collect())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn len(&self) -> usize {
|
||||
self.map.len()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn contains(&self, src: &str) -> bool {
|
||||
self.map.contains_key(src)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn bytes(&self) -> (usize, usize, usize) {
|
||||
(self.bytes, self.map.values().map(|e| e.bytes).sum(), self.max_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
/// Lock a cache; a panic elsewhere while it was held leaves a usable map.
|
||||
pub(crate) fn lock<V>(cache: &Mutex<SourceCache<V>>) -> MutexGuard<'_, SourceCache<V>> {
|
||||
cache.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// A cache that registers with [`check_lut_files`] on first use.
|
||||
pub(crate) struct Registered<V: 'static> {
|
||||
cache: OnceLock<Mutex<SourceCache<V>>>,
|
||||
max_entries: usize,
|
||||
max_bytes: usize,
|
||||
}
|
||||
|
||||
impl<V: Clone + Send + 'static> Registered<V> {
|
||||
pub(crate) const fn new(max_entries: usize, max_bytes: usize) -> Self {
|
||||
Registered { cache: OnceLock::new(), max_entries, max_bytes }
|
||||
}
|
||||
|
||||
pub(crate) fn get(&'static self) -> &'static Mutex<SourceCache<V>> {
|
||||
self.cache.get_or_init(|| {
|
||||
registry().push(self);
|
||||
Mutex::new(SourceCache::new(self.max_entries, self.max_bytes))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A cache [`check_lut_files`] tells to forget the results read from a changed file.
|
||||
trait ForgetFile: Sync {
|
||||
fn forget_file(&self, path: &str);
|
||||
/// Add the files the cached results were read from to `out`.
|
||||
fn held_files(&self, out: &mut HashSet<String>);
|
||||
}
|
||||
|
||||
impl<V: Send> ForgetFile for Registered<V> {
|
||||
fn forget_file(&self, path: &str) {
|
||||
if let Some(c) = self.cache.get() {
|
||||
lock(c).forget_file(path);
|
||||
}
|
||||
}
|
||||
|
||||
fn held_files(&self, out: &mut HashSet<String>) {
|
||||
if let Some(c) = self.cache.get() {
|
||||
out.extend(lock(c).map.values().flat_map(|e| e.files.iter().map(|(p, _)| p.clone())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn registry() -> MutexGuard<'static, Vec<&'static dyn ForgetFile>> {
|
||||
static REGISTRY: OnceLock<Mutex<Vec<&'static dyn ForgetFile>>> = OnceLock::new();
|
||||
REGISTRY.get_or_init(Mutex::default).lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// The loads running on this thread, innermost last: the files each read, and the bytes of
|
||||
/// the cached results it holds (a config holds the LUTs its `FileTransform`s loaded).
|
||||
static READING: RefCell<Vec<(Files, usize)>> = const { RefCell::new(Vec::new()) };
|
||||
}
|
||||
|
||||
/// Note that the running load read (or looked for) `path`, stamped `stamp`.
|
||||
pub(crate) fn note_file(path: &str, stamp: FileStamp) {
|
||||
READING.with(|r| {
|
||||
if let Ok(mut r) = r.try_borrow_mut()
|
||||
&& let Some((top, _)) = r.last_mut()
|
||||
&& !top.iter().any(|(p, _)| p == path)
|
||||
{
|
||||
top.push((path.to_string(), stamp));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Note that the running load holds a cached result of `bytes`.
|
||||
fn note_bytes(bytes: usize) {
|
||||
READING.with(|r| {
|
||||
if let Ok(mut r) = r.try_borrow_mut()
|
||||
&& let Some((_, held)) = r.last_mut()
|
||||
{
|
||||
*held = held.saturating_add(bytes);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Pops the files of a load when it ends, even by a panic.
|
||||
struct Reading {
|
||||
popped: bool,
|
||||
}
|
||||
|
||||
impl Reading {
|
||||
fn start() -> Reading {
|
||||
READING.with(|r| {
|
||||
if let Ok(mut r) = r.try_borrow_mut() {
|
||||
r.push((Vec::new(), 0));
|
||||
}
|
||||
});
|
||||
Reading { popped: false }
|
||||
}
|
||||
|
||||
fn pop() -> Option<(Files, usize)> {
|
||||
READING.with(|r| r.try_borrow_mut().ok().and_then(|mut r| r.pop()))
|
||||
}
|
||||
|
||||
/// The files the load read, and the bytes of the cached results it holds.
|
||||
fn finish(mut self) -> (Files, usize) {
|
||||
self.popped = true;
|
||||
Self::pop().unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Reading {
|
||||
fn drop(&mut self) {
|
||||
if !self.popped {
|
||||
let _ = Self::pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The stamp of the file at `path` now: one `metadata` call.
|
||||
pub(crate) fn file_stamp(path: &str) -> FileStamp {
|
||||
std::fs::metadata(path).ok().map(|md| (md.len(), md.modified().ok()))
|
||||
}
|
||||
|
||||
/// Read the text file at `path` for the running load, stamped before reading (a file written
|
||||
/// after it was stamped has a newer stamp, and is read again next time).
|
||||
pub(crate) fn read(path: &str) -> std::io::Result<String> {
|
||||
note_file(path, file_stamp(path));
|
||||
std::fs::read_to_string(path)
|
||||
}
|
||||
|
||||
/// Note that the running load looked for `path` and did not find it: it loads again once the file
|
||||
/// appears.
|
||||
pub(crate) fn note_missing(path: &str) {
|
||||
note_file(path, None);
|
||||
}
|
||||
|
||||
/// The result cached for `src`, or `load`'s (and its size estimate in bytes), cached. `load` reads
|
||||
/// files with [`read`] (and notes the ones it looked for with [`note_missing`]); no lock is held
|
||||
/// while it runs or while the files are stamped.
|
||||
pub(crate) fn cached<V: Clone>(cache: &Mutex<SourceCache<V>>, src: &str, load: impl FnOnce() -> (V, usize)) -> V {
|
||||
let files = lock(cache).map.get(src).map(|e| e.files.clone());
|
||||
let stale = files.is_some();
|
||||
if let Some(files) = files
|
||||
&& files.iter().all(|(p, s)| file_stamp(p) == *s)
|
||||
{
|
||||
let mut c = lock(cache);
|
||||
c.clock += 1;
|
||||
let now = c.clock;
|
||||
if let Some(e) = c.map.get_mut(src)
|
||||
&& e.files == files
|
||||
{
|
||||
e.used = now;
|
||||
let (value, bytes) = (e.value.clone(), e.bytes);
|
||||
drop(c);
|
||||
// (an enclosing load depends on these files too, and holds this result)
|
||||
for (p, s) in &files {
|
||||
note_file(p, *s);
|
||||
}
|
||||
note_bytes(bytes);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
let reading = Reading::start();
|
||||
let (value, size) = load();
|
||||
let (files, held) = reading.finish();
|
||||
follow(&files, stale);
|
||||
for (p, s) in &files {
|
||||
note_file(p, *s);
|
||||
}
|
||||
// (what it holds counts against this cache's bound too: another cache letting go of it
|
||||
// frees nothing while this result keeps it)
|
||||
let bytes = size.saturating_add(held).saturating_add(src.len());
|
||||
note_bytes(bytes);
|
||||
let mut c = lock(cache);
|
||||
c.clock += 1;
|
||||
let used = c.clock;
|
||||
c.remove(src);
|
||||
c.map.insert(src.to_string(), Entry { files, value: value.clone(), used, bytes });
|
||||
c.bytes = c.bytes.saturating_add(bytes);
|
||||
c.evict(src);
|
||||
value
|
||||
}
|
||||
|
||||
/// How often a file a cached load read was found changed on disk ([`lut_generation`]).
|
||||
static GENERATION: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
/// The files followed by [`check_lut_files`]: the stamp each was last read with, and when.
|
||||
#[derive(Default)]
|
||||
struct Followed {
|
||||
files: HashMap<String, (FileStamp, u64)>,
|
||||
clock: u64,
|
||||
}
|
||||
|
||||
fn followed() -> MutexGuard<'static, Followed> {
|
||||
static FOLLOWED: OnceLock<Mutex<Followed>> = OnceLock::new();
|
||||
FOLLOWED.get_or_init(Mutex::default).lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
|
||||
impl Followed {
|
||||
/// Note that `files` were read with these stamps; true when one has a different stamp than
|
||||
/// its last read's: a file changed on disk.
|
||||
fn read(&mut self, files: &Files) -> bool {
|
||||
let mut moved = false;
|
||||
for (path, stamp) in files {
|
||||
self.clock += 1;
|
||||
let now = self.clock;
|
||||
if self.files.insert(path.clone(), (*stamp, now)).is_some_and(|(old, _)| old != *stamp) {
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
moved
|
||||
}
|
||||
|
||||
/// Keep at most [`MAX_FOLLOWED_FILES`], letting go of the least recently read first but never
|
||||
/// of a file in `held` (a cached result was read from it) or in `files` (just read).
|
||||
fn trim(&mut self, held: &HashSet<String>, files: &Files) {
|
||||
while self.files.len() > MAX_FOLLOWED_FILES {
|
||||
let Some(oldest) = self
|
||||
.files
|
||||
.iter()
|
||||
.filter(|(p, _)| !held.contains(p.as_str()) && !files.iter().any(|(f, _)| f == *p))
|
||||
.min_by_key(|(_, (_, used))| *used)
|
||||
.map(|(p, _)| p.clone())
|
||||
else {
|
||||
break;
|
||||
};
|
||||
self.files.remove(&oldest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The files the registered caches' results were read from.
|
||||
fn held_files() -> HashSet<String> {
|
||||
let caches: Vec<&'static dyn ForgetFile> = registry().clone();
|
||||
let mut out = HashSet::new();
|
||||
for c in caches {
|
||||
c.held_files(&mut out);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Note that `files` were read with these stamps. A file read with a different stamp than last
|
||||
/// time, or a load replacing a result whose files moved (`stale`), means a file changed on disk,
|
||||
/// and frames made before may show the old contents.
|
||||
fn follow(files: &Files, stale: bool) {
|
||||
let (moved, over) = {
|
||||
let mut f = followed();
|
||||
(f.read(files), f.files.len() > MAX_FOLLOWED_FILES)
|
||||
};
|
||||
if over {
|
||||
// (gathered without the followed files' lock: no two of these locks are ever nested)
|
||||
let held = held_files();
|
||||
followed().trim(&held, files);
|
||||
}
|
||||
if moved || stale {
|
||||
GENERATION.fetch_add(1, Ordering::AcqRel);
|
||||
}
|
||||
}
|
||||
|
||||
/// Changes each time a file a cached load read is found changed on disk (by a load itself or
|
||||
/// [`check_lut_files`]): frames made before then may show the old contents.
|
||||
pub fn lut_generation() -> u64 {
|
||||
GENERATION.load(Ordering::Acquire)
|
||||
}
|
||||
|
||||
/// Look at every file read (one `metadata` call each) and forget the results read from the ones
|
||||
/// that changed on disk since, so the next load reads them again. Returns [`lut_generation`].
|
||||
pub fn check_lut_files() -> u64 {
|
||||
let files: Vec<(String, FileStamp)> = followed().files.iter().map(|(p, (s, _))| (p.clone(), *s)).collect();
|
||||
// (stamped without the lock: a file on a slow share must not hold up renders)
|
||||
let changed: Vec<(String, FileStamp, FileStamp)> =
|
||||
files.into_iter().filter_map(|(path, old)| Some(file_stamp(&path)).filter(|now| *now != old).map(|now| (path, old, now))).collect();
|
||||
let caches: Vec<&'static dyn ForgetFile> = registry().clone();
|
||||
for (path, old, now) in changed {
|
||||
{
|
||||
let mut f = followed();
|
||||
// (a render that read the file meanwhile has noted its new stamp already)
|
||||
match f.files.get_mut(&path) {
|
||||
Some((s, _)) if *s == old => *s = now,
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
for c in &caches {
|
||||
c.forget_file(&path);
|
||||
}
|
||||
GENERATION.fetch_add(1, Ordering::AcqRel);
|
||||
}
|
||||
lut_generation()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn followed_stamp(path: &str) -> Option<FileStamp> {
|
||||
followed().files.get(path).map(|(s, _)| *s)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Past [`MAX_FOLLOWED_FILES`], the file just read is still followed (a list of its own: the
|
||||
/// shared one's files belong to the other tests too).
|
||||
#[test]
|
||||
fn the_file_just_read_stays_followed_past_the_limit() {
|
||||
let mut f = Followed::default();
|
||||
let held: HashSet<String> = ["/followed/0.cube".to_string()].into();
|
||||
for i in 0..MAX_FOLLOWED_FILES + 8 {
|
||||
let files = vec![(format!("/followed/{i}.cube"), Some((1, None)))];
|
||||
assert!(!f.read(&files));
|
||||
f.trim(&held, &files);
|
||||
assert!(f.files.contains_key(&files[0].0), "{i}: the file just read");
|
||||
assert!(f.files.len() <= MAX_FOLLOWED_FILES);
|
||||
}
|
||||
assert!(f.files.contains_key("/followed/0.cube"), "a file a cached result was read from");
|
||||
assert!(!f.files.contains_key("/followed/1.cube"), "the least recently read other");
|
||||
assert!(f.read(&vec![("/followed/1030.cube".into(), Some((2, None)))]), "a moved stamp");
|
||||
}
|
||||
|
||||
/// A nested cached load's files count for the enclosing load.
|
||||
#[test]
|
||||
fn an_enclosing_load_depends_on_a_nested_loads_files() {
|
||||
static INNER: Registered<u32> = Registered::new(8, 1 << 20);
|
||||
static OUTER: Registered<u32> = Registered::new(8, 1 << 20);
|
||||
let v = cached(OUTER.get(), "outer", || {
|
||||
note_missing("/nested/outer-own.cube");
|
||||
let inner = cached(INNER.get(), "inner", || {
|
||||
note_missing("/nested/inner.cube");
|
||||
(7, 100)
|
||||
});
|
||||
(inner, 0)
|
||||
});
|
||||
assert_eq!(v, 7);
|
||||
let files = lock(OUTER.get()).files("outer").unwrap();
|
||||
let held = |src: &str| lock(OUTER.get()).map.get(src).map(|e| e.bytes);
|
||||
// ("inner" is 5 bytes, its result 100: "outer" holds it, on a load and on a hit alike)
|
||||
assert_eq!(held("outer"), Some(100 + 5 + "outer".len()), "the result an enclosing load holds counts");
|
||||
assert_eq!(files, vec!["/nested/outer-own.cube".to_string(), "/nested/inner.cube".to_string()]);
|
||||
// A hit on the inner result still reports its files to a new enclosing load.
|
||||
let w = cached(OUTER.get(), "outer 2", || (cached(INNER.get(), "inner", || (0, 0)), 0));
|
||||
assert_eq!(w, 7);
|
||||
assert_eq!(lock(OUTER.get()).files("outer 2").unwrap(), vec!["/nested/inner.cube".to_string()]);
|
||||
assert_eq!(held("outer 2"), Some(100 + 5 + "outer 2".len()));
|
||||
}
|
||||
}
|
||||
@@ -121,11 +121,17 @@ pub fn four_color_softening(blend: f64, scale: f64) -> f64 {
|
||||
(b * b).max(1e-6)
|
||||
}
|
||||
|
||||
/// 4-Color Gradient's Jitter (percent, up to After Effects' 500 %) as the dither's strength: 1 at
|
||||
/// 100 %, a few code values of noise per unit.
|
||||
pub fn four_color_jitter(jitter: f64) -> f32 {
|
||||
(jitter / 100.0).clamp(0.0, 5.0) as f32
|
||||
}
|
||||
|
||||
fn four_color(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let pts: Vec<((f64, f64), [f32; 4])> =
|
||||
(1..=4).map(|i| (b.to_px(ctx.params.v2(&format!("positionsColors/point{i}"))), ctx.params.color(&format!("positionsColors/color{i}")))).collect();
|
||||
let k = four_color_softening(ctx.params.f("blend"), b.scale);
|
||||
let jitter = (ctx.params.f("jitter") / 100.0).clamp(0.0, 1.0) as f32;
|
||||
let jitter = four_color_jitter(ctx.params.f("jitter"));
|
||||
let op = ctx.params.f("opacity") as f32 / 100.0;
|
||||
let mode = ctx.params.e("blendingMode");
|
||||
let seed = ctx.seed;
|
||||
@@ -349,8 +355,9 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
p("positionsColors/color3", "Color 3", col(1.0, 0.0, 1.0), ParamUi::Color),
|
||||
p("positionsColors/point4", "Point 4", pt(0.9, 0.9), ParamUi::Point),
|
||||
p("positionsColors/color4", "Color 4", col(0.0, 0.0, 1.0), ParamUi::Color),
|
||||
p("blend", "Blend", num(100.0), slider(1.0, 1000.0, 1.0, 1000.0, 1)),
|
||||
p("jitter", "Jitter", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 1)),
|
||||
// After Effects' valid ranges (#3478, #3479); Blend keeps its lower minimum (1, not 5).
|
||||
p("blend", "Blend", num(100.0), slider(1.0, 10000.0, 1.0, 1000.0, 1)),
|
||||
p("jitter", "Jitter", num(0.0), slider(0.0, 500.0, 0.0, 100.0, 1)),
|
||||
p("opacity", "Opacity", num(100.0), slider(0.0, 100.0, 0.0, 100.0, 1)),
|
||||
p("blendingMode", "Blending Mode", Value::Enum(0), popup(&GEN_BLEND_MODES[..18])),
|
||||
],
|
||||
@@ -384,7 +391,7 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
p("corner", "Corner", pt(0.1, 0.1), ParamUi::Point),
|
||||
p("width", "Width", num(64.0), slider(1.0, 4000.0, 1.0, 400.0, 1)),
|
||||
p("height", "Height", num(64.0), slider(1.0, 4000.0, 1.0, 400.0, 1)),
|
||||
p("border", "Border", num(2.0), slider(0.0, 400.0, 0.0, 40.0, 1)),
|
||||
p("border", "Border", num(2.0), slider(0.0, 2000.0, 0.0, 40.0, 1)),
|
||||
p("feather/featherWidth", "Width", num(0.0), slider(0.0, 1000.0, 0.0, 50.0, 1)),
|
||||
p("feather/featherHeight", "Height", num(0.0), slider(0.0, 1000.0, 0.0, 50.0, 1)),
|
||||
p("invertGrid", "Invert Grid", Value::Bool(false), ParamUi::Checkbox),
|
||||
@@ -465,6 +472,21 @@ mod generator_tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// #3479: Jitter goes up to After Effects' 500 %, and more jitter dithers more (the dither
|
||||
/// stopped growing at 100 %).
|
||||
#[test]
|
||||
fn four_color_jitter_grows_up_to_five_hundred_percent() {
|
||||
let img = Image::filled(32, 32, [0.0, 0.0, 0.0, 1.0]);
|
||||
let spread = |jitter: f64| {
|
||||
let o = run_fx("ec.generate.fourcolor", &[("jitter", num(jitter))], img.clone(), 0.0, EffectEnv::default()).img;
|
||||
let flat = run_fx("ec.generate.fourcolor", &[], img.clone(), 0.0, EffectEnv::default()).img;
|
||||
o.data.iter().zip(&flat.data).map(|(a, b)| (a[0] - b[0]).abs()).fold(0.0f32, f32::max)
|
||||
};
|
||||
let (at100, at500) = (spread(100.0), spread(500.0));
|
||||
assert!(at100 > 0.0 && at500 > 4.0 * at100, "{at100} {at500}");
|
||||
assert!(at500 < 0.2, "a few code values: {at500}");
|
||||
}
|
||||
|
||||
/// After Effects 2026, 4-Color Gradient defaults (yellow, green, magenta, blue at 10 % / 90 %
|
||||
/// of a 1920 × 1080 layer) at Blend 100 and 300, here on a tenth-size layer with Blend 10
|
||||
/// and 30 (the weights scale with the layer): colours at AE pixels (10x + 5, 10y + 5).
|
||||
|
||||
@@ -1111,8 +1111,8 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
p("endPoint", "Ending Point", pt(0.75, 0.5), ParamUi::Point),
|
||||
p("length", "Length", num(25.0), pct()),
|
||||
p("time", "Time", num(0.0), pct()),
|
||||
p("startThickness", "Starting Thickness", num(8.0), slider(0.0, 1000.0, 0.0, 100.0, 1)),
|
||||
p("endThickness", "Ending Thickness", num(8.0), slider(0.0, 1000.0, 0.0, 100.0, 1)),
|
||||
p("startThickness", "Starting Thickness", num(8.0), slider(0.0, 4000.0, 0.0, 100.0, 1)),
|
||||
p("endThickness", "Ending Thickness", num(8.0), slider(0.0, 4000.0, 0.0, 100.0, 1)),
|
||||
p("softness", "Softness", num(20.0), pct()),
|
||||
p("insideColor", "Inside Color", col(1.0, 1.0, 1.0), ParamUi::Color),
|
||||
p("outsideColor", "Outside Color", col(0.4, 0.4, 1.0), ParamUi::Color),
|
||||
|
||||
@@ -39,6 +39,7 @@ mod keying2;
|
||||
pub mod keylight;
|
||||
mod matte;
|
||||
|
||||
mod file_cache;
|
||||
pub mod migrate;
|
||||
mod misc;
|
||||
pub mod mocha_shape;
|
||||
@@ -98,14 +99,14 @@ pub use sim::particle_state;
|
||||
pub use sim3::{PgBlit, PgPlan, playground_plan, playground_state};
|
||||
pub use stylize2::strobe_on;
|
||||
// CPU helpers the GPU kernels share (effectcraft-gpu).
|
||||
pub use blur2::{camera_lens_plain, camera_lens_spans, radial_fast_pad};
|
||||
pub use generate::{four_color_softening, gen_mode};
|
||||
pub use blur2::{CAMERA_LENS_MAX_RADIUS, camera_lens_plain, camera_lens_radius, camera_lens_spans, radial_fast_pad};
|
||||
pub use generate::{four_color_jitter, four_color_softening, gen_mode};
|
||||
pub use generate2::pattern_kind as cell_pattern_kind;
|
||||
pub use generate2::{BoltSeg, Contour, Ghost, RadioPlan, Wave, flare_ghosts, lightning_segments, radio_plan};
|
||||
pub use generate3::{
|
||||
Coverage, MarksPlan, PaintPlan, Seg, eyedropper_color, marks_plan, paint_bucket_region, paint_plan, plan_reads_pixels, poly_segs, scribble_style,
|
||||
};
|
||||
pub use noise::GrainLook;
|
||||
pub use noise::{GrainLook, MEDIAN_MAX_RADIUS};
|
||||
pub use textfx::{BoltPlan, TextLook, TextPass, bolt_plan, text_passes};
|
||||
pub use transition::{GridWipe, place_layer};
|
||||
// effectcraft-gpu fx_noise.
|
||||
@@ -123,13 +124,14 @@ pub use channel3d::{
|
||||
crypto_layer_name, crypto_layer_named, extractor_aux_channel, extractor_own_channel, id_color, is_extractor_copy, matte_only_px, reads_aux,
|
||||
selection_hashes,
|
||||
};
|
||||
pub use file_cache::{check_lut_files, lut_generation};
|
||||
pub use ocio::{ColorOp, Straight, Tf, color_program};
|
||||
pub use sim::{Acc, Blob, Bubble, Post, Shape, Sprite, SpritePlan, Tint, bubble_list, bubble_sample_bounds, drizzle_drops, mercury_blobs, sprite_plan};
|
||||
pub use sim2::{
|
||||
CausticsSetup, FoamDisc, FoamEnv, FoamPlan, Piece, PiecePlan, PieceTex, PixelPlan, WavePlan, Waves, caustics_setup, foam_full_plan, piece_plan, pixel_plan,
|
||||
wave_world_plan,
|
||||
};
|
||||
pub use utility::{Lut, load_lut};
|
||||
pub use utility::{Lut, load_lut, lut_file, lut_files, lut_params};
|
||||
pub use vr::{equi_dir as vr_equi_dir, rotation as vr_rotation};
|
||||
|
||||
/// Effect categories in Effects & Presets order.
|
||||
|
||||
@@ -919,7 +919,7 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
p("startAngle", "Start Angle", num(0.0), ParamUi::Angle),
|
||||
p("center", "Wipe Center", Value::Vec2([0.5, 0.5]), ParamUi::Point),
|
||||
p("wipe", "Wipe", Value::Enum(0), popup(&["Clockwise", "Counterclockwise", "Both"])),
|
||||
p("feather", "Feather", num(0.0), slider(0.0, 360.0, 0.0, 50.0, 1)),
|
||||
p("feather", "Feather", num(0.0), slider(0.0, 32000.0, 0.0, 50.0, 1)),
|
||||
],
|
||||
radial_wipe,
|
||||
),
|
||||
@@ -930,8 +930,8 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
vec![
|
||||
p("completion", "Transition Completion", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 0)),
|
||||
p("direction", "Direction", num(90.0), ParamUi::Angle),
|
||||
p("width", "Width", num(30.0), slider(2.0, 1000.0, 2.0, 200.0, 0)),
|
||||
p("feather", "Feather", num(0.0), slider(0.0, 100.0, 0.0, 100.0, 1)),
|
||||
p("width", "Width", num(30.0), slider(1.0, 32000.0, 2.0, 200.0, 0)),
|
||||
p("feather", "Feather", num(0.0), slider(0.0, 32000.0, 0.0, 100.0, 1)),
|
||||
],
|
||||
venetian,
|
||||
),
|
||||
|
||||
@@ -370,9 +370,16 @@ fn quant(v: f32) -> u16 {
|
||||
(v.clamp(0.0, 1.0) * (NB - 1) as f32).round() as u16
|
||||
}
|
||||
|
||||
/// Largest median window radius (buffer pixels) for Median, Median (Legacy), Dust & Scratches
|
||||
/// and the other median users. Radius goes to 255 in the UI; filling one (2r + 1)² window per
|
||||
/// row costs the square of the radius, so a project value far beyond it is held here (also
|
||||
/// keeping the window's pixel count well inside `u32` / `i32` on the GPU).
|
||||
pub const MEDIAN_MAX_RADIUS: usize = 512;
|
||||
|
||||
/// Per-channel median over a (2r+1)² window of the premultiplied channels (values quantised to
|
||||
/// 512 levels in 0..1, edges repeated).
|
||||
pub(crate) fn median_image(img: &Image, r: usize) -> Image {
|
||||
let r = r.min(MEDIAN_MAX_RADIUS);
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
if r == 0 || w == 0 || h == 0 {
|
||||
return img.clone();
|
||||
@@ -759,10 +766,12 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
let hls_common = || {
|
||||
vec![
|
||||
p("noise", "Noise", Value::Enum(0), popup(&["Uniform", "Squared", "Grain"])),
|
||||
p("hue", "Hue", num(0.0), pct()),
|
||||
p("lightness", "Lightness", num(0.0), pct()),
|
||||
p("saturation", "Saturation", num(0.0), pct()),
|
||||
p("grainSize", "Grain Size", num(1.0), slider(0.1, 100.0, 0.5, 10.0, 2)),
|
||||
// After Effects' valid ranges (#3483, #3484): the amounts go to 1000 %, Grain Size
|
||||
// down to 0 (the renderer floors it at 0.1 px) and keeps its wider maximum.
|
||||
p("hue", "Hue", num(0.0), slider(0.0, 1000.0, 0.0, 100.0, 1)),
|
||||
p("lightness", "Lightness", num(0.0), slider(0.0, 1000.0, 0.0, 100.0, 1)),
|
||||
p("saturation", "Saturation", num(0.0), slider(0.0, 1000.0, 0.0, 100.0, 1)),
|
||||
p("grainSize", "Grain Size", num(1.0), slider(0.0, 100.0, 0.5, 10.0, 2)),
|
||||
]
|
||||
};
|
||||
let mut hls = hls_common();
|
||||
@@ -871,6 +880,17 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn median_radius_is_capped() {
|
||||
// A project's Radius far beyond the UI's 255 takes the capped window, not a
|
||||
// (2r + 1)² fill per row (or an overflow) on a tiny image.
|
||||
let mut img = Image::filled(4, 3, [0.2, 0.3, 0.4, 1.0]);
|
||||
img.set(1, 1, [1.0, 1.0, 1.0, 1.0]);
|
||||
let capped = median_image(&img, MEDIAN_MAX_RADIUS);
|
||||
assert_eq!(median_image(&img, usize::MAX).data, capped.data);
|
||||
assert_eq!(median_image(&img, MEDIAN_MAX_RADIUS + 1000).data, capped.data);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn median_matches_brute_force() {
|
||||
let (w, h) = (9u32, 7u32);
|
||||
@@ -949,6 +969,32 @@ mod tests {
|
||||
assert_eq!(still.img, img);
|
||||
}
|
||||
|
||||
/// #3483, #3484: amounts above 100 % keep adding noise, and Grain Size 0 renders (floored at
|
||||
/// 0.1 px) with finite output, for both effects.
|
||||
#[test]
|
||||
fn noise_hls_takes_after_effects_ranges() {
|
||||
let mut img = Image::new(16, 16);
|
||||
for (i, p) in img.data.iter_mut().enumerate() {
|
||||
*p = premul([0.6, 0.3, (i % 7) as f32 / 7.0], 1.0);
|
||||
}
|
||||
for id in ["ec.noise.noisehls", "ec.noise.noisehlsauto"] {
|
||||
for (param, lo, hi) in [("hue", 0.0, 1000.0), ("lightness", 0.0, 1000.0), ("saturation", 0.0, 1000.0), ("grainSize", 0.0, 100.0)] {
|
||||
let ui = &crate::find(id).unwrap().params.iter().find(|p| p.id == param).unwrap().ui;
|
||||
assert!(matches!(ui, ParamUi::Slider { min, max, .. } if *min == lo && *max == hi), "{id} {param}: {ui:?}");
|
||||
}
|
||||
let at = |amount: f64, grain: f64| {
|
||||
let vals =
|
||||
[("noise", Value::Enum(2)), ("hue", num(amount)), ("lightness", num(amount)), ("saturation", num(amount)), ("grainSize", num(grain))];
|
||||
run(id, &vals, img.clone(), 0.0).img
|
||||
};
|
||||
let (low, high) = (at(100.0, 1.0), at(1000.0, 1.0));
|
||||
assert_ne!(low, high, "{id}");
|
||||
for out in [high, at(1000.0, 0.0)] {
|
||||
assert!(out.data.iter().flatten().all(|v| v.is_finite() && (0.0..=1.0).contains(v)), "{id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_grain_keeps_flat_mean() {
|
||||
let out = run("ec.noise.addgrain", &[("tweaking/intensity", num(2.0)), ("application/blendingMode", Value::Enum(2))], gray(64, 64, 0.5), 0.0);
|
||||
|
||||
@@ -301,7 +301,8 @@ pub fn frozen_alpha(params: &Params, layer_size: [f64; 2], fps: f64, f: i64, w:
|
||||
let fr = fc.frames.get(&f)?;
|
||||
let [fw, fh] = fc.size;
|
||||
let expected = fw.checked_mul(fh)?;
|
||||
if fw == 0 || fh == 0 || expected > rle::MAX_PLANE_BYTES {
|
||||
// Only the layer's own full-resolution grid (see Roto Brush's frozen mattes).
|
||||
if fc.size != roto::frozen_grid(layer_size) || fw == 0 || fh == 0 || expected > rle::MAX_PLANE_BYTES {
|
||||
return None;
|
||||
}
|
||||
let a = rle::decode_bounded(&fr.a, expected)?;
|
||||
@@ -719,6 +720,12 @@ mod tests {
|
||||
p.values.insert("objectPoint".into(), Value::Vec2([40.0, 40.0]));
|
||||
p.values.insert(FROZEN.into(), Value::Str(fc2.to_json()));
|
||||
assert_eq!(frozen_alpha(&p, [100.0, 80.0], 25.0, 2, 100, 80, None), None);
|
||||
// A compact plane claiming a size other than the layer's grid is not decoded.
|
||||
let mut fc3 = fc.clone();
|
||||
fc3.size = [4000, 4000];
|
||||
fc3.frames.insert(2, roto::freeze_frame(&vec![1.0; 4000 * 4000], &[]));
|
||||
p.values.insert(FROZEN.into(), Value::Str(fc3.to_json()));
|
||||
assert_eq!(frozen_alpha(&p, [100.0, 80.0], 25.0, 2, 100, 80, None), None);
|
||||
}
|
||||
|
||||
/// Mattes frozen with a model stay usable in a session where that model is not loaded (a
|
||||
|
||||
@@ -24,8 +24,7 @@
|
||||
//! * Configuration ▸ Custom reads a `.ocio` file instead ([`crate::ocio_config`]); what it can't
|
||||
//! apply is reported by [`warning`].
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::sync::Arc;
|
||||
|
||||
use effectcraft_color::ColorSpace;
|
||||
use effectcraft_color::space::{invert, mul, mul_vec, rgb_to_xyz};
|
||||
@@ -51,6 +50,7 @@ pub use effects::parse_looks;
|
||||
pub use effects::{color_stabilizer_maps, warning};
|
||||
#[cfg(test)]
|
||||
use effects::{tonemap, tonemap_inv};
|
||||
pub(crate) use lut::cached_files;
|
||||
pub use lut::{FileXform, load_file};
|
||||
#[cfg(test)]
|
||||
pub use lut::{parse_file, parse_spi1d, parse_spi3d, parse_spimtx};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use super::cdl::{Cdl, parse_cdl_xml};
|
||||
use super::{Arc, HashMap, Lut, Mutex, OnceLock, parse_cube};
|
||||
use super::{Arc, Lut, parse_cube};
|
||||
use crate::file_cache::{self, Registered};
|
||||
|
||||
// ---------------------------------------------------------------- LUT files
|
||||
|
||||
@@ -264,33 +265,38 @@ pub fn parse_file(text: &str, ext: &str) -> Option<FileXform> {
|
||||
parse_3dl(text).map(|lut| FileXform::Lut { lut, shaper: None })
|
||||
}
|
||||
|
||||
type FileCache = Mutex<HashMap<String, Option<Arc<FileXform>>>>;
|
||||
/// [`load_file`]'s cache, by parameter text (bounded like [`crate::load_lut`]'s).
|
||||
static FILES: Registered<Option<Arc<FileXform>>> = Registered::new(64, 512 << 20);
|
||||
|
||||
/// Load (and cache) a transform from a file path or inline content.
|
||||
/// The files the transform cached for `src` was read from.
|
||||
pub(crate) fn cached_files(src: &str) -> Option<Vec<String>> {
|
||||
file_cache::lock(FILES.get()).files(src)
|
||||
}
|
||||
|
||||
/// Load (and cache) a transform from a file path or inline content. A file is stamped (one
|
||||
/// `metadata` call) on every load and read again once its size or modification time moved (see
|
||||
/// [`crate::file_cache`]); inline content is its own content.
|
||||
pub fn load_file(src: &str) -> Option<Arc<FileXform>> {
|
||||
let src_t = src.trim();
|
||||
if src_t.is_empty() {
|
||||
return None;
|
||||
}
|
||||
static CACHE: OnceLock<FileCache> = OnceLock::new();
|
||||
let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
|
||||
if let Ok(m) = cache.lock()
|
||||
&& let Some(v) = m.get(src)
|
||||
{
|
||||
return v.clone();
|
||||
}
|
||||
let inline = src.contains('\n');
|
||||
let parsed = if inline {
|
||||
parse_file(src, "")
|
||||
} else {
|
||||
let ext = std::path::Path::new(src_t).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("");
|
||||
std::fs::read_to_string(src_t).ok().and_then(|t| parse_file(&t, ext))
|
||||
}
|
||||
.map(Arc::new);
|
||||
if let Ok(mut m) = cache.lock() {
|
||||
m.insert(src.to_string(), parsed.clone());
|
||||
}
|
||||
parsed
|
||||
file_cache::cached(FILES.get(), src, || {
|
||||
let parsed = if src.contains('\n') {
|
||||
parse_file(src, "")
|
||||
} else {
|
||||
let ext = std::path::Path::new(src_t).extension().and_then(std::ffi::OsStr::to_str).unwrap_or("");
|
||||
file_cache::read(src_t).ok().and_then(|t| parse_file(&t, ext))
|
||||
}
|
||||
.map(Arc::new);
|
||||
let bytes = match parsed.as_deref() {
|
||||
Some(FileXform::Lut { lut, shaper }) => crate::utility::lut_bytes(lut)
|
||||
.saturating_add(shaper.as_ref().map_or(0, |s| s.iter().map(Vec::len).sum::<usize>().saturating_mul(std::mem::size_of::<(f32, f32)>()))),
|
||||
Some(FileXform::Cdl(c)) => c.len().saturating_mul(std::mem::size_of::<(String, Cdl)>()),
|
||||
Some(FileXform::Matrix { .. }) | None => 0,
|
||||
};
|
||||
(parsed, bytes)
|
||||
})
|
||||
}
|
||||
|
||||
impl FileXform {
|
||||
|
||||
@@ -396,3 +396,43 @@ fn color_stabilizer_samples_the_reference_at_the_points_reference_positions() {
|
||||
assert!((o(x) - (a + b * v(x))).abs() < 1e-3, "x {x}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes `text` to `dir/name` with modification time `secs` (so an edit within the file system's
|
||||
/// time resolution still moves the stamp), returning the path.
|
||||
fn write_dated(dir: &std::path::Path, name: &str, text: &str, secs: u64) -> String {
|
||||
let path = dir.join(name);
|
||||
std::fs::write(&path, text).unwrap();
|
||||
let f = std::fs::File::options().write(true).open(&path).unwrap();
|
||||
f.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs)).unwrap();
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
/// #3538: OCIO File Transform read a file once per process; an edited, created, repaired or
|
||||
/// deleted file was never followed.
|
||||
#[test]
|
||||
fn an_ocio_transform_file_is_read_again_once_it_changes() {
|
||||
const BLACK: &str = "LUT_1D_SIZE 2\n0 0 0\n0 0 0\n";
|
||||
const WHITE: &str = "LUT_1D_SIZE 2\n1 1 1\n1 1 1\n";
|
||||
let dir = std::env::temp_dir().join(format!("ec-fx-3538-file-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let at = |src: &str| load_file(src).map(|x| x.apply([0.5; 3], 1, false, ""));
|
||||
let edited = write_dated(dir.as_path(), "edited.cube", BLACK, 1_000);
|
||||
assert_eq!(at(&edited), Some([0.0; 3]));
|
||||
write_dated(dir.as_path(), "edited.cube", WHITE, 2_000);
|
||||
assert_eq!(at(&edited), Some([1.0; 3]), "an edited file, same size");
|
||||
let created = dir.as_path().join("created.cube").to_string_lossy().into_owned();
|
||||
assert_eq!(at(&created), None);
|
||||
write_dated(dir.as_path(), "created.cube", WHITE, 1_000);
|
||||
assert_eq!(at(&created), Some([1.0; 3]), "a file created after a failed load");
|
||||
let repaired = write_dated(dir.as_path(), "repaired.cube", "not a lut", 1_000);
|
||||
assert_eq!(at(&repaired), None);
|
||||
write_dated(dir.as_path(), "repaired.cube", WHITE, 2_000);
|
||||
assert_eq!(at(&repaired), Some([1.0; 3]), "a repaired file");
|
||||
std::fs::remove_file(&repaired).unwrap();
|
||||
assert_eq!(at(&repaired), None, "a deleted file");
|
||||
// Inline text is its own content: the same text is the same transform.
|
||||
let inline = load_file(BLACK).unwrap();
|
||||
assert!(Arc::ptr_eq(&inline, &load_file(BLACK).unwrap()));
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
@@ -42,9 +42,10 @@
|
||||
//! resolving named transforms [`MAX_NAMED_DEPTH`] levels down or past [`MAX_OPS`] steps
|
||||
//! (a config that refers to itself in a loop), so a hostile config can't exhaust the stack.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::file_cache::{self, Registered};
|
||||
|
||||
use super::ocio::{ACES_CG, ACES2065_1, Cdl, D65, FileXform, P3, REC709, REC2020, Space, Tf, load_file, matrix, sp};
|
||||
|
||||
@@ -882,13 +883,21 @@ fn resolve(src: &str, dir: &Path, search: &[String]) -> Option<PathBuf> {
|
||||
if src.trim().is_empty() {
|
||||
return None;
|
||||
}
|
||||
// (a candidate not there is noted: the config is read again once it appears)
|
||||
let found = |c: PathBuf| {
|
||||
let there = c.exists();
|
||||
if !there {
|
||||
file_cache::note_missing(&c.to_string_lossy());
|
||||
}
|
||||
there.then_some(c)
|
||||
};
|
||||
let p = Path::new(src);
|
||||
if p.is_absolute() {
|
||||
return p.exists().then(|| p.to_path_buf());
|
||||
return found(p.to_path_buf());
|
||||
}
|
||||
let mut dirs: Vec<PathBuf> = search.iter().map(|s| dir.join(s)).collect();
|
||||
dirs.push(dir.to_path_buf());
|
||||
dirs.into_iter().map(|d| d.join(src)).find(|c| c.exists())
|
||||
dirs.into_iter().map(|d| d.join(src)).find_map(found)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- the config
|
||||
@@ -1338,33 +1347,34 @@ impl<'c> Build<'c> {
|
||||
}
|
||||
}
|
||||
|
||||
type ConfigCache = Mutex<HashMap<String, Result<Arc<Config>, String>>>;
|
||||
/// [`load_config`]'s cache, by parameter text.
|
||||
static CONFIGS: Registered<Result<Arc<Config>, String>> = Registered::new(16, 64 << 20);
|
||||
|
||||
/// The files the config cached for `src` was read from: the `.ocio` file, the files its
|
||||
/// `FileTransform`s read and the places it looked for missing ones.
|
||||
pub(crate) fn cached_files(src: &str) -> Option<Vec<String>> {
|
||||
file_cache::lock(CONFIGS.get()).files(src)
|
||||
}
|
||||
|
||||
/// Load (and cache) a config from a `.ocio` path or the config text itself; why not, when it
|
||||
/// can't be read.
|
||||
/// can't be read. The config file and the files its `FileTransform`s read are stamped (one
|
||||
/// `metadata` call each) on every load, and the config is read again once one of them moved or a
|
||||
/// missing one appeared (see [`crate::file_cache`]).
|
||||
pub fn load_config(src: &str) -> Result<Arc<Config>, String> {
|
||||
let t = src.trim();
|
||||
if t.is_empty() {
|
||||
return Err("no config file chosen".into());
|
||||
}
|
||||
static CACHE: OnceLock<ConfigCache> = OnceLock::new();
|
||||
let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
|
||||
if let Ok(m) = cache.lock()
|
||||
&& let Some(v) = m.get(src)
|
||||
{
|
||||
return v.clone();
|
||||
}
|
||||
let parsed = if src.contains('\n') {
|
||||
Config::parse(src, Path::new("."))
|
||||
} else {
|
||||
file_cache::cached(CONFIGS.get(), src, || {
|
||||
if src.contains('\n') {
|
||||
return (Config::parse(src, Path::new(".")).map(Arc::new), 0);
|
||||
}
|
||||
let p = Path::new(t);
|
||||
std::fs::read_to_string(p).map_err(|e| format!("{t}: {e}")).and_then(|text| Config::parse(&text, p.parent().unwrap_or(Path::new("."))))
|
||||
}
|
||||
.map(Arc::new);
|
||||
if let Ok(mut m) = cache.lock() {
|
||||
m.insert(src.to_string(), parsed.clone());
|
||||
}
|
||||
parsed
|
||||
match file_cache::read(t) {
|
||||
Ok(text) => (Config::parse(&text, p.parent().unwrap_or(Path::new("."))).map(Arc::new), text.len()),
|
||||
Err(e) => (Err(format!("{t}: {e}")), 0),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -410,3 +410,69 @@ fn builtin_styles() {
|
||||
fn rgb_white_xyz() -> [f64; 3] {
|
||||
[D65[0] / D65[1], 1.0, (1.0 - D65[0] - D65[1]) / D65[1]]
|
||||
}
|
||||
|
||||
/// #3538: a custom `.ocio` config, and the LUTs it reads, were read once per process.
|
||||
#[test]
|
||||
fn an_ocio_config_and_its_luts_are_read_again_once_they_change() {
|
||||
const BLACK: &str = "LUT_1D_SIZE 2\n0 0 0\n0 0 0\n";
|
||||
const WHITE: &str = "LUT_1D_SIZE 2\n1 1 1\n1 1 1\n";
|
||||
let dir = std::env::temp_dir().join(format!("ec-fx-3538-config-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::create_dir_all(dir.as_path().join("luts")).unwrap();
|
||||
let write = |name: &str, text: &str, secs: u64| {
|
||||
let path = dir.as_path().join(name);
|
||||
std::fs::write(&path, text).unwrap();
|
||||
let f = std::fs::File::options().write(true).open(&path).unwrap();
|
||||
f.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs)).unwrap();
|
||||
path.to_string_lossy().into_owned()
|
||||
};
|
||||
let config = |space: &str| {
|
||||
format!(
|
||||
"ocio_profile_version: 2\nsearch_path: luts\nroles:\n scene_linear: lin\ncolorspaces:\n - !<ColorSpace>\n name: lin\n - !<ColorSpace>\n name: {space}\n to_reference: !<FileTransform> {{src: grade.cube, interpolation: linear}}\n - !<ColorSpace>\n name: later\n to_reference: !<FileTransform> {{src: later.cube, interpolation: linear}}\n"
|
||||
)
|
||||
};
|
||||
let src = dir.as_path().join("config.ocio").to_string_lossy().into_owned();
|
||||
assert!(load_config(&src).is_err(), "no config yet");
|
||||
write("config.ocio", &config("graded"), 1_000);
|
||||
write("luts/grade.cube", BLACK, 1_000);
|
||||
let to_lin = |space: &str| {
|
||||
let c = load_config(&src).unwrap();
|
||||
c.convert(c.space(space).unwrap(), c.space("lin").unwrap(), [0.5; 3])
|
||||
};
|
||||
assert_eq!(to_lin("graded"), [0.0; 3], "a config created after a failed load");
|
||||
write("luts/grade.cube", WHITE, 2_000);
|
||||
assert_eq!(to_lin("graded"), [1.0; 3], "a LUT the config reads, edited");
|
||||
assert_eq!(to_lin("later"), [0.5; 3], "a LUT not there yet passes through");
|
||||
write("luts/later.cube", BLACK, 1_000);
|
||||
assert_eq!(to_lin("later"), [0.0; 3], "a LUT the config reads, created");
|
||||
write("config.ocio", &config("regraded"), 2_000);
|
||||
assert!(load_config(&src).unwrap().space("regraded").is_some(), "an edited config");
|
||||
std::fs::remove_file(&src).unwrap();
|
||||
assert!(load_config(&src).is_err(), "a deleted config");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// A LUT a cached config read that changes is noticed without a render loading the config (the
|
||||
/// interface's watcher), and the config's files name it (frame and layer cache keys hold them).
|
||||
#[test]
|
||||
fn check_lut_files_notices_a_configs_lut() {
|
||||
let dir = std::env::temp_dir().join(format!("ec-fx-3538-check-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let lut = dir.join("watched.cube");
|
||||
std::fs::write(&lut, "LUT_1D_SIZE 2\n0 0 0\n1 1 1\n").unwrap();
|
||||
let config = dir.join("config.ocio");
|
||||
std::fs::write(&config, "ocio_profile_version: 2\ncolorspaces:\n - !<ColorSpace>\n name: lin\n - !<ColorSpace>\n name: graded\n to_reference: !<FileTransform> {src: watched.cube}\n").unwrap();
|
||||
let src = config.to_string_lossy().into_owned();
|
||||
// A config not read yet is read: its files name its LUT from the first render on.
|
||||
let files = crate::lut_files(&src);
|
||||
assert!(files.contains(&lut.to_string_lossy().into_owned()), "{files:?}");
|
||||
assert!(load_config(&src).is_ok());
|
||||
let before = crate::check_lut_files();
|
||||
assert!(cached_files(&src).is_some(), "an unchanged config stays");
|
||||
std::fs::write(&lut, "LUT_1D_SIZE 3\n0 0 0\n0.5 0.5 0.5\n1 1 1\n").unwrap();
|
||||
assert!(crate::check_lut_files() > before);
|
||||
assert!(cached_files(&src).is_none(), "a config whose LUT changed is read again");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
@@ -394,7 +394,7 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
};
|
||||
let render = || popup(&["Full", "Outside", "Inside"]);
|
||||
let mut cyl = vec![
|
||||
p("radius", "Radius", num(100.0), slider(0.0, 400.0, 0.0, 200.0, 1)),
|
||||
p("radius", "Radius", num(100.0), slider(0.0, 30000.0, 0.0, 200.0, 1)),
|
||||
p("position", "Position", pt(0.5, 0.5), ParamUi::Point),
|
||||
p("rotation", "Rotation", num(0.0), ParamUi::Angle),
|
||||
p("rotationX", "Rotation X", num(0.0), ParamUi::Angle),
|
||||
@@ -565,6 +565,23 @@ mod tests {
|
||||
assert_eq!(out.get(50 + 18, 10)[3], 0.0);
|
||||
}
|
||||
|
||||
/// #3368: Radius takes the range After Effects' CC Cylinder has, up to 30000 %, and a cylinder
|
||||
/// that large still renders (nearly flat, fully opaque) instead of clamping to 400 %.
|
||||
#[test]
|
||||
fn cylinder_radius_goes_up_to_thirty_thousand_percent() {
|
||||
let spec = find("ec.perspective.cccylinder").unwrap();
|
||||
let radius = spec.params.iter().find(|p| p.id == "radius").unwrap();
|
||||
assert!(matches!(radius.ui, ParamUi::Slider { min, max, .. } if min == 0.0 && max == 30000.0), "{:?}", radius.ui);
|
||||
// The cylinder's axis on the left edge: at 400 % its radius is 400 / 100 * 100 / 2π ≈ 63.7 px,
|
||||
// so a pixel 70 px along is past it there and inside the 30000 % one.
|
||||
let img = Image::filled(100, 20, [0.5, 0.5, 0.5, 1.0]);
|
||||
let at_edge = |radius: f64| run("ec.perspective.cccylinder", &img, &[("radius", num(radius)), ("position", Value::Vec2([0.0, 10.0]))]).img;
|
||||
let clamped = at_edge(400.0).get(70, 10);
|
||||
assert_eq!(clamped[3], 0.0, "beyond a 400 % cylinder: {clamped:?}");
|
||||
let px = at_edge(30000.0).get(70, 10);
|
||||
assert!(px.iter().all(|v| v.is_finite()) && px[3] > 0.99, "{px:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cylinder_axis_rotations_affect_geometry_and_texture() {
|
||||
let img = Image::filled(100, 100, [0.5, 0.5, 0.5, 1.0]);
|
||||
|
||||
@@ -614,11 +614,72 @@ pub fn triangulate(outline: &[[f64; 2]], opts: &MeshOpts) -> Mesh {
|
||||
Mesh { verts, tris, outline: poly }
|
||||
}
|
||||
|
||||
/// The Expansion range the outline honours, in layer pixels (the property's slider range). A
|
||||
/// project file can store any number; outside this range it is clamped, so the padded grid
|
||||
/// below stays a few hundred pixels larger than the buffer.
|
||||
pub const EXPANSION_RANGE: (f64, f64) = (-100.0, 200.0);
|
||||
/// Most buffer pixels of padding the outline grid takes on each side (Expansion × buffer scale).
|
||||
const MAX_PAD: f64 = 4096.0;
|
||||
/// Most cells the padded outline grid may have (it is expanded with two `f64` distance buffers).
|
||||
const MAX_GRID_CELLS: usize = 1 << 25;
|
||||
/// Most the alpha is shrunk by, per axis, to make the padded grid fit; a layer needing more has no
|
||||
/// outline.
|
||||
const MAX_DOWNSAMPLE: usize = 8;
|
||||
|
||||
/// The outline of the alpha region under `seed` (buffer pixels → layer space via `scale` and
|
||||
/// `offset`), expanded by `expansion` layer pixels.
|
||||
/// `offset`), expanded by `expansion` layer pixels (clamped to [`EXPANSION_RANGE`]). When the
|
||||
/// padded grid would pass [`MAX_PAD`] or [`MAX_GRID_CELLS`] (a high-scale layer with a large
|
||||
/// Expansion), the alpha is traced at a coarser resolution instead of being skipped.
|
||||
pub fn outline(alpha: &[f32], w: usize, h: usize, scale: f64, offset: [f64; 2], seed: [f64; 2], expansion: f64) -> Vec<[f64; 2]> {
|
||||
// Pad so expansion can grow past the buffer edge.
|
||||
let pad = (expansion.max(0.0) * scale).ceil() as usize + 2;
|
||||
let expansion = if expansion.is_finite() { expansion.clamp(EXPANSION_RANGE.0, EXPANSION_RANGE.1) } else { 0.0 };
|
||||
if alpha.len() < w.saturating_mul(h) {
|
||||
return vec![];
|
||||
}
|
||||
let want = (expansion.max(0.0) * scale).ceil();
|
||||
if !want.is_finite() {
|
||||
return vec![];
|
||||
}
|
||||
for k in 1..=MAX_DOWNSAMPLE {
|
||||
let pad = (want / k as f64).ceil();
|
||||
let (kw, kh) = (w.div_ceil(k), h.div_ceil(k));
|
||||
let fits = pad <= MAX_PAD && {
|
||||
let pad = pad as usize + 2;
|
||||
kw.checked_add(2 * pad).zip(kh.checked_add(2 * pad)).and_then(|(pw, ph)| pw.checked_mul(ph)).is_some_and(|c| c <= MAX_GRID_CELLS)
|
||||
};
|
||||
if !fits {
|
||||
continue;
|
||||
}
|
||||
if k == 1 {
|
||||
return outline_padded(alpha, [w, h], scale, offset, seed, expansion, pad as usize + 2);
|
||||
}
|
||||
let small = shrink(alpha, w, h, k);
|
||||
let kf = k as f64;
|
||||
return outline_padded(&small, [kw, kh], scale / kf, [offset[0] / kf, offset[1] / kf], seed, expansion, pad as usize + 2);
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// `alpha` (`w × h`) with every `k × k` block averaged into one pixel.
|
||||
fn shrink(alpha: &[f32], w: usize, h: usize, k: usize) -> Vec<f32> {
|
||||
let (kw, kh) = (w.div_ceil(k), h.div_ceil(k));
|
||||
let mut out = vec![0.0f32; kw * kh];
|
||||
for y in 0..kh {
|
||||
for x in 0..kw {
|
||||
let (x1, y1) = (((x + 1) * k).min(w), ((y + 1) * k).min(h));
|
||||
let (mut sum, mut n) = (0.0f32, 0u32);
|
||||
for yy in y * k..y1 {
|
||||
for xx in x * k..x1 {
|
||||
sum += alpha[yy * w + xx];
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
out[y * kw + x] = sum / n.max(1) as f32;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn outline_padded(alpha: &[f32], [w, h]: [usize; 2], scale: f64, offset: [f64; 2], seed: [f64; 2], expansion: f64, pad: usize) -> Vec<[f64; 2]> {
|
||||
let (pw, ph) = (w + 2 * pad, h + 2 * pad);
|
||||
let mut on = vec![false; pw * ph];
|
||||
for y in 0..h {
|
||||
|
||||
@@ -106,6 +106,42 @@ fn expansion_grows_the_outline() {
|
||||
assert!((am - pi * 225.0).abs() < 60.0, "{am}");
|
||||
}
|
||||
|
||||
/// An imported Expansion far past the property's range is clamped instead of padding the grid
|
||||
/// by that many pixels (a 1e12 expansion would ask for terabytes).
|
||||
#[test]
|
||||
fn hostile_expansion_is_clamped() {
|
||||
let (w, h) = (40, 40);
|
||||
let alpha = disc_alpha(w, h, [20.0, 20.0], 10.0);
|
||||
let max = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], EXPANSION_RANGE.1)).abs();
|
||||
// A non-finite Expansion is taken as zero (the unexpanded outline), not clamped to the maximum.
|
||||
let zero = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], 0.0)).abs();
|
||||
for e in [1e12, f64::MAX, f64::INFINITY, f64::NAN] {
|
||||
let a = signed_area(&outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], e)).abs();
|
||||
let want = if e.is_finite() { max } else { zero };
|
||||
assert!((a - want).abs() < 1.0, "{e}: {a} vs {want}");
|
||||
}
|
||||
assert!(outline(&alpha, w, h, 1e9, [0.0; 2], [20.0, 20.0], 100.0).is_empty());
|
||||
let _ = outline(&alpha, w, h, 1.0, [0.0; 2], [20.0, 20.0], -1e12);
|
||||
}
|
||||
|
||||
/// A high-scale layer with a large Expansion is traced at a coarser resolution, not skipped, and a
|
||||
/// tiny buffer with a huge padded grid stays within the grid budget.
|
||||
#[test]
|
||||
fn high_scale_expansion_is_traced_coarser_instead_of_skipped() {
|
||||
let (w, h) = (40, 40);
|
||||
let alpha = disc_alpha(w, h, [20.0, 20.0], 10.0);
|
||||
// Scale 32, Expansion 130: 4160 px of padding, past MAX_PAD, so it is shrunk by 2.
|
||||
let scale = 32.0;
|
||||
let o = outline(&alpha, w, h, scale, [0.0; 2], [20.0, 20.0], 130.0);
|
||||
let area = signed_area(&o).abs();
|
||||
// The disc has radius 10 / 32 layer px; expanded by 130.
|
||||
let r = 10.0 / scale + 130.0;
|
||||
assert!((area - std::f64::consts::PI * r * r).abs() < 0.03 * std::f64::consts::PI * r * r, "{area}");
|
||||
// One pixel at scale 20.48 and Expansion 1e12 (clamped): its 8197 px square grid is over budget.
|
||||
let o = outline(&[1.0], 1, 1, 20.48, [0.0; 2], [0.5, 0.5], 1e12);
|
||||
assert!(o.len() >= 3);
|
||||
}
|
||||
|
||||
fn rect_mesh(w: f64, h: f64, tris: f64) -> Mesh {
|
||||
triangulate(&[[0.0, 0.0], [w, 0.0], [w, h], [0.0, h]], &MeshOpts { triangles: tris, density: 50.0, expansion: 0.0 })
|
||||
}
|
||||
|
||||
@@ -378,10 +378,22 @@ pub fn purge() {
|
||||
c.bytes = 0;
|
||||
}
|
||||
|
||||
/// The pixel size of the layer-space grid at `scale` (`round(size · scale)`, at least 1).
|
||||
fn grid_size(layer_size: [f64; 2], scale: f64) -> (u32, u32) {
|
||||
(((layer_size[0] * scale).round() as u32).max(1), ((layer_size[1] * scale).round() as u32).max(1))
|
||||
}
|
||||
|
||||
/// The full-resolution layer grid frozen mattes are stored at. A frozen cache claiming any
|
||||
/// other size did not come from freezing this layer and is not decoded.
|
||||
pub fn frozen_grid(layer_size: [f64; 2]) -> [usize; 2] {
|
||||
let (w, h) = grid_size(layer_size, 1.0);
|
||||
[w as usize, h as usize]
|
||||
}
|
||||
|
||||
/// The layer-space grid (`round(size · scale)` pixels) of a buffer.
|
||||
pub fn layer_grid(buf: &Buf, layer_size: [f64; 2]) -> Image {
|
||||
let s = if buf.scale > 0.0 { buf.scale } else { 1.0 };
|
||||
let (w, h) = (((layer_size[0] * s).round() as u32).max(1), ((layer_size[1] * s).round() as u32).max(1));
|
||||
let (w, h) = grid_size(layer_size, s);
|
||||
let (ox, oy) = (buf.offset[0].round() as i64, buf.offset[1].round() as i64);
|
||||
let mut img = Image::new(w, h);
|
||||
img.rows_mut().for_each(|(y, row)| {
|
||||
@@ -1212,8 +1224,11 @@ pub fn freeze_frame(alpha: &[f32], band: &[u8]) -> FrozenFrame {
|
||||
fn matte_for(ctx: &EffectCtx, f: i64, img: &Arc<Image>, scale: f64) -> Option<(Vec<f32>, Vec<u8>, f64)> {
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
// Frozen mattes.
|
||||
// Only the layer's own full-resolution grid: a project cannot make a tiny layer decode (and
|
||||
// convert to floats) planes as large as the RLE allows. Any other size falls through to a computed matte.
|
||||
if let Some(fc) = frozen(ctx.params)
|
||||
&& frozen_applies(&fc, ctx.params, ctx.layer_size, ctx.fps())
|
||||
&& fc.size == frozen_grid(ctx.layer_size)
|
||||
&& let Some(fr) = fc.frames.get(&f)
|
||||
{
|
||||
let [fw, fh] = fc.size;
|
||||
@@ -1409,6 +1424,33 @@ mod tests {
|
||||
params
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mismatched_frozen_size_falls_back_to_a_computed_matte() {
|
||||
let mut d = RotoData::default();
|
||||
d.add(Stroke { kind: StrokeKind::Fg, frame: 0, radius: 3.0, points: vec![[35.0, 40.0], [45.0, 40.0]], object: 0 }, 20, [0, 24]);
|
||||
let mut params = params_with(&d);
|
||||
let fc =
|
||||
FrozenCache { key: frozen_key(¶ms, [100.0, 80.0], 25.0), size: [10, 8], frames: BTreeMap::from([(5, freeze_frame(&[1.0; 80], &[0; 80]))]) };
|
||||
let host = Seq;
|
||||
let render = |params: &Params| {
|
||||
let ctx = EffectCtx {
|
||||
params,
|
||||
time: 5.0 / 25.0 + 0.001,
|
||||
layer_size: [100.0, 80.0],
|
||||
seed: 1,
|
||||
adjustment: false,
|
||||
env: EffectEnv { host: Some(&host), frame_rate: 25.0, ..Default::default() },
|
||||
};
|
||||
crate::apply(&specs()[0], &ctx, Buf { img: frame(5.0 / 25.0 + 0.001), offset: [0.0; 2], scale: 1.0 }).img
|
||||
};
|
||||
let computed = render(¶ms);
|
||||
params.values.insert(FROZEN.into(), Value::Str(fc.to_json()));
|
||||
let out = render(¶ms);
|
||||
assert!(out.data[(40 * 100 + 50) as usize][3] > 0.5, "the disk is missing from the output");
|
||||
// The cache's all-opaque matte would leave the background opaque too; the fallback must match a render with no cache.
|
||||
assert!(out.data == computed.data, "the mismatched frozen cache was used");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renders_the_segmented_disk_on_demand() {
|
||||
let mut d = RotoData::default();
|
||||
@@ -1980,6 +2022,18 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Frozen mattes are only decoded at the size the freeze pass stores: the layer grid at
|
||||
/// full resolution.
|
||||
#[test]
|
||||
fn frozen_grid_is_the_full_resolution_layer_grid() {
|
||||
for size in [[100.0, 80.0], [99.6, 0.2], [1.0, 1.0]] {
|
||||
let buf = Buf { img: Image::new(3, 3), offset: [0.0; 2], scale: 1.0 };
|
||||
let g = layer_grid(&buf, size);
|
||||
assert_eq!(frozen_grid(size), [g.width as usize, g.height as usize]);
|
||||
}
|
||||
assert_eq!(frozen_grid([100.0, 80.0]), [100, 80]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_tracking_decoders_validate_dimensions_and_plane_lengths() {
|
||||
let valid = rle::encode(&[255, 0, 255, 0]);
|
||||
|
||||
@@ -8,9 +8,11 @@ use super::{Buf, EffectCtx, Image, IntoParallelRefMutIterator, ParallelIterator,
|
||||
/// Steps per second of the stepped particle simulations.
|
||||
pub(crate) const SPS: f64 = 60.0;
|
||||
|
||||
/// Number of whole steps reached at layer time `t`.
|
||||
/// Number of whole steps reached at layer time `t` (at most [`crate::util::MAX_SIM_STEPS`]: the
|
||||
/// per-step histories built from it, such as [`rate_history`], stay bounded however late a
|
||||
/// project places the layer's time).
|
||||
pub(crate) fn steps_at(t: f64) -> u64 {
|
||||
if t <= 0.0 { 0 } else { (t * SPS + 1e-6).floor() as u64 }
|
||||
if t <= 0.0 { 0 } else { ((t * SPS + 1e-6).floor() as u64).min(crate::util::MAX_SIM_STEPS) }
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
||||
@@ -612,7 +612,7 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
"ec.stylize.scatter",
|
||||
"Scatter",
|
||||
vec![
|
||||
p("amount", "Scatter Amount", num(0.0), slider(0.0, 127.0, 0.0, 127.0, 1)),
|
||||
p("amount", "Scatter Amount", num(0.0), slider(0.0, 1000.0, 0.0, 127.0, 1)),
|
||||
p("grain", "Grain", Value::Enum(0), popup(&["Both", "Horizontal", "Vertical"])),
|
||||
p("randomizeEveryFrame", "Scatter Randomness", Value::Bool(false), ParamUi::Checkbox),
|
||||
],
|
||||
|
||||
@@ -565,7 +565,8 @@ impl PixelMotion {
|
||||
let px = |id: &str| (g(id, 0.0).max(0.0) * scale).round() as u32;
|
||||
PixelMotion {
|
||||
tile,
|
||||
window: ((g("tuning/blockSize", 8.0).max(4.0)) * scale).round().max(4.0) as u32,
|
||||
// Block Size is declared 4..64; a project can store any number.
|
||||
window: ((g("tuning/blockSize", 8.0).clamp(4.0, 64.0)) * scale).round().max(4.0) as u32,
|
||||
global_smoothness: g("tuning/smoothing/globalSmoothness", 0.5).clamp(0.0, 1.0) as f32,
|
||||
local_smoothness: g("tuning/smoothing/localSmoothness", 0.5).clamp(0.0, 1.0) as f32,
|
||||
iterations: g("tuning/smoothing/smoothingIterations", 20.0).clamp(0.0, 200.0) as u32,
|
||||
|
||||
+130
-21
@@ -115,18 +115,34 @@ fn box_row(src: &[f32], dst: &mut [f32], r: usize) {
|
||||
dst.copy_from_slice(src);
|
||||
return;
|
||||
}
|
||||
let get = |i: isize| src[i.clamp(0, n as isize - 1) as usize];
|
||||
let norm = 1.0 / (2 * r + 1) as f32;
|
||||
let mut acc = 0.0f64;
|
||||
for i in -(r as isize)..=(r as isize) {
|
||||
acc += get(i) as f64;
|
||||
// Past the row the window only adds more copies of the edge pixels; the radius is clamped
|
||||
// so the index arithmetic below cannot overflow (the means are unchanged in practice).
|
||||
let r = r.min(MAX_BOX_RADIUS);
|
||||
let (last, ri) = (n - 1, r as i64);
|
||||
let get = |i: i64| src[i.clamp(0, last as i64) as usize];
|
||||
let norm = 1.0 / (2.0 * r as f64 + 1.0);
|
||||
// The first window, -r..=r: the edge pixels it repeats are counted, not visited, so the
|
||||
// work depends on the row length and not on the radius.
|
||||
let inside = r.min(last);
|
||||
let mut acc = (r as f64 + 1.0) * src[0] as f64 + src[1..=inside].iter().map(|v| *v as f64).sum::<f64>();
|
||||
// Only when the window actually repeats the last pixel: 0 * inf would be NaN otherwise.
|
||||
if r > inside {
|
||||
acc += (r - inside) as f64 * src[last] as f64;
|
||||
}
|
||||
for x in 0..n {
|
||||
dst[x] = acc as f32 * norm;
|
||||
acc += get(x as isize + r as isize + 1) as f64 - get(x as isize - r as isize) as f64;
|
||||
dst[x] = (acc * norm) as f32;
|
||||
let xi = x as i64;
|
||||
acc += get(xi + ri + 1) as f64 - get(xi - ri) as f64;
|
||||
}
|
||||
}
|
||||
|
||||
/// Largest box radius [`box_row`] uses (larger radii come from absurd blur amounts in a
|
||||
/// project, such as a Mocha shape's feather).
|
||||
const MAX_BOX_RADIUS: usize = 1 << 30;
|
||||
|
||||
/// Largest sigma [`box_radii`] spreads into boxes (see [`MAX_BOX_RADIUS`]).
|
||||
const MAX_BOX_SIGMA: f64 = 1e8;
|
||||
|
||||
/// Separable box mean with radii `rx`, `ry` (edges repeated).
|
||||
pub fn box_plane(p: &Plane, rx: usize, ry: usize) -> Plane {
|
||||
let mut cur = if rx > 0 { p.map_rows(|s, d| box_row(s, d, rx)) } else { p.clone() };
|
||||
@@ -138,9 +154,10 @@ pub fn box_plane(p: &Plane, rx: usize, ry: usize) -> Plane {
|
||||
|
||||
/// Box radii for `n` passes approximating a Gaussian of `sigma`.
|
||||
pub fn box_radii(sigma: f64, n: usize) -> Vec<usize> {
|
||||
if sigma <= 0.0 {
|
||||
if sigma.is_nan() || sigma <= 0.0 || n == 0 {
|
||||
return vec![0; n];
|
||||
}
|
||||
let sigma = sigma.min(MAX_BOX_SIGMA);
|
||||
let w_ideal = (12.0 * sigma * sigma / n as f64 + 1.0).sqrt();
|
||||
let mut wl = w_ideal.floor() as i64;
|
||||
if wl % 2 == 0 {
|
||||
@@ -545,13 +562,22 @@ struct SimEntry<S> {
|
||||
/// Checkpoint every this many steps.
|
||||
pub const SIM_CHECKPOINT: u64 = 15;
|
||||
|
||||
/// Most steps a simulation runs: three hours of layer time at 60 steps per second. The layer
|
||||
/// time comes from the project (start time, stretch), so without a limit a tiny file could ask
|
||||
/// for years of steps; past this the state stays where it is.
|
||||
pub const MAX_SIM_STEPS: u64 = 60 * 60 * 60 * 3;
|
||||
|
||||
/// Most checkpoints kept per simulation (and made by one run, which spaces them further apart
|
||||
/// as it goes).
|
||||
const MAX_SIM_CHECKPOINTS: usize = 256;
|
||||
|
||||
impl<S: Clone> SimCache<S> {
|
||||
pub const fn new(cap: usize) -> SimCache<S> {
|
||||
SimCache { inner: std::sync::Mutex::new(Vec::new()), cap }
|
||||
}
|
||||
|
||||
/// State after `target` steps. `init` builds step 0; `step(state, i)` advances from step
|
||||
/// `i` to `i + 1`.
|
||||
/// State after `target` steps (at most [`MAX_SIM_STEPS`]). `init` builds step 0;
|
||||
/// `step(state, i)` advances from step `i` to `i + 1`.
|
||||
pub fn run(&self, key: u64, target: u64, init: impl FnOnce() -> S, step: impl FnMut(&mut S, u64)) -> std::sync::Arc<S> {
|
||||
self.run_where(key, target, init, step, |_, _| true)
|
||||
}
|
||||
@@ -569,6 +595,7 @@ impl<S: Clone> SimCache<S> {
|
||||
valid: impl Fn(u64, &S) -> bool,
|
||||
) -> std::sync::Arc<S> {
|
||||
use std::sync::Arc;
|
||||
let target = target.min(MAX_SIM_STEPS);
|
||||
let start = {
|
||||
let mut g = self.inner.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let tick = g.iter().map(|e| e.used).max().unwrap_or(0) + 1;
|
||||
@@ -587,11 +614,17 @@ impl<S: Clone> SimCache<S> {
|
||||
if !had_start {
|
||||
fresh.push((0, Arc::new(st.clone())));
|
||||
}
|
||||
let mut every = SIM_CHECKPOINT;
|
||||
while i < target {
|
||||
step(&mut st, i);
|
||||
i += 1;
|
||||
if i % SIM_CHECKPOINT == 0 && i != target {
|
||||
if i % every == 0 && i != target {
|
||||
fresh.push((i, Arc::new(st.clone())));
|
||||
// A long run keeps a bounded number of states: spread them twice as far apart.
|
||||
if fresh.len() > MAX_SIM_CHECKPOINTS {
|
||||
every = every.saturating_mul(2);
|
||||
fresh.retain(|(s, _)| s % every == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
let out = Arc::new(st);
|
||||
@@ -611,19 +644,23 @@ impl<S: Clone> SimCache<S> {
|
||||
};
|
||||
let e = &mut g[idx];
|
||||
e.used = tick;
|
||||
for (s, st) in fresh {
|
||||
e.checkpoints.retain(|(c, _)| *c != s);
|
||||
e.checkpoints.push((s, st));
|
||||
}
|
||||
// Keep memory bounded: drop unaligned (exact-target) checkpoints first, then thin.
|
||||
if e.checkpoints.len() > 256 {
|
||||
e.checkpoints.retain(|(s, _)| s % SIM_CHECKPOINT == 0);
|
||||
// Fresh checkpoints replace those at the same step (one pass, not one per checkpoint).
|
||||
let steps: std::collections::HashSet<u64> = fresh.iter().map(|(s, _)| *s).collect();
|
||||
e.checkpoints.retain(|(c, _)| !steps.contains(c));
|
||||
e.checkpoints.extend(fresh);
|
||||
// Keep memory bounded: drop unaligned (exact-target) checkpoints first, then thin. The
|
||||
// state just asked for keeps its slot, so asking for it again is answered from the cache.
|
||||
if e.checkpoints.len() > MAX_SIM_CHECKPOINTS {
|
||||
e.checkpoints.retain(|(s, _)| s % SIM_CHECKPOINT == 0 && *s != target);
|
||||
e.checkpoints.sort_by_key(|(s, _)| *s);
|
||||
let mut k = 0;
|
||||
// Drop only the excess, evenly spaced, so the rest keep their seek distance.
|
||||
let (len, excess) = (e.checkpoints.len(), (e.checkpoints.len() + 1).saturating_sub(MAX_SIM_CHECKPOINTS));
|
||||
let mut i = 0;
|
||||
e.checkpoints.retain(|_| {
|
||||
k += 1;
|
||||
k % 2 == 1
|
||||
i += 1;
|
||||
(i * excess) / len == ((i - 1) * excess) / len
|
||||
});
|
||||
e.checkpoints.push((target, out.clone()));
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -801,6 +838,45 @@ mod tests {
|
||||
assert_eq!(d, e);
|
||||
}
|
||||
|
||||
/// Layer time from a project cannot run a simulation without end, and a long run keeps a
|
||||
/// bounded number of checkpoints that still give the same answer when seeking.
|
||||
#[test]
|
||||
fn sim_cache_bounds_steps_and_checkpoints() {
|
||||
let c: SimCache<u64> = SimCache::new(2);
|
||||
let count = |s: &mut u64, _: u64| *s += 1;
|
||||
assert_eq!(*c.run(1, u64::MAX, || 0, count), MAX_SIM_STEPS);
|
||||
let n = c.inner.lock().map(|g| g.iter().map(|e| e.checkpoints.len()).max().unwrap_or(0)).unwrap_or(0);
|
||||
assert!(n <= MAX_SIM_CHECKPOINTS + 1, "{n} checkpoints");
|
||||
let fresh: SimCache<u64> = SimCache::new(2);
|
||||
let step = |s: &mut u64, i: u64| *s = s.wrapping_mul(6364136223846793005).wrapping_add(i);
|
||||
let far = *fresh.run(1, 100_000, || 7, step);
|
||||
let mid = *fresh.run(1, 54_321, || 7, step);
|
||||
let again: SimCache<u64> = SimCache::new(2);
|
||||
assert_eq!(*again.run(1, 54_321, || 7, step), mid);
|
||||
assert_eq!(*again.run(1, 100_000, || 7, step), far);
|
||||
}
|
||||
|
||||
/// A request that leaves the cache over its checkpoint cap still finds its own state there.
|
||||
#[test]
|
||||
fn sim_cache_keeps_the_requested_state_when_trimming() {
|
||||
let c: SimCache<u64> = SimCache::new(2);
|
||||
let steps = std::cell::Cell::new(0u64);
|
||||
let count = |s: &mut u64, _: u64| {
|
||||
steps.set(steps.get() + 1);
|
||||
*s += 1;
|
||||
};
|
||||
let target = SIM_CHECKPOINT * (MAX_SIM_CHECKPOINTS as u64 - 1) + SIM_CHECKPOINT - 1;
|
||||
assert_eq!(*c.run(1, target, || 0, count), target);
|
||||
let n = c.inner.lock().map(|g| g.iter().map(|e| e.checkpoints.len()).max().unwrap_or(0)).unwrap_or(0);
|
||||
assert!(n <= MAX_SIM_CHECKPOINTS, "{n} checkpoints");
|
||||
steps.set(0);
|
||||
assert_eq!(*c.run(1, target, || 0, count), target);
|
||||
assert_eq!(steps.get(), 0, "the second request is answered from the cache");
|
||||
// Only the excess is dropped: the other aligned checkpoints stay.
|
||||
let kept = c.inner.lock().map(|g| g[0].checkpoints.len()).unwrap_or(0);
|
||||
assert_eq!(kept, MAX_SIM_CHECKPOINTS, "trimmed to the cap, not halved");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn poly_helpers() {
|
||||
let sq = [[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 10.0]];
|
||||
@@ -841,4 +917,37 @@ mod tests {
|
||||
let o = guided_filter(&g, &p, 2, 1e-3);
|
||||
assert!(o.data.iter().all(|v| (v - 0.7).abs() < 1e-3));
|
||||
}
|
||||
|
||||
/// The box mean counts repeated edge pixels instead of visiting them: the same means as a
|
||||
/// direct sum, and a huge radius (a Mocha feather of 1e300) costs a row's worth of work.
|
||||
#[test]
|
||||
fn box_row_matches_a_direct_sum_and_bounds_the_radius() {
|
||||
let src = [1.0f32, 2.0, 3.0, 5.0, 8.0];
|
||||
for r in [0usize, 1, 2, 4, 5, 9, 40] {
|
||||
let mut dst = [0.0f32; 5];
|
||||
box_row(&src, &mut dst, r);
|
||||
for (x, d) in dst.iter().enumerate() {
|
||||
let ri = r as i64;
|
||||
let sum: f64 = (x as i64 - ri..=x as i64 + ri).map(|i| src[i.clamp(0, 4) as usize] as f64).sum();
|
||||
let want = (sum / (2 * r + 1) as f64) as f32;
|
||||
assert!((d - want).abs() < 1e-5, "r={r} x={x}: {d} vs {want}");
|
||||
}
|
||||
}
|
||||
// An infinite last pixel inside the window still yields a finite first mean.
|
||||
let mut inf = [0.0f32; 3];
|
||||
box_row(&[1.0, 2.0, f32::INFINITY], &mut inf, 1);
|
||||
assert!((inf[0] - 4.0 / 3.0).abs() < 1e-6, "{inf:?}");
|
||||
let mut one = [0.0f32];
|
||||
box_row(&[0.25], &mut one, usize::MAX);
|
||||
assert!((one[0] - 0.25).abs() < 1e-6);
|
||||
let mut dst = [0.0f32; 5];
|
||||
box_row(&src, &mut dst, usize::MAX);
|
||||
assert!(dst.iter().all(|v| v.is_finite() && (1.0..=8.0).contains(v)), "{dst:?}");
|
||||
for sigma in [1e300, f64::MAX, f64::INFINITY, f64::NAN, -1.0] {
|
||||
assert_eq!(box_radii(sigma, 3).len(), 3);
|
||||
}
|
||||
let p = Plane { w: 4, h: 3, data: vec![0.5; 12] };
|
||||
let g = gauss_plane(&p, 1e300, 1e300);
|
||||
assert!(g.data.iter().all(|v| (v - 0.5).abs() < 1e-4), "{:?}", g.data);
|
||||
}
|
||||
}
|
||||
|
||||
+170
-17
@@ -1,9 +1,9 @@
|
||||
//! Utility effects: bounds growth, HDR inspection/compression, Cineon log conversion and colour
|
||||
//! lookup tables (.cube).
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::file_cache::{self, Registered};
|
||||
use effectcraft_color::luminance;
|
||||
use effectcraft_keyframe::Value;
|
||||
use effectcraft_project::ParamUi;
|
||||
@@ -330,26 +330,71 @@ impl Lut {
|
||||
}
|
||||
}
|
||||
|
||||
type LutCache = Mutex<HashMap<String, Option<Arc<Lut>>>>;
|
||||
/// At most this many parsed LUTs (files and inline texts) are kept, and at most
|
||||
/// [`MAX_CACHED_BYTES`] of them; the least recently used go first.
|
||||
const MAX_CACHED_LUTS: usize = 64;
|
||||
const MAX_CACHED_BYTES: usize = 512 << 20;
|
||||
|
||||
/// Load (and cache) a LUT from inline `.cube` text or a file path.
|
||||
/// [`load_lut`]'s cache, by parameter text.
|
||||
static LUTS: Registered<Option<Arc<Lut>>> = Registered::new(MAX_CACHED_LUTS, MAX_CACHED_BYTES);
|
||||
|
||||
/// The parameters (paths in the effect's group) of `effect` that name a file an effect reads
|
||||
/// through a cache that follows it ([`load_lut`], the OCIO file transforms and configs).
|
||||
pub fn lut_params(effect: &str) -> &'static [&'static str] {
|
||||
match effect {
|
||||
"ec.utility.applylut" => &["lut"],
|
||||
"ec.color.lumetri" => &["basicCorrection/inputLutFile", "creative/lookFile"],
|
||||
"ec.color.ociofile" => &["file"],
|
||||
"ec.color.ociocolorspace" | "ec.color.ociodisplay" => &["configFile"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
|
||||
/// The file a LUT parameter names; `None` for inline `.cube` text or an empty parameter.
|
||||
pub fn lut_file(src: &str) -> Option<&str> {
|
||||
// `.cube` text always spans lines (a size header, then data rows); a path never does, even one
|
||||
// whose file name contains `LUT_` (#3454). Trimmed, so a trailing newline is still a path.
|
||||
let path = src.trim();
|
||||
(!path.is_empty() && !path.contains('\n')).then_some(path)
|
||||
}
|
||||
|
||||
/// The files a [`lut_params`] parameter set to `src` reads: the ones its cached result was read
|
||||
/// from (an OCIO config's also the LUTs it names, and the places it looked for missing ones; an
|
||||
/// `.ocio` config not read yet is read), else the file it names.
|
||||
pub fn lut_files(src: &str) -> Vec<String> {
|
||||
// A config not read yet is read now: the LUTs it names are its files from the first render.
|
||||
if crate::ocio_config::cached_files(src).is_none()
|
||||
&& lut_file(src).is_some_and(|p| std::path::Path::new(p).extension().is_some_and(|e| e.eq_ignore_ascii_case("ocio")))
|
||||
{
|
||||
let _ = crate::ocio_config::load_config(src);
|
||||
}
|
||||
crate::ocio_config::cached_files(src)
|
||||
.or_else(|| crate::ocio::cached_files(src))
|
||||
.or_else(|| file_cache::lock(LUTS.get()).files(src))
|
||||
.unwrap_or_else(|| lut_file(src).map(str::to_string).into_iter().collect())
|
||||
}
|
||||
|
||||
/// Load (and cache) a LUT from inline `.cube` text or a file path. A file is stamped (one
|
||||
/// `metadata` call) on every load and read again once its size or modification time moved (see
|
||||
/// [`crate::file_cache`]); inline text is its own content.
|
||||
pub fn load_lut(src: &str) -> Option<Arc<Lut>> {
|
||||
if src.trim().is_empty() {
|
||||
return None;
|
||||
}
|
||||
static CACHE: OnceLock<LutCache> = OnceLock::new();
|
||||
let cache = CACHE.get_or_init(|| Mutex::new(HashMap::new()));
|
||||
if let Ok(m) = cache.lock()
|
||||
&& let Some(v) = m.get(src)
|
||||
{
|
||||
return v.clone();
|
||||
}
|
||||
let inline = src.contains('\n') || src.contains("LUT_");
|
||||
let parsed = if inline { parse_cube(src) } else { std::fs::read_to_string(src.trim()).ok().and_then(|t| parse_cube(&t)) }.map(Arc::new);
|
||||
if let Ok(mut m) = cache.lock() {
|
||||
m.insert(src.to_string(), parsed.clone());
|
||||
}
|
||||
parsed
|
||||
file_cache::cached(LUTS.get(), src, || {
|
||||
let lut = match lut_file(src) {
|
||||
Some(path) => file_cache::read(path).ok().and_then(|t| parse_cube(&t)),
|
||||
None => parse_cube(src),
|
||||
}
|
||||
.map(Arc::new);
|
||||
let bytes = lut.as_ref().map_or(0, |l| lut_bytes(l));
|
||||
(lut, bytes)
|
||||
})
|
||||
}
|
||||
|
||||
/// The size of a LUT's tables.
|
||||
pub(crate) fn lut_bytes(l: &Lut) -> usize {
|
||||
l.d1.len().saturating_add(l.d3.len()).saturating_mul(std::mem::size_of::<[f32; 3]>())
|
||||
}
|
||||
|
||||
fn apply_lut(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
@@ -484,6 +529,114 @@ mod tests {
|
||||
assert_eq!(out.img, img);
|
||||
}
|
||||
|
||||
/// #3454: a path containing `LUT_` was taken for `.cube` text and the file never loaded.
|
||||
#[test]
|
||||
fn a_path_whose_name_contains_lut_loads_the_file() {
|
||||
let dir = std::env::temp_dir().join(format!("ec-fx-3454-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("LUT_x.cube");
|
||||
std::fs::write(&path, identity_cube(2)).unwrap();
|
||||
let path = path.to_string_lossy().into_owned();
|
||||
assert!(load_lut(&path).is_some(), "{path}");
|
||||
assert!(load_lut(&format!("{path}\r\n")).is_some(), "{path} with a trailing newline");
|
||||
assert!(load_lut(&identity_cube(2)).is_some(), "inline text");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// #3493: a LUT file was read once per process; an edited or newly created file never loaded.
|
||||
#[test]
|
||||
fn a_lut_file_is_read_again_once_it_changes() {
|
||||
const BLACK: &str = "LUT_1D_SIZE 2\n0 0 0\n0 0 0\n";
|
||||
const WHITE: &str = "LUT_1D_SIZE 2\n1 1 1\n1 1 1\n";
|
||||
let dir = std::env::temp_dir().join(format!("ec-fx-3493-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let write = |name: &str, text: &str, secs: u64| {
|
||||
let path = dir.join(name);
|
||||
std::fs::write(&path, text).unwrap();
|
||||
let f = std::fs::File::options().write(true).open(&path).unwrap();
|
||||
f.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs)).unwrap();
|
||||
path.to_string_lossy().into_owned()
|
||||
};
|
||||
let at = |src: &str| load_lut(src).map(|l| l.apply([0.5; 3]));
|
||||
let edited = write("edited.cube", BLACK, 1_000);
|
||||
assert_eq!(at(&edited), Some([0.0; 3]));
|
||||
write("edited.cube", WHITE, 2_000);
|
||||
assert_eq!(at(&edited), Some([1.0; 3]), "an edited file, same size");
|
||||
let created = dir.join("created.cube").to_string_lossy().into_owned();
|
||||
assert_eq!(at(&created), None);
|
||||
write("created.cube", WHITE, 1_000);
|
||||
assert_eq!(at(&created), Some([1.0; 3]), "a file created after a failed load");
|
||||
let repaired = write("repaired.cube", "not a lut", 1_000);
|
||||
assert_eq!(at(&repaired), None);
|
||||
write("repaired.cube", WHITE, 2_000);
|
||||
assert_eq!(at(&repaired), Some([1.0; 3]), "a repaired file");
|
||||
std::fs::remove_file(&repaired).unwrap();
|
||||
assert_eq!(at(&repaired), None, "a deleted file");
|
||||
// Inline text is its own content: the same text is the same LUT.
|
||||
let inline = load_lut(BLACK).unwrap();
|
||||
assert!(Arc::ptr_eq(&inline, &load_lut(BLACK).unwrap()));
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// A file that changed is noticed without a render loading it, and the generation that tells
|
||||
/// frames made before it apart moves.
|
||||
#[test]
|
||||
fn check_lut_files_forgets_a_changed_file() {
|
||||
let dir = std::env::temp_dir().join(format!("ec-fx-3493-check-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("grade.cube");
|
||||
std::fs::write(&path, identity_cube(2)).unwrap();
|
||||
let src = path.to_string_lossy().into_owned();
|
||||
assert!(load_lut(&src).is_some());
|
||||
let before = crate::check_lut_files();
|
||||
assert!(file_cache::lock(LUTS.get()).contains(&src), "an unchanged file stays");
|
||||
std::fs::write(&path, identity_cube(3)).unwrap();
|
||||
assert!(crate::check_lut_files() > before);
|
||||
assert!(!file_cache::lock(LUTS.get()).contains(&src), "a changed file is read again");
|
||||
assert_eq!(load_lut(&src).map(|l| l.n3), Some(3));
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
/// The cache keeps at most [`MAX_CACHED_LUTS`] LUTs, the least recently used going first.
|
||||
#[test]
|
||||
fn the_lut_cache_is_bounded() {
|
||||
// (text no other test loads: a load elsewhere would make it recently used)
|
||||
let oldest = format!("TITLE \"bounded first\"\n{}", identity_cube(2));
|
||||
let first = load_lut(&oldest).unwrap();
|
||||
for i in 0..MAX_CACHED_LUTS + 8 {
|
||||
let text = format!("TITLE \"bounded {i}\"\n{}", identity_cube(2));
|
||||
assert!(load_lut(&text).is_some());
|
||||
assert!(file_cache::lock(LUTS.get()).len() <= MAX_CACHED_LUTS);
|
||||
}
|
||||
assert!(!Arc::ptr_eq(&first, &load_lut(&oldest).unwrap()), "the oldest was let go");
|
||||
let (bytes, sum, max) = file_cache::lock(LUTS.get()).bytes();
|
||||
assert_eq!(bytes, sum, "the bytes kept add up");
|
||||
assert!(bytes <= max);
|
||||
}
|
||||
|
||||
/// A LUT file whose LUT left the cache is still followed: its frames are dropped when it
|
||||
/// changes, though no render loads it again first.
|
||||
#[test]
|
||||
fn a_file_whose_lut_left_the_cache_is_still_followed() {
|
||||
let dir = std::env::temp_dir().join(format!("ec-fx-3493-evicted-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("old.cube");
|
||||
std::fs::write(&path, identity_cube(2)).unwrap();
|
||||
let src = path.to_string_lossy().into_owned();
|
||||
assert!(load_lut(&src).is_some());
|
||||
for i in 0..MAX_CACHED_LUTS + 8 {
|
||||
assert!(load_lut(&format!("TITLE \"evict {i}\"\n{}", identity_cube(2))).is_some());
|
||||
}
|
||||
assert!(!file_cache::lock(LUTS.get()).contains(&src), "its LUT left the cache");
|
||||
std::fs::write(&path, identity_cube(3)).unwrap();
|
||||
crate::check_lut_files();
|
||||
assert_eq!(file_cache::followed_stamp(&src), Some(file_cache::file_stamp(&src)), "the change was noticed");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_d_lut_with_domain() {
|
||||
let lut = parse_cube("LUT_1D_SIZE 2\nDOMAIN_MIN 0 0 0\nDOMAIN_MAX 2 2 2\n1 1 1\n0 0 0\n").unwrap();
|
||||
|
||||
@@ -413,46 +413,46 @@ fn plan_effects_golden_hashes() {
|
||||
const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.obsolete.lightning/0/8bpc/t0.4", 0xd829bb3a79ec2d61),
|
||||
("ec.obsolete.lightning/0/8bpc/t1.3", 0xb2221364c0defe9b),
|
||||
("ec.obsolete.lightning/0/32bpc/t0.4", 0x828cc4e53d579af7),
|
||||
("ec.obsolete.lightning/0/32bpc/t1.3", 0x15770238cde9e766),
|
||||
("ec.obsolete.lightning/0/32bpc/half/t0.4", 0xf64f2b87fa9a09d3),
|
||||
("ec.obsolete.lightning/0/32bpc/half/t1.3", 0x625a00bd52fde5aa),
|
||||
("ec.obsolete.lightning/0/32bpc/t0.4", 0x38c8b3ca5d03eb81),
|
||||
("ec.obsolete.lightning/0/32bpc/t1.3", 0x552241c6035fa660),
|
||||
("ec.obsolete.lightning/0/32bpc/half/t0.4", 0x6158941311ce44d9),
|
||||
("ec.obsolete.lightning/0/32bpc/half/t1.3", 0xc7c2a804de6140a3),
|
||||
("ec.obsolete.lightning/1/8bpc/t0.4", 0xa7c8cb94beebca84),
|
||||
("ec.obsolete.lightning/1/8bpc/t1.3", 0x6e583b1d1f455099),
|
||||
("ec.obsolete.lightning/1/32bpc/t0.4", 0x2ffd973cc7de98f7),
|
||||
("ec.obsolete.lightning/1/32bpc/t1.3", 0x8f35e6018cac57d7),
|
||||
("ec.obsolete.lightning/1/32bpc/half/t0.4", 0x96c14e911783f8d6),
|
||||
("ec.obsolete.lightning/1/32bpc/half/t1.3", 0x620ee605f56c2068),
|
||||
("ec.obsolete.lightning/1/32bpc/t0.4", 0xe34f30bf669e94d5),
|
||||
("ec.obsolete.lightning/1/32bpc/t1.3", 0xb1ec8fad75bbbc32),
|
||||
("ec.obsolete.lightning/1/32bpc/half/t0.4", 0x9e2934559f25d0d9),
|
||||
("ec.obsolete.lightning/1/32bpc/half/t1.3", 0x8d2f6967b73bbf54),
|
||||
("ec.obsolete.lightning/2/8bpc/t0.4", 0x119e5cedac21cd9b),
|
||||
("ec.obsolete.lightning/2/8bpc/t1.3", 0x04b775fba837ddce),
|
||||
("ec.obsolete.lightning/2/32bpc/t0.4", 0xfee767091adac4ce),
|
||||
("ec.obsolete.lightning/2/32bpc/t1.3", 0x2ae26f9670c93751),
|
||||
("ec.obsolete.lightning/2/32bpc/half/t0.4", 0x99111f77bf26704f),
|
||||
("ec.obsolete.lightning/2/32bpc/half/t1.3", 0x8eb781efc14183c3),
|
||||
("ec.obsolete.lightning/2/32bpc/t0.4", 0x551415572abb270a),
|
||||
("ec.obsolete.lightning/2/32bpc/t1.3", 0x3e1b68b5b9cf2e8c),
|
||||
("ec.obsolete.lightning/2/32bpc/half/t0.4", 0x69b347a75e8ac280),
|
||||
("ec.obsolete.lightning/2/32bpc/half/t1.3", 0xe89cc144b611a083),
|
||||
("ec.generate.advancedlightning/0/8bpc/t0.4", 0x5a53bf1e0236225d),
|
||||
("ec.generate.advancedlightning/0/8bpc/t1.3", 0x5a53bf1e0236225d),
|
||||
("ec.generate.advancedlightning/0/32bpc/t0.4", 0x6817bf5b3583e0a0),
|
||||
("ec.generate.advancedlightning/0/32bpc/t1.3", 0x6817bf5b3583e0a0),
|
||||
("ec.generate.advancedlightning/0/32bpc/half/t0.4", 0x5dcc055f6cca9922),
|
||||
("ec.generate.advancedlightning/0/32bpc/half/t1.3", 0x5dcc055f6cca9922),
|
||||
("ec.generate.advancedlightning/0/32bpc/t0.4", 0x8ae323866971eaf4),
|
||||
("ec.generate.advancedlightning/0/32bpc/t1.3", 0x8ae323866971eaf4),
|
||||
("ec.generate.advancedlightning/0/32bpc/half/t0.4", 0x74b4ba806ab8e78b),
|
||||
("ec.generate.advancedlightning/0/32bpc/half/t1.3", 0x74b4ba806ab8e78b),
|
||||
("ec.generate.advancedlightning/1/8bpc/t0.4", 0xf07f11457fa683d0),
|
||||
("ec.generate.advancedlightning/1/8bpc/t1.3", 0xf07f11457fa683d0),
|
||||
("ec.generate.advancedlightning/1/32bpc/t0.4", 0x1b21531df9187431),
|
||||
("ec.generate.advancedlightning/1/32bpc/t1.3", 0x1b21531df9187431),
|
||||
("ec.generate.advancedlightning/1/32bpc/half/t0.4", 0xf1032a0a9345edf1),
|
||||
("ec.generate.advancedlightning/1/32bpc/half/t1.3", 0xf1032a0a9345edf1),
|
||||
("ec.generate.advancedlightning/1/32bpc/t0.4", 0x1474a59cbdbcb6aa),
|
||||
("ec.generate.advancedlightning/1/32bpc/t1.3", 0x1474a59cbdbcb6aa),
|
||||
("ec.generate.advancedlightning/1/32bpc/half/t0.4", 0x4808561ce9d8a4a6),
|
||||
("ec.generate.advancedlightning/1/32bpc/half/t1.3", 0x4808561ce9d8a4a6),
|
||||
("ec.generate.advancedlightning/2/8bpc/t0.4", 0x62b5d179f05c6a4c),
|
||||
("ec.generate.advancedlightning/2/8bpc/t1.3", 0x62b5d179f05c6a4c),
|
||||
("ec.generate.advancedlightning/2/32bpc/t0.4", 0x192256fe81f73975),
|
||||
("ec.generate.advancedlightning/2/32bpc/t1.3", 0x192256fe81f73975),
|
||||
("ec.generate.advancedlightning/2/32bpc/half/t0.4", 0x953dd51008c3ce31),
|
||||
("ec.generate.advancedlightning/2/32bpc/half/t1.3", 0x953dd51008c3ce31),
|
||||
("ec.generate.advancedlightning/2/32bpc/t0.4", 0x73c4b44db599a5c4),
|
||||
("ec.generate.advancedlightning/2/32bpc/t1.3", 0x73c4b44db599a5c4),
|
||||
("ec.generate.advancedlightning/2/32bpc/half/t0.4", 0x3085cf1b5f6e8311),
|
||||
("ec.generate.advancedlightning/2/32bpc/half/t1.3", 0x3085cf1b5f6e8311),
|
||||
("ec.generate.advancedlightning/3/8bpc/t0.4", 0x57b6ad61bd3d89e0),
|
||||
("ec.generate.advancedlightning/3/8bpc/t1.3", 0x57b6ad61bd3d89e0),
|
||||
("ec.generate.advancedlightning/3/32bpc/t0.4", 0x7e7c9d20e94c8b6c),
|
||||
("ec.generate.advancedlightning/3/32bpc/t1.3", 0x7e7c9d20e94c8b6c),
|
||||
("ec.generate.advancedlightning/3/32bpc/half/t0.4", 0xa37940f5a490a0fe),
|
||||
("ec.generate.advancedlightning/3/32bpc/half/t1.3", 0xa37940f5a490a0fe),
|
||||
("ec.generate.advancedlightning/3/32bpc/t0.4", 0x86eee3d36bfd41f2),
|
||||
("ec.generate.advancedlightning/3/32bpc/t1.3", 0x86eee3d36bfd41f2),
|
||||
("ec.generate.advancedlightning/3/32bpc/half/t0.4", 0xb24444985859d820),
|
||||
("ec.generate.advancedlightning/3/32bpc/half/t1.3", 0xb24444985859d820),
|
||||
("ec.generate.radiowaves/0/8bpc/t0.4", 0xbc3643b8af12c44b),
|
||||
("ec.generate.radiowaves/0/8bpc/t1.3", 0x703e948dc562e132),
|
||||
("ec.generate.radiowaves/0/32bpc/t0.4", 0x2b0db208ee7517c1),
|
||||
@@ -479,8 +479,8 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.generate.radiowaves/3/32bpc/half/t1.3", 0xcf62c191e2aa8cc4),
|
||||
("ec.generate.vegas/0/8bpc/t0.4", 0x5e58d44254e9101b),
|
||||
("ec.generate.vegas/0/8bpc/t1.3", 0x5e58d44254e9101b),
|
||||
("ec.generate.vegas/0/32bpc/t0.4", 0xe18712686e402759),
|
||||
("ec.generate.vegas/0/32bpc/t1.3", 0xe18712686e402759),
|
||||
("ec.generate.vegas/0/32bpc/t0.4", 0x2c6093dc655c6dbe),
|
||||
("ec.generate.vegas/0/32bpc/t1.3", 0x2c6093dc655c6dbe),
|
||||
("ec.generate.vegas/0/32bpc/half/t0.4", 0xb037c97234bbd936),
|
||||
("ec.generate.vegas/0/32bpc/half/t1.3", 0xb037c97234bbd936),
|
||||
("ec.generate.vegas/1/8bpc/t0.4", 0x68566eb5dc411cd3),
|
||||
@@ -491,8 +491,8 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.generate.vegas/1/32bpc/half/t1.3", 0xe2f8f5f4a12628d6),
|
||||
("ec.generate.vegas/2/8bpc/t0.4", 0x002c48a91aea2dcf),
|
||||
("ec.generate.vegas/2/8bpc/t1.3", 0x002c48a91aea2dcf),
|
||||
("ec.generate.vegas/2/32bpc/t0.4", 0x4f550bbbf8b8316b),
|
||||
("ec.generate.vegas/2/32bpc/t1.3", 0x4f550bbbf8b8316b),
|
||||
("ec.generate.vegas/2/32bpc/t0.4", 0xd703631844687600),
|
||||
("ec.generate.vegas/2/32bpc/t1.3", 0xd703631844687600),
|
||||
("ec.generate.vegas/2/32bpc/half/t0.4", 0x30036df3e56a01ed),
|
||||
("ec.generate.vegas/2/32bpc/half/t1.3", 0x30036df3e56a01ed),
|
||||
("ec.generate.stroke/0/8bpc/t0.4", 0xa580fbfb806d655c),
|
||||
@@ -557,10 +557,10 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.generate.paintbucket/1/32bpc/half/t1.3", 0xf5ea3b0a7ac224ed),
|
||||
("ec.generate.paintbucket/2/8bpc/t0.4", 0x530d0a248dc22c72),
|
||||
("ec.generate.paintbucket/2/8bpc/t1.3", 0x530d0a248dc22c72),
|
||||
("ec.generate.paintbucket/2/32bpc/t0.4", 0xe1b655f8815e1357),
|
||||
("ec.generate.paintbucket/2/32bpc/t1.3", 0xe1b655f8815e1357),
|
||||
("ec.generate.paintbucket/2/32bpc/half/t0.4", 0xb95b3b205255cd7e),
|
||||
("ec.generate.paintbucket/2/32bpc/half/t1.3", 0xb95b3b205255cd7e),
|
||||
("ec.generate.paintbucket/2/32bpc/t0.4", 0xf05e50b4261103a3),
|
||||
("ec.generate.paintbucket/2/32bpc/t1.3", 0xf05e50b4261103a3),
|
||||
("ec.generate.paintbucket/2/32bpc/half/t0.4", 0x7e74e09fcc83615c),
|
||||
("ec.generate.paintbucket/2/32bpc/half/t1.3", 0x7e74e09fcc83615c),
|
||||
("ec.generate.paintbucket/3/8bpc/t0.4", 0xa7485b7277e03e66),
|
||||
("ec.generate.paintbucket/3/8bpc/t1.3", 0xa7485b7277e03e66),
|
||||
("ec.generate.paintbucket/3/32bpc/t0.4", 0xc8e06c6eed30448b),
|
||||
@@ -666,16 +666,16 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.key.innerouter/0/32bpc/half/t1.3", 0x003941eb2cd18339),
|
||||
("ec.key.innerouter/1/8bpc/t0.4", 0xd7bfea3d9550db56),
|
||||
("ec.key.innerouter/1/8bpc/t1.3", 0xd7bfea3d9550db56),
|
||||
("ec.key.innerouter/1/32bpc/t0.4", 0x62f7705e43cc6a71),
|
||||
("ec.key.innerouter/1/32bpc/t1.3", 0x62f7705e43cc6a71),
|
||||
("ec.key.innerouter/1/32bpc/half/t0.4", 0x22f15f770f22f072),
|
||||
("ec.key.innerouter/1/32bpc/half/t1.3", 0x22f15f770f22f072),
|
||||
("ec.key.innerouter/1/32bpc/t0.4", 0x36661c78c393b52a),
|
||||
("ec.key.innerouter/1/32bpc/t1.3", 0x36661c78c393b52a),
|
||||
("ec.key.innerouter/1/32bpc/half/t0.4", 0x73f19ba1aea7e496),
|
||||
("ec.key.innerouter/1/32bpc/half/t1.3", 0x73f19ba1aea7e496),
|
||||
("ec.key.innerouter/2/8bpc/t0.4", 0x7bdece44f841bb11),
|
||||
("ec.key.innerouter/2/8bpc/t1.3", 0x7bdece44f841bb11),
|
||||
("ec.key.innerouter/2/32bpc/t0.4", 0x5130a1fc09c42120),
|
||||
("ec.key.innerouter/2/32bpc/t1.3", 0x5130a1fc09c42120),
|
||||
("ec.key.innerouter/2/32bpc/half/t0.4", 0x46c0880853618fc8),
|
||||
("ec.key.innerouter/2/32bpc/half/t1.3", 0x46c0880853618fc8),
|
||||
("ec.key.innerouter/2/32bpc/t0.4", 0x093157401c50396e),
|
||||
("ec.key.innerouter/2/32bpc/t1.3", 0x093157401c50396e),
|
||||
("ec.key.innerouter/2/32bpc/half/t0.4", 0xbde58c0668a48529),
|
||||
("ec.key.innerouter/2/32bpc/half/t1.3", 0xbde58c0668a48529),
|
||||
("ec.color.colorlink/0/8bpc/t0.4", 0x539d1da6b2ed744a),
|
||||
("ec.color.colorlink/0/8bpc/t1.3", 0x539d1da6b2ed744a),
|
||||
("ec.color.colorlink/0/32bpc/t0.4", 0x67ed14da67ce1a97),
|
||||
|
||||
@@ -537,18 +537,18 @@ const GOLDEN: &[(&str, u64)] = &[
|
||||
("ec.sim.caustics/1/8bpc/t1.5", 0x282968317c36ccdf),
|
||||
("ec.sim.caustics/1/8bpc/t0.8", 0x282968317c36ccdf),
|
||||
("ec.sim.caustics/1/8bpc/t2.2", 0x282968317c36ccdf),
|
||||
("ec.sim.caustics/1/32bpc/t0.5", 0xc12df8561f396d45),
|
||||
("ec.sim.caustics/1/32bpc/t1.5", 0xc12df8561f396d45),
|
||||
("ec.sim.caustics/1/32bpc/t0.8", 0xc12df8561f396d45),
|
||||
("ec.sim.caustics/1/32bpc/t2.2", 0xc12df8561f396d45),
|
||||
("ec.sim.caustics/1/32bpc/t0.5", 0xaa63d15038f75de2),
|
||||
("ec.sim.caustics/1/32bpc/t1.5", 0xaa63d15038f75de2),
|
||||
("ec.sim.caustics/1/32bpc/t0.8", 0xaa63d15038f75de2),
|
||||
("ec.sim.caustics/1/32bpc/t2.2", 0xaa63d15038f75de2),
|
||||
("ec.sim.caustics/2/8bpc/t0.5", 0x52ab258ae56e9b15),
|
||||
("ec.sim.caustics/2/8bpc/t1.5", 0x52ab258ae56e9b15),
|
||||
("ec.sim.caustics/2/8bpc/t0.8", 0x52ab258ae56e9b15),
|
||||
("ec.sim.caustics/2/8bpc/t2.2", 0x52ab258ae56e9b15),
|
||||
("ec.sim.caustics/2/32bpc/t0.5", 0x9a820d0e7c6d11ea),
|
||||
("ec.sim.caustics/2/32bpc/t1.5", 0x9a820d0e7c6d11ea),
|
||||
("ec.sim.caustics/2/32bpc/t0.8", 0x9a820d0e7c6d11ea),
|
||||
("ec.sim.caustics/2/32bpc/t2.2", 0x9a820d0e7c6d11ea),
|
||||
("ec.sim.caustics/2/32bpc/t0.5", 0x51d4304b505b5462),
|
||||
("ec.sim.caustics/2/32bpc/t1.5", 0x51d4304b505b5462),
|
||||
("ec.sim.caustics/2/32bpc/t0.8", 0x51d4304b505b5462),
|
||||
("ec.sim.caustics/2/32bpc/t2.2", 0x51d4304b505b5462),
|
||||
("ec.sim.waveworld/0/8bpc/t0.5", 0x425d5d639d2a6495),
|
||||
("ec.sim.waveworld/0/8bpc/t1.5", 0x233d7370e580804a),
|
||||
("ec.sim.waveworld/0/8bpc/t0.8", 0x866da86ce5e78eb2),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod camera_track;
|
||||
pub(crate) mod frames_key;
|
||||
pub mod mask_track;
|
||||
pub mod models;
|
||||
pub mod roto;
|
||||
|
||||
@@ -26,9 +26,10 @@ use effectcraft_track::camtrack::linalg::{self, Similarity, V3};
|
||||
use effectcraft_track::camtrack::{AnalyzeOpts, CameraSolve, CameraTracks, SolveSettings, Target, TrackAnalyzer};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::analysis::frames_key;
|
||||
use crate::offload::JobKind;
|
||||
use crate::tracking::lock;
|
||||
use crate::warp::{analysis_times, signature};
|
||||
use crate::warp::analysis_times;
|
||||
use crate::{Event, Session};
|
||||
|
||||
/// Live analysis progress (serde for agents / the control channel).
|
||||
@@ -81,7 +82,7 @@ pub struct CameraJob {
|
||||
pub comp: ItemId,
|
||||
pub layer: LayerId,
|
||||
pub effect: Uid,
|
||||
/// The layer signature the tracks are made from, and the solve settings key.
|
||||
/// What the tracks are made from (`tracks_key`), and the solve settings key.
|
||||
key: String,
|
||||
solve_key: String,
|
||||
}
|
||||
@@ -122,10 +123,21 @@ fn str_of(g: &PropGroup, m: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// What a tracker's tracks are made from: the layer's frames and Detailed Analysis.
|
||||
/// What a tracker's tracks are made from: its input (the layer's frames under its masks and the
|
||||
/// effects above it, `frames_key::analysis_key`) and Detailed Analysis.
|
||||
fn tracks_key(project: &Project, cid: ItemId, l: &Layer, g: &PropGroup) -> String {
|
||||
tracks_key_of(frames_key::analysis_key(project, cid, l, g.uid), g)
|
||||
}
|
||||
|
||||
fn tracks_key_of(input: String, g: &PropGroup) -> String {
|
||||
let detailed = effectcraft_effects::warp_stab::param(&static_params(g), "advanced/detailedAnalysis").is_some_and(Value::as_bool);
|
||||
format!("{}{}", signature(project, cid, l), if detailed { "+detailed" } else { "" })
|
||||
format!("{input}{}", if detailed { "+detailed" } else { "" })
|
||||
}
|
||||
|
||||
/// Whether the tracker's saved tracks are made from its current input and settings, after an
|
||||
/// edit from `before` to `project` ([`frames_key::analysis_key_matches`]).
|
||||
fn tracks_match(before: &Project, project: &Project, cid: ItemId, l: &Layer, g: &PropGroup) -> bool {
|
||||
frames_key::analysis_key_matches(before, project, cid, l, g.uid, &str_of(g, TRACKS_KEY), |k| tracks_key_of(k, g))
|
||||
}
|
||||
|
||||
/// Parameters of an instance from static values (the tracker's settings are not animated).
|
||||
@@ -165,7 +177,7 @@ pub(crate) fn invalidate(before: &Project, after: &mut Project) -> Vec<(ItemId,
|
||||
let old_layer = before.comp(*cid).and_then(|bc| bc.layer(l.id));
|
||||
for g in fx.groups().filter(|g| is_tracker(g)) {
|
||||
let tracked = !str_of(g, TRACKS).is_empty();
|
||||
if tracked && str_of(g, TRACKS_KEY) != tracks_key(after, *cid, l, g) {
|
||||
if tracked && !tracks_match(before, after, *cid, l, g) {
|
||||
clears.push((*cid, l.id, g.uid, true));
|
||||
todo.push((*cid, l.id, g.uid));
|
||||
} else if tracked && str_of(g, SOLVE_KEY) != ct::solve_key(&ct::settings(&static_params(g))) {
|
||||
@@ -325,7 +337,7 @@ impl Session {
|
||||
let key = tracks_key(&self.project, comp, l, g);
|
||||
let settings = ct::settings(¶ms);
|
||||
let solve_key = ct::solve_key(&settings);
|
||||
let tracks = ct::tracks(¶ms).filter(|_| str_of(g, TRACKS_KEY) == key);
|
||||
let tracks = ct::tracks(¶ms).filter(|_| tracks_match(&self.project, &self.project, comp, l, g));
|
||||
let n = times.len();
|
||||
let work = CamWork {
|
||||
project: self.project.clone(),
|
||||
|
||||
@@ -0,0 +1,777 @@
|
||||
//! What an analysis of a layer is made from. One writer keys the Warp Stabilizer and the 3D
|
||||
//! Camera Tracker ([`analysis_key`]: frames, masks, effects above, a precomp's contents) and Roto
|
||||
//! Brush / Object Matte ([`input_key`]: frames, masks, effects above).
|
||||
//!
|
||||
//! The key hashes a text this module writes field by field. The source item (footage and its
|
||||
//! settings, solid), the Time Remap property, the masks and effect groups (their properties,
|
||||
//! keyframes and values) and a precomp's comp and layers are destructured or matched in full, and
|
||||
//! each variant is written as a fixed word (or, for an enum the project file names, its name
|
||||
//! there): a new field or variant doesn't compile until it is decided here, written if it changes
|
||||
//! the layer's frames, left out (with a reason) if it doesn't. Renaming a field or variant doesn't
|
||||
//! change keys. Of the analysed layer itself, only the fields named in `layer_frames` and
|
||||
//! [`analysis_key`] are read. The text leaves out observations (whether footage is available) and
|
||||
//! data that never changes pixels (colour labels, UI hints, markers but a comp's timing markers;
|
||||
//! only expressions read the others), so they never drop saved
|
||||
//! analyses (#3534, #3536).
|
||||
//!
|
||||
//! Keys saved by 0.22–0.24 hashed derived `Debug` text instead. [`legacy`] still recognises
|
||||
//! them, and a matching saved key is kept rather than rewritten: frozen Roto Brush mattes and
|
||||
//! segmentations hash the saved key itself.
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::fmt::Write as _;
|
||||
|
||||
use effectcraft_color::ColorSpace;
|
||||
use effectcraft_keyframe::{Ease, FeatherPoint, Gradient, Interp, Keyframe, ShapePath, Value};
|
||||
use effectcraft_project::{
|
||||
AlphaMode, CineonSettings, CineonUnits, Comp, Expression, FeatherFalloff, FieldOrder, Footage, FootageKind, GroupKind, ItemId, ItemKind, Layer,
|
||||
LayerSource, LightKind, Marker, MaskMode, MaskMotionBlur, Node, PrimitiveKind, Project, PropGroup, Property, Solid, SourceLayer, Switches, TrackMatte, Uid,
|
||||
essential, styles::GlobalLight,
|
||||
};
|
||||
use effectcraft_time::{FrameRate, Tick};
|
||||
|
||||
pub(crate) mod legacy;
|
||||
|
||||
/// The version of the text, its first word: a later change to what it writes bumps it.
|
||||
const VERSION: &str = "frames/1";
|
||||
|
||||
/// Property groups nest a few levels (effect ▸ group ▸ property); deeper trees are cut here.
|
||||
const MAX_DEPTH: usize = 32;
|
||||
|
||||
/// Precomps nest deeper than the renderer draws them (16) only in broken projects; deeper
|
||||
/// contents are cut here.
|
||||
const MAX_NESTING: usize = 32;
|
||||
|
||||
fn fnv(s: &str) -> u64 {
|
||||
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
|
||||
for b in s.as_bytes() {
|
||||
h ^= *b as u64;
|
||||
h = h.wrapping_mul(0x0100_0000_01b3);
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
fn hex(s: &str) -> String {
|
||||
format!("{:016x}", fnv(s))
|
||||
}
|
||||
|
||||
/// Which signature a key is made from: today's, or one an earlier release saved.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum Era {
|
||||
Current,
|
||||
Legacy,
|
||||
}
|
||||
|
||||
/// The signature of `layer`'s frames: its source (and the source item), In/Out, start time,
|
||||
/// stretch, Time Remap and the comp frame rate. Roto Brush Input Keys and [`analysis_key`] are
|
||||
/// made from it.
|
||||
pub(crate) fn signature(p: &Project, comp: ItemId, layer: &Layer) -> String {
|
||||
hex(&signature_text(p, comp, layer))
|
||||
}
|
||||
|
||||
/// The text [`signature`] hashes.
|
||||
pub(crate) fn signature_text(p: &Project, comp: ItemId, layer: &Layer) -> String {
|
||||
let mut t = Text::new();
|
||||
t.word(VERSION);
|
||||
t.layer_frames(p, comp, layer);
|
||||
t.0
|
||||
}
|
||||
|
||||
/// A Roto Brush's (or Object Matte's) Input Key: the layer's frames, its masks and the effects
|
||||
/// above the instance `uid`.
|
||||
pub(crate) fn input_key(p: &Project, comp: ItemId, layer: &Layer, uid: Uid) -> String {
|
||||
input_key_of(signature(p, comp, layer), layer, uid)
|
||||
}
|
||||
|
||||
/// An Input Key from a [`signature`].
|
||||
pub(crate) fn input_key_of(signature: String, layer: &Layer, uid: Uid) -> String {
|
||||
let mut t = Text::new();
|
||||
t.word(VERSION);
|
||||
t.word(&signature);
|
||||
t.inputs_above(layer, uid, true);
|
||||
hex(&t.0)
|
||||
}
|
||||
|
||||
/// The first word of an [`analysis_key`]'s text: a later change to what it writes bumps it.
|
||||
const ANALYSIS_VERSION: &str = "analysis/1";
|
||||
|
||||
/// What a Warp Stabilizer or 3D Camera Tracker, the instance `uid` of `layer`, analyses: its
|
||||
/// input, the layer's frames ([`signature`]) under its masks and the effects above it, and a
|
||||
/// precomp's contents (#3535). The instance's own settings and the effects below it are not part
|
||||
/// of it.
|
||||
pub(crate) fn analysis_key(p: &Project, comp: ItemId, layer: &Layer, uid: Uid) -> String {
|
||||
let mut t = Text::new();
|
||||
t.word(ANALYSIS_VERSION);
|
||||
t.word(&signature(p, comp, layer));
|
||||
// The Effects switch turns the effects above on or off (it has none when it is the first).
|
||||
if layer.effects().and_then(|fx| fx.groups().next()).is_some_and(|g| g.uid != uid) {
|
||||
t.field("effectsSwitch", layer.switches.effects);
|
||||
}
|
||||
// While it is off the renderer skips the whole effect stack: the effects above draw nothing.
|
||||
t.inputs_above(layer, uid, layer.switches.effects);
|
||||
t.precomp(p, comp, layer);
|
||||
hex(&t.0)
|
||||
}
|
||||
|
||||
/// Whether `saved`, the key saved with the analysis of instance `uid` of `layer` in `after`, is
|
||||
/// for its current input ([`analysis_key`]). `key` makes the saved form from an input key or a
|
||||
/// frames signature (the tracker adds its Detailed Analysis).
|
||||
///
|
||||
/// Keys saved before #3535 hashed only the layer's frames ([`signature`], or a [`legacy`] one).
|
||||
/// Such a key is taken to be for the input the layer had when it was opened, and stays valid
|
||||
/// while each edit (from `before` to `after`) keeps the input: the first edit that changes it
|
||||
/// clears the analysis. A layer or instance new in `after` (pasted, opened) keeps such a key
|
||||
/// while its frames match.
|
||||
pub(crate) fn analysis_key_matches(
|
||||
before: &Project,
|
||||
after: &Project,
|
||||
comp: ItemId,
|
||||
layer: &Layer,
|
||||
uid: Uid,
|
||||
saved: &str,
|
||||
key: impl Fn(String) -> String,
|
||||
) -> bool {
|
||||
let now = analysis_key(after, comp, layer, uid);
|
||||
if key(now.clone()) == saved {
|
||||
return true;
|
||||
}
|
||||
let old = before.comp(comp).and_then(|c| c.layer(layer.id)).filter(|l| l.props.find_group(uid).is_some());
|
||||
match old {
|
||||
Some(old) => key_matches(before, comp, old, saved, |s, _| key(s)) && analysis_key(before, comp, old, uid) == now,
|
||||
None => key_matches(after, comp, layer, saved, |s, _| key(s)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `saved`, a key saved with an analysis of `layer`, is for the layer's current frames.
|
||||
/// `key` makes such a key from a signature of the given era. A key an earlier release saved for
|
||||
/// the same frames matches too: keep it rather than rewrite it (analysing writes today's).
|
||||
pub(crate) fn key_matches(p: &Project, comp: ItemId, layer: &Layer, saved: &str, key: impl Fn(String, Era) -> String) -> bool {
|
||||
key(signature(p, comp, layer), Era::Current) == saved || legacy::signatures(p, comp, layer).into_iter().any(|s| key(s, Era::Legacy) == saved)
|
||||
}
|
||||
|
||||
/// The text a key is hashed from. Fields are `name=value;`; strings are quoted and escaped, so
|
||||
/// no value can end its field early.
|
||||
struct Text(String);
|
||||
|
||||
impl Text {
|
||||
fn new() -> Text {
|
||||
Text(String::new())
|
||||
}
|
||||
|
||||
fn word(&mut self, w: &str) {
|
||||
self.0.push_str(w);
|
||||
self.0.push(';');
|
||||
}
|
||||
|
||||
fn field(&mut self, name: &str, value: impl std::fmt::Display) {
|
||||
let _ = write!(self.0, "{name}={value};");
|
||||
}
|
||||
|
||||
fn str(&mut self, name: &str, s: &str) {
|
||||
let _ = write!(self.0, "{name}={s:?};");
|
||||
}
|
||||
|
||||
fn num(&mut self, name: &str, v: f64) {
|
||||
let _ = write!(self.0, "{name}={v:?};");
|
||||
}
|
||||
|
||||
fn nums(&mut self, name: &str, v: &[f64]) {
|
||||
let _ = write!(self.0, "{name}={v:?};");
|
||||
}
|
||||
|
||||
fn floats(&mut self, name: &str, v: &[f32]) {
|
||||
let _ = write!(self.0, "{name}={v:?};");
|
||||
}
|
||||
|
||||
fn opt<T>(&mut self, name: &str, v: Option<&T>, write: impl FnOnce(&mut Text, &T)) {
|
||||
match v {
|
||||
Some(v) => {
|
||||
let _ = write!(self.0, "{name}{{");
|
||||
write(self, v);
|
||||
self.0.push_str("};");
|
||||
}
|
||||
None => self.word(&format!("{name}=none")),
|
||||
}
|
||||
}
|
||||
|
||||
fn tick(&mut self, name: &str, t: Tick) {
|
||||
self.field(name, t.0);
|
||||
}
|
||||
|
||||
fn rate(&mut self, name: &str, r: FrameRate) {
|
||||
let FrameRate { num, den } = r;
|
||||
let _ = write!(self.0, "{name}={num}/{den};");
|
||||
}
|
||||
|
||||
/// What a layer's frames are made of, before its masks and effects.
|
||||
fn layer_frames(&mut self, p: &Project, comp: ItemId, layer: &Layer) {
|
||||
self.layer_source(&layer.source);
|
||||
if let Some(item) = layer.source.item() {
|
||||
match p.item(item).map(|i| &i.kind) {
|
||||
Some(ItemKind::Footage(f)) => self.footage(f),
|
||||
Some(ItemKind::Solid(s)) => self.solid(s),
|
||||
// Its contents are in an [`analysis_key`], not here: Roto Brush keys are made from this.
|
||||
Some(ItemKind::Comp(c)) => {
|
||||
self.word("comp");
|
||||
self.field("w", c.width);
|
||||
self.field("h", c.height);
|
||||
self.rate("rate", c.frame_rate);
|
||||
self.tick("duration", c.duration);
|
||||
}
|
||||
Some(ItemKind::Folder) | None => self.word("item=none"),
|
||||
}
|
||||
}
|
||||
self.tick("in", layer.in_point);
|
||||
self.tick("out", layer.out_point);
|
||||
self.tick("start", layer.start_time);
|
||||
self.num("stretch", layer.stretch);
|
||||
self.opt("timeRemap", layer.props.get("timeRemap"), |t, pr| t.property(pr));
|
||||
self.opt("compRate", p.comp(comp).map(|c| &c.frame_rate), |t, r| t.rate("rate", *r));
|
||||
}
|
||||
|
||||
fn layer_source(&mut self, s: &LayerSource) {
|
||||
match s {
|
||||
LayerSource::Footage { item } => self.field("footage", item.0),
|
||||
LayerSource::Comp { item } => self.field("comp", item.0),
|
||||
LayerSource::Solid { item } => self.field("solid", item.0),
|
||||
LayerSource::Model { item } => self.field("model", item.0),
|
||||
LayerSource::Text => self.word("text"),
|
||||
LayerSource::Shape => self.word("shape"),
|
||||
LayerSource::Null => self.word("null"),
|
||||
LayerSource::Camera => self.word("camera"),
|
||||
LayerSource::Light { kind } => self.field(
|
||||
"light",
|
||||
match kind {
|
||||
LightKind::Parallel => "parallel",
|
||||
LightKind::Spot => "spot",
|
||||
LightKind::Point => "point",
|
||||
LightKind::Ambient => "ambient",
|
||||
LightKind::Environment => "environment",
|
||||
},
|
||||
),
|
||||
LayerSource::Primitive { kind } => self.field(
|
||||
"primitive",
|
||||
match kind {
|
||||
PrimitiveKind::Cube => "cube",
|
||||
PrimitiveKind::Sphere => "sphere",
|
||||
PrimitiveKind::Plane => "plane",
|
||||
PrimitiveKind::Torus => "torus",
|
||||
PrimitiveKind::Cone => "cone",
|
||||
PrimitiveKind::Cylinder => "cylinder",
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn solid(&mut self, s: &Solid) {
|
||||
let Solid { color, width, height, pixel_aspect } = s;
|
||||
self.word("solid");
|
||||
self.floats("color", color);
|
||||
self.field("w", width);
|
||||
self.field("h", height);
|
||||
self.num("par", *pixel_aspect);
|
||||
}
|
||||
|
||||
fn footage(&mut self, f: &Footage) {
|
||||
let Footage {
|
||||
path,
|
||||
kind,
|
||||
width,
|
||||
height,
|
||||
pixel_aspect,
|
||||
frame_rate,
|
||||
native_rate,
|
||||
duration,
|
||||
has_video,
|
||||
has_audio,
|
||||
alpha,
|
||||
premul_color,
|
||||
loop_count,
|
||||
codec,
|
||||
// Whether the file is there is an observation (`footage.check`), not an edit: an
|
||||
// offline file is the same footage, and its analyses stay valid (#3534).
|
||||
missing: _,
|
||||
sequence,
|
||||
color_profile,
|
||||
layer,
|
||||
fields,
|
||||
invert_alpha,
|
||||
linear_light,
|
||||
preserve_rgb,
|
||||
cineon,
|
||||
data,
|
||||
page,
|
||||
alphabetical,
|
||||
start_timecode,
|
||||
file_timecode,
|
||||
// Markers annotate the footage; they never change its frames.
|
||||
markers: _,
|
||||
placed_page,
|
||||
} = f;
|
||||
self.word("footage");
|
||||
self.str("path", path);
|
||||
self.field(
|
||||
"kind",
|
||||
match kind {
|
||||
FootageKind::Video => "video",
|
||||
FootageKind::Still => "still",
|
||||
FootageKind::Sequence => "sequence",
|
||||
FootageKind::Audio => "audio",
|
||||
FootageKind::Model => "model",
|
||||
FootageKind::Data => "data",
|
||||
},
|
||||
);
|
||||
self.field("w", width);
|
||||
self.field("h", height);
|
||||
self.num("par", *pixel_aspect);
|
||||
self.rate("rate", *frame_rate);
|
||||
self.opt("nativeRate", native_rate.as_ref(), |t, r| t.rate("rate", *r));
|
||||
self.tick("duration", *duration);
|
||||
self.field("video", has_video);
|
||||
self.field("audio", has_audio);
|
||||
self.field(
|
||||
"alpha",
|
||||
match alpha {
|
||||
AlphaMode::Straight => "straight",
|
||||
AlphaMode::Premultiplied => "premultiplied",
|
||||
AlphaMode::Ignore => "ignore",
|
||||
},
|
||||
);
|
||||
self.floats("matte", premul_color);
|
||||
self.field("loop", loop_count);
|
||||
self.str("codec", codec);
|
||||
self.field("frames", sequence.len());
|
||||
for s in sequence {
|
||||
self.str("file", s);
|
||||
}
|
||||
self.opt("profile", color_profile.as_ref(), |t, c| t.word(color_space(*c)));
|
||||
self.opt("layer", layer.as_ref(), |t, l| t.source_layer(l));
|
||||
self.field(
|
||||
"fields",
|
||||
match fields {
|
||||
FieldOrder::Off => "off",
|
||||
FieldOrder::UpperFirst => "upper",
|
||||
FieldOrder::LowerFirst => "lower",
|
||||
},
|
||||
);
|
||||
self.field("invertAlpha", invert_alpha);
|
||||
self.field("linearLight", linear_light);
|
||||
self.field("preserveRgb", preserve_rgb);
|
||||
self.cineon(cineon);
|
||||
self.opt("data", data.as_ref(), |t, d| t.str("text", d));
|
||||
self.field("page", page);
|
||||
self.field("alphabetical", alphabetical);
|
||||
self.opt("startTimecode", start_timecode.as_ref(), |t, v| t.field("frame", v));
|
||||
self.opt("fileTimecode", file_timecode.as_ref(), |t, v| t.field("frame", v));
|
||||
self.field("placedPage", placed_page);
|
||||
}
|
||||
|
||||
fn source_layer(&mut self, l: &SourceLayer) {
|
||||
let SourceLayer { index, name, layer_size, embedded, placed } = l;
|
||||
self.field("index", index);
|
||||
// The layer is found by its index; the name only labels it.
|
||||
let _ = name;
|
||||
self.field("layerSize", layer_size);
|
||||
self.opt("embedded", embedded.as_ref(), |t, e| t.str("id", e));
|
||||
self.field("placed", placed);
|
||||
}
|
||||
|
||||
fn cineon(&mut self, c: &CineonSettings) {
|
||||
let CineonSettings { log_conversion, converted_black, converted_white, black, white, gamma, rolloff, units } = c;
|
||||
self.word("cineon");
|
||||
self.field("log", log_conversion);
|
||||
self.floats("points", &[*converted_black, *converted_white, *black, *white, *gamma, *rolloff]);
|
||||
self.field(
|
||||
"units",
|
||||
match units {
|
||||
CineonUnits::TenBit => "10bit",
|
||||
CineonUnits::EightBit => "8bit",
|
||||
CineonUnits::Float => "float",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// The layer's masks and the effects above the instance `uid`: what the effect's input is
|
||||
/// made of besides the layer's frames.
|
||||
/// `effects`: whether to write the effects (a Roto Brush Input Key always does: frozen mattes
|
||||
/// hash the saved key, so it keeps its text).
|
||||
fn inputs_above(&mut self, layer: &Layer, uid: Uid, effects: bool) {
|
||||
if let Some(m) = layer.masks() {
|
||||
self.word("masks");
|
||||
self.group(m, 0);
|
||||
}
|
||||
if let Some(fx) = layer.effects() {
|
||||
self.word("effects");
|
||||
for g in fx.groups().take_while(|g| effects && g.uid != uid) {
|
||||
self.group(g, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A precomp layer's contents: its Essential Properties (they set values inside it) and what
|
||||
/// its comp draws, nested precomps included.
|
||||
fn precomp(&mut self, p: &Project, comp: ItemId, layer: &Layer) {
|
||||
let LayerSource::Comp { item } = layer.source else { return };
|
||||
if let Some(e) = layer.props.group(essential::GROUP) {
|
||||
self.word("essential");
|
||||
self.group(e, 0);
|
||||
}
|
||||
// A precomp never draws the comp it is in.
|
||||
let mut seen = HashSet::from([comp]);
|
||||
self.comp_contents(p, item, 0, &mut seen);
|
||||
}
|
||||
|
||||
/// What comp `id` draws. Each comp is written once per key, where it is first reached; a
|
||||
/// later reference (a comp used twice, or a cycle) writes only its id, so the work is bounded
|
||||
/// by the size of the project.
|
||||
fn comp_contents(&mut self, p: &Project, id: ItemId, depth: usize, seen: &mut HashSet<ItemId>) {
|
||||
self.field("contents", id.0);
|
||||
// Before `seen`: a comp first reached too deep is written in full where it is shallower.
|
||||
if depth >= MAX_NESTING {
|
||||
self.word("tooDeep");
|
||||
return;
|
||||
}
|
||||
if !seen.insert(id) {
|
||||
self.word("seen");
|
||||
return;
|
||||
}
|
||||
let Some(ItemKind::Comp(c)) = p.item(id).map(|i| &i.kind) else {
|
||||
self.word("none");
|
||||
return;
|
||||
};
|
||||
let Comp {
|
||||
width,
|
||||
height,
|
||||
pixel_aspect,
|
||||
frame_rate,
|
||||
duration,
|
||||
display_start,
|
||||
background,
|
||||
// The work area, Hide Shy, the viewer's Resolution and guides, and the poster frame
|
||||
// only show the comp or say what to preview; they never change its frames.
|
||||
work_area: _,
|
||||
layers,
|
||||
markers,
|
||||
shutter_angle,
|
||||
shutter_phase,
|
||||
motion_blur_samples,
|
||||
motion_blur_adaptive_limit,
|
||||
renderer,
|
||||
hide_shy: _,
|
||||
enable_motion_blur,
|
||||
enable_frame_blending,
|
||||
draft_3d,
|
||||
preserve_frame_rate,
|
||||
preserve_resolution,
|
||||
poster_time: _,
|
||||
global_light,
|
||||
guides: _,
|
||||
essential,
|
||||
resolution: _,
|
||||
} = &**c;
|
||||
self.field("w", width);
|
||||
self.field("h", height);
|
||||
self.num("par", *pixel_aspect);
|
||||
self.rate("rate", *frame_rate);
|
||||
self.tick("duration", *duration);
|
||||
self.tick("displayStart", *display_start);
|
||||
self.floats("background", background);
|
||||
self.timing_markers(markers);
|
||||
self.nums("shutter", &[*shutter_angle, *shutter_phase]);
|
||||
self.field("samples", format_args!("{motion_blur_samples}/{motion_blur_adaptive_limit}"));
|
||||
self.form("renderer", renderer);
|
||||
self.field("motionBlur", enable_motion_blur);
|
||||
self.field("frameBlending", enable_frame_blending);
|
||||
self.field("draft3d", draft_3d);
|
||||
self.field("preserveRate", preserve_frame_rate);
|
||||
self.field("preserveResolution", preserve_resolution);
|
||||
let GlobalLight { angle, altitude } = global_light;
|
||||
self.property(angle);
|
||||
self.property(altitude);
|
||||
self.opt("essentialGraphics", essential.as_ref(), |t, e| t.form("definition", e));
|
||||
for l in layers.iter() {
|
||||
self.nested_layer(p, l, depth, seen);
|
||||
}
|
||||
self.word("end");
|
||||
}
|
||||
|
||||
/// A layer of a precomp.
|
||||
fn nested_layer(&mut self, p: &Project, l: &Layer, depth: usize, seen: &mut HashSet<ItemId>) {
|
||||
let Layer {
|
||||
id,
|
||||
name,
|
||||
// Whether the name follows the Source Text changes nothing until the name changes.
|
||||
name_follows_text: _,
|
||||
source,
|
||||
// Labels and comments only show the layer.
|
||||
label: _,
|
||||
comment: _,
|
||||
start_time,
|
||||
in_point,
|
||||
out_point,
|
||||
stretch,
|
||||
switches,
|
||||
blend_mode,
|
||||
preserve_transparency,
|
||||
track_matte,
|
||||
parent,
|
||||
// Layer markers annotate it (only expressions read them, as for the analysed layer);
|
||||
// Lock Markers protects them.
|
||||
markers: _,
|
||||
markers_locked: _,
|
||||
auto_orient,
|
||||
environment,
|
||||
environment_background,
|
||||
props,
|
||||
} = l;
|
||||
self.field("layer", id.0);
|
||||
// Expressions find layers by name (`thisComp.layer("Clip")`), as they find effects.
|
||||
self.str("name", name);
|
||||
self.layer_source(source);
|
||||
if let Some(item) = source.item() {
|
||||
match p.item(item).map(|i| &i.kind) {
|
||||
Some(ItemKind::Footage(f)) => self.footage(f),
|
||||
Some(ItemKind::Solid(s)) => self.solid(s),
|
||||
Some(ItemKind::Comp(_)) => self.comp_contents(p, item, depth.saturating_add(1), seen),
|
||||
Some(ItemKind::Folder) | None => self.word("item=none"),
|
||||
}
|
||||
}
|
||||
self.tick("in", *in_point);
|
||||
self.tick("out", *out_point);
|
||||
self.tick("start", *start_time);
|
||||
self.num("stretch", *stretch);
|
||||
self.switches(switches);
|
||||
self.form("blend", blend_mode);
|
||||
self.field("preserveTransparency", preserve_transparency);
|
||||
self.opt("matte", track_matte.as_ref(), |t, m| {
|
||||
let TrackMatte { layer, kind } = m;
|
||||
t.field("layer", layer.0);
|
||||
t.form("kind", kind);
|
||||
});
|
||||
self.opt("parent", parent.as_ref(), |t, id| t.field("id", id.0));
|
||||
self.form("autoOrient", auto_orient);
|
||||
self.field("environment", format_args!("{environment}/{environment_background}"));
|
||||
self.group(props, 0);
|
||||
}
|
||||
|
||||
fn switches(&mut self, s: &Switches) {
|
||||
// Lock and Shy only protect and hide the layer in the Timeline. Audio is written (as are
|
||||
// the Audio group and audio effects, in the property tree): a waveform effect can draw it.
|
||||
let Switches { video, audio, solo, locked: _, shy: _, collapse, quality, sampling, effects, frame_blend, motion_blur, adjustment, three_d, guide } = s;
|
||||
self.field("switches", format_args!("{video}/{audio}/{solo}/{collapse}/{effects}/{motion_blur}/{adjustment}/{three_d}/{guide}"));
|
||||
self.form("quality", quality);
|
||||
self.form("sampling", sampling);
|
||||
self.form("frameBlend", frame_blend);
|
||||
}
|
||||
|
||||
/// A comp's protected markers with a duration: they set its timing (Responsive Design).
|
||||
/// Other markers annotate it; only expressions read them.
|
||||
fn timing_markers(&mut self, markers: &[Marker]) {
|
||||
for m in markers.iter().filter(|m| m.protected && m.duration > Tick::ZERO) {
|
||||
self.tick("timingMarker", m.time);
|
||||
self.tick("for", m.duration);
|
||||
}
|
||||
}
|
||||
|
||||
/// A value in its project-file form: enums the project file names, and the Essential Graphics
|
||||
/// definition.
|
||||
fn form<T: serde::Serialize>(&mut self, name: &str, v: &T) {
|
||||
self.str(name, &serde_json::to_string(v).unwrap_or_default());
|
||||
}
|
||||
|
||||
fn group(&mut self, g: &PropGroup, depth: usize) {
|
||||
// Expressions find effects and groups by name (`effect("Blur")`, `propertyGroup(1).name`).
|
||||
// The uid and colour label never change pixels.
|
||||
let PropGroup { uid: _, match_id, name, kind, enabled, label: _, children } = g;
|
||||
if depth >= MAX_DEPTH {
|
||||
self.word("tooDeep");
|
||||
return;
|
||||
}
|
||||
self.str("group", match_id);
|
||||
self.str("name", name);
|
||||
self.group_kind(kind);
|
||||
self.field("enabled", enabled);
|
||||
for c in children {
|
||||
match c {
|
||||
Node::Prop(p) => self.property(p),
|
||||
Node::Group(g) => self.group(g, depth + 1),
|
||||
}
|
||||
}
|
||||
self.word("end");
|
||||
}
|
||||
|
||||
fn group_kind(&mut self, k: &GroupKind) {
|
||||
match k {
|
||||
GroupKind::Plain => self.word("plain"),
|
||||
GroupKind::Indexed => self.word("indexed"),
|
||||
// A mask's colour and lock only show it in the UI.
|
||||
GroupKind::Mask { mode, inverted, color: _, locked: _, motion_blur, feather_falloff, roto_bezier } => {
|
||||
self.field(
|
||||
"mask",
|
||||
match mode {
|
||||
MaskMode::None => "none",
|
||||
MaskMode::Add => "add",
|
||||
MaskMode::Subtract => "subtract",
|
||||
MaskMode::Intersect => "intersect",
|
||||
MaskMode::Lighten => "lighten",
|
||||
MaskMode::Darken => "darken",
|
||||
MaskMode::Difference => "difference",
|
||||
},
|
||||
);
|
||||
self.field("inverted", inverted);
|
||||
self.field(
|
||||
"motionBlur",
|
||||
match motion_blur {
|
||||
MaskMotionBlur::SameAsLayer => "layer",
|
||||
MaskMotionBlur::On => "on",
|
||||
MaskMotionBlur::Off => "off",
|
||||
},
|
||||
);
|
||||
self.field(
|
||||
"falloff",
|
||||
match feather_falloff {
|
||||
FeatherFalloff::Smooth => "smooth",
|
||||
FeatherFalloff::Linear => "linear",
|
||||
},
|
||||
);
|
||||
self.field("rotoBezier", roto_bezier);
|
||||
}
|
||||
GroupKind::Effect { effect } => self.str("effect", effect),
|
||||
// A motion tracker's settings say how to track, not how to draw the layer.
|
||||
GroupKind::Tracker { settings: _ } => self.word("tracker"),
|
||||
GroupKind::Essential { overridden } => {
|
||||
self.word("essential");
|
||||
for u in overridden {
|
||||
self.field("overridden", u);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn property(&mut self, pr: &Property) {
|
||||
// Expressions read the name (`thisProperty.name`) and seed `random`/`wiggle` with the
|
||||
// uid: the uid is written for a property with an expression. UI hints and the 2D/3D-only
|
||||
// and hold-only flags say where and how it can be edited, not what it evaluates to.
|
||||
let Property { uid, match_id, name, value, keys, expr, ui: _, spatial, hold_only: _, three_d_only: _, two_d_only: _, shown_dims: _, static_only: _ } =
|
||||
pr;
|
||||
self.str("prop", match_id);
|
||||
self.str("name", name);
|
||||
if expr.is_some() {
|
||||
self.field("uid", uid);
|
||||
}
|
||||
self.value(value);
|
||||
self.field("spatial", spatial);
|
||||
for k in keys {
|
||||
self.keyframe(k);
|
||||
}
|
||||
self.opt("expr", expr.as_ref(), |t, e| {
|
||||
let Expression { text, enabled } = e;
|
||||
t.str("text", text);
|
||||
t.field("enabled", enabled);
|
||||
});
|
||||
}
|
||||
|
||||
fn keyframe(&mut self, k: &Keyframe) {
|
||||
// A key's colour label only shows it in the UI.
|
||||
let Keyframe {
|
||||
time,
|
||||
value,
|
||||
in_interp,
|
||||
out_interp,
|
||||
in_ease,
|
||||
out_ease,
|
||||
continuous,
|
||||
auto_bezier,
|
||||
spatial_in,
|
||||
spatial_out,
|
||||
spatial_auto,
|
||||
spatial_continuous,
|
||||
roving,
|
||||
label: _,
|
||||
muted,
|
||||
} = k;
|
||||
self.tick("key", *time);
|
||||
self.value(value);
|
||||
self.field("interp", format_args!("{}/{}", interp(*in_interp), interp(*out_interp)));
|
||||
for (side, eases) in [("inEase", in_ease), ("outEase", out_ease)] {
|
||||
for e in eases {
|
||||
let Ease { speed, influence } = e;
|
||||
self.nums(side, &[*speed, *influence]);
|
||||
}
|
||||
}
|
||||
self.field("continuous", continuous);
|
||||
self.field("autoBezier", auto_bezier);
|
||||
self.nums("spatialIn", spatial_in);
|
||||
self.nums("spatialOut", spatial_out);
|
||||
self.field("spatialAuto", spatial_auto);
|
||||
self.field("spatialContinuous", spatial_continuous);
|
||||
self.field("roving", roving);
|
||||
self.field("muted", muted);
|
||||
}
|
||||
|
||||
fn value(&mut self, v: &Value) {
|
||||
match v {
|
||||
Value::Scalar(x) => self.num("scalar", *x),
|
||||
Value::Vec2(x) => self.nums("vec2", x),
|
||||
Value::Vec3(x) => self.nums("vec3", x),
|
||||
Value::Color(x) => self.nums("color", x),
|
||||
Value::Bool(b) => self.field("bool", b),
|
||||
Value::Enum(i) => self.field("enum", i),
|
||||
Value::Path(p) => self.shape_path(p),
|
||||
// A text document has dozens of fields and no place in what a key covers today (a
|
||||
// text layer's Source Text is not under its masks or effects): its project-file form.
|
||||
Value::Text(d) => self.str("text", &serde_json::to_string(d).unwrap_or_default()),
|
||||
Value::Layer(l) => self.opt("layer", l.as_ref(), |t, id| t.field("id", id)),
|
||||
Value::Gradient(g) => {
|
||||
let Gradient { colors, opacities } = g;
|
||||
self.word("gradient");
|
||||
for (at, c) in colors {
|
||||
self.num("stop", *at);
|
||||
self.floats("rgba", c);
|
||||
}
|
||||
for (at, o) in opacities {
|
||||
self.num("stop", *at);
|
||||
self.floats("opacity", &[*o]);
|
||||
}
|
||||
}
|
||||
Value::Str(s) => self.str("str", s),
|
||||
}
|
||||
}
|
||||
|
||||
fn shape_path(&mut self, p: &ShapePath) {
|
||||
let ShapePath { vertices, in_tangents, out_tangents, closed, feather } = p;
|
||||
self.word("path");
|
||||
for (name, points) in [("v", vertices), ("in", in_tangents), ("out", out_tangents)] {
|
||||
for pt in points {
|
||||
self.nums(name, pt);
|
||||
}
|
||||
}
|
||||
self.field("closed", closed);
|
||||
for f in feather {
|
||||
let FeatherPoint { segment, t, radius, tension } = f;
|
||||
self.field("featherSegment", segment);
|
||||
self.nums("feather", &[*t, *radius, *tension]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn interp(i: Interp) -> &'static str {
|
||||
match i {
|
||||
Interp::Linear => "linear",
|
||||
Interp::Bezier => "bezier",
|
||||
Interp::Hold => "hold",
|
||||
}
|
||||
}
|
||||
|
||||
fn color_space(c: ColorSpace) -> &'static str {
|
||||
match c {
|
||||
ColorSpace::Srgb => "srgb",
|
||||
ColorSpace::Rec709 => "rec709",
|
||||
ColorSpace::Rec2020 => "rec2020",
|
||||
ColorSpace::DisplayP3 => "p3",
|
||||
ColorSpace::AcesCg => "acescg",
|
||||
ColorSpace::Aces2065 => "aces2065",
|
||||
ColorSpace::Rec2100Pq => "rec2100pq",
|
||||
ColorSpace::Rec2100Hlg => "rec2100hlg",
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
//! Signatures as 0.22–0.24 wrote them, to recognise the keys those releases saved. Nothing new is
|
||||
//! keyed with them. They hash derived `Debug` text, so `tests::analysis_signature` pins keys
|
||||
//! those releases saved: if a type change alters this text, write that part out here.
|
||||
|
||||
use effectcraft_project::{Footage, ItemId, ItemKind, Layer, LayerSource, Project, Uid};
|
||||
|
||||
use super::fnv;
|
||||
|
||||
/// The signatures a saved key of `layer` may have been made from. The footage's availability is
|
||||
/// tried both ways: those releases hashed it, though it isn't part of the footage (#3534). Before
|
||||
/// `Footage::placed_page` was added in 0.23, the footage text ended without that field.
|
||||
pub(crate) fn signatures(p: &Project, comp: ItemId, layer: &Layer) -> Vec<String> {
|
||||
let src = match &layer.source {
|
||||
LayerSource::Footage { item } | LayerSource::Solid { item } | LayerSource::Comp { item } => match p.item(*item).map(|i| &i.kind) {
|
||||
Some(ItemKind::Footage(f)) => {
|
||||
let mut out = Vec::with_capacity(4);
|
||||
for missing in [false, true] {
|
||||
out.push(signature_with_source(p, comp, layer, &footage_text(f, missing, true)));
|
||||
if matches!(layer.source, LayerSource::Footage { .. }) && !f.placed_page {
|
||||
out.push(signature_with_source(p, comp, layer, &footage_text(f, missing, false)));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Some(ItemKind::Solid(s)) => format!("{s:?}"),
|
||||
Some(ItemKind::Comp(c)) => format!("{}x{}@{:?}/{:?}", c.width, c.height, c.frame_rate, c.duration),
|
||||
_ => String::new(),
|
||||
},
|
||||
_ => String::new(),
|
||||
};
|
||||
vec![signature_with_source(p, comp, layer, &src)]
|
||||
}
|
||||
|
||||
/// A Roto Brush Input Key as 0.22–0.24 made it from a [`signatures`] entry.
|
||||
pub(crate) fn input_key_of(mut s: String, layer: &Layer, uid: Uid) -> String {
|
||||
if let Some(m) = layer.masks() {
|
||||
s.push_str(&format!("{m:?}"));
|
||||
}
|
||||
if let Some(fx) = layer.effects() {
|
||||
for g in fx.groups() {
|
||||
if g.uid == uid {
|
||||
break;
|
||||
}
|
||||
s.push_str(&format!("{g:?}"));
|
||||
}
|
||||
}
|
||||
format!("{:016x}", fnv(&s))
|
||||
}
|
||||
|
||||
/// A footage item as 0.23–0.24 wrote it into a signature (without `placed_page` before 0.23),
|
||||
/// with `missing` in place of the item's.
|
||||
pub(crate) fn footage_text(f: &Footage, missing: bool, placed_page: bool) -> String {
|
||||
format!("{:?}", FootageText { f, missing, placed_page })
|
||||
}
|
||||
|
||||
struct FootageText<'a> {
|
||||
f: &'a Footage,
|
||||
missing: bool,
|
||||
placed_page: bool,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for FootageText<'_> {
|
||||
fn fmt(&self, out: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
// Every field is named so that a new one doesn't compile until it is decided here: a new
|
||||
// field was never in these keys, so it stays out.
|
||||
let Footage {
|
||||
path,
|
||||
kind,
|
||||
width,
|
||||
height,
|
||||
pixel_aspect,
|
||||
frame_rate,
|
||||
native_rate,
|
||||
duration,
|
||||
has_video,
|
||||
has_audio,
|
||||
alpha,
|
||||
premul_color,
|
||||
loop_count,
|
||||
codec,
|
||||
missing: _,
|
||||
sequence,
|
||||
color_profile,
|
||||
layer,
|
||||
fields,
|
||||
invert_alpha,
|
||||
linear_light,
|
||||
preserve_rgb,
|
||||
cineon,
|
||||
data,
|
||||
page,
|
||||
alphabetical,
|
||||
start_timecode,
|
||||
file_timecode,
|
||||
markers,
|
||||
placed_page,
|
||||
} = self.f;
|
||||
let mut d = out.debug_struct("Footage");
|
||||
d.field("path", path)
|
||||
.field("kind", kind)
|
||||
.field("width", width)
|
||||
.field("height", height)
|
||||
.field("pixel_aspect", pixel_aspect)
|
||||
.field("frame_rate", frame_rate)
|
||||
.field("native_rate", native_rate)
|
||||
.field("duration", duration)
|
||||
.field("has_video", has_video)
|
||||
.field("has_audio", has_audio)
|
||||
.field("alpha", alpha)
|
||||
.field("premul_color", premul_color)
|
||||
.field("loop_count", loop_count)
|
||||
.field("codec", codec)
|
||||
.field("missing", &self.missing)
|
||||
.field("sequence", sequence)
|
||||
.field("color_profile", color_profile)
|
||||
.field("layer", layer)
|
||||
.field("fields", fields)
|
||||
.field("invert_alpha", invert_alpha)
|
||||
.field("linear_light", linear_light)
|
||||
.field("preserve_rgb", preserve_rgb)
|
||||
.field("cineon", cineon)
|
||||
.field("data", data)
|
||||
.field("page", page)
|
||||
.field("alphabetical", alphabetical)
|
||||
.field("start_timecode", start_timecode)
|
||||
.field("file_timecode", file_timecode)
|
||||
.field("markers", markers);
|
||||
if self.placed_page {
|
||||
d.field("placed_page", placed_page);
|
||||
}
|
||||
d.finish()
|
||||
}
|
||||
}
|
||||
|
||||
fn signature_with_source(p: &Project, comp: ItemId, layer: &Layer, src: &str) -> String {
|
||||
let rate = p.comp(comp).map(|c| format!("{:?}", c.frame_rate)).unwrap_or_default();
|
||||
let s = format!(
|
||||
"{:?}|{src}|{:?}|{:?}|{:?}|{}|{:?}|{rate}",
|
||||
layer.source,
|
||||
layer.in_point,
|
||||
layer.out_point,
|
||||
layer.start_time,
|
||||
layer.stretch,
|
||||
layer.props.get("timeRemap")
|
||||
);
|
||||
format!("{:016x}", fnv(&s))
|
||||
}
|
||||
@@ -8,8 +8,8 @@
|
||||
//! (`Renderer::layer_input`, source → masks → the effects above it). `roto.freeze` runs the same
|
||||
//! pass and stores the final mattes in the effect (one undo step). `sync`, run after every
|
||||
//! edit, keeps each instance's hidden Input Key equal to a signature of what its frames are made
|
||||
//! of (the layer's source and timing, its masks and the effects above it) and drops frozen
|
||||
//! mattes made from other frames.
|
||||
//! of (the layer's source and timing, its masks and the effects above it: `analysis::frames_key`),
|
||||
//! and drops frozen mattes made from other frames. Footage going offline keeps them.
|
||||
//!
|
||||
//! Object Matte instances run through the same jobs (`objectMatte.propagate` / `.freeze`): the
|
||||
//! frames are [`om::job_frames`] around the reference frame, each segmented through
|
||||
@@ -31,6 +31,7 @@ use effectcraft_render::{EvalCtx, ExprHost, FootageSource, LayerCache, RenderOpt
|
||||
use effectcraft_time::Tick;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::analysis::frames_key::{self, Era};
|
||||
use crate::tracking::lock;
|
||||
use crate::{Event, Session};
|
||||
|
||||
@@ -207,31 +208,10 @@ fn set_str(g: &mut PropGroup, m: &str, v: String) {
|
||||
}
|
||||
}
|
||||
|
||||
fn fnv(s: &str) -> u64 {
|
||||
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
|
||||
for b in s.as_bytes() {
|
||||
h ^= *b as u64;
|
||||
h = h.wrapping_mul(0x0100_0000_01b3);
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
/// What the effect's input frames are made of: the layer's source and timing, its masks and the
|
||||
/// effects above the Roto Brush.
|
||||
pub fn input_key(p: &Project, comp: ItemId, layer: &Layer, uid: Uid) -> String {
|
||||
let mut s = crate::warp::signature(p, comp, layer);
|
||||
if let Some(m) = layer.masks() {
|
||||
s.push_str(&format!("{m:?}"));
|
||||
}
|
||||
if let Some(fx) = layer.effects() {
|
||||
for g in fx.groups() {
|
||||
if g.uid == uid {
|
||||
break;
|
||||
}
|
||||
s.push_str(&format!("{g:?}"));
|
||||
}
|
||||
}
|
||||
format!("{:016x}", fnv(&s))
|
||||
frames_key::input_key(p, comp, layer, uid)
|
||||
}
|
||||
|
||||
/// After an edit: refresh Input Keys (Roto Brush and Object Matte); frozen mattes made from
|
||||
@@ -247,7 +227,13 @@ pub(crate) fn sync(before: &Project, after: &mut Project) -> Vec<(ItemId, LayerI
|
||||
for g in fxg.groups().filter(|g| is_roto(g) || is_object_matte(g)) {
|
||||
let key = input_key(after, *cid, l, g.uid);
|
||||
let old = before.comp(*cid).and_then(|bc| bc.layer(l.id)).and_then(|ol| ol.props.find_group(g.uid));
|
||||
if str_of(g, INPUT_KEY) != key {
|
||||
let saved_key = str_of(g, INPUT_KEY);
|
||||
// Keep a matching old key: the frozen cache itself also hashes that key.
|
||||
let key_of = |s, era| match era {
|
||||
Era::Current => frames_key::input_key_of(s, l, g.uid),
|
||||
Era::Legacy => frames_key::legacy::input_key_of(s, l, g.uid),
|
||||
};
|
||||
if !frames_key::key_matches(after, *cid, l, &saved_key, key_of) {
|
||||
set.push((*cid, l.id, g.uid, key));
|
||||
todo.push((*cid, l.id, g.uid));
|
||||
} else if old.is_none_or(|o| guard_of(o) != guard_of(g)) {
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
//! analyzer (step 1 of 2, "Analyzing in background"); step 2 ("Stabilizing") derives the
|
||||
//! stabilization plan once to validate and cache the result. When the job finishes, the analysis is
|
||||
//! written into the effect's hidden Analysis parameter as one undo step, together with a key of
|
||||
//! what it was made from (the layer's source, In/Out, start, stretch and Time Remap). Edits that
|
||||
//! change those clear the analysis (`invalidate`, run after every edit) and queue the effect
|
||||
//! for re-analysis, which frontends start with [`Session::poll_warp`] (After Effects re-analyses
|
||||
//! automatically too).
|
||||
//! what it was made from (the layer's source, In/Out, start, stretch and Time Remap, its masks, the
|
||||
//! effects above the Warp Stabilizer and a precomp's contents: `analysis::frames_key::analysis_key`,
|
||||
//! shared with the 3D Camera Tracker). Edits that change those clear the analysis (`invalidate`, run after every edit) and queue the effect for
|
||||
//! re-analysis, which frontends start with [`Session::poll_warp`] (After Effects re-analyses
|
||||
//! automatically too). Footage going offline is not such a change.
|
||||
|
||||
use crate::offload::{JobKind, rate};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
@@ -17,13 +18,14 @@ use std::sync::{Arc, Mutex};
|
||||
|
||||
use effectcraft_effects::warp_stab::{self, ANALYSIS, ANALYSIS_KEY};
|
||||
use effectcraft_keyframe::Value;
|
||||
use effectcraft_project::{GroupKind, ItemId, ItemKind, Layer, LayerId, LayerSource, Project, PropGroup, Uid};
|
||||
use effectcraft_project::{GroupKind, ItemId, ItemKind, Layer, LayerId, Project, PropGroup, Uid};
|
||||
use effectcraft_raster::Image;
|
||||
use effectcraft_render::{EvalCtx, ExprHost, FootageSource, LayerCache, Renderer};
|
||||
use effectcraft_time::Tick;
|
||||
use effectcraft_track::stabilize::{AnalyzeOpts, Analyzer, WarpAnalysis};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::analysis::frames_key;
|
||||
use crate::tracking::lock;
|
||||
use crate::{Event, Session};
|
||||
|
||||
@@ -69,7 +71,7 @@ pub struct WarpJob {
|
||||
pub comp: ItemId,
|
||||
pub layer: LayerId,
|
||||
pub effect: Uid,
|
||||
/// The layer signature the analysis is made from.
|
||||
/// What the analysis is made from (`frames_key::analysis_key`).
|
||||
key: String,
|
||||
}
|
||||
|
||||
@@ -98,40 +100,6 @@ pub(crate) struct WarpWork {
|
||||
pub times: Vec<Tick>,
|
||||
}
|
||||
|
||||
fn fnv(s: &str) -> u64 {
|
||||
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
|
||||
for b in s.as_bytes() {
|
||||
h ^= *b as u64;
|
||||
h = h.wrapping_mul(0x0100_0000_01b3);
|
||||
}
|
||||
h
|
||||
}
|
||||
|
||||
/// What an analysis of `layer` depends on (its frames): source (and the footage item's
|
||||
/// interpretation), In/Out, start time, stretch, Time Remap and the comp frame rate.
|
||||
pub fn signature(p: &Project, comp: ItemId, layer: &Layer) -> String {
|
||||
let src = match &layer.source {
|
||||
LayerSource::Footage { item } | LayerSource::Solid { item } | LayerSource::Comp { item } => match p.item(*item).map(|i| &i.kind) {
|
||||
Some(ItemKind::Footage(f)) => format!("{f:?}"),
|
||||
Some(ItemKind::Solid(s)) => format!("{s:?}"),
|
||||
Some(ItemKind::Comp(c)) => format!("{}x{}@{:?}/{:?}", c.width, c.height, c.frame_rate, c.duration),
|
||||
_ => String::new(),
|
||||
},
|
||||
_ => String::new(),
|
||||
};
|
||||
let rate = p.comp(comp).map(|c| format!("{:?}", c.frame_rate)).unwrap_or_default();
|
||||
let s = format!(
|
||||
"{:?}|{src}|{:?}|{:?}|{:?}|{}|{:?}|{rate}",
|
||||
layer.source,
|
||||
layer.in_point,
|
||||
layer.out_point,
|
||||
layer.start_time,
|
||||
layer.stretch,
|
||||
layer.props.get("timeRemap")
|
||||
);
|
||||
format!("{:016x}", fnv(&s))
|
||||
}
|
||||
|
||||
fn is_warp(g: &PropGroup) -> bool {
|
||||
matches!(&g.kind, GroupKind::Effect { effect } if effect == warp_stab::ID)
|
||||
}
|
||||
@@ -155,11 +123,10 @@ pub(crate) fn invalidate(before: &Project, after: &mut Project) -> Vec<(ItemId,
|
||||
if !fx.groups().any(is_warp) {
|
||||
continue;
|
||||
}
|
||||
let sig = signature(after, *cid, l);
|
||||
let old_layer = before.comp(*cid).and_then(|bc| bc.layer(l.id));
|
||||
for g in fx.groups().filter(|g| is_warp(g)) {
|
||||
let analysed = !str_of(g, ANALYSIS).is_empty();
|
||||
if analysed && str_of(g, ANALYSIS_KEY) != sig {
|
||||
if analysed && !frames_key::analysis_key_matches(before, after, *cid, l, g.uid, &str_of(g, ANALYSIS_KEY), |k| k) {
|
||||
clear.push((*cid, l.id, g.uid));
|
||||
todo.push((*cid, l.id, g.uid));
|
||||
} else if !analysed && old_layer.and_then(|ol| ol.props.find_group(g.uid)).is_none() {
|
||||
@@ -338,7 +305,7 @@ impl Session {
|
||||
detailed,
|
||||
times,
|
||||
};
|
||||
let key = signature(&self.project, comp, l);
|
||||
let key = frames_key::analysis_key(&self.project, comp, l, effect);
|
||||
if !wait && self.offloads() {
|
||||
drop(work);
|
||||
self.offload_analysis(crate::offload::WorkerJob::Warp { comp, layer, effect })?;
|
||||
|
||||
@@ -430,7 +430,10 @@ impl FrameServer {
|
||||
}
|
||||
}
|
||||
// A frame still waiting for readbacks is replaced: end it, so every `frame_begin` has its `frame_end`.
|
||||
if self.pending.take().is_some() {
|
||||
if let Some(old) = self.pending.take() {
|
||||
if let (Some((key, chose)), Some(pick)) = (old.auto, a.auto_pick()) {
|
||||
pick.abandoned(key, chose);
|
||||
}
|
||||
a.frame_end();
|
||||
}
|
||||
a.frame_begin();
|
||||
|
||||
@@ -31,6 +31,7 @@ pub(crate) mod frame_export;
|
||||
mod frontend;
|
||||
mod help;
|
||||
mod import_project;
|
||||
mod journal;
|
||||
mod key_labels;
|
||||
mod key_transform;
|
||||
mod keys_more;
|
||||
@@ -48,7 +49,7 @@ mod nav_more;
|
||||
pub mod paint;
|
||||
pub mod panels_cmds;
|
||||
pub(crate) mod path_nulls;
|
||||
mod paths;
|
||||
pub(crate) mod paths;
|
||||
mod project_items;
|
||||
pub(crate) mod prop;
|
||||
mod prop_groups;
|
||||
@@ -161,6 +162,7 @@ mod params;
|
||||
mod schema;
|
||||
|
||||
use super::commands;
|
||||
pub use aep::is_aep_path;
|
||||
pub use enabled::always;
|
||||
pub(crate) use enabled::{has_comp, has_keys, has_layers, has_project_selection, not_yet, not_yet_run};
|
||||
pub use params::match_path_of;
|
||||
@@ -201,6 +203,7 @@ pub fn command_specs() -> &'static [CommandSpec] {
|
||||
v.extend(nav_more::specs());
|
||||
v.extend(render_queue::specs());
|
||||
v.extend(help::specs());
|
||||
v.extend(journal::specs());
|
||||
v.extend(query::specs());
|
||||
v.extend(batch::specs());
|
||||
v.extend(layer_menu::specs());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//! File ▸ Import ▸ After Effects Project… (`effectcraft_aep::import`) and File ▸ Export ▸ After Effects
|
||||
//! Project… (`effectcraft_aep::export`).
|
||||
//! File ▸ Import ▸ Adobe After Effects Project… (`effectcraft_aep::import`) and File ▸ Export ▸
|
||||
//! Adobe After Effects Project… (`effectcraft_aep::export`).
|
||||
//!
|
||||
//! Adds the items of an `.aep` (or its XML form, `.aepx`) to the project in a folder named after
|
||||
//! the file (`docs/aep-import.md` lists what arrives). What the importer cannot bring over (effects
|
||||
@@ -16,6 +16,14 @@ use serde_json::{Value, json};
|
||||
use super::{CommandSpec, always, b_p, bad, str_p};
|
||||
use crate::{EngineError, Result, Session, cmd};
|
||||
|
||||
/// Whether `path` is an After Effects project (`.aep`, or its XML form `.aepx`): imported as a
|
||||
/// project (`file.importAep`), not as footage.
|
||||
pub fn is_aep_path(path: &str) -> bool {
|
||||
std::path::Path::new(path).extension().and_then(std::ffi::OsStr::to_str).is_some_and(|e| e.eq_ignore_ascii_case("aep") || e.eq_ignore_ascii_case("aepx"))
|
||||
}
|
||||
|
||||
/// `file.importAep`: the `.aep` / `.aepx` at `path` into the open project, in a folder named after
|
||||
/// the file; refused ([`EngineError::Refused`]) when parts would be lost and `allowLosses` is not set.
|
||||
fn import_aep(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let path = str_p(p, "path").ok_or_else(|| bad("file.importAep", "missing `path`"))?.to_string();
|
||||
let allow_losses = b_p(p, "allowLosses").unwrap_or(false);
|
||||
@@ -40,11 +48,12 @@ fn import_aep(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
return Ok(json!({"dryRun": true, "report": report}));
|
||||
}
|
||||
let importer = s.importer.clone();
|
||||
let res = s.edit("Import After Effects Project", None, |proj, st| {
|
||||
let res = s.edit("Import Adobe After Effects Project", None, |proj, st| {
|
||||
let r = effectcraft_aep::import::import(proj, &bytes, &options).map_err(|e| match e {
|
||||
AepImportError::Refused(_) => {
|
||||
EngineError::Other(format!("{e} (nothing was imported: pass `allowLosses` to import anyway, or `dryRun` for the full report)"))
|
||||
}
|
||||
AepImportError::Refused(_) => EngineError::Refused {
|
||||
losses: e.to_string(),
|
||||
advice: "nothing was imported: pass `allowLosses` to import anyway, or `dryRun` for the full report",
|
||||
},
|
||||
other => EngineError::Other(format!("cannot import {path}: {other}")),
|
||||
})?;
|
||||
if apply {
|
||||
@@ -90,6 +99,8 @@ fn import_aep(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}))
|
||||
}
|
||||
|
||||
/// `file.exportAep`: the open project as an `.aep` at `path`; refused ([`EngineError::Refused`])
|
||||
/// when parts would be lost and `allowLosses` is not set.
|
||||
fn export_aep(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let path = str_p(p, "path").ok_or_else(|| bad("file.exportAep", "missing `path`"))?.to_string();
|
||||
let allow_losses = b_p(p, "allowLosses").unwrap_or(false);
|
||||
@@ -112,7 +123,7 @@ fn export_aep(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
let res = effectcraft_aep::export::export_with(&s.project, &ExportOptions { allow_losses }, &folder_time).map_err(|e| match e {
|
||||
AepExportError::Refused(_) => {
|
||||
EngineError::Other(format!("{e} (nothing was written: pass `allowLosses` to export anyway, or `dryRun` for the full report)"))
|
||||
EngineError::Refused { losses: e.to_string(), advice: "nothing was written: pass `allowLosses` to export anyway, or `dryRun` for the full report" }
|
||||
}
|
||||
other => EngineError::Other(format!("cannot export {path}: {other}")),
|
||||
})?;
|
||||
@@ -126,11 +137,12 @@ fn export_aep(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"path": path, "bytes": res.bytes.len(), "report": res.report}))
|
||||
}
|
||||
|
||||
/// File ▸ Import / Export ▸ Adobe After Effects Project….
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!(
|
||||
"file.importAep",
|
||||
"After Effects Project...",
|
||||
"Adobe After Effects Project...",
|
||||
["File", "Import"],
|
||||
None,
|
||||
"{path (.aep or .aepx), allowLosses?: bool (import although parts cannot be imported), dryRun?: bool (only return the report), applySettings?: bool (take over the file's project settings; default: when the project has no compositions)}",
|
||||
@@ -139,7 +151,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
cmd!(
|
||||
"file.exportAep",
|
||||
"After Effects Project...",
|
||||
"Adobe After Effects Project...",
|
||||
["File", "Export"],
|
||||
None,
|
||||
"{path (.aep), allowLosses?: bool (export although parts cannot be exported), dryRun?: bool (only return the report)}",
|
||||
|
||||
@@ -319,15 +319,19 @@ impl Prober {
|
||||
return f.map(|footage| Probed { name, footage, ask_alpha: false, warning: None }).map_err(fail);
|
||||
}
|
||||
let importer = self.importer.as_ref().ok_or_else(|| fail("media import is not available in this build".into()))?;
|
||||
let mut f = crate::sequence::probe(importer.as_ref(), src, self.sequence_rate).map_err(fail)?;
|
||||
let mut warning = None;
|
||||
// What the file's own import left out (a model's textures over the limits) is a warning too.
|
||||
let (mut f, mut warning) = match src {
|
||||
Source::File(_) => importer.probe_noting(path).map_err(fail)?,
|
||||
_ => (crate::sequence::probe(importer.as_ref(), src, self.sequence_rate).map_err(fail)?, None),
|
||||
};
|
||||
// Settings ▸ Import ▸ Report Missing Frames (numbered sequences).
|
||||
if f.kind == FootageKind::Sequence
|
||||
&& !f.alphabetical
|
||||
&& self.report_missing_frames
|
||||
&& let Some(gaps) = crate::sequence::missing_report(&f.sequence)
|
||||
{
|
||||
warning = Some(format!("{name}: {gaps} (the previous frame is held)"));
|
||||
let gaps = format!("{name}: {gaps} (the previous frame is held)");
|
||||
warning = Some(warning.map_or(gaps.clone(), |w| format!("{w}; {gaps}")));
|
||||
}
|
||||
// Settings ▸ Import ▸ Interpret Unlabeled Alpha As: images only (a 3D model renders its
|
||||
// own alpha, and audio and data have none).
|
||||
@@ -404,8 +408,11 @@ fn prepare_import(s: &mut Session, p: &Value) -> Result<PendingImport> {
|
||||
let mut comps = vec![];
|
||||
let mut items = vec![];
|
||||
let mut errors = vec![];
|
||||
// EffectCraft projects import as projects, as After Effects imports an .aep.
|
||||
let (project_paths, mut paths): (Vec<String>, Vec<String>) = paths.into_iter().partition(|x| super::import_project::is_project_path(x));
|
||||
// EffectCraft projects import as projects, as After Effects imports an .aep; so do After
|
||||
// Effects projects, refused when parts of them cannot come over. The app asks about those
|
||||
// before it gets here (`panels::aep_losses`); a script or agent gets the refusal.
|
||||
let (project_paths, paths): (Vec<String>, Vec<String>) = paths.into_iter().partition(|x| super::import_project::is_project_path(x));
|
||||
let (aep_paths, mut paths): (Vec<String>, Vec<String>) = paths.into_iter().partition(|x| super::is_aep_path(x));
|
||||
let mut projects = vec![];
|
||||
for path in project_paths {
|
||||
match s.execute("file.importProject", json!({"path": path})) {
|
||||
@@ -413,6 +420,17 @@ fn prepare_import(s: &mut Session, p: &Value) -> Result<PendingImport> {
|
||||
Err(e) => errors.push(format!("{path}: {e}")),
|
||||
}
|
||||
}
|
||||
for path in aep_paths {
|
||||
match s.execute("file.importAep", json!({"path": path})) {
|
||||
Ok(r) => projects.push(r),
|
||||
// `file.import` has no `allowLosses`: point at the entry that lists the losses, and at
|
||||
// the command that takes the consent.
|
||||
Err(EngineError::Refused { losses, .. }) => errors.push(format!(
|
||||
"{path}: {losses}; File ▸ Import ▸ Adobe After Effects Project… lists them and can import the rest (`file.importAep` with `allowLosses`)"
|
||||
)),
|
||||
Err(e) => errors.push(format!("{path}: {e}")),
|
||||
}
|
||||
}
|
||||
if let Some(retain) = retain {
|
||||
let mut rest = vec![];
|
||||
for path in paths {
|
||||
|
||||
@@ -395,6 +395,9 @@ fn collect_files(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// reads (other footage, a proxy, the project file itself), and then nothing is written.
|
||||
let protected = s.protected_files();
|
||||
let mut used_names = BTreeSet::new();
|
||||
// Where each file name's numbering resumes: a name repeated many times is numbered from its
|
||||
// own file name, not by prefixing the previous candidate again.
|
||||
let mut next_k: BTreeMap<String, usize> = BTreeMap::new();
|
||||
let mut plan: Vec<(ItemId, Vec<(String, String)>)> = vec![];
|
||||
for it in s.project.items.values() {
|
||||
let ItemKind::Footage(f) = &it.kind else { continue };
|
||||
@@ -404,11 +407,17 @@ fn collect_files(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let files: Vec<&String> = if f.sequence.is_empty() { vec![&f.path] } else { f.sequence.iter().collect() };
|
||||
let mut pairs = vec![];
|
||||
for src in files {
|
||||
let mut name = std::path::Path::new(src).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "footage".into());
|
||||
let mut k = 2;
|
||||
while !used_names.insert(name.clone()) {
|
||||
name = format!("{k}_{name}");
|
||||
k += 1;
|
||||
let file = std::path::Path::new(src).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "footage".into());
|
||||
let mut name = file.clone();
|
||||
if !used_names.insert(name.clone()) {
|
||||
let k = next_k.entry(file.clone()).or_insert(2);
|
||||
loop {
|
||||
name = format!("{k}_{file}");
|
||||
*k += 1;
|
||||
if used_names.insert(name.clone()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let dst = format!("{folder}/(Footage)/{name}");
|
||||
// A file that is already where its copy goes stays (it is not copied onto itself).
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
//! premultiplied): the composite as `R`, `G`, `B`, `A` plus one `<layer name>.R/G/B/A` channel
|
||||
//! set per layer (the layer-prefixed channel convention multi-layer EXR readers use).
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use effectcraft_color::BlendMode;
|
||||
use effectcraft_project::{ItemId, LayerId, Project};
|
||||
use effectcraft_raster::Image;
|
||||
@@ -141,17 +143,22 @@ pub fn layered_psd(s: &Session, cid: ItemId, t: Tick, scale: f64) -> Result<(Vec
|
||||
|
||||
/// A unique EXR layer name (no dots: they separate the layer from the channel). Printable ASCII only:
|
||||
/// the `exr` crate panics on characters it cannot store in a channel name (#1702).
|
||||
fn exr_name(name: &str, used: &mut Vec<String>) -> String {
|
||||
/// `used` holds the names given so far, `next` the suffix each base's search resumes at: many
|
||||
/// layers of one name take a name each without retrying every suffix before theirs.
|
||||
fn exr_name(name: &str, used: &mut HashSet<String>, next: &mut HashMap<String, usize>) -> String {
|
||||
let base: String = name.chars().map(|c| if c == '.' || !(c == ' ' || c.is_ascii_graphic()) { '_' } else { c }).collect();
|
||||
let base = if base.trim().is_empty() { "Layer".to_string() } else { base };
|
||||
let mut n = base.clone();
|
||||
let mut k = 2;
|
||||
while used.contains(&n) {
|
||||
n = format!("{base} {k}");
|
||||
k += 1;
|
||||
if used.insert(base.clone()) {
|
||||
return base;
|
||||
}
|
||||
let k = next.entry(base.clone()).or_insert(2);
|
||||
loop {
|
||||
let n = format!("{base} {k}");
|
||||
*k += 1;
|
||||
if used.insert(n.clone()) {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
used.push(n.clone());
|
||||
n
|
||||
}
|
||||
|
||||
/// The multi-layer EXR bytes of a frame and the channel names.
|
||||
@@ -186,9 +193,9 @@ pub fn layered_exr(s: &Session, cid: ItemId, t: Tick, scale: f64) -> Result<(Vec
|
||||
}
|
||||
};
|
||||
push("", &full);
|
||||
let mut used = vec![];
|
||||
let (mut used, mut next) = (HashSet::new(), HashMap::new());
|
||||
for l in &layers {
|
||||
let n = exr_name(&l.name, &mut used);
|
||||
let n = exr_name(&l.name, &mut used, &mut next);
|
||||
push(&n, &l.image);
|
||||
}
|
||||
let layer = Layer::new((w, h), LayerAttributes::default(), Encoding::SMALL_LOSSLESS, AnyChannels::sort(channels.into()));
|
||||
@@ -248,3 +255,23 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use super::exr_name;
|
||||
|
||||
#[test]
|
||||
fn many_layers_of_one_name_take_a_name_each_in_order() {
|
||||
let (mut used, mut next) = (HashSet::new(), HashMap::new());
|
||||
// `A 3` is taken by a layer of its own before the repeats reach it.
|
||||
let names: Vec<String> = ["A", "A", "A 3", "A", "A.b"].iter().map(|n| exr_name(n, &mut used, &mut next)).collect();
|
||||
assert_eq!(names, ["A", "A 2", "A 3", "A 4", "A_b"]);
|
||||
// 20 000 layers of one name: each found without retrying the suffixes before it.
|
||||
for _ in 0..20_000 {
|
||||
exr_name("Same", &mut used, &mut next);
|
||||
}
|
||||
assert_eq!(next["Same"], 20_001);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
//! The command journal as a replay script (see [`crate::replay`]): `journal.export` for agents
|
||||
//! and `help.exportRepro` for bug reports. Neither is journaled.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{CommandSpec, always, str_p};
|
||||
use crate::{Result, Session, replay};
|
||||
|
||||
/// `journal.export {path?}`: the script itself, or written to `path`.
|
||||
fn export(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(path) = str_p(p, "path") {
|
||||
let (path, steps) = replay::save_repro(s, Some(path))?;
|
||||
return Ok(json!({"path": path, "steps": steps}));
|
||||
}
|
||||
let script = replay::export(s, None);
|
||||
Ok(json!({"steps": script.lines().count().saturating_sub(1), "script": script}))
|
||||
}
|
||||
|
||||
/// `help.exportRepro {path?}`: save the steps so far for a bug report (default: the settings
|
||||
/// folder's `Repros`).
|
||||
fn export_repro(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (path, steps) = replay::save_repro(s, str_p(p, "path"))?;
|
||||
Ok(json!({"path": path, "steps": steps}))
|
||||
}
|
||||
|
||||
const fn spec(id: &'static str, label: &'static str, params: &'static str, run: super::Run) -> CommandSpec {
|
||||
CommandSpec { id, label, menu: &[], shortcut: None, alt_shortcuts: &[], params, enabled: always, run, journal: false }
|
||||
}
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
#[allow(unused_mut)]
|
||||
let mut v = vec![
|
||||
spec(
|
||||
"journal.export",
|
||||
"Export Command Journal",
|
||||
"{path? (write the script there)} → {script, steps} or {path, steps}: the commands run since the project was last opened, as a replay script (docs/agents.md, Repro scripts)",
|
||||
export,
|
||||
),
|
||||
spec(
|
||||
"help.exportRepro",
|
||||
"Save Bug Repro...",
|
||||
"{path? (default: Repros in the settings folder)} → {path, steps}: the steps since the project was opened, for a bug report (commands and parameters only)",
|
||||
export_repro,
|
||||
),
|
||||
];
|
||||
// A command with a bug, for the crash-repro tests.
|
||||
#[cfg(test)]
|
||||
v.push(spec("test.panic", "Panic (test)", "{why?}", |_, p| {
|
||||
let why = str_p(p, "why").unwrap_or("test").to_string();
|
||||
std::panic::panic_any(why)
|
||||
}));
|
||||
v
|
||||
}
|
||||
@@ -24,7 +24,7 @@ mod transform;
|
||||
|
||||
use contents::{add_mask, add_shape_item, mask_props, set_text};
|
||||
use create::{add_item, new_adjustment, new_null, new_shape, new_solid, new_text};
|
||||
pub(crate) use create::{color_p, index_p, insert_layer, insert_layer_at, place, position_p};
|
||||
pub(crate) use create::{color_p, fit_span, index_p, insert_layer, insert_layer_at, new_layer_span, place, position_p, show_span};
|
||||
pub(crate) use parent::parent_fixes;
|
||||
use parent::set_parent;
|
||||
use replace::{has_replace_source, replace_source};
|
||||
|
||||
@@ -14,6 +14,39 @@ pub(crate) fn color_p(p: &Value, k: &str) -> Option<[f32; 3]> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings ▸ General ▸ Fit New Layers to Work Area: the `(start, end)` that new text, solid,
|
||||
/// shape, null and adjustment layers in `comp` take, or `None` for the whole composition.
|
||||
pub(crate) fn new_layer_span(s: &Session, comp: &effectcraft_project::Comp) -> Option<(Tick, Tick)> {
|
||||
let (start, end) = (comp.work_area.0, comp.work_area.1.min(comp.duration));
|
||||
(s.prefs.general.fit_new_layers_to_work_area && start >= Tick::ZERO && start < end).then_some((start, end))
|
||||
}
|
||||
|
||||
/// Start a new layer at `span`'s start and end it at `span`'s end, when there is a span.
|
||||
pub(crate) fn fit_span(l: &mut Layer, span: Option<(Tick, Tick)>) {
|
||||
if let Some((start, end)) = span {
|
||||
l.start_time = start;
|
||||
l.in_point = start;
|
||||
l.out_point = end;
|
||||
}
|
||||
}
|
||||
|
||||
/// Tools draw on the layer they create: when it was fitted to a span the playhead is outside,
|
||||
/// move `comp`'s playhead to the first frame in the span so the new layer shows. The layer's timing
|
||||
/// is kept. The work area need not sit on frames (a typed start, a frame-rate change), so the
|
||||
/// frame is rounded up from the start, not to the nearest; a span holding no frame moves nothing.
|
||||
pub(crate) fn show_span(s: &mut Session, comp: ItemId, span: Option<(Tick, Tick)>) {
|
||||
let Some(fr) = s.project.comp(comp).map(|c| c.frame_rate) else { return };
|
||||
if let Some((start, end)) = span
|
||||
&& !(start..end).contains(&s.time_of(comp))
|
||||
{
|
||||
let first = fr.snap(start);
|
||||
let first = if first < start { fr.tick_of(fr.frame_at(start).saturating_add(1)) } else { first };
|
||||
if first < end {
|
||||
s.set_time_of(comp, first);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a new layer above the selection (or at the top) and select it.
|
||||
pub(crate) fn insert_layer(proj: &mut Project, st: &mut crate::EditorState, cid: ItemId, layer: Layer) -> Result<LayerId> {
|
||||
insert_layer_at(proj, st, cid, layer, None)
|
||||
@@ -89,10 +122,12 @@ fn new_solid_like(s: &mut Session, p: &Value, adjustment: bool) -> Result<Value>
|
||||
let pixel_aspect = pixel_aspect_p(p)?.unwrap_or(comp.pixel_aspect);
|
||||
let default_name = if adjustment { "Adjustment Layer 1".to_string() } else { "Solid 1".to_string() };
|
||||
let name = str_p(p, "name").map(str::to_string).unwrap_or(default_name);
|
||||
let span = new_layer_span(s, &comp);
|
||||
let id = s.edit(if adjustment { "New Adjustment Layer" } else { "New Solid" }, None, |proj, st| {
|
||||
let folder = solids_folder(proj);
|
||||
let sid = proj.add_item(&name, Label::Red, Some(folder), ItemKind::Solid(Solid { color, width: w, height: h, pixel_aspect }));
|
||||
let mut l = build::layer(proj, &comp, &name, LayerSource::Solid { item: sid }, (w, h), None);
|
||||
fit_span(&mut l, span);
|
||||
if adjustment {
|
||||
l.switches.adjustment = true;
|
||||
l.label = Label::Purple;
|
||||
@@ -186,9 +221,11 @@ pub(super) fn new_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
}
|
||||
let edit = b_p(p, "edit").unwrap_or(false);
|
||||
let span = new_layer_span(s, &comp);
|
||||
let id = s.edit("New Text Layer", None, |proj, st| {
|
||||
let name: String = layer_name.clone().unwrap_or_else(|| effectcraft_project::text_layer_name(&doc.text));
|
||||
let mut l = build::layer(proj, &comp, if name.is_empty() { "Text" } else { &name }, LayerSource::Text, (comp.width, comp.height), None);
|
||||
fit_span(&mut l, span);
|
||||
// Named after its text, the name follows the text until renamed; a given name does not.
|
||||
l.name_follows_text = Some(layer_name.as_deref().is_none_or(str::is_empty));
|
||||
if let Some(pr) = l.props.prop_mut("text/sourceText") {
|
||||
@@ -213,6 +250,9 @@ pub(super) fn new_text(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
Ok(id)
|
||||
})?;
|
||||
if edit {
|
||||
show_span(s, cid, span);
|
||||
}
|
||||
Ok(json!({"layer": id.0}))
|
||||
}
|
||||
|
||||
@@ -232,8 +272,9 @@ pub(super) fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.and_then(|v| v.as_array())
|
||||
.map(|a| [a.first().and_then(Value::as_f64).unwrap_or(0.0), a.get(1).and_then(Value::as_f64).unwrap_or(0.0)]);
|
||||
let name = str_p(p, "name").unwrap_or("Shape Layer 1").to_string();
|
||||
let span = new_layer_span(s, &comp);
|
||||
let id = s.edit("New Shape Layer", None, |proj, st| {
|
||||
let lid = crate::cmd::shape_tool::new_shape_layer(proj, st, &comp, cid, Some(&name), pos)?;
|
||||
let lid = crate::cmd::shape_tool::new_shape_layer(proj, st, &comp, cid, Some(&name), pos, span)?;
|
||||
let mut next = proj.next_id;
|
||||
let g = crate::cmd::shape_tool::shape_group(&mut Ids(&mut next), &kind, size, &paint);
|
||||
proj.next_id = next;
|
||||
@@ -249,8 +290,10 @@ fn new_simple(s: &mut Session, p: &Value, src: LayerSource, name: &str, label: &
|
||||
let cid = comp_id(s, p)?;
|
||||
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone();
|
||||
let name = str_p(p, "name").unwrap_or(name).to_string();
|
||||
let span = new_layer_span(s, &comp);
|
||||
let id = s.edit(label, None, |proj, st| {
|
||||
let l = build::layer(proj, &comp, &name, src, (comp.width, comp.height), None);
|
||||
let mut l = build::layer(proj, &comp, &name, src, (comp.width, comp.height), None);
|
||||
fit_span(&mut l, span);
|
||||
insert_layer(proj, st, cid, l)
|
||||
})?;
|
||||
Ok(json!({"layer": id.0}))
|
||||
|
||||
@@ -762,7 +762,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
cmd!("mask.new", "New Mask from Points", [], None, "{layer?, vertices: [[x,y]…], inTangents?, outTangents?, closed?, mode?}", has_layers, new_mask),
|
||||
cmd!("mask.addVertex", "Add Mask Vertex", [], None, "{layer?, mask, point: [x,y], in?, out?, index?}", has_layers, add_vertex),
|
||||
cmd!("mask.setVertex", "Set Mask Vertex", [], None, "{layer?, mask, index, point?, in?, out?, merge?}", has_layers, set_vertex),
|
||||
cmd!("mask.setClosed", "Closed", ["Layer", "Mask and Shape Path"], None, "{layer?, mask, closed?}", has_layers, set_closed),
|
||||
cmd!("mask.setClosed", "Closed", [], None, "{layer?, mask, closed?}", has_layers, set_closed),
|
||||
cmd!("mask.setRoundness", "Set Mask Roundness", [], None, "{layer?, mask, roundness (corner radius in px; 0 = square)}", has_layers, set_roundness),
|
||||
cmd!("mask.selectVertices", "Select Mask Vertices", [], None, "{vertices: [{layer, mask, index}], add?, toggle?}", has_comp, select_vertices),
|
||||
cmd!("mask.moveVertices", "Move Mask Vertices", [], None, "{vertices?: [{layer, mask, index}], delta: [dx,dy], merge?}", has_vertices, move_vertices),
|
||||
|
||||
@@ -12,6 +12,9 @@ use super::layer_menu::target_masks;
|
||||
use super::{CommandSpec, b_p, bad, f_p, has_comp, has_layers, layer_mut, layer_p, merge_p, str_p};
|
||||
use crate::{EngineError, Result, Session, VertexRef, cmd};
|
||||
|
||||
mod closed;
|
||||
pub(crate) use closed::selected_paths_closed;
|
||||
|
||||
// ---------------------------------------------------------------- RotoBezier
|
||||
|
||||
/// Automatic (RotoBezier) tangents: each vertex's tangents follow the line through its
|
||||
@@ -547,6 +550,7 @@ fn ungroup_shapes(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!("path.toggleClosed", "Closed", ["Layer", "Mask and Shape Path"], None, "{}", closed::has_paths, closed::toggle_closed),
|
||||
cmd!("path.rotoBezier", "RotoBezier", ["Layer", "Mask and Shape Path"], None, "{layer?, mask?, value?}", has_layers, roto_bezier),
|
||||
cmd!("path.convertToBezier", "Convert To Bezier Path", ["Layer", "Mask and Shape Path"], None, "{layer?, mask?}", has_layers, convert_to_bezier),
|
||||
cmd!("path.groupShapes", "Group Shapes", ["Layer"], Some("Cmd+G"), "{layer?, items?: [uid]}", has_shape_items, group_shapes),
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
//! The selection-aware Closed menu action. Explicit edits retain `mask.setClosed`'s API.
|
||||
|
||||
use effectcraft_keyframe::Value as KV;
|
||||
use effectcraft_project::{GroupKind, Layer, LayerId, Node, PropGroup, Uid};
|
||||
use effectcraft_time::Tick;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::super::{bad, layer_mut};
|
||||
use crate::{EngineError, Result, Session, VertexRef};
|
||||
|
||||
const MAX_DEPTH: usize = 128;
|
||||
|
||||
struct Target {
|
||||
layer: LayerId,
|
||||
route: Vec<usize>,
|
||||
closed: bool,
|
||||
editable: bool,
|
||||
}
|
||||
|
||||
struct Selection<'a> {
|
||||
vertices: &'a [VertexRef],
|
||||
props: &'a [(LayerId, Uid)],
|
||||
time: Tick,
|
||||
}
|
||||
|
||||
/// A route uses checked child access and never recurses through an input-controlled tree.
|
||||
fn group_at_mut<'a>(mut group: &'a mut PropGroup, route: &[usize]) -> Option<&'a mut PropGroup> {
|
||||
for &index in route {
|
||||
group = group.children.get_mut(index)?.as_group_mut()?;
|
||||
}
|
||||
Some(group)
|
||||
}
|
||||
|
||||
/// The selected paths on the selected layers, else every path of those layers (as the other
|
||||
/// Mask and Shape Path commands do). Locked layers and masks count as matches, so selecting one
|
||||
/// never falls back to its siblings, and are then left out.
|
||||
fn targets(s: &Session) -> Result<Vec<Target>> {
|
||||
let comp = s.active_comp().ok_or(EngineError::NoComp)?;
|
||||
let selection = Selection { vertices: &s.state.selected_vertices, props: &s.state.selected_props, time: s.time() };
|
||||
let mut targets = Vec::new();
|
||||
for whole_layers in [false, true] {
|
||||
// Only selected layers: navigation and insertion can retain point/property selections
|
||||
// on other layers, and every match below is per layer.
|
||||
for layer in comp.layers.iter().filter(|l| s.state.selected_layers.contains(&l.id)) {
|
||||
collect(&selection, layer, &layer.props, &mut Vec::new(), whole_layers, layer.switches.locked, &mut targets)?;
|
||||
}
|
||||
if !targets.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
targets.retain(|target| target.editable);
|
||||
if targets.is_empty() {
|
||||
return Err(bad("path.toggleClosed", "select an unlocked mask or Bezier shape path"));
|
||||
}
|
||||
Ok(targets)
|
||||
}
|
||||
|
||||
fn collect(
|
||||
selection: &Selection,
|
||||
layer: &Layer,
|
||||
group: &PropGroup,
|
||||
route: &mut Vec<usize>,
|
||||
inherited: bool,
|
||||
locked: bool,
|
||||
targets: &mut Vec<Target>,
|
||||
) -> Result<()> {
|
||||
if route.len() > MAX_DEPTH {
|
||||
return Err(bad("path.toggleClosed", "path group nesting exceeds 128 levels"));
|
||||
}
|
||||
let locked = locked || matches!(group.kind, GroupKind::Mask { locked: true, .. });
|
||||
let selected = inherited || selection.props.contains(&(layer.id, group.uid));
|
||||
if super::super::mask::is_path_group(group)
|
||||
&& let Some(prop) = group.get("path")
|
||||
&& let KV::Path(path) = prop.value_at(layer.layer_time(selection.time))
|
||||
{
|
||||
// Selecting any of a path group's properties (Mask Path, Feather, Opacity…) or its points
|
||||
// selects the path.
|
||||
let selected = selected
|
||||
|| group.children.iter().any(|c| matches!(c, Node::Prop(p) if selection.props.contains(&(layer.id, p.uid))))
|
||||
|| selection.vertices.iter().any(|v| v.layer == layer.id && v.mask == group.uid && v.index < path.vertices.len());
|
||||
if selected {
|
||||
targets.push(Target { layer: layer.id, route: route.clone(), closed: path.closed, editable: !locked });
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
for (index, child) in group.children.iter().enumerate() {
|
||||
let Some(child) = child.as_group() else { continue };
|
||||
route.push(index);
|
||||
collect(selection, layer, child, route, selected, locked, targets)?;
|
||||
route.pop();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn selected_paths_closed(s: &Session) -> Option<bool> {
|
||||
targets(s).ok().map(|targets| targets.iter().all(|target| target.closed))
|
||||
}
|
||||
|
||||
pub(super) fn has_paths(s: &Session) -> std::result::Result<(), String> {
|
||||
targets(s).map(|_| ()).map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
pub(super) fn toggle_closed(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
let targets = targets(s)?;
|
||||
let closed = !targets.iter().all(|target| target.closed);
|
||||
let cid = s.active_comp_id().ok_or(EngineError::NoComp)?;
|
||||
let time = s.time();
|
||||
s.edit("Closed", None, |project, _| {
|
||||
for target in &targets {
|
||||
let layer = layer_mut(project, cid, target.layer)?;
|
||||
let time = layer.layer_time(time);
|
||||
let group = group_at_mut(&mut layer.props, &target.route).ok_or_else(|| bad("path.toggleClosed", "the selected path no longer exists"))?;
|
||||
let roto = super::is_roto(group);
|
||||
let prop = group.get_mut("path").ok_or_else(|| bad("path.toggleClosed", "the selected group has no path"))?;
|
||||
let KV::Path(mut path) = prop.value_at(time) else { return Err(bad("path.toggleClosed", "the selected value is not a path")) };
|
||||
if path.closed == closed {
|
||||
continue;
|
||||
}
|
||||
// As `mask.setClosed`: a path read from a file may have fewer tangents than vertices (#1250).
|
||||
path.fit_tangents();
|
||||
path.closed = closed;
|
||||
if roto {
|
||||
super::roto_smooth(&mut path);
|
||||
}
|
||||
prop.set_value_at(time, KV::Path(path));
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
Ok(json!({"paths": targets.len(), "closed": closed}))
|
||||
}
|
||||
@@ -1370,6 +1370,13 @@ fn convert_expr_to_keys(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if sel.is_empty() {
|
||||
return Err(bad(CMD, "no `path` given and no selected property has an expression"));
|
||||
}
|
||||
// Every layer is checked before the first is converted: a refusal leaves them all as they were.
|
||||
let step = comp.frame_duration().0;
|
||||
for (lid, _) in &sel {
|
||||
if let Some(l) = comp.layer(*lid) {
|
||||
check_convertible(step, l)?;
|
||||
}
|
||||
}
|
||||
let mut n = 0;
|
||||
for (lid, uid) in sel {
|
||||
n += convert_one_expr(s, cid, lid, uid)?.as_u64().unwrap_or(0);
|
||||
@@ -1380,14 +1387,36 @@ fn convert_expr_to_keys(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
convert_one_expr(s, cid, lid, uid)
|
||||
}
|
||||
|
||||
/// Most keyframes Convert Expression to Keyframes makes: 2^20, 4.8 hours at 60 fps (After Effects
|
||||
/// compositions run to 3 hours, 648,000 frames at 60 fps).
|
||||
const MAX_CONVERTED_KEYS: u64 = 1 << 20;
|
||||
const _: () = assert!(MAX_CONVERTED_KEYS >= 3 * 3600 * 60);
|
||||
|
||||
/// One evaluation and one key per frame of the layer: its In and Out points and the composition's
|
||||
/// frame duration are saved data, so the step must advance and the count (a partial last frame
|
||||
/// rounded up, as the loop steps, in 128 bits) is checked before any key is made.
|
||||
fn check_convertible(step: i64, layer: &Layer) -> Result<()> {
|
||||
const CMD: &str = "prop.convertExpressionToKeyframes";
|
||||
if step <= 0 {
|
||||
return Err(bad(CMD, "the composition's frame duration is not positive"));
|
||||
}
|
||||
let (step, span) = (i128::from(step), i128::from(layer.out_point.0) - i128::from(layer.in_point.0));
|
||||
let frames = if span > 0 { (span + step - 1) / step } else { 0 };
|
||||
if frames > i128::from(MAX_CONVERTED_KEYS) {
|
||||
return Err(bad(CMD, format!("the layer spans {frames} frames; at most {MAX_CONVERTED_KEYS} are converted")));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Replaces the expression on one property with keyframes sampled from its evaluated values.
|
||||
/// Returns the number of properties converted.
|
||||
fn convert_one_expr(s: &mut Session, cid: ItemId, lid: LayerId, uid: Uid) -> Result<Value> {
|
||||
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone();
|
||||
let layer = comp.layer(lid).ok_or(EngineError::NoComp)?.clone();
|
||||
let pr = layer.props.find(uid).ok_or_else(|| bad("prop.convertExpressionToKeyframes", "no property"))?.clone();
|
||||
let mut keys = vec![];
|
||||
let fd = comp.frame_duration();
|
||||
check_convertible(fd.0, &layer)?;
|
||||
let mut keys = vec![];
|
||||
let mut t = layer.in_point;
|
||||
while t < layer.out_point {
|
||||
let ctx = effectcraft_render::EvalCtx {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Recover moved media without replacing interpretation, sequence ranges or layer timing.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::path::Path;
|
||||
|
||||
use effectcraft_project::{Footage, FootageKind, ItemId, ItemKind};
|
||||
@@ -11,6 +11,8 @@ use crate::{Result, Session, cmd};
|
||||
|
||||
const COMMAND: &str = "file.relinkFootage";
|
||||
const ENTRY_LIMIT: usize = 100_000;
|
||||
/// Candidates listed for one missing item (more than one already makes it ambiguous).
|
||||
const MAX_CANDIDATES: usize = 32;
|
||||
|
||||
fn enabled(_: &Session) -> std::result::Result<(), String> {
|
||||
if cfg!(target_arch = "wasm32") { Err("folder relinking requires the desktop filesystem".into()) } else { Ok(()) }
|
||||
@@ -95,6 +97,8 @@ fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let wait = p.get("wait").and_then(Value::as_bool).unwrap_or(false);
|
||||
s.spawn_task("relink", "Relinking missing footage", wait, move |ctl| {
|
||||
let index = files(&folder, ctl)?;
|
||||
// Each candidate file is probed once, however many missing items share its name.
|
||||
let mut probes: HashMap<String, std::result::Result<Footage, String>> = HashMap::new();
|
||||
let mut rows = vec![];
|
||||
let mut updates = vec![];
|
||||
for (n, (id, old)) in missing.iter().enumerate() {
|
||||
@@ -104,19 +108,21 @@ fn run(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let supported = matches!(old.kind, FootageKind::Still | FootageKind::Sequence | FootageKind::Video | FootageKind::Audio)
|
||||
&& old.layer.is_none() && old.page == 0;
|
||||
let name = Path::new(&old.path).file_name().and_then(std::ffi::OsStr::to_str).unwrap_or("");
|
||||
let paths = index.get(name).cloned().unwrap_or_default();
|
||||
let paths = index.get(name).map_or(&[][..], Vec::as_slice);
|
||||
let mut candidates = vec![];
|
||||
let mut accepted = vec![];
|
||||
for path in paths {
|
||||
// Past a few candidates the item is ambiguous anyway: list no more of them.
|
||||
for path in paths.iter().take(MAX_CANDIDATES) {
|
||||
if ctl.cancelled() {
|
||||
return Err(crate::render_queue::CANCELLED.into());
|
||||
}
|
||||
let mut reason = "source type requires manual replacement".to_string();
|
||||
let mut confidence = "low";
|
||||
if supported {
|
||||
match importer.probe(&path) {
|
||||
Ok(f) if compatible(old, &f) => {
|
||||
if old.kind != FootageKind::Sequence || (!old.sequence.is_empty() && sequence_at(old, &path).is_some()) {
|
||||
let probed = probes.entry(path.clone()).or_insert_with(|| importer.probe(path));
|
||||
match probed {
|
||||
Ok(f) if compatible(old, f) => {
|
||||
if old.kind != FootageKind::Sequence || (!old.sequence.is_empty() && sequence_at(old, path).is_some()) {
|
||||
confidence = "high";
|
||||
reason = "exact filename and compatible source metadata".into();
|
||||
accepted.push(path.clone());
|
||||
@@ -284,6 +290,19 @@ mod tests {
|
||||
assert_eq!(get(&s, id), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn many_same_named_candidates_list_at_most_the_cap() {
|
||||
let dir = Fixture::new();
|
||||
let (mut s, id, old) = setup(&dir, "plate.png");
|
||||
for i in 0..MAX_CANDIDATES + 8 {
|
||||
dir.file(&format!("new/{i}/plate.png"), b"valid");
|
||||
}
|
||||
let r = search(&mut s, &dir, false);
|
||||
assert_eq!(r["items"][0]["status"], "ambiguous");
|
||||
assert_eq!(r["items"][0]["candidates"].as_array().unwrap().len(), MAX_CANDIDATES);
|
||||
assert_eq!(get(&s, id), old);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicates_and_invalid_media_are_reported_without_edits() {
|
||||
let dir = Fixture::new();
|
||||
|
||||
@@ -8,6 +8,7 @@ use effectcraft_geom::vec2;
|
||||
use effectcraft_keyframe::{Gradient, ShapePath, Value as KV};
|
||||
use effectcraft_project::build::{self, Ids};
|
||||
use effectcraft_project::{Comp, ItemId, LayerId, LayerSource, Project, PropGroup};
|
||||
use effectcraft_time::Tick;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -263,7 +264,8 @@ pub(crate) fn draw_target(s: &Session, comp: &Comp, p: &Value, cmd: &str) -> Res
|
||||
}
|
||||
|
||||
/// A new empty shape layer ("Shape Layer n" unless `name`) with its Position at `position` (comp
|
||||
/// pixels; else the comp centre), above the selected layer and selected.
|
||||
/// pixels; else the comp centre), above the selected layer and selected. `span` is the
|
||||
/// [`new_layer_span`](super::layer::new_layer_span) the layer takes, if any.
|
||||
pub(crate) fn new_shape_layer(
|
||||
proj: &mut Project,
|
||||
st: &mut EditorState,
|
||||
@@ -271,11 +273,13 @@ pub(crate) fn new_shape_layer(
|
||||
cid: ItemId,
|
||||
name: Option<&str>,
|
||||
position: Option<[f64; 2]>,
|
||||
span: Option<(Tick, Tick)>,
|
||||
) -> Result<LayerId> {
|
||||
let count = comp.layers.iter().filter(|l| matches!(l.source, LayerSource::Shape)).count();
|
||||
let name = name.map(str::to_string).unwrap_or_else(|| format!("Shape Layer {}", count + 1));
|
||||
let mut l = build::layer(proj, comp, &name, LayerSource::Shape, (comp.width, comp.height), None);
|
||||
place(&mut l, position);
|
||||
super::layer::fit_span(&mut l, span);
|
||||
insert_layer(proj, st, cid, l)
|
||||
}
|
||||
|
||||
@@ -346,10 +350,11 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
_ => "Rectangle Tool",
|
||||
};
|
||||
let name = str_p(p, "name");
|
||||
let span = super::layer::new_layer_span(s, &comp);
|
||||
let (lid, group) = s.edit(label, None, |proj, st| {
|
||||
let lid = match target {
|
||||
Some(l) => l,
|
||||
None => new_shape_layer(proj, st, &comp, cid, name, Some(pos))?,
|
||||
None => new_shape_layer(proj, st, &comp, cid, name, Some(pos), span)?,
|
||||
};
|
||||
let mut next = proj.next_id;
|
||||
let g = shape_group(&mut Ids(&mut next), kind, size, &paint).ok_or_else(|| bad(c, "unknown kind"))?;
|
||||
@@ -358,6 +363,9 @@ fn new_shape(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
st.selected_layers = vec![lid];
|
||||
Ok((lid, group))
|
||||
})?;
|
||||
if target.is_none() {
|
||||
super::layer::show_span(s, cid, span);
|
||||
}
|
||||
Ok(json!({"layer": lid.0, "group": group}))
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,9 @@ pub fn display_duration(s: &Session, comp: &Comp, len: Tick) -> String {
|
||||
}
|
||||
|
||||
/// Parse a time typed in the project's Time Display Style (relative `+n`/`-n` from frame
|
||||
/// `current`) into a comp frame.
|
||||
/// `current`) into a comp frame. In Timecode style, in a comp whose Start Timecode is negative, a
|
||||
/// `-` before a timecode with separators (`-0:00:00:12`, `-1.00`) is the sign of a displayed time,
|
||||
/// not a step back; a bare `-n` still steps back.
|
||||
pub fn parse_display_time(s: &Session, comp: &Comp, text: &str, current: i64) -> std::result::Result<i64, String> {
|
||||
let fr = comp.frame_rate;
|
||||
let st = &s.project.settings;
|
||||
@@ -53,6 +55,20 @@ pub fn parse_display_time(s: &Session, comp: &Comp, text: &str, current: i64) ->
|
||||
effectcraft_time::parse_feet_frames(text, pf, current + start).map(|f| f - start).map_err(|e| e.0)
|
||||
}
|
||||
None => {
|
||||
// A comp whose Start Timecode is negative shows the times before 0 with a `-`
|
||||
// (`-0:00:00:12`), so there a `-` before a timecode with separators is the sign of a
|
||||
// displayed time and typing what the display shows lands on the frame it shows. A bare
|
||||
// `-n` still moves n frames back.
|
||||
if st.time_display == TimeDisplayStyle::Timecode
|
||||
&& comp.display_start < Tick::ZERO
|
||||
&& let Some(body) = text.trim().strip_prefix('-').map(str::trim_start)
|
||||
&& body.contains([':', ';', '.', ','])
|
||||
&& !body.starts_with(['+', '-'])
|
||||
{
|
||||
let drop_frame = fr.supports_drop_frame() && body.contains(';');
|
||||
let shown = effectcraft_time::parse_timecode(body, fr, drop_frame, current).map_err(|e| e.0)?;
|
||||
return Ok(shown.saturating_neg().saturating_sub(fr.frame_at(comp.display_start)));
|
||||
}
|
||||
// The Timeline shows non-drop timecode (`:`), so typed `:`/`.` text is read as non-drop
|
||||
// too; only explicit `;` fields mean drop-frame (#890).
|
||||
let drop_frame = fr.supports_drop_frame() && text.contains(';');
|
||||
|
||||
@@ -282,10 +282,11 @@ fn new_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
path.vertices.iter().fold(([f64::MAX; 2], [f64::MIN; 2]), |(lo, hi), v| ([lo[0].min(v[0]), lo[1].min(v[1])], [hi[0].max(v[0]), hi[1].max(v[1])]));
|
||||
let bounds = [lo[0], lo[1], hi[0] - lo[0], hi[1] - lo[1]];
|
||||
let name = str_p(p, "name").map(str::to_string);
|
||||
let span = super::layer::new_layer_span(s, &comp);
|
||||
let (lid, group, path_uid) = s.edit("Pen Tool", None, |proj, st| {
|
||||
let lid = match target {
|
||||
Some(l) => l,
|
||||
None => super::shape_tool::new_shape_layer(proj, st, &comp, cid, name.as_deref(), None)?,
|
||||
None => super::shape_tool::new_shape_layer(proj, st, &comp, cid, name.as_deref(), None, span)?,
|
||||
};
|
||||
let mut next = proj.next_id;
|
||||
let mut ids = Ids(&mut next);
|
||||
@@ -300,6 +301,9 @@ fn new_path(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
st.selected_vertices = vec![VertexRef { layer: lid, mask: path_uid, index: n - 1 }];
|
||||
Ok((lid, group, path_uid))
|
||||
})?;
|
||||
if target.is_none() {
|
||||
super::layer::show_span(s, cid, span);
|
||||
}
|
||||
Ok(json!({"layer": lid.0, "group": group, "path": path_uid}))
|
||||
}
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user