Merge current main and pin merged shared Sony LUT readers

This commit is contained in:
Dylan M. Taylor committed 2026-11-16 06:51:41 -05:00
commit 9fa8a069da
105 files changed
+10974 -18064

No files matched your search

Generated
+8537 -8323
View File
File diff suppressed because it is too large. Load diff
+240 -251
View File
@@ -1,251 +1,240 @@
[workspace]
resolver = "3"
members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/*"]
[workspace.package]
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 = "47107f660191de2f0b425569506f4daa4faadac9", version = "=0.1.0", default-features = false, features = ["read"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-eps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-mesh = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", default-features = false, features = ["stl", "obj", "gltf", "fbx"] }
craft-aep = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-doc = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-vectorcraft-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
# craft-update: app-specific pretend versions apply to startup and manual checks.
craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "47107f660191de2f0b425569506f4daa4faadac9", features = ["egui"] }
# internal
effectcraft-time = { path = "crates/time" }
effectcraft-geom = { path = "crates/geom" }
effectcraft-color = { path = "crates/color" }
effectcraft-raster = { path = "crates/raster" }
effectcraft-path = { path = "crates/path" }
effectcraft-keyframe = { path = "crates/keyframe" }
effectcraft-project = { path = "crates/project" }
effectcraft-text = { path = "crates/text" }
effectcraft-effects = { path = "crates/effects" }
effectcraft-track = { path = "crates/track" }
effectcraft-segment = { path = "crates/segment" }
effectcraft-sam2 = { path = "crates/sam2" }
effectcraft-model = { path = "crates/model" }
effectcraft-render = { path = "crates/render" }
effectcraft-gpu = { path = "crates/gpu" }
effectcraft-media = { path = "crates/media" }
effectcraft-export = { path = "crates/export" }
effectcraft-expr = { path = "crates/expr" }
effectcraft-lottie = { path = "crates/lottie" }
effectcraft-interchange = { path = "crates/interchange" }
effectcraft-pdf = { path = "crates/pdf" }
effectcraft-psd = { path = "crates/psd" }
effectcraft-aep = { path = "crates/aep" }
effectcraft-svg = { path = "crates/svg" }
effectcraft-engine = { path = "crates/engine" }
effectcraft-host = { path = "crates/host" }
effectcraft-ui-egui = { path = "crates/ui-egui" }
effectcraft-automation = { path = "crates/automation" }
effectcraft-opusenc = { path = "crates/opusenc" }
effectcraft-script = { path = "crates/script" }
effectcraft-plugin = { path = "crates/plugin" }
effectcraft-helper-host = { path = "crates/helper-host" }
# foundation
thiserror = "2"
serde = { version = "1", features = ["derive", "rc"] }
serde_json = { version = "1", features = ["float_roundtrip"] }
log = "0.4"
# All date/time conversion goes through chrono (no hand-rolled calendar code beyond the 400-year range extension; AGENTS.md).
chrono = { version = "0.4.45", default-features = false, features = ["std"] }
rayon = "1"
# std::time::Instant/SystemTime on native; the browser clock on wasm32 (std's panic there).
web-time = "1"
gif = "0.14"
proptest = "1"
# scripting (expressions)
boa_engine = { version = "0.22", default-features = false }
# WebAssembly effect plug-ins (sandboxed interpreter; MIT/Apache-2.0, pure Rust)
wasmi = { version = "2", default-features = false, features = ["std", "validate", "wat", "deterministic", "portable-dispatch"] }
# geometry / fonts
kurbo = { version = "0.13", features = ["serde"] }
tiny-skia = { version = "0.12", default-features = false, features = ["std", "simd"] }
skrifa = { version = "0.44", default-features = false, features = ["std"] }
harfrust = { version = "0.12", default-features = false, features = ["std"] }
exr = "1.74"
image = { version = "0.25", default-features = false, features = ["png", "jpeg"] }
same-file = "1.0"
tempfile = "3.27"
# codecs + containers shared by the Craft apps (crates/media, crates/export); keep all three on one rev.
craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", default-features = false, features = ["isobmff", "matroska"] }
craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", default-features = false, features = ["aac-decode", "aac-encode", "opus-decode"] }
craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", features = ["h264-encode", "prores", "hevc-encode", "vp9-encode", "av1", "threads"] }
# footage decoding: FilmCraft's pure-Rust crates, pinned to a revision (plan/adr/0001).
# Only `crates/media`, `crates/export` and `crates/interchange` may depend on these.
filmcraft-codecs = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
filmcraft-media = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
filmcraft-frame = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
filmcraft-color = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
filmcraft-time = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
# 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" }
# 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" }
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)
filmcraft-interchange = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
filmcraft-project = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
filmcraft-geom = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
# gpu (the same wgpu eframe / egui-wgpu use)
wgpu = { version = "30.0.1", features = ["fragile-send-sync-non-atomic-wasm"] }
bytemuck = "1"
pollster = "1"
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 = "47107f660191de2f0b425569506f4daa4faadac9", features = ["serde"] }
# `default_fonts` and `monochrome_emoji_fonts` are explicit: the app's `FontDefinitions::default()`
# and its `✔`/`▣` chrome glyphs need both, and kittest's own dev-dependencies do not propagate.
egui = { version = "0.37", default-features = false, features = ["default_fonts", "monochrome_emoji_fonts"] }
eframe = { version = "0.37", default-features = false }
# Resizable table columns (About ▸ Contributors / Models); no image loaders.
egui_extras = { version = "0.37", default-features = false }
egui_kittest = "0.37"
rfd = "0.17"
cpal = "0.16"
# The operating system's interface language (MIT/Apache-2.0, no dependencies on desktop).
sys-locale = "0.3"
# Local co-development against a FilmCraft checkout next to this repo: uncomment.
# [patch."https://github.com/storytold/filmcraft"]
# filmcraft-codecs = { path = "../filmcraft/crates/codecs" }
# filmcraft-media = { path = "../filmcraft/crates/media" }
# filmcraft-frame = { path = "../filmcraft/crates/frame" }
# filmcraft-color = { path = "../filmcraft/crates/color" }
# filmcraft-time = { path = "../filmcraft/crates/time" }
# filmcraft-mpegts = { path = "../filmcraft/crates/mpegts" }
[workspace.lints.rust]
unsafe_code = "forbid"
[workspace.lints.clippy]
cognitive_complexity = "deny"
too_many_arguments = "deny"
wildcard_imports = "deny"
dbg_macro = "warn"
todo = "warn"
needless_range_loop = "allow"
type_complexity = "allow"
[profile.dev.package."*"]
opt-level = 2
debug = false
[profile.dev]
opt-level = 1
# Panics and backtraces keep file and line; full debug info only made the many test binaries large.
debug = "line-tables-only"
# Same settings as `dev`, but with full debug info for interactive debugger sessions: local
# variables, parameters and column breakpoints work. Use `--profile dev-full` when debugging;
# everything else stays on the fast default. See CONTRIBUTING.md, "Setup".
[profile.dev-full]
inherits = "dev"
debug = true
# Pixel hot loops: keep debug builds interactive.
[profile.dev.package.effectcraft-raster]
opt-level = 3
[profile.dev.package.effectcraft-render]
opt-level = 3
[profile.dev.package.effectcraft-gpu]
opt-level = 3
[profile.dev.package.effectcraft-effects]
opt-level = 3
[profile.dev.package.effectcraft-track]
opt-level = 3
[profile.dev.package.effectcraft-path]
opt-level = 3
[profile.dev.package.effectcraft-media]
opt-level = 3
[profile.dev.package.effectcraft-opusenc]
opt-level = 3
[profile.dev.package.effectcraft-export]
opt-level = 3
# craft-libs codec hot loops.
[profile.dev.package.craft-video]
opt-level = 3
[profile.dev.package.craft-audio]
opt-level = 3
[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-hevc]
opt-level = 3
[profile.dev.package.filmcraft-vp9]
opt-level = 3
[profile.dev.package.filmcraft-dnx]
opt-level = 3
[profile.dev.package.filmcraft-av1]
opt-level = 3
[profile.release]
lto = "thin"
codegen-units = 1
[patch.crates-io]
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", version = "=0.7.2" }
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", version = "=0.3.0" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
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" }
eframe = { 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" }
[workspace]
resolver = "3"
members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/*"]
[workspace.package]
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-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", version = "=0.1.0", default-features = false, features = ["read"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-ps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", default-features = false, features = ["read"] }
craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-mesh = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87", default-features = false, features = ["stl", "obj", "gltf", "fbx"] }
craft-aep = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-doc = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-vectorcraft-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
# 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 = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87", features = ["egui"] }
# internal
effectcraft-time = { path = "crates/time" }
effectcraft-geom = { path = "crates/geom" }
effectcraft-color = { path = "crates/color" }
effectcraft-raster = { path = "crates/raster" }
effectcraft-path = { path = "crates/path" }
effectcraft-keyframe = { path = "crates/keyframe" }
effectcraft-project = { path = "crates/project" }
effectcraft-text = { path = "crates/text" }
effectcraft-effects = { path = "crates/effects" }
effectcraft-track = { path = "crates/track" }
effectcraft-segment = { path = "crates/segment" }
effectcraft-sam2 = { path = "crates/sam2" }
effectcraft-model = { path = "crates/model" }
effectcraft-render = { path = "crates/render" }
effectcraft-gpu = { path = "crates/gpu" }
effectcraft-media = { path = "crates/media" }
effectcraft-export = { path = "crates/export" }
effectcraft-expr = { path = "crates/expr" }
effectcraft-lottie = { path = "crates/lottie" }
effectcraft-interchange = { path = "crates/interchange" }
effectcraft-pdf = { path = "crates/pdf" }
effectcraft-psd = { path = "crates/psd" }
effectcraft-aep = { path = "crates/aep" }
effectcraft-svg = { path = "crates/svg" }
effectcraft-engine = { path = "crates/engine" }
effectcraft-host = { path = "crates/host" }
effectcraft-ui-egui = { path = "crates/ui-egui" }
effectcraft-automation = { path = "crates/automation" }
effectcraft-script = { path = "crates/script" }
effectcraft-plugin = { path = "crates/plugin" }
effectcraft-helper-host = { path = "crates/helper-host" }
# foundation
thiserror = "2"
serde = { version = "1", features = ["derive", "rc"] }
serde_json = { version = "1", features = ["float_roundtrip"] }
log = "0.4"
# All date/time conversion goes through chrono (no hand-rolled calendar code beyond the 400-year range extension; AGENTS.md).
chrono = { version = "0.4.45", default-features = false, features = ["std"] }
rayon = "1"
# std::time::Instant/SystemTime on native; the browser clock on wasm32 (std's panic there).
web-time = "1"
gif = "0.14"
proptest = "1"
# scripting (expressions)
boa_engine = { version = "0.22", default-features = false }
# WebAssembly effect plug-ins (sandboxed interpreter; MIT/Apache-2.0, pure Rust)
wasmi = { version = "2", default-features = false, features = ["std", "validate", "wat", "deterministic", "portable-dispatch"] }
# geometry / fonts
kurbo = { version = "0.13", features = ["serde"] }
tiny-skia = { version = "0.12", default-features = false, features = ["std", "simd"] }
skrifa = { version = "0.44", default-features = false, features = ["std"] }
harfrust = { version = "0.12", default-features = false, features = ["std"] }
exr = "1.74"
image = { version = "0.25", default-features = false, features = ["png", "jpeg"] }
same-file = "1.0"
tempfile = "3.27"
# 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 = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87", default-features = false, features = ["isobmff", "matroska"] }
craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87", default-features = false, features = ["aac-decode", "aac-encode", "opus-decode", "opus-encode"] }
craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87", 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" }
# timeline interchange (EDL, FCP7 XML, FCPXML, OTIO) for Premiere Pro interop (crates/interchange only)
filmcraft-interchange = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
filmcraft-project = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
filmcraft-geom = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" }
# gpu (the same wgpu eframe / egui-wgpu use)
wgpu = { version = "30.0.1", features = ["fragile-send-sync-non-atomic-wasm"] }
bytemuck = "1"
pollster = "1"
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 = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87", 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"] }
eframe = { version = "0.37", default-features = false }
# Resizable table columns (About ▸ Contributors / Models); no image loaders.
egui_extras = { version = "0.37", default-features = false }
egui_kittest = "0.37"
rfd = "0.17"
cpal = "0.16"
# The operating system's interface language (MIT/Apache-2.0, no dependencies on desktop).
sys-locale = "0.3"
# Local co-development against a FilmCraft checkout next to this repo: uncomment.
# [patch."https://github.com/storytold/filmcraft"]
# filmcraft-codecs = { path = "../filmcraft/crates/codecs" }
# filmcraft-media = { path = "../filmcraft/crates/media" }
# filmcraft-frame = { path = "../filmcraft/crates/frame" }
# filmcraft-color = { path = "../filmcraft/crates/color" }
# filmcraft-time = { path = "../filmcraft/crates/time" }
[workspace.lints.rust]
unsafe_code = "forbid"
[workspace.lints.clippy]
cognitive_complexity = "deny"
too_many_arguments = "deny"
wildcard_imports = "deny"
dbg_macro = "warn"
todo = "warn"
needless_range_loop = "allow"
type_complexity = "allow"
[profile.dev.package."*"]
opt-level = 2
debug = false
[profile.dev]
opt-level = 1
# Panics and backtraces keep file and line; full debug info only made the many test binaries large.
debug = "line-tables-only"
# Same settings as `dev`, but with full debug info for interactive debugger sessions: local
# variables, parameters and column breakpoints work. Use `--profile dev-full` when debugging;
# everything else stays on the fast default. See CONTRIBUTING.md, "Setup".
[profile.dev-full]
inherits = "dev"
debug = true
# Pixel hot loops: keep debug builds interactive.
[profile.dev.package.effectcraft-raster]
opt-level = 3
[profile.dev.package.effectcraft-render]
opt-level = 3
[profile.dev.package.effectcraft-gpu]
opt-level = 3
[profile.dev.package.effectcraft-effects]
opt-level = 3
[profile.dev.package.effectcraft-track]
opt-level = 3
[profile.dev.package.effectcraft-path]
opt-level = 3
[profile.dev.package.effectcraft-media]
opt-level = 3
[profile.dev.package.effectcraft-export]
opt-level = 3
# craft-libs codec hot loops.
[profile.dev.package.craft-video]
opt-level = 3
[profile.dev.package.craft-audio]
opt-level = 3
[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-hevc]
opt-level = 3
[profile.dev.package.filmcraft-vp9]
opt-level = 3
[profile.dev.package.filmcraft-dnx]
opt-level = 3
[profile.dev.package.filmcraft-av1]
opt-level = 3
[profile.release]
lto = "thin"
codegen-units = 1
[patch.crates-io]
hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", version = "=0.7.2" }
hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", 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" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" }
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" }
eframe = { 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" }
+1
View File
@@ -2,6 +2,7 @@
**Stage: alpha** · next: beta, ~30 points of "ready for real work" and ~350–620 h away
> **Last reviewed:** 2026-11-15 · **Last updated:** 2026-11-15 · **Change:** minor (releases v0.22.0 through v0.24.1; .aep gained project export (#2594), drag-and-drop import (#3103), an audit of hostile inputs with every loss reported (#3299, #3300, #3319, #3365), placeholders for unmapped effects (#3087), Lumetri Color, Puppet, CC Color Neutralizer, EXtractoR/IDentifier, HDR Highlight Compression, Echo, Linear Wipe, Offset and Color Stabilizer mappings, text-animator and Source Text expressions (#3170) and twice the import speed (#3302); GPU rendering 2.6× faster with one upload per frame and single-copy readback (#3335), smooth preview lanes (#3345), bounded GPU memory (#3028), shared multi-layer EXR decode (#474), EXR output options (#2915, #2916) and a WebAssembly ABI 2 plug-in path (#3329); eighteen effect parameters now take After Effects' ranges (#3470–#3480, #3483–#3484, #3487–#3491), with Color Stabilizer sampling (#3125), masked Grid/Checkerboard/Circle (#3121), Cryptomatte selection and Matte Only (#2839, #3130), fast-conformed frame blending (#3180) and bounded Levels and time-stretch math; number-field arithmetic (#3195), expressionError (#3219), prop.get with footage (#3231), reversed-layer audio (#3239), keyframed audio waveforms (#1722), Effect Controls lock and rename selection (#3193, #3198), viewer snapping (#2681, #3229), Graph Editor theming (#3202), Convert To Bezier Path (#3237), stopwatch Add Expression (#3192), Shift-drag timeline snapping (#1828), anchored marquee (#2859), Go to Time (#3373), text layers whose names follow their text (#3233), mute-aware audio preview (#2104), ScriptUI panel bars (#2796), Closed-path and Effect Controls arrow fixes (#2817, #2869) and accessible Info, Flowchart, Script Console and 3D Camera Tracker blocks (#1341); a file-dialog crash fix (#3084), crash diagnostics and replayable repros (#3357, #1224), Flatpak updater and nightly release fixes (#3516, #3520), craft-libs format and update cutovers, and CI test stabilisation (#3650, #3689, #3704); readiness estimates unchanged) · **Target:** Adobe After Effects 2026 (26.5.0.89)
> **Last reviewed:** 2026-10-22 · **Last updated:** 2026-11-05 · **Change:** minor (ProRes alpha final-run conformance; previously: features of 30 October in the progress log: text Line Join, Timeline drag-reorder and multi-select of effects, viewer edge pan, ScriptUI drag capture and dock size; previously: localization recount after #1774) · **Target:** Adobe After Effects 2026 (26.5.0.89)
EffectCraft aims to do what After Effects does, with the same panels, menus and behaviour, written
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-mime = { workspace = true }
craft-aep = { workspace = true }
effectcraft-time = { workspace = true }
effectcraft-color = { workspace = true }
+1 -13
View File
@@ -224,19 +224,7 @@ pub(crate) fn profile_json(icc: &[u8], name: &str) -> String {
/// Standard base64 with padding.
pub(crate) fn base64(data: &[u8]) -> String {
const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
for c in data.chunks(3) {
let n = (u32::from(c[0]) << 16) | (u32::from(c.get(1).copied().unwrap_or(0)) << 8) | u32::from(c.get(2).copied().unwrap_or(0));
for (i, shift) in [18u32, 12, 6, 0].iter().enumerate() {
if i <= c.len() {
out.push(char::from(T[((n >> shift) & 63) as usize]));
} else {
out.push('=');
}
}
}
out
craft_mime::base64::STANDARD.encode(data)
}
#[cfg(test)]
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-mime = { workspace = true }
effectcraft-engine = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
+13 -10
View File
@@ -1,16 +1,7 @@
//! Standard base64 (RFC 4648 §4, padded): MCP image content and the control channel's inline PNGs.
const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
pub fn encode(data: &[u8]) -> String {
let mut s = String::with_capacity(data.len().div_ceil(3) * 4);
for c in data.chunks(3) {
let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32;
for i in 0..4 {
s.push(if i <= c.len() { A[(n >> (18 - 6 * i) & 63) as usize] as char } else { '=' });
}
}
s
craft_mime::base64::STANDARD.encode(data)
}
/// Decode, ignoring ASCII whitespace; `None` on any other invalid input.
@@ -48,6 +39,18 @@ pub fn decode(s: &str) -> Option<Vec<u8>> {
#[cfg(test)]
mod tests {
#[test]
fn shared_encoder_round_trips_every_byte_and_remainder() {
let all: Vec<u8> = (0..=255).cycle().take(513).collect();
for length in 0..=all.len() {
let input = &all[..length];
let encoded = super::encode(input);
assert_eq!(encoded.len(), length.div_ceil(3) * 4);
assert_eq!(super::decode(&encoded).as_deref(), Some(input));
assert_eq!(encoded.bytes().rev().take_while(|b| *b == b'=').count(), (3 - length % 3) % 3);
}
}
#[test]
fn rfc4648_vectors() {
for (i, o) in [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] {
+1 -1
View File
@@ -10,7 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-cms = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5c0fc062b4ae518fc2736a7f489a4c4e8222e883" }
craft-cms = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "58964adec11197594d9b6b79a94ae6232221211a" }
effectcraft-geom = { workspace = true }
effectcraft-color = { workspace = true }
effectcraft-keyframe = { workspace = true }
+2 -33
View File
@@ -25,39 +25,8 @@ pub fn parse_spimtx(text: &str) -> Option<FileXform> {
/// Parse a Sony Imageworks `.spi1d` 1D LUT: `From <min> <max>`, `Length <n>`, `Components
/// <1 | 3>`, then `{` the values `}`.
pub fn parse_spi1d(text: &str) -> Option<Lut> {
let (mut from, mut len, mut comps) = ([0.0f32, 1.0], 0usize, 1usize);
let mut values: Vec<f32> = vec![];
let mut body = false;
for line in text.lines().map(str::trim).filter(|l| !l.is_empty()) {
if body {
if line.starts_with('}') {
break;
}
for t in line.split_whitespace() {
values.push(t.parse().ok()?);
}
continue;
}
let mut t = line.split_whitespace();
match t.next()? {
"{" => body = true,
"From" => from = [t.next()?.parse().ok()?, t.next()?.parse().ok()?],
"Length" => len = t.next()?.parse().ok()?,
"Components" => comps = t.next()?.parse().ok()?,
_ => {}
}
}
// A 1D table is interpolated by index, so cap it like the lattices (2²⁴ entries).
if !(2..=MAX_1D).contains(&len)
|| !matches!(comps, 1 | 3)
|| values.len() != len.checked_mul(comps)?
|| from[1] <= from[0]
|| from.iter().any(|v| !v.is_finite())
{
return None;
}
let d1 = values.chunks_exact(comps).map(|c| if comps == 3 { [c[0], c[1], c[2]] } else { [c[0]; 3] }).collect();
Some(Lut { n1: len, d1, n3: 0, d3: vec![], dmin: [from[0]; 3], dmax: [from[1]; 3] })
let table = craft_cms::spi1d::parse(text)?;
Some(Lut { n1: table.values.len(), d1: table.values, n3: 0, d3: vec![], dmin: [table.domain[0]; 3], dmax: [table.domain[1]; 3] })
}
/// Parse a Sony Imageworks `.spi3d` lattice: `SPILUT 1.0`, `3 3`, the sizes, then one
+7
View File
@@ -116,6 +116,13 @@ fn spi_files_parse() {
assert!((back[0] - 1.0).abs() < 1e-5 && (back[2] - 2.0).abs() < 1e-5, "{back:?}");
let three = parse_spi1d("Version 1\nFrom 0 1\nLength 2\nComponents 3\n{\n0 0 0\n1 0.5 2\n}\n").unwrap();
assert_eq!(three.d1, vec![[0.0; 3], [1.0, 0.5, 2.0]]);
let eof = parse_spi1d("Length 2\nFrom -1 2\n{\n0 1").unwrap();
assert_eq!(eof.n1, 2);
assert_eq!(eof.dmin, [-1.0; 3]);
assert_eq!(eof.dmax, [2.0; 3]);
assert_eq!(eof.n3, 0);
assert!(eof.d3.is_empty());
assert!(parse_spi1d("Length 2\n{\n0 1 2").is_none());
let mut spi3d = String::from("SPILUT 1.0\n3 3\n2 2 2\n");
for (r, g, b) in (0..8).map(|i| (i & 1, (i >> 1) & 1, i >> 2)) {
spi3d += &format!("{r} {g} {b} {} {} {}\n", r as f32 * 0.5, g, b);
+1 -1
View File
@@ -17,7 +17,7 @@ fn export_lottie(s: &mut Session, p: &Value) -> Result<Value> {
let dot = path.to_ascii_lowercase().ends_with(".lottie");
let bytes = if dot {
let stem = std::path::Path::new(&path).file_stem().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
effectcraft_lottie::to_dotlottie(&res, &stem)
effectcraft_lottie::try_to_dotlottie(&res, &stem).map_err(|e| EngineError::Other(e.to_string()))?
} else {
res.to_string_compact().into_bytes()
};
+240 -15
View File
@@ -255,6 +255,80 @@ fn release_problem(r: &Release) -> Option<String> {
None
}
/// An owner or repository name on GitHub: letters, digits, `.`, `-`, `_`, and not `.` or `..`.
fn valid_repo_part(p: &str) -> bool {
!p.is_empty() && p != "." && p != ".." && p.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
}
/// The rest of `s` after `prefix`, ignoring ASCII case.
fn strip_prefix_ignore_case<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
let head = s.get(..prefix.len())?;
head.eq_ignore_ascii_case(prefix).then(|| s.get(prefix.len()..)).flatten()
}
/// A `.` or `..` path segment, also percent-encoded.
fn dot_segment(p: &str) -> bool {
let p = p.to_ascii_lowercase().replace("%2e", ".");
p == "." || p == ".."
}
/// A character a release or repository URL may not contain: `?`, `#`, `\`, Unicode white space,
/// or U+FEFF (the catalog schema's `\s` matches it too).
fn bad_url_char(c: char) -> bool {
matches!(c, '?' | '#' | '\\' | '\u{feff}') || c.is_whitespace()
}
/// `(owner, name)` in lower case of a `https://github.com/<owner>/<name>` repository URL (an
/// optional trailing `/` or `.git`) or of a folder in one, `https://github.com/<owner>/<name>/tree/<ref>/<path>`;
/// `None` for anything else.
fn github_repo(repo: &str) -> Option<(String, String)> {
let rest = strip_prefix_ignore_case(repo.trim(), "https://github.com/")?;
if rest.contains(bad_url_char) {
return None;
}
let rest = rest.strip_suffix('/').unwrap_or(rest);
let parts: Vec<&str> = rest.split('/').collect();
let (owner, name) = match parts.as_slice() {
[owner, name] => (*owner, name.strip_suffix(".git").unwrap_or(name)),
[owner, name, "tree", folder @ ..] if !folder.is_empty() && folder.iter().all(|p| !p.is_empty() && !dot_segment(p)) => (*owner, *name),
_ => return None,
};
(valid_repo_part(owner) && valid_repo_part(name)).then(|| (owner.to_ascii_lowercase(), name.to_ascii_lowercase()))
}
/// Why an `https://` release URL can't be used, if it can't: it must be a release asset,
/// `https://github.com/<owner>/<name>/releases/download/<tag>/<file>`, and when the package
/// names a `repo`, that must be a GitHub repository URL (see [`github_repo`]) of `<owner>/<name>`.
fn release_url_problem(repo: &str, url: &str) -> Option<String> {
let not_asset = || Some(format!("`{url}` isn't a release asset (https://github.com/<owner>/<repo>/releases/download/<tag>/<file>)"));
let Some(rest) = strip_prefix_ignore_case(url, "https://github.com/") else { return not_asset() };
if rest.contains(bad_url_char) {
return not_asset();
}
let parts: Vec<&str> = rest.split('/').collect();
// At least six segments, not exactly six: a tag may contain `/` (`release/v1`), and its assets are
// then at `releases/download/release/v1/<file>`. `.` and `..` (also percent-encoded) would move
// the download to another path.
if parts.len() < 6 || parts.iter().any(|p| p.is_empty() || dot_segment(p)) {
return not_asset();
}
let (Some(owner), Some(name), Some(releases), Some(download)) = (parts.first(), parts.get(1), parts.get(2), parts.get(3)) else { return not_asset() };
if !valid_repo_part(owner) || !valid_repo_part(name) || *releases != "releases" || *download != "download" {
return not_asset();
}
let repo = repo.trim();
if repo.is_empty() {
return None;
}
match github_repo(repo) {
Some((o, n)) if o == owner.to_ascii_lowercase() && n == name.to_ascii_lowercase() => None,
Some(_) => Some(format!("`{url}` isn't a release asset of the package's repository {repo}")),
None => Some(format!(
"the package's `repo` {repo} isn't a GitHub repository URL (https://github.com/<owner>/<repo>), so `{url}` can't be checked against it"
)),
}
}
/// Read a catalog: packages and releases that are malformed are left out (and reported), so one
/// bad entry doesn't hide the others.
pub fn parse_catalog(bytes: &[u8]) -> std::result::Result<(Catalog, Vec<String>), String> {
@@ -325,27 +399,44 @@ enum Source {
File(PathBuf),
}
/// `file:///C:/x` or `file:///home/x` as a path (percent-decoded; no remote hosts).
fn file_url_path(url: &str) -> Option<PathBuf> {
let rest = url.strip_prefix("file://")?;
/// `file:///C:/x` or `file:///home/x` as a path (percent-decoded; no remote hosts). A path that
/// starts with two separators or holds a `\` (raw or percent-encoded), or a percent-encoded `/`,
/// is refused: on Windows such a path can name a network share, and reading it contacts that host.
fn file_url_path(url: &str) -> std::result::Result<PathBuf, String> {
let bad = |why: &str| format!("bad file URL `{url}`: {why}");
let rest = url.strip_prefix("file://").ok_or_else(|| bad("it doesn't start with file://"))?;
let rest = rest.strip_prefix("localhost").unwrap_or(rest);
if !rest.starts_with('/') {
return None;
return Err(bad("only local files can be read (file:///path)"));
}
let b = rest.as_bytes();
// `/C:/…` on Windows.
let rest = if b.len() > 2 && b.get(2) == Some(&b':') { rest.get(1..)? } else { rest };
let rest =
if b.get(1).is_some_and(u8::is_ascii_alphabetic) && b.get(2) == Some(&b':') { rest.get(1..).ok_or_else(|| bad("bad drive letter"))? } else { rest };
let mut out = Vec::with_capacity(rest.len());
let mut bytes = rest.bytes();
while let Some(c) = bytes.next() {
if c == b'%' {
let hex = [bytes.next()?, bytes.next()?];
out.push(u8::from_str_radix(std::str::from_utf8(&hex).ok()?, 16).ok()?);
let hex = [bytes.next(), bytes.next()];
let [Some(hi), Some(lo)] = hex else { return Err(bad("a % isn't followed by two hex digits")) };
let byte =
std::str::from_utf8(&[hi, lo]).ok().and_then(|h| u8::from_str_radix(h, 16).ok()).ok_or_else(|| bad("a % isn't followed by two hex digits"))?;
if matches!(byte, b'/' | b'\\') {
return Err(bad("a percent-encoded / or \\ isn't allowed"));
}
out.push(byte);
} else {
out.push(c);
}
}
String::from_utf8(out).ok().map(PathBuf::from)
let path = String::from_utf8(out).map_err(|_| bad("the path isn't UTF-8"))?;
if path.contains('\\') {
return Err(bad("use / between folders, not \\"));
}
if path.starts_with("//") {
return Err(bad("network paths can't be read, only local files"));
}
Ok(PathBuf::from(path))
}
/// Check `url` against the rules: `https://` on GitHub's hosts; `file://` when the catalog
@@ -360,7 +451,7 @@ fn source(url: &str, catalog: Option<&str>) -> std::result::Result<Source, Strin
}
let local_catalog = catalog.is_none_or(|c| c.starts_with("file://"));
if url.starts_with("file://") && local_catalog {
return file_url_path(url).map(Source::File).ok_or_else(|| format!("bad file URL `{url}`"));
return file_url_path(url).map(Source::File);
}
Err(format!("`{url}` isn't an https:// URL"))
}
@@ -801,7 +892,11 @@ fn pick(s: &Session, c: &str, p: &Value) -> Result<(String, Package, Release)> {
),
));
}
source(&rel.url, Some(&url)).map_err(|e| bad(c, e))?;
if let Source::Https(u) = source(&rel.url, Some(&url)).map_err(|e| bad(c, e))?
&& let Some(why) = release_url_problem(&pkg.repo, &u)
{
return Err(bad(c, format!("{}: {why}", pkg.name)));
}
Ok((url, pkg, rel))
}
@@ -1033,7 +1128,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Refresh Plug-in Catalog",
[],
None,
"{catalog?: https:// or file:// URL of an index.json (default: the community catalog), wait?}",
"{catalog?: https:// or file:// URL of an index.json (default: the community catalog; file:// takes a local path, not a network path such as //server/share), wait?}",
always,
refresh
),
@@ -1051,7 +1146,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Install Plug-in",
[],
None,
"{id, version?, wait?} — downloads the release, checks its SHA-256 against the catalog, loads it",
"{id, version?, wait?} — downloads the release (a GitHub release asset, in the package's repo when it names one), checks its SHA-256 against the catalog, loads it",
always,
install
),
@@ -1135,6 +1230,99 @@ mod tests {
assert!(latest_compatible(&Package::default()).is_none());
}
#[test]
fn release_urls_are_assets_of_the_declared_repository() {
let asset = "https://github.com/Me/Glow/releases/download/v1.0.0/glow.wasm";
for repo in ["https://github.com/me/glow", "https://github.com/Me/Glow/", "https://GitHub.com/me/glow.git", ""] {
assert_eq!(release_url_problem(repo, asset), None, "{repo}");
}
assert_eq!(release_url_problem("", "https://github.com/a/b/releases/download/tags/v1/x.wasm"), None, "a tag with a slash");
assert!(
release_url_problem("https://github.com/me/glow", "https://github.com/me/other/releases/download/v1/x.wasm")
.unwrap()
.contains("package's repository")
);
assert!(release_url_problem("https://github.com/me/glow", "https://github.com/you/glow/releases/download/v1/x.wasm").is_some());
for no in [
"https://raw.githubusercontent.com/me/glow/main/x.wasm",
"https://objects.githubusercontent.com/x.wasm",
"https://github.com/me/glow/blob/main/x.wasm",
"https://github.com/me/glow/releases/download/v1",
"https://github.com/me/glow/releases/download//x.wasm",
"https://github.com/me/glow/releases/download/v1/x.wasm?x=1",
"https://github.com/me/glow/releases/download/v1/x.wasm#x",
"https://github.com/me/glow/releases/download/../../../you/evil/releases/download/v1/x.wasm",
"https://github.com/me/glow/releases/download/%2e%2e/%2E./x/y/x.wasm",
"https://github.com/me/glow/releases/download/./v1/x.wasm",
"https://github.com/me/glow/releases/download/v1\\x.wasm",
"https://github.com/m e/glow/releases/download/v1/x.wasm",
"https://github.com/me/glow/releases/download/v 1/x.wasm",
"https://github.com/me/glow/releases/download/v1/x\t.wasm",
"https://github.com/me/glow/releases/download/v1/x.wasm\n",
"https://github.com/me/glow/releases/download/v1\u{a0}/x.wasm",
"https://github.com/me/glow/releases/download/v1/\u{85}x.wasm",
"https://github.com/me/glow/releases/download/v1/x\u{feff}.wasm",
"https://github.com/../glow/releases/download/v1/x.wasm",
"https://github.com.evil.com/me/glow/releases/download/v1/x.wasm",
] {
assert!(release_url_problem("https://github.com/me/glow", no).is_some(), "{no}");
assert!(release_url_problem("", no).is_some(), "{no}");
}
assert_eq!(github_repo("https://github.com/Me/Glow.git"), Some(("me".into(), "glow".into())));
assert_eq!(github_repo("https://github.com/Me/Glow/tree/main/plugins/glow"), Some(("me".into(), "glow".into())));
for no in ["https://github.com/me", "https://github.com/me/glow/tree", "http://github.com/me/glow", "me/glow"] {
assert_eq!(github_repo(no), None, "{no}");
}
}
#[test]
fn a_repo_that_isnt_a_github_repository_url_refuses_every_release() {
let asset = "https://github.com/me/glow/releases/download/v1/glow.wasm";
for repo in [
"https://github.com/me/glow",
"https://github.com/me/glow/",
"https://github.com/me/glow.git",
"https://github.com/me/glow.git/",
" https://github.com/ME/glow ",
"https://github.com/me/glow/tree/main",
"https://github.com/me/glow/tree/main/plugins/glow/",
"",
" ",
] {
assert_eq!(release_url_problem(repo, asset), None, "{repo:?}");
}
let elsewhere = release_url_problem("https://github.com/me/glow/tree/main", "https://github.com/you/evil/releases/download/v1/glow.wasm");
assert!(elsewhere.as_deref().is_some_and(|w| w.contains("package's repository")), "{elsewhere:?}");
for repo in [
"https://github.com/me/glow/tree",
"https://github.com/me/glow/tree/",
"https://github.com/me/glow/tree//x",
"https://github.com/me/glow/tree/main/../../you/evil",
"https://github.com/me/glow/tree/main/%2e%2e",
"https://github.com/me/glow/blob/main/README.md",
"https://github.com/me/glow.git/x",
"https://github.com/me/glow/releases",
"https://github.com/me/glow//",
"https://github.com/me",
"https://github.com/me/",
"http://github.com/me/glow",
"github.com/me/glow",
"me/glow",
"https://www.github.com/me/glow",
"https://gitlab.com/me/glow",
"https://github.com/me/glow?tab=readme",
"https://github.com/me/glow#readme",
"https://github.com/me/gl ow",
"https://github.com/me/glow/tree/main/my plug-in",
"https://github.com/me/glow/tree/ma\tin",
"https://github.com/me/glow/tree/main/x\u{feff}",
"https://github.com/me/..",
] {
let why = release_url_problem(repo, asset);
assert!(why.as_deref().is_some_and(|w| w.contains("isn't a GitHub repository URL")), "{repo:?}: {why:?}");
}
}
#[test]
fn catalogs_leave_out_bad_entries() {
let good = "a".repeat(64);
@@ -1180,10 +1368,47 @@ mod tests {
assert_eq!(source("file:///C:/p/x.wasm", None), Ok(Source::File(PathBuf::from("C:/p/x.wasm"))));
assert!(source("file://", None).is_err());
// Percent-decoded; no remote hosts.
assert_eq!(file_url_path("file:///C:/My%20Plug-ins/Jos%C3%A9%23.wasm"), Some(PathBuf::from("C:/My Plug-ins/José#.wasm")));
assert_eq!(file_url_path("file://localhost/home/x.wasm"), Some(PathBuf::from("/home/x.wasm")));
assert_eq!(file_url_path("file:///C:/My%20Plug-ins/Jos%C3%A9%23.wasm"), Ok(PathBuf::from("C:/My Plug-ins/José#.wasm")));
assert_eq!(file_url_path("file://localhost/home/x.wasm"), Ok(PathBuf::from("/home/x.wasm")));
for no in ["file://server/share/x.wasm", "file:///bad%2", "file:///bad%zz"] {
assert_eq!(file_url_path(no), None, "{no}");
assert!(file_url_path(no).is_err(), "{no}");
}
}
#[test]
fn file_urls_cannot_name_a_network_path() {
for ok in [
("file:///C:/p/x.wasm", "C:/p/x.wasm"),
("file:///c:/p/x.wasm", "c:/p/x.wasm"),
("file:///home/me/x.wasm", "/home/me/x.wasm"),
("file://localhost/C:/p/x.wasm", "C:/p/x.wasm"),
("file:///home/me/a%20b/x.wasm", "/home/me/a b/x.wasm"),
("file:///C:/p//x.wasm", "C:/p//x.wasm"),
] {
assert_eq!(file_url_path(ok.0), Ok(PathBuf::from(ok.1)), "{}", ok.0);
}
for no in [
// Two separators at the start: `//server/share/…`.
"file:////server/share/x.wasm",
"file://localhost//server/share/x.wasm",
"file://///server/share/x.wasm",
"file:////:x.wasm",
// Backslashes, raw or percent-encoded.
"file:///\\\\server\\share\\x.wasm",
"file:///\\server/share/x.wasm",
"file:///C:\\p\\x.wasm",
"file:///%5C%5Cserver/share/x.wasm",
"file:///%5c%5cserver/share/x.wasm",
"file:///C:/p%5Cx.wasm",
// Percent-encoded slashes.
"file:///%2F%2Fserver/share/x.wasm",
"file:///%2fserver/share/x.wasm",
"file:///C:/p%2Fx.wasm",
] {
let e = file_url_path(no).expect_err(no);
assert!(e.starts_with("bad file URL"), "{no}: {e}");
assert!(source(no, None).is_err(), "{no}");
assert!(source(no, Some("file:///tmp/index.json")).is_err(), "{no}");
}
}
+5 -6
View File
@@ -1,6 +1,6 @@
[package]
name = "effectcraft-export"
description = "Render Queue jobs and output modules: H.264 MP4 (+AAC), ProRes and uncompressed MOV (+PCM) , HEVC / AV1 MP4 (+AAC) and WebM (VP9 or AV1) via craft-libs codecs, Opus via effectcraft-opusenc, PNG/JPEG/TIFF/EXR/Targa sequences, animated GIF, WAV/AIFF"
description = "Render Queue jobs and output modules: H.264 MP4 (+AAC), ProRes and uncompressed MOV (+PCM) , HEVC / AV1 MP4 (+AAC) and WebM (VP9 or AV1) via craft-libs codecs, Opus via craft-audio, PNG/JPEG/TIFF/EXR/Targa sequences, animated GIF, WAV/AIFF"
version.workspace = true
edition.workspace = true
license.workspace = true
@@ -17,21 +17,20 @@ effectcraft-render = { workspace = true }
craft-video = { workspace = true }
craft-audio = { workspace = true }
craft-media = { workspace = true }
effectcraft-opusenc = { workspace = true }
craft-gif = { workspace = true }
image = { workspace = true, features = ["png", "jpeg", "tiff", "exr", "tga"] }
exr = { workspace = true }
gif = { workspace = true }
rayon = { workspace = true }
web-time = { workspace = true }
thiserror = { workspace = true }
log = { workspace = true }
[dev-dependencies]
gif = { workspace = true }
tempfile = { workspace = true }
effectcraft-media = { workspace = true }
effectcraft-effects = { workspace = true }
effectcraft-color = { workspace = true }
filmcraft-hevc = { workspace = true }
filmcraft-av1 = { workspace = true }
filmcraft-vp9 = { workspace = true }
# The decoders that check the VP9, HEVC and AV1 encoders round-trip (av1 is already on).
craft-video = { workspace = true, features = ["vp9-decode", "hevc-decode"] }
effectcraft-keyframe = { workspace = true }
+37 -18
View File
@@ -9,7 +9,7 @@
//! | OpenEXR sequence | one file per frame, scan lines | 32-bit float (default) or half float, linear light, premultiplied; compression None, RLE, ZIPS, ZIP (default), PIZ, PXR24, B44, B44A, DWAA, DWAB | yes | — |
//! | Targa sequence | one file per frame | uncompressed 8-bit: 24-bit RGB, 32-bit RGB + Alpha (8 alpha bits), 8-bit grey for Alpha; top-left origin | yes (straight or premultiplied) | — |
//! | Animated GIF | GIF89a | 256-colour palette per frame (NeuQuant) | 1-bit | — |
//! | WebM | WebM (Matroska) | VP9 profile 0 (`craft_video::vp9`: key + inter frames, a key frame every 2 s, loop filter), 8-bit 4:2:0; or AV1 (`craft_video::av1`) | VP9 alpha (BlockAdditional) | Opus (`effectcraft-opusenc`: SILK / hybrid / CELT by bitrate and application), 48 kHz |
//! | WebM | WebM (Matroska) | VP9 profile 0 (`craft_video::vp9`: key + inter frames, a key frame every 2 s, loop filter), 8-bit 4:2:0; or AV1 (`craft_video::av1`) | VP9 alpha (BlockAdditional) | Opus (`craft_audio::opus::Encoder`: SILK / hybrid / CELT by bitrate and application), 48 kHz |
//! | HEVC | MP4 (`hvc1`) | `craft_video::hevc` Main / Main 10 4:2:0, I + P slices, bitrate or constant quality, level, key-frame interval | no | AAC-LC (`craft_audio::aac`) |
//! | AV1 | MP4 (`av01`) | `craft_video::av1` Main 8/10-bit 4:2:0, key + inter frames | no | AAC-LC (`craft_audio::aac`) |
//! | WAV / AIFF | RIFF WAVE / AIFF (AIFF-C for float) | — | — | PCM 16/24-bit or 32-bit float, mono/stereo (audio only) |
@@ -421,13 +421,22 @@ fn write_still<W: std::io::Write + std::io::Seek>(cx: &Cx, file: &mut W, img: &I
}
async fn gif_export(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut State<'_>) -> Result<Report> {
if w > u16::MAX as u32 || h > u16::MAX as u32 {
return Err(ExportError::Unsupported("GIF frames are limited to 65535 px".into()));
}
use craft_gif::write::{AnimationWriter, Disposal, Options, PreparedFrame};
validate_gif_dimensions(w, h)?;
let path = job.place(job.path, 1);
let mut file = job.create(&path)?;
let mut enc = gif::Encoder::new(&mut file, w as u16, h as u16, &[]).map_err(|e| ExportError::Encode(e.to_string()))?;
enc.set_repeat(if job.output.gif_loop { gif::Repeat::Infinite } else { gif::Repeat::Finite(0) }).map_err(|e| ExportError::Encode(e.to_string()))?;
let mut enc = AnimationWriter::new(
&mut file,
w,
h,
Options {
loop_forever: job.output.gif_loop,
disposal: if job.output.channels == Channels::Rgba { Disposal::Background } else { Disposal::Keep },
speed: 10,
},
)
.map_err(|e| ExportError::Encode(e.to_string()))?;
let rate = job.settings.rate(comp);
// GIF delays are in 1/100 s: carry the rounding error so long animations keep their timing.
let cs_per_frame = 100.0 * rate.den as f64 / rate.num as f64;
@@ -437,30 +446,31 @@ async fn gif_export(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut State<'_
let mut i = 0;
while i < st.total {
let end = (i + batch).min(st.total);
let frames: Vec<gif::Frame<'static>> = job
.frames(comp, (i..end).collect(), |_, img| {
let mut px = job.pixels_with(&img, comp, job.output.channels, w, h, false);
gif::Frame::from_rgba_speed(w as u16, h as u16, &mut px, 10)
})
let frames: Vec<std::result::Result<PreparedFrame, craft_gif::write::Error>> = job
.frames(comp, (i..end).collect(), |_, img| craft_gif::write::prepare_frame(w, h, job.pixels_with(&img, comp, job.output.channels, w, h, false), 10))
.await;
for mut f in frames {
for frame in frames {
clock += cs_per_frame;
let d = (clock.round() as u64).saturating_sub(emitted).max(1);
emitted += d;
f.delay = d.min(u16::MAX as u64) as u16;
if job.output.channels == Channels::Rgba {
f.dispose = gif::DisposalMethod::Background;
}
enc.write_frame(&f).map_err(|e| ExportError::Encode(e.to_string()))?;
let frame = frame.map_err(|e| ExportError::Encode(e.to_string()))?;
enc.write_prepared_frame(frame, d.min(u16::MAX as u64) as u16).map_err(|e| ExportError::Encode(e.to_string()))?;
}
st.advance(end - i)?;
i = end;
}
drop(enc);
let _ = enc.finish().map_err(|e| ExportError::Encode(e.to_string()))?;
let bytes = file.finish()?;
Ok(Report { path, frames: 0, width: w, height: h, seconds: 0.0, bytes, audio: false, log: None, overflow: vec![] })
}
fn validate_gif_dimensions(w: u32, h: u32) -> Result<()> {
if w > u16::MAX as u32 || h > u16::MAX as u32 {
return Err(ExportError::Unsupported("GIF frames are limited to 65535 px".into()));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
@@ -478,4 +488,13 @@ mod tests {
assert_eq!(OutputFormat::H264.coded_size(1, 101), (2, 100));
assert_eq!(OutputFormat::Gif.coded_size(1, 101), (1, 101));
}
#[test]
fn gif_dimension_limit_keeps_the_existing_unsupported_error() {
assert!(validate_gif_dimensions(u16::MAX as u32, u16::MAX as u32).is_ok());
assert!(matches!(
validate_gif_dimensions(u16::MAX as u32 + 1, 1),
Err(ExportError::Unsupported(message)) if message == "GIF frames are limited to 65535 px"
));
}
}
+20 -13
View File
@@ -8,18 +8,19 @@
//! with `AlphaMode` 1 on the track, as WebM defines. VP9 comes from `craft_video::vp9` (key
//! frames every two seconds, inter frames between them: SimpleBlocks carry the key flag, inter
//! BlockGroups a ReferenceBlock; the alpha stream's key frames line up with the colour stream's),
//! Opus from `effectcraft-opusenc` (CELT, 48 kHz stereo, 20 ms packets,
//! Opus from `craft_audio::opus::Encoder` (SILK / hybrid / CELT, 48 kHz, 20 ms packets,
//! `CodecDelay` = pre-skip). WAV (RIFF, PCM or IEEE float) and AIFF (big-endian PCM, 80-bit
//! extended sample rate; AIFF-C `fl32` for float) follow their published file layouts.
use std::io::{Seek, SeekFrom, Write};
use craft_audio::opus::encoder as opus;
use effectcraft_project::Comp;
use effectcraft_project::render_queue::{AudioFormat, Channels};
use effectcraft_time::{TICKS_PER_SECOND, Tick};
use crate::encode::{mix, pcm_bytes};
use crate::{Cx, Report, Result, State, batch_size, io, wants_audio};
use crate::{Cx, ExportError, Report, Result, State, batch_size, io, wants_audio};
pub(crate) const OPUS_RATE: u32 = 48_000;
@@ -124,13 +125,18 @@ fn cluster(frames: &mut [Frame]) -> Vec<u8> {
}
/// The Opus encoder of a WebM output: the module's channel count, bitrate and application.
pub(crate) fn opus_encoder(job: &Cx) -> effectcraft_opusenc::OpusEncoder {
pub(crate) fn opus_encoder(job: &Cx) -> Result<opus::Encoder> {
let channels = if job.output.audio_channels == 1 { 1 } else { 2 };
let app = match job.output.opus_application {
effectcraft_project::render_queue::OpusApplication::Audio => effectcraft_opusenc::Application::Audio,
effectcraft_project::render_queue::OpusApplication::Voip => effectcraft_opusenc::Application::Voip,
effectcraft_project::render_queue::OpusApplication::Audio => opus::Application::Audio,
effectcraft_project::render_queue::OpusApplication::Voip => opus::Application::Voip,
};
effectcraft_opusenc::OpusEncoder::with_application(channels, job.output.opus_bitrate_kbps.clamp(6, 510) * 1000, app)
opus::Encoder::with_application(channels, job.output.opus_bitrate_kbps.clamp(6, 510) * 1000, app).map_err(opus_error)
}
/// An Opus encoder error as an export error.
pub(crate) fn opus_error(e: opus::Error) -> ExportError {
ExportError::Encode(format!("Opus: {e}"))
}
pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut State<'_>) -> Result<Report> {
@@ -156,7 +162,7 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta
let mut alpha_enc = alpha.then(|| craft_video::vp9::Vp9Encoder::new(cfg.clone()));
let with_audio = wants_audio(job);
let opus_channels = if job.output.audio_channels == 1 { 1usize } else { 2 };
let mut opus = with_audio.then(|| opus_encoder(job));
let mut opus = with_audio.then(|| opus_encoder(job)).transpose()?;
let total = st.total;
let frame_ms = |k: u64| -> i64 { ((k as i128 * 1000 * rate.den as i128 + rate.num as i128 / 2) / rate.num as i128) as i64 };
let duration_ms = frame_ms(total) as f64;
@@ -229,8 +235,8 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta
let mut cursor = span_start.to_units_floor(OPUS_RATE as i64);
let mut pcm_left: Vec<f32> = vec![];
let mut packets = 0u64;
let mut audio_until = |until: Tick, frames: &mut Vec<Frame>, finish: bool| {
let Some(enc) = opus.as_mut() else { return };
let mut audio_until = |until: Tick, frames: &mut Vec<Frame>, finish: bool| -> Result<()> {
let Some(enc) = opus.as_mut() else { return Ok(()) };
let end = until.to_units_floor(OPUS_RATE as i64);
if end > cursor {
let n = (end - cursor) as usize;
@@ -238,7 +244,7 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta
pcm_left.extend(mix(job, start, n, OPUS_RATE));
cursor = end;
}
let fs = effectcraft_opusenc::OpusEncoder::FRAME_SIZE * opus_channels;
let fs = craft_audio::opus::Encoder::FRAME_SIZE * opus_channels;
if finish && !pcm_left.is_empty() {
// Pad the last packet (pre-skip + padding cover the encoder delay).
let pad = (fs - pcm_left.len() % fs) % fs + fs;
@@ -247,11 +253,12 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta
// Packets are read in place and the encoded samples dropped once: draining each packet
// from the front would shift the rest of the batch's audio every 20 ms.
for packet in pcm_left.chunks_exact(fs) {
let pkt = enc.encode_float(packet);
let pkt = enc.encode_float(packet).map_err(crate::webm::opus_error)?;
frames.push(Frame { ms: packets as i64 * 20, track: 2, key: true, prev_ms: None, data: pkt, alpha: None });
packets += 1;
}
pcm_left.drain(..pcm_left.len() / fs * fs);
Ok(())
};
let batch = batch_size();
@@ -281,7 +288,7 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta
}
drop(pixels);
let t_end = if end >= total { span_end } else { job.settings.frame_time(comp, end) };
audio_until(t_end, &mut frames, end >= total);
audio_until(t_end, &mut frames, end >= total)?;
let c = cluster(&mut frames);
file.write_all(&c).map_err(io)?;
seg_len += c.len() as u64;
@@ -290,7 +297,7 @@ pub(crate) async fn webm(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut Sta
}
if total == 0 {
let mut frames = vec![];
audio_until(span_end, &mut frames, true);
audio_until(span_end, &mut frames, true)?;
if !frames.is_empty() {
let c = cluster(&mut frames);
file.write_all(&c).map_err(io)?;
+8 -7
View File
@@ -46,7 +46,7 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut
let mut venc = crate::hevc_av1::Av1::new(w, h, rate, job.output, codes)?;
let with_audio = wants_audio(job);
let opus_channels = if job.output.audio_channels == 1 { 1usize } else { 2 };
let mut opus = with_audio.then(|| opus_encoder(job));
let mut opus = with_audio.then(|| opus_encoder(job)).transpose()?;
let total = st.total;
let frame_ms = |k: u64| -> i64 { ((k as i128 * 1000 * rate.den as i128 + rate.num as i128 / 2) / rate.num as i128) as i64 };
let duration_ms = frame_ms(total) as f64;
@@ -111,8 +111,8 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut
let mut cursor = span_start.to_units_floor(OPUS_RATE as i64);
let mut pcm_left: Vec<f32> = vec![];
let mut packets = 0u64;
let mut audio_until = |until: Tick, blocks: &mut Vec<Block>, finish: bool| {
let Some(enc) = opus.as_mut() else { return };
let mut audio_until = |until: Tick, blocks: &mut Vec<Block>, finish: bool| -> Result<()> {
let Some(enc) = opus.as_mut() else { return Ok(()) };
let end = until.to_units_floor(OPUS_RATE as i64);
if end > cursor {
let n = (end - cursor) as usize;
@@ -120,7 +120,7 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut
pcm_left.extend(mix(job, start, n, OPUS_RATE));
cursor = end;
}
let fs = effectcraft_opusenc::OpusEncoder::FRAME_SIZE * opus_channels;
let fs = craft_audio::opus::Encoder::FRAME_SIZE * opus_channels;
if finish && !pcm_left.is_empty() {
let pad = (fs - pcm_left.len() % fs) % fs + fs;
pcm_left.extend(std::iter::repeat_n(0.0, pad));
@@ -128,11 +128,12 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut
// Packets are read in place and the encoded samples dropped once: draining each packet
// from the front would shift the rest of the batch's audio every 20 ms.
for packet in pcm_left.chunks_exact(fs) {
let pkt = enc.encode_float(packet);
let pkt = enc.encode_float(packet).map_err(crate::webm::opus_error)?;
blocks.push(Block { ms: packets as i64 * 20, track: 2, key: true, data: pkt });
packets += 1;
}
pcm_left.drain(..pcm_left.len() / fs * fs);
Ok(())
};
let batch = batch_size();
@@ -159,13 +160,13 @@ pub(crate) async fn webm_av1(job: &Cx<'_>, comp: &Comp, w: u32, h: u32, st: &mut
}
}
let t_end = if end >= total { span_end } else { job.settings.frame_time(comp, end) };
audio_until(t_end, &mut blocks, end >= total);
audio_until(t_end, &mut blocks, end >= total)?;
flush_blocks(&mut blocks, &mut file, &mut seg_len)?;
st.advance(end - i)?;
i = end;
}
if total == 0 {
audio_until(span_end, &mut blocks, true);
audio_until(span_end, &mut blocks, true)?;
flush_blocks(&mut blocks, &mut file, &mut seg_len)?;
}
let mut size = [0u8; 8];
+2 -2
View File
@@ -86,13 +86,13 @@ pub struct Run {
pub lf_levels: Vec<u32>,
}
/// Encodes `pics`, checks every packet decodes (filmcraft-av1) to exactly the encoder's
/// Encodes `pics`, checks every packet decodes (craft-video's AV1 decoder) to exactly the encoder's
/// reconstruction and returns packets, reconstructions and luma PSNRs.
pub fn encode_and_check(cfg: EncoderConfig, pics: &[Pic]) -> Run {
let bd = cfg.bit_depth as u32;
let mut enc = Encoder::new(cfg).expect("config");
// single-threaded: each packet's picture comes out of its own decode call
let mut dec = filmcraft_av1::Decoder::with_threads(1);
let mut dec = craft_video::av1::decode::Decoder::with_threads(1);
let mut run = Run { packets: Vec::new(), recons: Vec::new(), psnr_y: Vec::new(), lf_levels: Vec::new() };
for (i, p) in pics.iter().enumerate() {
let pkt = enc.encode(&p.frame()).expect("encode");
+1 -1
View File
@@ -1,4 +1,4 @@
//! craft-video's AV1 encoder against the filmcraft-av1 decoder and ffmpeg's libdav1d.
//! craft-video's AV1 encoder against craft-video's AV1 decoder and ffmpeg's libdav1d.
mod bench;
mod common;
+1 -27
View File
@@ -8,6 +8,7 @@
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::support::write_tone;
use effectcraft_color::Label;
use effectcraft_export::render_queue::{
AudioOutput, CodecProfile, OpusApplication, OutputFormat, OutputModule, RateControlMode, RenderSettings, TimeSpan, WebmVideoCodec,
@@ -299,30 +300,3 @@ fn rms(v: impl Iterator<Item = f32>) -> f32 {
let (s, n) = v.fold((0.0f64, 0usize), |(s, n), x| (s + (x as f64) * (x as f64), n + 1));
(s / n.max(1) as f64).sqrt() as f32
}
/// 16-bit stereo WAV with a 440 Hz sine at amplitude 0.5 (left) and silence (right).
fn write_tone(path: &Path, secs: f64) {
let sr = 48_000u32;
let n = (secs * sr as f64) as usize;
let mut data = Vec::with_capacity(n * 4);
for i in 0..n {
let s = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * i as f64 / sr as f64).sin() * 32767.0).round() as i16;
data.extend_from_slice(&s.to_le_bytes());
data.extend_from_slice(&0i16.to_le_bytes());
}
let mut w = Vec::new();
w.extend_from_slice(b"RIFF");
w.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
w.extend_from_slice(b"WAVEfmt ");
w.extend_from_slice(&16u32.to_le_bytes());
w.extend_from_slice(&1u16.to_le_bytes());
w.extend_from_slice(&2u16.to_le_bytes());
w.extend_from_slice(&sr.to_le_bytes());
w.extend_from_slice(&(sr * 4).to_le_bytes());
w.extend_from_slice(&4u16.to_le_bytes());
w.extend_from_slice(&16u16.to_le_bytes());
w.extend_from_slice(b"data");
w.extend_from_slice(&(data.len() as u32).to_le_bytes());
w.extend_from_slice(&data);
std::fs::write(path, w).expect("write wav");
}
+13 -13
View File
@@ -1,4 +1,4 @@
//! Round trips through the filmcraft-hevc decoder and (when installed) ffmpeg: decoded pictures must
//! Round trips through the craft-video HEVC decoder and (when installed) ffmpeg: decoded pictures must
//! equal the encoder's own reconstruction bit-exactly.
use craft_video::hevc::{Encoder, EncoderConfig, Frame, Profile, RateControl};
@@ -84,9 +84,9 @@ fn encode(cfg: EncoderConfig, pics: &[Pic]) -> Run {
run
}
/// Decode with filmcraft-hevc and compare with the reconstructions.
fn check_filmcraft(run: &Run, w: usize, h: usize) {
let mut dec = filmcraft_hevc::Decoder::from_hvcc(&run.hvcc).unwrap();
/// Decode with craft-video's HEVC decoder and compare with the reconstructions.
fn check_decoder(run: &Run, w: usize, h: usize) {
let mut dec = craft_video::hevc::decode::Decoder::from_hvcc(&run.hvcc).unwrap();
let mut pics = Vec::new();
for (i, p) in run.packets.iter().enumerate() {
pics.extend(dec.decode(p, i as i64).unwrap());
@@ -211,7 +211,7 @@ fn intra_8bit_quality_and_decoders() {
let run = encode(cfg(w as u32, h as u32, 22, 1, Profile::Main), &pics);
eprintln!("intra QP22: {} bytes, PSNR-Y {:.2}", run.packets[0].len(), run.psnr_y[0]);
assert!(run.psnr_y[0] > 35.0, "PSNR {}", run.psnr_y[0]);
check_filmcraft(&run, w, h);
check_decoder(&run, w, h);
check_ffmpeg(&run, w, h, 8, "intra8");
}
@@ -267,7 +267,7 @@ fn inter_8bit_moving_pattern() {
let p_avg = sizes[1..].iter().sum::<usize>() as f64 / 5.0;
assert!(p_avg < sizes[0] as f64 * 0.6, "P frames {p_avg} vs I {}", sizes[0]);
assert!(run.psnr_y.iter().all(|&p| p > 33.0));
check_filmcraft(&run, w, h);
check_decoder(&run, w, h);
check_ffmpeg(&run, w, h, 8, "inter8");
}
@@ -276,7 +276,7 @@ fn odd_multiple_sizes_and_cropping() {
for (w, h) in [(34usize, 18usize), (66, 38), (8, 8), (130, 72), (2, 2), (2, 70), (264, 6)] {
let pics: Vec<Pic> = (0..3).map(|i| synth(w, h, 8, i, 2 * i)).collect();
let run = encode(cfg(w as u32, h as u32, 30, 2, Profile::Main), &pics);
check_filmcraft(&run, w, h);
check_decoder(&run, w, h);
check_ffmpeg(&run, w, h, 8, &format!("size{w}x{h}"));
}
}
@@ -305,7 +305,7 @@ fn noise_low_qp() {
.collect();
let run = encode(cfg(w as u32, h as u32, 0, 2, profile), &pics);
assert!(run.psnr_y.iter().all(|&p| p > 45.0), "{:?}", run.psnr_y);
check_filmcraft(&run, w, h);
check_decoder(&run, w, h);
check_ffmpeg(&run, w, h, bd, &format!("noise{bd}"));
}
}
@@ -317,7 +317,7 @@ fn high_qp_deblocking() {
for (qp, profile, bd) in [(38u8, Profile::Main, 8u32), (44, Profile::Main, 8), (37, Profile::Main10, 10)] {
let pics: Vec<Pic> = (0..5).map(|i| synth(w, h, bd, 2 * i, i)).collect();
let run = encode(cfg(w as u32, h as u32, qp, 4, profile), &pics);
check_filmcraft(&run, w, h);
check_decoder(&run, w, h);
check_ffmpeg(&run, w, h, bd, &format!("hq{qp}"));
}
}
@@ -329,7 +329,7 @@ fn long_gop_poc_wrap() {
let pics: Vec<Pic> = (0..300).map(|i| synth(w, h, 8, i % 7, i % 5)).collect();
let run = encode(cfg(w as u32, h as u32, 30, 0, Profile::Main), &pics);
assert_eq!(run.keys.iter().filter(|&&k| k).count(), 1);
check_filmcraft(&run, w, h);
check_decoder(&run, w, h);
check_ffmpeg(&run, w, h, 8, "pocwrap");
let run = encode(cfg(w as u32, h as u32, 30, 260, Profile::Main), &pics);
assert_eq!(run.keys.iter().filter(|&&k| k).count(), 2);
@@ -348,7 +348,7 @@ fn main10_round_trip() {
let run = encode(cfg(w as u32, h as u32, 24, 3, Profile::Main10), &pics);
eprintln!("main10 psnr {:?}", run.psnr_y);
assert!(run.psnr_y[0] > 35.0);
check_filmcraft(&run, w, h);
check_decoder(&run, w, h);
check_ffmpeg(&run, w, h, 10, "main10");
}
@@ -358,7 +358,7 @@ fn qp_extremes() {
for qp in [0u8, 51] {
let pics: Vec<Pic> = (0..2).map(|i| synth(w, h, 8, i, i)).collect();
let run = encode(cfg(w as u32, h as u32, qp, 30, Profile::Main), &pics);
check_filmcraft(&run, w, h);
check_decoder(&run, w, h);
check_ffmpeg(&run, w, h, 8, &format!("qp{qp}"));
}
}
@@ -375,7 +375,7 @@ fn bitrate_mode_hits_target() {
let kbps = bits as f64 / (pics.len() as f64 / 30.0) / 1000.0;
eprintln!("bitrate mode: {kbps:.1} kbit/s for a 400 kbit/s target");
assert!(kbps > 400.0 / 2.0 && kbps < 400.0 * 2.0, "{kbps}");
check_filmcraft(&run, w, h);
check_decoder(&run, w, h);
}
#[test]
+1
View File
@@ -18,6 +18,7 @@ mod opus_roundtrip;
mod prores_color;
mod render_settings;
mod roundtrip;
mod support;
mod targa_sequence;
mod uncompressed_mov;
mod vp9_roundtrip;
+5 -5
View File
@@ -1,9 +1,9 @@
//! Round trip of `effectcraft-opusenc` through FilmCraft's spec-derived Opus decoder, plus an
//! Round trip of WebM export's Opus encoder (`craft_audio::opus::Encoder`) through FilmCraft's spec-derived Opus decoder, plus an
//! ffmpeg oracle (skipped when ffmpeg is not installed) reading an Ogg Opus file written here.
use std::process::Command;
use effectcraft_opusenc::OpusEncoder;
use craft_audio::opus::Encoder as OpusEncoder;
const SR: f32 = 48_000.0;
const FRAME: usize = OpusEncoder::FRAME_SIZE;
@@ -11,12 +11,12 @@ const FRAME: usize = OpusEncoder::FRAME_SIZE;
/// Encodes interleaved `pcm`, padding the tail so the encoder's look-ahead is flushed.
fn encode(pcm: &[f32], channels: u8, bitrate: u32) -> (OpusEncoder, Vec<Vec<u8>>) {
let c = channels as usize;
let mut enc = OpusEncoder::new(channels, bitrate);
let mut enc = OpusEncoder::new(channels, bitrate).expect("1 or 2 channels");
let total = pcm.len() / c + enc.pre_skip() as usize;
let frames = total.div_ceil(FRAME);
let mut padded = pcm.to_vec();
padded.resize(frames * FRAME * c, 0.0);
let packets = padded.chunks(FRAME * c).map(|f| enc.encode_float(f)).collect();
let packets = padded.chunks(FRAME * c).map(|f| enc.encode_float(f).expect("one whole frame")).collect();
(enc, packets)
}
@@ -303,7 +303,7 @@ fn ogg_page(out: &mut Vec<u8>, packet: &[u8], header_type: u8, granule: u64, seq
fn ogg_opus(enc: &OpusEncoder, packets: &[Vec<u8>], len: usize) -> Vec<u8> {
let mut out = Vec::new();
ogg_page(&mut out, &enc.opus_head(), 0x02, 0, 0);
let vendor = b"effectcraft-opusenc";
let vendor = b"craft-audio";
let mut tags = b"OpusTags".to_vec();
tags.extend_from_slice(&(vendor.len() as u32).to_le_bytes());
tags.extend_from_slice(vendor);
+58 -34
View File
@@ -9,6 +9,7 @@
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::support::write_tone;
use effectcraft_color::Label;
use effectcraft_export::render_queue::{AlphaMode, AudioOutput, Channels, OutputFormat, OutputModule, ProResProfile, RenderSettings, TimeSpan, sequence_path};
use effectcraft_export::{Job, Progress, export};
@@ -543,21 +544,47 @@ fn exr_sequence_values_at_or_below_one_are_unchanged() {
assert!((c[0] - 1.0).abs() < 1e-4 && (c[1] - lin(0.4)).abs() < 1e-4 && c[2].abs() < 1e-6, "{channels:?}: {c:?}");
}
}
#[test]
fn gif_roundtrip() {
/// Render the test project as a GIF with `om` into `out_dir(name)`, and open it for RGBA decoding.
fn render_gif(name: &str, om: &OutputModule) -> (PathBuf, gif::Decoder<std::fs::File>) {
let (p, cid) = project(None);
let d = out_dir("gif");
let om = OutputModule::for_format(OutputFormat::Gif);
let path = d.join("anim.gif");
run(&p, cid, &NoFootage, &om, &path);
let path = out_dir(name).join("anim.gif");
run(&p, cid, &NoFootage, om, &path);
let mut opts = gif::DecodeOptions::new();
opts.set_color_output(gif::ColorOutput::RGBA);
let mut dec = opts.read_info(std::fs::File::open(&path).expect("open")).expect("gif header");
let dec = opts.read_info(std::fs::File::open(&path).expect("open")).expect("gif header");
(path, dec)
}
/// The test project's frames, rendered as PNGs, re-encoded through the former app-local gif 0.13
/// path (`Frame::from_rgba_speed` at speed 10, 1/10 s per frame, looping).
fn reference_gif() -> Vec<u8> {
let (p, cid) = project(None);
let png_pattern = out_dir("gif-byte-parity").join("frame_[#####].png");
run(&p, cid, &NoFootage, &OutputModule::for_format(OutputFormat::PngSequence), &png_pattern);
let png_pattern = png_pattern.to_string_lossy();
let mut expected = Vec::new();
let mut encoder = gif::Encoder::new(&mut expected, W as u16, H as u16, &[]).expect("reference GIF encoder");
encoder.set_repeat(gif::Repeat::Infinite).expect("reference GIF repeat");
for frame_number in 0..FRAMES {
let mut rgba = image::open(sequence_path(&png_pattern, frame_number as i64)).expect("reference PNG").to_rgba8().into_raw();
let mut frame = gif::Frame::from_rgba_speed(W as u16, H as u16, &mut rgba, 10);
frame.delay = 10;
encoder.write_frame(&frame).expect("reference GIF frame");
}
drop(encoder);
expected
}
#[test]
fn gif_roundtrip() {
let (path, mut dec) = render_gif("gif", &OutputModule::for_format(OutputFormat::Gif));
assert_eq!((dec.width() as u32, dec.height() as u32), (W, H));
assert_eq!(dec.repeat(), gif::Repeat::Infinite);
let mut k = 0;
let mut total_delay = 0u32;
while let Some(f) = dec.read_next_frame().expect("frame") {
assert_eq!((f.width as u32, f.height as u32), (W, H));
assert_eq!(f.dispose, gif::DisposalMethod::Keep);
total_delay += f.delay as u32;
let img = Image::from_rgba8(W, H, &f.buffer);
check_frame(&img, k, 0.06);
@@ -570,6 +597,30 @@ fn gif_roundtrip() {
}
}
#[test]
fn gif_matches_the_former_encoder_byte_for_byte() {
// The shared writer must keep the complete output, quantization and LZW data included.
let (path, _) = render_gif("gif-parity", &OutputModule::for_format(OutputFormat::Gif));
let actual = std::fs::read(&path).expect("GIF bytes");
let expected = reference_gif();
let difference = actual.iter().zip(&expected).position(|(a, b)| a != b);
assert!(
actual.len() == expected.len() && difference.is_none(),
"GIF bytes differ: {} vs {} bytes, first difference at {difference:?}",
actual.len(),
expected.len()
);
}
#[test]
fn gif_rgba_keeps_background_disposal_and_non_looping() {
let om = OutputModule { channels: Channels::Rgba, gif_loop: false, ..OutputModule::for_format(OutputFormat::Gif) };
let (_, mut dec) = render_gif("gif-rgba", &om);
assert_eq!(dec.repeat(), gif::Repeat::Finite(0));
let first = dec.read_next_frame().expect("frame").expect("first frame");
assert_eq!(first.dispose, gif::DisposalMethod::Background);
}
#[test]
fn cancel_stops_early() {
let (p, cid) = project(None);
@@ -596,33 +647,6 @@ fn cancel_stops_early() {
assert_eq!(written, 0, "cancelled before the first batch");
}
/// 16-bit stereo WAV with a 440 Hz sine at amplitude 0.5 (left) and silence (right).
fn write_tone(path: &Path, secs: f64) {
let sr = 48_000u32;
let n = (secs * sr as f64) as usize;
let mut data = Vec::with_capacity(n * 4);
for i in 0..n {
let s = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * i as f64 / sr as f64).sin() * 32767.0).round() as i16;
data.extend_from_slice(&s.to_le_bytes());
data.extend_from_slice(&0i16.to_le_bytes());
}
let mut w = Vec::new();
w.extend_from_slice(b"RIFF");
w.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
w.extend_from_slice(b"WAVEfmt ");
w.extend_from_slice(&16u32.to_le_bytes());
w.extend_from_slice(&1u16.to_le_bytes());
w.extend_from_slice(&2u16.to_le_bytes());
w.extend_from_slice(&sr.to_le_bytes());
w.extend_from_slice(&(sr * 4).to_le_bytes());
w.extend_from_slice(&4u16.to_le_bytes());
w.extend_from_slice(&16u16.to_le_bytes());
w.extend_from_slice(b"data");
w.extend_from_slice(&(data.len() as u32).to_le_bytes());
w.extend_from_slice(&data);
std::fs::write(path, w).expect("write wav");
}
fn rms(v: impl Iterator<Item = f32>) -> f32 {
let (s, n) = v.fold((0.0f64, 0usize), |(s, n), x| (s + (x as f64) * (x as f64), n + 1));
(s / n.max(1) as f64).sqrt() as f32
+30
View File
@@ -0,0 +1,30 @@
//! Helpers shared by the integration test modules.
use std::path::Path;
/// 16-bit stereo 48 kHz WAV: a 440 Hz sine at 0.5 on the left, silence on the right.
pub(crate) fn write_tone(path: &Path, secs: f64) {
let sr = 48_000u32;
let n = (secs * sr as f64) as usize;
let mut data = Vec::with_capacity(n * 4);
for i in 0..n {
let s = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * i as f64 / sr as f64).sin() * 32767.0).round() as i16;
data.extend_from_slice(&s.to_le_bytes());
data.extend_from_slice(&0i16.to_le_bytes());
}
let mut w = Vec::new();
w.extend_from_slice(b"RIFF");
w.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
w.extend_from_slice(b"WAVEfmt ");
w.extend_from_slice(&16u32.to_le_bytes());
w.extend_from_slice(&1u16.to_le_bytes());
w.extend_from_slice(&2u16.to_le_bytes());
w.extend_from_slice(&sr.to_le_bytes());
w.extend_from_slice(&(sr * 4).to_le_bytes());
w.extend_from_slice(&4u16.to_le_bytes());
w.extend_from_slice(&16u16.to_le_bytes());
w.extend_from_slice(b"data");
w.extend_from_slice(&(data.len() as u32).to_le_bytes());
w.extend_from_slice(&data);
std::fs::write(path, w).expect("write wav");
}
+1 -27
View File
@@ -8,6 +8,7 @@
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::support::write_tone;
use craft_media::isobmff::TrackKind;
use effectcraft_color::Label;
use effectcraft_export::render_queue::{AudioFormat, AudioOutput, Channels, OutputFormat, OutputModule, RenderSettings, TimeSpan, sequence_path};
@@ -28,33 +29,6 @@ fn out_dir(name: &str) -> PathBuf {
d
}
/// 16-bit stereo 48 kHz WAV: a 440 Hz sine at 0.5 on the left, silence on the right.
fn write_tone(path: &Path, secs: f64) {
let sr = 48_000u32;
let n = (secs * sr as f64) as usize;
let mut data = Vec::with_capacity(n * 4);
for i in 0..n {
let s = (0.5 * (2.0 * std::f64::consts::PI * 440.0 * i as f64 / sr as f64).sin() * 32767.0).round() as i16;
data.extend_from_slice(&s.to_le_bytes());
data.extend_from_slice(&0i16.to_le_bytes());
}
let mut w = Vec::new();
w.extend_from_slice(b"RIFF");
w.extend_from_slice(&(36 + data.len() as u32).to_le_bytes());
w.extend_from_slice(b"WAVEfmt ");
w.extend_from_slice(&16u32.to_le_bytes());
w.extend_from_slice(&1u16.to_le_bytes());
w.extend_from_slice(&2u16.to_le_bytes());
w.extend_from_slice(&sr.to_le_bytes());
w.extend_from_slice(&(sr * 4).to_le_bytes());
w.extend_from_slice(&4u16.to_le_bytes());
w.extend_from_slice(&16u16.to_le_bytes());
w.extend_from_slice(b"data");
w.extend_from_slice(&(data.len() as u32).to_le_bytes());
w.extend_from_slice(&data);
std::fs::write(path, w).expect("write wav");
}
fn project(audio: Option<&Path>) -> (Project, ItemId) {
let mut p = Project::default();
let mut comp = Comp::new(W, H, FrameRate::new(10, 1), Tick::from_seconds_f64(FRAMES as f64 / 10.0));
+4 -4
View File
@@ -1,8 +1,8 @@
//! The VP9 intra encoder (`craft_video::vp9`) round-trips through FilmCraft's VP9 decoder (and
//! The VP9 intra encoder (`craft_video::vp9`) round-trips through craft-video's VP9 decoder (and
//! ffmpeg as an external oracle when installed): sizes, lossless exactness, PSNR.
use craft_video::vp9::decode::{Decoder, Picture, Plane};
use craft_video::vp9::{EncoderConfig, Vp9Encoder};
use filmcraft_vp9::{Decoder, Plane};
fn plane_u8(p: &Plane) -> Vec<u8> {
match p {
@@ -40,7 +40,7 @@ fn source(w: usize, h: usize, kind: u32) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
(y, u, v)
}
fn decode(frame: &[u8]) -> filmcraft_vp9::Picture {
fn decode(frame: &[u8]) -> Picture {
let mut d = Decoder::with_threads(1);
let mut pics = d.decode(frame, 0).expect("decode");
assert_eq!(pics.len(), 1);
@@ -179,7 +179,7 @@ fn moving(w: usize, h: usize, t: usize, kind: u32) -> (Vec<u8>, Vec<u8>, Vec<u8>
(y, u, v)
}
fn planes_of(p: &filmcraft_vp9::Picture, w: usize, h: usize) -> [Vec<u8>; 3] {
fn planes_of(p: &Picture, w: usize, h: usize) -> [Vec<u8>; 3] {
let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
[crop(&plane_u8(&p.y), p.y_stride, w, h), crop(&plane_u8(&p.u), p.uv_stride, cw, ch), crop(&plane_u8(&p.v), p.uv_stride, cw, ch)]
}
+31 -13
View File
@@ -1207,17 +1207,25 @@ impl GpuContext {
if !ticket.active() {
return;
}
let mapping = Arc::new(crate::readback::Mapping::new());
let b = buffer.clone();
let callback_mapping = Arc::clone(&mapping);
buffer.map_async(wgpu::MapMode::Read, .., move |r| {
if !ticket.active() {
b.unmap();
if let Err(e) = r {
callback_mapping.failed();
ticket.complete(Err(format!("GPU buffer mapping: {e}")));
return;
}
let result = r.map_err(|e| format!("GPU buffer mapping: {e}")).and_then(|_| {
let view = b.get_mapped_range(..).map_err(|e| format!("GPU mapped range: {e}"))?;
crate::readback::packed(&view, row, stride, height)
});
b.unmap();
if !callback_mapping.mapped() {
return;
}
if !ticket.active() {
callback_mapping.unmap(|| b.unmap());
return;
}
let result =
b.get_mapped_range(..).map_err(|e| format!("GPU mapped range: {e}")).and_then(|view| crate::readback::packed(&view, row, stride, height));
callback_mapping.unmap(|| b.unmap());
ticket.complete(result);
});
}
@@ -1264,20 +1272,30 @@ impl GpuContext {
if !ticket.active() {
return Err("GPU readback cancelled".into());
}
let mapping = Arc::new(crate::readback::Mapping::new());
let b = buffer.clone();
let callback_mapping = Arc::clone(&mapping);
buffer.map_async(wgpu::MapMode::Read, .., move |r| {
if let Err(e) = r {
callback_mapping.failed();
ticket.complete(Err(format!("GPU buffer mapping: {e}")));
return;
}
if !callback_mapping.mapped() {
return;
}
// Timed out or cancelled: nobody reads the mapping.
if !ticket.active() {
b.unmap();
callback_mapping.unmap(|| b.unmap());
return;
}
// Mapped: the waiting thread reads the rows and unmaps (an empty vector = mapped).
ticket.complete(r.map(|_| Vec::new()).map_err(|e| format!("GPU buffer mapping: {e}")));
ticket.complete(Ok(Vec::new()));
});
// On any failure, unmap (harmless when not mapped; cancels a pending map) so a mapped
// buffer can never be handed out again.
// A timeout can cancel a pending map while its callback is being dispatched. The shared
// guard makes sure the callback and waiter cannot both unmap the same buffer.
let fail = |e: String| {
buffer.unmap();
mapping.unmap(|| buffer.unmap());
e
};
self.wait_mapped(&rx, submission).map_err(fail)?.map_err(fail)?;
@@ -1285,7 +1303,7 @@ impl GpuContext {
let view = buffer.get_mapped_range(..).map_err(|e| format!("GPU mapped range: {e}"));
view.and_then(|view| read(&view))
};
buffer.unmap();
mapping.unmap(|| buffer.unmap());
result
}
+120
View File
@@ -4,6 +4,7 @@
use std::collections::BTreeMap;
#[cfg(not(target_arch = "wasm32"))]
use std::sync::Condvar;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::time::Duration;
@@ -92,6 +93,48 @@ pub(crate) struct Ticket {
id: u64,
}
/// Tracks one GPU buffer mapping shared by the callback and its waiter. A timeout can race a
/// late callback, so the buffer must be unmapped exactly once. A failed map is never unmapped.
pub(crate) struct Mapping(AtomicU8);
impl Mapping {
const PENDING: u8 = 0;
const MAPPED: u8 = 1;
const FAILED: u8 = 2;
const FINISHED: u8 = 3;
pub(crate) fn new() -> Self {
Self(AtomicU8::new(Self::PENDING))
}
/// Claim a successful map callback. False means a timeout already cancelled the map.
pub(crate) fn mapped(&self) -> bool {
self.0.compare_exchange(Self::PENDING, Self::MAPPED, Ordering::AcqRel, Ordering::Acquire).is_ok()
}
/// Record a failed map so the waiter will not unmap a buffer that was never mapped.
pub(crate) fn failed(&self) {
let _ = self.0.compare_exchange(Self::PENDING, Self::FAILED, Ordering::AcqRel, Ordering::Acquire);
}
/// Unmap once if mapping is pending (cancel it) or mapped (release it).
pub(crate) fn unmap(&self, unmap: impl FnOnce()) {
let mut unmap = Some(unmap);
loop {
let state = self.0.load(Ordering::Acquire);
if state != Self::PENDING && state != Self::MAPPED {
return;
}
if self.0.compare_exchange(state, Self::FINISHED, Ordering::AcqRel, Ordering::Acquire).is_ok() {
if let Some(unmap) = unmap.take() {
unmap();
}
return;
}
}
}
}
/// User callbacks run outside locks and are isolated one at a time. On panic=abort
/// (including the usual wasm build), Rust panics cannot be caught: the host must
/// avoid panicking callbacks. This branch does not claim abort-mode recovery.
@@ -402,6 +445,83 @@ mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
#[test]
fn timeout_racing_late_map_callback_unmaps_once() {
let mapping = Arc::new(Mapping::new());
let unmap_calls = Arc::new(AtomicUsize::new(0));
let barrier = Arc::new(std::sync::Barrier::new(3));
let timeout_mapping = Arc::clone(&mapping);
let timeout_calls = Arc::clone(&unmap_calls);
let timeout_barrier = Arc::clone(&barrier);
let timeout = std::thread::spawn(move || {
timeout_barrier.wait();
timeout_mapping.unmap(|| {
timeout_calls.fetch_add(1, Ordering::SeqCst);
});
});
let callback_mapping = Arc::clone(&mapping);
let callback_calls = Arc::clone(&unmap_calls);
let callback_barrier = Arc::clone(&barrier);
let callback = std::thread::spawn(move || {
callback_barrier.wait();
if callback_mapping.mapped() {
callback_mapping.unmap(|| {
callback_calls.fetch_add(1, Ordering::SeqCst);
});
}
});
barrier.wait();
timeout.join().unwrap();
callback.join().unwrap();
assert_eq!(unmap_calls.load(Ordering::SeqCst), 1);
}
#[test]
fn timeout_before_map_callback_cancels_late_map() {
let mapping = Mapping::new();
let unmap_calls = AtomicUsize::new(0);
mapping.unmap(|| {
unmap_calls.fetch_add(1, Ordering::SeqCst);
});
assert!(!mapping.mapped());
mapping.unmap(|| {
unmap_calls.fetch_add(1, Ordering::SeqCst);
});
assert_eq!(unmap_calls.load(Ordering::SeqCst), 1);
}
#[test]
fn mapped_callback_and_timeout_release_once() {
let mapping = Mapping::new();
let unmap_calls = AtomicUsize::new(0);
assert!(mapping.mapped());
mapping.unmap(|| {
unmap_calls.fetch_add(1, Ordering::SeqCst);
});
mapping.unmap(|| {
unmap_calls.fetch_add(1, Ordering::SeqCst);
});
assert_eq!(unmap_calls.load(Ordering::SeqCst), 1);
}
#[test]
fn failed_map_is_not_unmapped() {
let mapping = Mapping::new();
let unmap_calls = AtomicUsize::new(0);
mapping.failed();
mapping.unmap(|| {
unmap_calls.fetch_add(1, Ordering::SeqCst);
});
assert_eq!(unmap_calls.load(Ordering::SeqCst), 0);
}
#[test]
fn early_rejection_reports_no_pixels_exactly_once() {
let calls = Arc::new(AtomicUsize::new(0));
+8 -7
View File
@@ -75,8 +75,9 @@ fn fxc_colorama_phase(c: vec3<f32>, a: f32, mode: u32) -> f32 {
// Selective Color range weights (color2::sc_weights).
fn fxc_sc_weights(c0: vec3<f32>) -> array<f32, 9> {
let c = clamp(c0, vec3<f32>(0.0), vec3<f32>(1.0));
// Stable descending sort of the channel indices.
var idx = vec3<u32>(0u, 1u, 2u);
// Stable descending sort of the channel indices. Use an array because llvmpipe does not
// preserve dynamically indexed writes to vector components here.
var idx = array<u32, 3>(0u, 1u, 2u);
for (var i = 1u; i < 3u; i++) {
var j = i;
while (j > 0u && c[idx[j]] > c[idx[j - 1u]]) {
@@ -86,14 +87,14 @@ fn fxc_sc_weights(c0: vec3<f32>) -> array<f32, 9> {
j--;
}
}
let mx = c[idx.x];
let md = c[idx.y];
let mn = c[idx.z];
let mx = c[idx[0]];
let md = c[idx[1]];
let mn = c[idx[2]];
var w = array<f32, 9>(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
var prim = array<u32, 3>(0u, 2u, 4u);
var sec = array<u32, 3>(3u, 5u, 1u);
w[prim[idx.x]] = mx - md;
w[sec[idx.z]] = md - mn;
w[prim[idx[0]]] = mx - md;
w[sec[idx[2]]] = md - mn;
w[6] = max((mn - 0.5) * 2.0, 0.0);
w[8] = max((0.5 - mx) * 2.0, 0.0);
w[7] = clamp(1.0 - (abs(mx - 0.5) + abs(mn - 0.5)), 0.0, 1.0);
+38 -26
View File
@@ -130,12 +130,15 @@ fn fxl_shape(at: u32, n: u32, x: f32, flip: bool) -> f32 {
// partition_point(|p| p.x < x).
var lo = 0u;
var hi = n;
while (lo < hi) {
let mid = (lo + hi) / 2u;
if (data[at + mid * 2u + kx] < x) {
lo = mid + 1u;
} else {
hi = mid;
// Bound the partition search as for inverse 1D LUTs below.
for (var step = 0u; step < 32u; step++) {
if (lo < hi) {
let mid = lo + (hi - lo) / 2u;
if (data[at + mid * 2u + kx] < x) {
lo = mid + 1u;
} else {
hi = mid;
}
}
}
let i = clamp(lo, 1u, n - 1u);
@@ -156,13 +159,22 @@ fn fxl_shaper_at(s: u32, k: u32) -> vec2<u32> {
return vec2<u32>(at, u32(data[s + k]));
}
// Evaluate independent shaper channels without dynamically writing vector lanes.
fn fxl_shaper_apply(sh: u32, c: vec3<f32>, flip: bool) -> vec3<f32> {
let r = fxl_shaper_at(sh, 0u);
let g = fxl_shaper_at(sh, 1u);
let b = fxl_shaper_at(sh, 2u);
return vec3<f32>(
fxl_shape(r.x, r.y, c.x, flip),
fxl_shape(g.x, g.y, c.y, flip),
fxl_shape(b.x, b.y, c.z, flip)
);
}
fn fxl_file_fwd(o: u32, sh: i32, c0: vec3<f32>, interp: u32) -> vec3<f32> {
var c = c0;
if (sh >= 0) {
for (var k = 0u; k < 3u; k++) {
let a = fxl_shaper_at(u32(sh), k);
c[k] = fxl_shape(a.x, a.y, c[k], false);
}
c = fxl_shaper_apply(u32(sh), c, false);
}
return fxl_lut_apply(o, c, interp);
}
@@ -176,12 +188,16 @@ fn fxl_inv_1d(o: u32, i: u32, y: f32) -> f32 {
// partition_point over the table's values.
var lo = 0u;
var hi = n;
while (lo < hi) {
let mid = (lo + hi) / 2u;
if (data[b1 + mid * 3u + i] < y) {
lo = mid + 1u;
} else {
hi = mid;
// A u32 interval needs at most 32 halvings. A fixed trip count avoids
// a divergent data-dependent loop on software Vulkan without changing lookup.
for (var step = 0u; step < 32u; step++) {
if (lo < hi) {
let mid = lo + (hi - lo) / 2u;
if (data[b1 + mid * 3u + i] < y) {
lo = mid + 1u;
} else {
hi = mid;
}
}
}
let k = clamp(lo, 1u, n - 1u);
@@ -205,10 +221,7 @@ fn fxl_file(o: u32, sh: i32, c: vec3<f32>, interp: u32, inverse: bool) -> vec3<f
x = vec3<f32>(fxl_inv_1d(o, 0u, c.x), fxl_inv_1d(o, 1u, c.y), fxl_inv_1d(o, 2u, c.z));
}
if (sh >= 0) {
for (var k = 0u; k < 3u; k++) {
let a = fxl_shaper_at(u32(sh), k);
x[k] = fxl_shape(a.x, a.y, x[k], true);
}
x = fxl_shaper_apply(u32(sh), x, true);
}
return x;
}
@@ -444,12 +457,11 @@ fn fxl_run(c0: vec3<f32>) -> vec3<f32> {
case 1u, 2u: {
let tf = u32(data[pc + 1u]);
let g = data[pc + 2u];
for (var i = 0u; i < 3u; i++) {
if (op == 1u) {
c[i] = fxl_decode(tf, g, c[i]);
} else {
c[i] = fxl_encode(tf, g, c[i]);
}
// Explicit components avoid llvmpipe's dynamically indexed vector writes.
if (op == 1u) {
c = vec3<f32>(fxl_decode(tf, g, c.x), fxl_decode(tf, g, c.y), fxl_decode(tf, g, c.z));
} else {
c = vec3<f32>(fxl_encode(tf, g, c.x), fxl_encode(tf, g, c.y), fxl_encode(tf, g, c.z));
}
pc += 3u;
}
+29
View File
@@ -97,6 +97,35 @@ fn apply_color_lut() {
effect_case("ec.utility.applylut", &[("lut", s("/nonexistent/grade.cube"))]);
}
// Keep this separate so Nextest runs the inverse lookup in a fresh process/device,
// without the preceding 3D LUT submissions in `ocio_file_transform`. The combined
// test stays intact to cover ordering and shared-device state as well.
#[test]
fn inverse_1d_lut_on_a_fresh_device_matches_cpu() {
effect_direct("ec.color.ociofile", &[("file", s(&cube_1d())), ("direction", e(1))], false);
}
// Isolate the first profile conversion that failed in hosted llvmpipe coverage.
// This retains the same oracle and tolerance as the broader profile test.
#[test]
fn profile_conversion_on_a_fresh_device_matches_cpu() {
effect_direct("ec.utility.colorprofileconverter", &[("inputProfile", e(1)), ("outputProfile", e(3)), ("intent", e(0))], false);
}
// Unequal CSP channel point counts exercise shaper offsets on a fresh device.
#[test]
fn csp_shaper_on_a_fresh_device_matches_cpu() {
effect_direct("ec.color.ociofile", &[("file", s(&csp())), ("interpolation", e(1))], false);
effect_direct("ec.color.ociofile", &[("file", s(&csp())), ("direction", e(1))], false);
}
// A 1D CSP takes the direct inverse-shaper path (flip=true), unlike a 3D CSP.
#[test]
fn inverse_1d_csp_shaper_on_a_fresh_device_matches_cpu() {
let lut = "CSPLUTV100\n1D\n3\n0 0.5 1\n0 0.3 1\n2\n0 1\n0 1\n4\n0 0.2 0.6 1\n0 0.4 0.7 1\n2\n0 0 0\n1 1 1\n";
effect_direct("ec.color.ociofile", &[("file", s(lut)), ("direction", e(1))], false);
}
#[test]
fn ocio_file_transform() {
for interp in 1..4 {
+38
View File
@@ -156,6 +156,44 @@ fn a_wrong_checksum_or_a_bad_module_installs_nothing() {
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn downloads_are_release_assets_of_the_package_repository() {
let dir = tmp("assets");
let sum = "0".repeat(64);
let pkg =
|id: &str, repo: &str, url: &str| json!({"id": id, "name": id, "repo": repo, "versions": [{"version": "1", "api": 1, "url": url, "sha256": sum}]});
let catalog = write_catalog(
&dir,
json!([
pkg("org.test.other", "https://github.com/me/glow", "https://github.com/someone/else/releases/download/v1/glow.wasm"),
pkg("org.test.raw", "https://github.com/me/glow", "https://raw.githubusercontent.com/me/glow/main/glow.wasm"),
pkg("org.test.page", "", "https://github.com/me/glow/blob/main/glow.wasm"),
pkg("org.test.dots", "https://github.com/me/glow", "https://github.com/me/glow/releases/download/%2e%2e/%2E%2E/x/y/glow.wasm"),
pkg("org.test.tree", "https://github.com/me/glow/tree/main", "https://github.com/someone/else/releases/download/v1/glow.wasm"),
pkg("org.test.http", "http://github.com/me/glow", "https://github.com/me/glow/releases/download/v1/glow.wasm"),
pkg("org.test.blob", "https://github.com/me/glow/blob/main/README.md", "https://github.com/me/glow/releases/download/v1/glow.wasm"),
]),
);
let mut s = session(&dir);
s.execute("library.refresh", json!({"catalog": catalog, "wait": true})).unwrap();
for (id, why) in [
("org.test.other", "isn't a release asset of the package's repository https://github.com/me/glow"),
("org.test.raw", "isn't a release asset"),
("org.test.page", "isn't a release asset"),
("org.test.dots", "isn't a release asset"),
("org.test.tree", "isn't a release asset of the package's repository https://github.com/me/glow/tree/main"),
("org.test.http", "org.test.http: the package's `repo` http://github.com/me/glow isn't a GitHub repository URL"),
("org.test.blob", "org.test.blob: the package's `repo` https://github.com/me/glow/blob/main/README.md isn't a GitHub repository URL"),
] {
for c in ["library.install", "library.preview"] {
let e = s.execute(c, json!({"id": id, "wait": true})).unwrap_err().to_string();
assert!(e.contains(why), "{c} {id}: {e}");
}
}
assert!(!dir.join("Plug-ins").join("Library").join("installed.json").exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn installed_packages_load_at_start_and_changed_files_are_refused() {
let dir = tmp("startup");
+9 -3
View File
@@ -505,11 +505,17 @@ pub fn export_comp(project: &Project, comp: ItemId, opts: &ExportOptions, read:
/// Package an export as a dotLottie archive (`manifest.json` + `animations/<id>.json`).
pub fn to_dotlottie(res: &ExportResult, id: &str) -> Vec<u8> {
try_to_dotlottie(res, id).unwrap_or_default()
}
/// Package an export, reporting serialization or ZIP failures before any output is written.
pub fn try_to_dotlottie(res: &ExportResult, id: &str) -> Result<Vec<u8>, LottieError> {
let id: String = id.chars().map(|c| if c.is_ascii_alphanumeric() || c == '_' || c == '-' { c } else { '_' }).collect();
let id = if id.is_empty() { "animation".to_string() } else { id };
let manifest = json!({"version": "1", "generator": "EffectCraft", "author": "", "animations": [{"id": id, "speed": 1, "loop": true}]});
crate::zip::store(&[
("manifest.json".into(), serde_json::to_vec(&manifest).unwrap_or_default()),
(format!("animations/{id}.json"), serde_json::to_vec(&res.json).unwrap_or_default()),
crate::zip::try_store(&[
("manifest.json".into(), serde_json::to_vec(&manifest).map_err(|e| LottieError::Invalid(e.to_string()))?),
(format!("animations/{id}.json"), serde_json::to_vec(&res.json).map_err(|e| LottieError::Invalid(e.to_string()))?),
])
.map_err(|e| LottieError::Invalid(format!("dotLottie ZIP: {e}")))
}
+4 -4
View File
@@ -41,7 +41,7 @@ pub struct ImportResult {
pub warnings: Vec<String>,
}
/// The Lottie document inside raw bytes (plain JSON, or a stored dotLottie archive), read into
/// The Lottie document inside raw bytes (plain JSON, or a dotLottie archive), read into
/// `craft-lottie`'s model within its default limits.
fn document(bytes: &[u8]) -> Result<Animation, LottieError> {
let read = |b: &[u8]| {
@@ -51,12 +51,12 @@ fn document(bytes: &[u8]) -> Result<Animation, LottieError> {
})
};
if bytes.starts_with(b"PK") {
let entries = crate::zip::read_stored(bytes);
let entries = crate::zip::read_entries(bytes).map_err(|e| LottieError::Parse(format!("dotLottie ZIP: {e}")))?;
let anim = entries
.iter()
.find(|(n, _)| n.starts_with("animations/") && n.ends_with(".json"))
.or_else(|| entries.iter().find(|(n, _)| n.ends_with(".json") && n != "manifest.json"))
.ok_or_else(|| LottieError::Parse("dotLottie archive without an uncompressed animation".into()))?;
.ok_or_else(|| LottieError::Parse("dotLottie archive without a JSON animation".into()))?;
return read(&anim.1);
}
read(bytes)
@@ -102,7 +102,7 @@ fn external_image(stored: &str, base_dir: &str) -> Result<String, String> {
}
}
/// Import a Lottie document (JSON or a stored `.lottie` archive) into `project` as a new
/// Import a Lottie document (JSON or a `.lottie` archive) into `project` as a new
/// composition. `store` saves embedded images and returns the file path to use;
/// `base_dir` resolves external image references.
pub fn import(
+1 -1
View File
@@ -30,7 +30,7 @@ pub mod zip;
use effectcraft_color::BlendMode;
use effectcraft_time::{FrameRate, TICKS_PER_SECOND, Tick};
pub use export::{ExportOptions, ExportResult, export_comp, to_dotlottie};
pub use export::{ExportOptions, ExportResult, export_comp, to_dotlottie, try_to_dotlottie};
pub use import::{ImportResult, ImportedImage, import};
#[derive(Debug, thiserror::Error)]
+36 -1
View File
@@ -12,7 +12,7 @@ use effectcraft_render::render_frame;
use effectcraft_time::{FrameRate, Tick};
use serde_json::{Value as Json, json};
use crate::{ExportOptions, export_comp, import};
use crate::{ExportOptions, LottieError, export_comp, import};
fn s(x: f64) -> Tick {
Tick::from_seconds_f64(x)
@@ -799,6 +799,32 @@ fn dotlottie_archive_roundtrip() {
assert_eq!(q.comp(imp.comp).unwrap().layers.len(), 1);
}
#[test]
fn dotlottie_archive_without_animation_is_rejected() {
let zip = craft_lottie::zip::try_store(&[("manifest.json".into(), b"{}".to_vec())]).unwrap();
let mut project = Project::default();
let err = import(&mut project, &zip, "empty archive", "", &mut |_| None).unwrap_err();
assert!(matches!(err, LottieError::Parse(message) if message.contains("without a JSON animation")));
}
#[test]
fn deflated_dotlottie_archive_imports() {
let (mut p, cid) = setup();
let l = solid_layer(&mut p, cid, [0.0, 1.0, 0.0], 40, 40);
push(&mut p, cid, l);
let res = export_comp(&p, cid, &ExportOptions::default(), &no_read).unwrap();
let manifest = serde_json::to_vec(&serde_json::json!({"version":"1","animations":[{"id":"main"}]})).unwrap();
let animation = serde_json::to_vec(&res.json).unwrap();
let zip = craft_lottie::zip::try_store_with_compression(
&[("manifest.json".into(), manifest), ("animations/main.json".into(), animation)],
craft_lottie::zip::Compression::Deflated,
)
.unwrap();
let mut q = Project::default();
let imp = import(&mut q, &zip, "compressed", "", &mut |_| None).unwrap();
assert_eq!(q.comp(imp.comp).unwrap().layers.len(), 1);
}
// ---------------------------------------------------------------- hand-written fixtures
fn fixture(name: &str) -> (Project, ItemId, Vec<String>) {
@@ -1062,3 +1088,12 @@ fn only_image_bytes_are_embedded() {
assert!(res.json["assets"].as_array().is_none_or(|a| a.is_empty()), "{}", res.json["assets"]);
assert!(res.warnings.iter().any(|w| w.contains("not a PNG")), "{:?}", res.warnings);
}
#[test]
fn dotlottie_export_rejects_an_unrepresentable_entry_name() {
let (p, cid) = setup();
let res = export_comp(&p, cid, &ExportOptions::default(), &no_read).unwrap();
let err = crate::try_to_dotlottie(&res, &"a".repeat(usize::from(u16::MAX))).unwrap_err();
assert!(matches!(err, LottieError::Invalid(message) if message.contains("dotLottie ZIP")));
assert_eq!(crate::try_to_dotlottie(&res, "Main").unwrap(), crate::to_dotlottie(&res, "Main"));
}
+2 -120
View File
@@ -1,121 +1,3 @@
//! A minimal ZIP writer (stored entries, PKWARE APPNOTE 6.3 local headers + central directory)
//! for `.lottie` (dotLottie) archives.
//! Shared stored-entry ZIP functions used by dotLottie imports and exports.
fn crc32(data: &[u8]) -> u32 {
let mut crc = 0xFFFF_FFFFu32;
for &b in data {
crc ^= b as u32;
for _ in 0..8 {
crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 };
}
}
!crc
}
/// Build a ZIP archive with the given (name, bytes) entries, uncompressed.
pub fn store(entries: &[(String, Vec<u8>)]) -> Vec<u8> {
let mut out = Vec::new();
let mut central = Vec::new();
let put16 = |v: &mut Vec<u8>, x: u16| v.extend_from_slice(&x.to_le_bytes());
let put32 = |v: &mut Vec<u8>, x: u32| v.extend_from_slice(&x.to_le_bytes());
for (name, data) in entries {
let offset = out.len() as u32;
let crc = crc32(data);
let n = name.as_bytes();
// Local file header.
put32(&mut out, 0x0403_4b50);
put16(&mut out, 20); // version needed
put16(&mut out, 0x0800); // flags: UTF-8 names
put16(&mut out, 0); // stored
put16(&mut out, 0); // time
put16(&mut out, 0x21); // date (1980-01-01)
put32(&mut out, crc);
put32(&mut out, data.len() as u32);
put32(&mut out, data.len() as u32);
put16(&mut out, n.len() as u16);
put16(&mut out, 0);
out.extend_from_slice(n);
out.extend_from_slice(data);
// Central directory record.
put32(&mut central, 0x0201_4b50);
put16(&mut central, 20); // made by
put16(&mut central, 20);
put16(&mut central, 0x0800);
put16(&mut central, 0);
put16(&mut central, 0);
put16(&mut central, 0x21);
put32(&mut central, crc);
put32(&mut central, data.len() as u32);
put32(&mut central, data.len() as u32);
put16(&mut central, n.len() as u16);
put16(&mut central, 0); // extra
put16(&mut central, 0); // comment
put16(&mut central, 0); // disk
put16(&mut central, 0); // internal attrs
put32(&mut central, 0); // external attrs
put32(&mut central, offset);
central.extend_from_slice(n);
}
let cd_offset = out.len() as u32;
let cd_len = central.len() as u32;
out.extend_from_slice(&central);
put32(&mut out, 0x0605_4b50);
put16(&mut out, 0);
put16(&mut out, 0);
put16(&mut out, entries.len() as u16);
put16(&mut out, entries.len() as u16);
put32(&mut out, cd_len);
put32(&mut out, cd_offset);
put16(&mut out, 0);
out
}
/// Whether an entry name is a relative path inside the archive: not absolute (`/…`, `\…`), no
/// drive (`C:…`) and no `..` component.
fn is_relative_name(name: &str) -> bool {
let drive = name.as_bytes().get(1) == Some(&b':');
!name.starts_with(['/', '\\']) && !drive && !name.split(['/', '\\']).any(|c| c == "..")
}
/// Read the stored (uncompressed) entries of a ZIP archive written by [`store`] or any other
/// writer that did not compress. Compressed entries and entries whose names are not relative
/// paths inside the archive are skipped.
pub fn read_stored(data: &[u8]) -> Vec<(String, Vec<u8>)> {
let mut out = vec![];
let mut i = 0usize;
let u16_at = |p: usize| data.get(p..p + 2).map(|b| u16::from_le_bytes([b[0], b[1]]) as usize);
let u32_at = |p: usize| data.get(p..p + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize);
while u32_at(i) == Some(0x0403_4b50) {
let (Some(method), Some(size), Some(nlen), Some(xlen)) = (u16_at(i + 8), u32_at(i + 18), u16_at(i + 26), u16_at(i + 28)) else { break };
let name_start = i + 30;
let body = name_start + nlen + xlen;
let (Some(name), Some(bytes)) = (data.get(name_start..name_start + nlen), data.get(body..body + size)) else { break };
let name = String::from_utf8_lossy(name).to_string();
if method == 0 && is_relative_name(&name) {
out.push((name, bytes.to_vec()));
}
i = body + size;
}
out
}
#[cfg(test)]
mod tests {
#[test]
fn crc_and_roundtrip() {
assert_eq!(super::crc32(b"123456789"), 0xCBF4_3926);
let z = super::store(&[("a.json".into(), b"{}".to_vec()), ("b/c.txt".into(), b"hello".to_vec())]);
let back = super::read_stored(&z);
assert_eq!(back.len(), 2);
assert_eq!(back[1].0, "b/c.txt");
assert_eq!(back[1].1, b"hello");
}
#[test]
fn entries_outside_the_archive_root_are_skipped() {
let names = ["../up.txt", "a/../../up.txt", "/abs/x", "\\abs\\x", "C:\\x", "c:x", "a\\..\\x", "ok/x.txt", "ok/..x"];
let z = super::store(&names.iter().map(|n| (n.to_string(), b"x".to_vec())).collect::<Vec<_>>());
let back: Vec<String> = super::read_stored(&z).into_iter().map(|(n, _)| n).collect();
assert_eq!(back, ["ok/x.txt", "ok/..x"]);
}
}
pub use craft_lottie::zip::{Limits, read_entries, read_entries_with_limits, read_stored, store, try_store, try_store_with_compression};
+6 -7
View File
@@ -23,16 +23,14 @@ effectcraft-model = { workspace = true }
filmcraft-codecs = { workspace = true }
filmcraft-media = { workspace = true }
craft-dpx = { workspace = true }
craft-exr = { workspace = true }
craft-gif = { workspace = true }
craft-webp = { workspace = true }
craft-audio = { workspace = true }
craft-media = { workspace = true }
filmcraft-mpegts = { workspace = true }
craft-exr = { workspace = true, features = ["decode"] }
craft-gif = { workspace = true, features = ["frames"] }
craft-webp = { workspace = true, features = ["frames"] }
craft-audio = { workspace = true, features = ["aac-decode"] }
craft-media = { workspace = true, features = ["mpegts"] }
filmcraft-frame = { workspace = true }
filmcraft-color = { workspace = true }
filmcraft-time = { workspace = true }
exr = { workspace = true }
image = { workspace = true, features = ["gif", "webp", "tiff", "bmp", "exr", "tga", "hdr"] }
rayon = { workspace = true }
serde_json = { workspace = true }
@@ -40,6 +38,7 @@ thiserror = { workspace = true }
log = { workspace = true }
[dev-dependencies]
exr = { workspace = true }
# Writes the animated GIFs of the tests.
gif = { workspace = true }
tempfile = { workspace = true }
+5 -14
View File
@@ -62,15 +62,15 @@ fn matroska(file: &dyn ByteReader) -> Option<Vec<u8>> {
/// first unit it can read. Only AAC is asked about, so the streams before that one were refused
/// whatever their codec, and an ADTS stream played has no configuration outside its frames.
fn latm(file: &dyn ByteReader) -> Option<Vec<u8>> {
use filmcraft_mpegts::Codec;
use craft_media::mpegts::{self, Codec};
let head = Head(file, file.len().min(MPEG_HEAD));
let mpeg = filmcraft_mpegts::open(&head).ok()?;
let audio = mpeg.streams.iter().enumerate().filter(|(_, s)| s.kind() == filmcraft_mpegts::Kind::Audio && !s.units.is_empty());
let mpeg = mpegts::open(&head).ok()?;
let audio = mpeg.streams.iter().enumerate().filter(|(_, s)| s.kind() == mpegts::Kind::Audio && !s.units.is_empty());
let played = audio.take(filmcraft_media::MAX_AUDIO_STREAMS).find_map(|(i, s)| {
let first = mpeg.read_unit(&head, i, 0).ok()?;
match s.codec {
Codec::AacAdts => filmcraft_mpegts::frame_info(&s.codec, &first).map(|_| None),
Codec::AacLatm => filmcraft_codecs::audio::latm_config(&first).map(|c| Some(c.asc)),
Codec::AacAdts => mpegts::frame_info(&s.codec, &first).map(|_| None),
Codec::AacLatm => craft_audio::aac::latm_config(&first).map(|c| Some(c.asc)),
_ => None,
}
});
@@ -97,12 +97,3 @@ impl craft_media::ByteSource for Head<'_> {
self.read(offset, buf)
}
}
impl filmcraft_mpegts::ByteSource for Head<'_> {
fn len(&self) -> u64 {
self.1
}
fn read_at(&self, offset: u64, buf: &mut [u8]) -> io::Result<()> {
self.read(offset, buf)
}
}
+176 -224
View File
@@ -1,5 +1,5 @@
//! Auxiliary channels from multi-layer OpenEXR files (depth, object / material IDs, normals,
//! Cryptomatte, …) for the 3D Channel effects, read with the pure-Rust `exr` crate.
//! Cryptomatte, …) for the 3D Channel effects, read through the shared `craft-exr` decoder.
//!
//! Every channel of every layer becomes a plane named `layer.channel` (or just `channel` for
//! the unnamed layer), placed in the display window (pixels outside a layer's data window are
@@ -18,12 +18,6 @@ use std::sync::{Arc, Mutex, OnceLock, PoisonError};
use effectcraft_raster::channels3d::{BACKGROUND_DEPTH, split_channel};
use effectcraft_raster::{AuxChannels, Image};
use effectcraft_render::reads::{ReadFile, ReadFiles};
use exr::block::UncompressedBlock;
use exr::block::chunk::TileCoordinates;
use exr::image::read::image::ReadImage;
use exr::image::read::layers::{ChannelsReader, ReadAllLayers, ReadChannels};
use exr::meta::attribute::SampleType;
use exr::meta::header::Header;
use crate::convert::{AlphaOp, float_to_image};
@@ -35,179 +29,20 @@ const MAX_DISPLAY_PIXELS: usize = 1 << 28;
use craft_exr::channels::{is_depth, part_channel_name as channel_name};
pub use craft_exr::cryptomatte::parse_manifest;
/// The part name channels are prefixed with (`None`: unnamed).
fn part_name(header: &Header) -> Option<String> {
header.own_attributes.layer_name.as_ref().map(|t| t.to_string()).filter(|s| !s.is_empty())
}
/// Why an OpenEXR file's pixels can't be decoded when its headers name a compression this build
/// can't decompress (HTJ2K, OpenEXR 3.4's High-Throughput JPEG 2000): the decoders only said
/// "no non-deep rgb channels" or showed a transparent frame (#482). `None` for other files.
pub fn unsupported_compression(bytes: &[u8]) -> Option<&'static str> {
use exr::compression::Compression;
let meta = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false).ok()?;
meta.headers
let mut limits = craft_exr::decode::Limits::default();
limits.max_pixels = u64::MAX;
limits.max_bytes = u64::MAX;
craft_exr::decode::parts(bytes, &limits)
.ok()?
.iter()
.any(|h| matches!(h.compression, Compression::HTJ2K32 | Compression::HTJ2K256))
.any(|part| matches!(part.compression.as_str(), "HTJ2K32" | "HTJ2K256"))
.then_some("OpenEXR HTJ2K compression isn't supported yet: re-save the file with ZIP, PIZ or DWAA compression")
}
/// Which channels a decode keeps, by plane name, and the display window they are placed in
/// (the first part's, as the `exr` crate reports the image's).
struct Planes<'k> {
keep: &'k dyn Fn(&str) -> bool,
display: exr::meta::attribute::IntegerBounds,
}
/// One part's kept channels while it is decoded: each line of samples goes straight into its
/// channel's display-window plane (no copy of the part's samples in their own type first).
struct PlanesReader {
/// Per channel of the part: its plane name, sample type and plane (`None`: not kept).
planes: Vec<Option<(String, SampleType, Vec<f32>)>>,
/// The data window's position in the display window, and the data window's and display
/// window's sizes.
offset: (i64, i64),
layer: (usize, usize),
display: (usize, usize),
f16s: Vec<exr::prelude::f16>,
u32s: Vec<u32>,
f32s: Vec<f32>,
/// The part's channel indices of exact `R`, `G`, `B` and `A`, when it has R, G and B.
rgb: Option<([usize; 3], Option<usize>)>,
/// The part's display window differs from the first part's.
own_display: bool,
}
/// A decoded part: its kept planes (in channel order) and what [`PlanesReader`] found.
struct PartPlanes {
planes: Vec<(String, Vec<f32>)>,
/// Positions in `planes` of the part's R, G, B (and A) channels, when it has them and keeps
/// them all.
rgb: Option<([usize; 3], Option<usize>)>,
has_rgb: bool,
exact: bool,
}
impl<'s> ReadChannels<'s> for Planes<'_> {
type Reader = PlanesReader;
fn create_channels_reader(&'s self, header: &Header) -> exr::error::Result<PlanesReader> {
if header.deep {
return Err(exr::error::Error::NotSupported("deep data".into()));
}
let dw = self.display;
let display = (dw.size.0, dw.size.1);
// Each channel is a display-window plane: a 60000² window over a 1×1 data window was a
// multi-gigabyte allocation per channel (#2141).
let n = display.0.checked_mul(display.1).filter(|n| *n > 0 && *n <= MAX_DISPLAY_PIXELS).ok_or(exr::error::Error::Invalid("display window".into()))?;
let prefix = part_name(header);
let pos = header.own_attributes.layer_position;
let offset = (i64::from(pos.0) - i64::from(dw.position.0), i64::from(pos.1) - i64::from(dw.position.1));
let planes = header
.channels
.list
.iter()
.map(|ch| {
let name = channel_name(prefix.as_deref(), &ch.name.to_string());
let fill = if is_depth(&name) { BACKGROUND_DEPTH } else { 0.0 };
(self.keep)(&name).then(|| (name, ch.sample_type, vec![fill; n]))
})
.collect();
let find = |c: &str| header.channels.find_index_of_channel(&exr::meta::attribute::Text::from(c));
let rgb = match (find("R"), find("G"), find("B")) {
(Some(r), Some(g), Some(b)) => Some(([r, g, b], find("A"))),
_ => None,
};
Ok(PlanesReader {
planes,
offset,
layer: (header.layer_size.0, header.layer_size.1),
display,
f16s: vec![],
u32s: vec![],
f32s: vec![],
rgb,
own_display: header.shared_attributes.display_window != dw,
})
}
}
impl ChannelsReader for PlanesReader {
type Channels = PartPlanes;
fn filter_block(&self, tile: TileCoordinates) -> bool {
tile.is_largest_resolution_level() && self.planes.iter().any(Option::is_some)
}
fn read_block(&mut self, header: &Header, block: UncompressedBlock) -> exr::error::UnitResult {
let ((lw, lh), (w, h)) = (self.layer, self.display);
for line in block.lines(&header.channels) {
let Some(Some((_, kind, plane))) = self.planes.get_mut(line.location.channel) else { continue };
let (x0, y, count) = (line.location.position.0, line.location.position.1, line.location.sample_count);
// The samples of data-window row `y`, columns `x0..x0 + count`, at display row `ty`
// from display column `tx0`; clipped to the display window.
if y >= lh || x0.checked_add(count).is_none_or(|end| end > lw) {
continue;
}
let Some(ty) = usize::try_from(y as i64 + self.offset.1).ok().filter(|ty| *ty < h) else { continue };
let tx0 = x0 as i64 + self.offset.0;
let skip = usize::try_from(-tx0).unwrap_or(0).min(count);
let tx = usize::try_from(tx0.max(0)).unwrap_or(usize::MAX);
let take = (count - skip).min(w.saturating_sub(tx));
let start = ty.checked_mul(w).and_then(|row| row.checked_add(tx));
let Some(dst) = start.filter(|_| take > 0).and_then(|s| plane.get_mut(s..s.checked_add(take)?)) else { continue };
match kind {
SampleType::F16 => {
self.f16s.resize(count, exr::prelude::f16::ZERO);
line.read_samples_into_slice(&mut self.f16s)?;
for (d, s) in dst.iter_mut().zip(self.f16s.iter().skip(skip)) {
*d = s.to_f32();
}
}
SampleType::F32 => {
self.f32s.resize(count, 0.0);
line.read_samples_into_slice(&mut self.f32s)?;
for (d, s) in dst.iter_mut().zip(self.f32s.iter().skip(skip)) {
*d = *s;
}
}
SampleType::U32 => {
self.u32s.resize(count, 0);
line.read_samples_into_slice(&mut self.u32s)?;
for (d, s) in dst.iter_mut().zip(self.u32s.iter().skip(skip)) {
*d = *s as f32;
}
}
}
}
Ok(())
}
fn into_channels(self) -> PartPlanes {
// Positions of the kept channels among the kept planes.
let mut at = vec![None; self.planes.len()];
let mut planes = Vec::with_capacity(self.planes.len());
for (i, p) in self.planes.into_iter().enumerate() {
if let Some((name, _, data)) = p {
if let Some(slot) = at.get_mut(i) {
*slot = Some(planes.len());
}
planes.push((name, data));
}
}
let kept = |i: usize| at.get(i).copied().flatten();
let has_rgb = self.rgb.is_some();
let rgb = self.rgb.and_then(|([r, g, b], a)| {
let a = match a {
Some(a) => Some(kept(a)?),
None => None,
};
Some(([kept(r)?, kept(g)?, kept(b)?], a))
});
PartPlanes { planes, rgb, has_rgb, exact: !self.own_display }
}
}
/// How the `image` crate's OpenEXR decoder would show a file: the first part with exact `R`, `G`
/// and `B` channels (alpha `A` when it has one, else opaque), placed in the display window with
/// 0 outside the data window.
@@ -253,73 +88,117 @@ enum Refused {
TooLarge(String),
}
/// Bytes the planes of the channels `keep` names would take (`None`: not a readable header).
fn planes_bytes(meta: &exr::meta::MetaData, keep: &dyn Fn(&str) -> bool) -> Option<usize> {
let dw = meta.headers.first()?.shared_attributes.display_window;
let n = dw.size.0.checked_mul(dw.size.1)?;
let mut kept = 0usize;
for header in meta.headers.iter() {
let prefix = part_name(header);
kept = kept.saturating_add(header.channels.list.iter().filter(|ch| keep(&channel_name(prefix.as_deref(), &ch.name.to_string()))).count());
}
Some(n.saturating_mul(kept).saturating_mul(std::mem::size_of::<f32>()))
/// Bytes the selected channels occupy in each part's own display window. The channel cache
/// reserves this before decoding so concurrent imports cannot exceed its soft budget.
fn planes_bytes(infos: &[craft_exr::decode::PartInfo], keep: &dyn Fn(&str) -> bool) -> Option<usize> {
infos.first()?;
let total = infos.iter().try_fold(0u64, |total, part| {
let count = part.channels.iter().filter(|channel| keep(&channel_name(part.name.as_deref(), &channel.name))).count() as u64;
if count == 0 {
return Some(total);
}
let pixels = u64::from(part.display_window.width).checked_mul(u64::from(part.display_window.height))?;
total.checked_add(pixels.checked_mul(count)?.checked_mul(4)?)
})?;
usize::try_from(total).ok()
}
/// Decode the channels of an EXR file's bytes that `keep` names (by plane name).
/// Decode selected EXR channels through craft-exr, which places each selected block directly
/// into its bounded display-window plane.
fn decode(bytes: &[u8], keep: &dyn Fn(&str) -> bool) -> Result<Decoded, Refused> {
let meta = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false).map_err(|_| Refused::Unreadable)?;
let display = meta.headers.first().ok_or(Refused::Unreadable)?.shared_attributes.display_window;
let need = planes_bytes(&meta, keep).ok_or(Refused::Unreadable)?;
if need > MAX_DECODE_BYTES {
let gb = |b: usize| b as f64 / f64::from(1u32 << 30);
use craft_exr::decode::{self, Error as ExrError, Limits};
let mut limits = Limits::default();
limits.max_pixels = MAX_DISPLAY_PIXELS as u64;
limits.max_bytes = MAX_DECODE_BYTES as u64;
// Header-only checks must not charge channels this caller does not decode. The shared display
// decoder independently checks the exact selected display planes before allocating them. It
// also bounds a selected part's data window across all of its channels (decoded blocks carry
// every channel before the unused ones are dropped), so a part whose full channel set exceeds
// `max_bytes` is refused even when the kept planes alone would fit.
let mut header_limits = limits;
header_limits.max_width = u32::MAX;
header_limits.max_height = u32::MAX;
header_limits.max_pixels = u64::MAX;
header_limits.max_bytes = u64::MAX;
let infos = decode::parts(bytes, &header_limits).map_err(|e| match e {
ExrError::LimitExceeded(message) => Refused::TooLarge(message),
_ => Refused::Unreadable,
})?;
let display = infos.first().ok_or(Refused::Unreadable)?.display_window;
let needed = planes_bytes(&infos, keep).filter(|bytes| *bytes > 0).ok_or(Refused::Unreadable)?;
if needed > MAX_DECODE_BYTES {
let gb = |b: u64| b as f64 / f64::from(1u32 << 30);
return Err(Refused::TooLarge(format!(
"its {}×{} OpenEXR channels need {:.1} GB of memory, more than the {:.1} GB one file may use",
display.size.0,
display.size.1,
gb(need),
gb(MAX_DECODE_BYTES)
display.width,
display.height,
gb(needed as u64),
gb(MAX_DECODE_BYTES as u64),
)));
}
let read = ReadImage::new(ReadAllLayers { read_channels: Planes { keep, display } }, |_| {});
let img = read.from_buffered(std::io::Cursor::new(bytes)).map_err(|_| Refused::Unreadable)?;
let dw = img.attributes.display_window;
let mut aux = AuxChannels::new(dw.size.0 as u32, dw.size.1 as u32, 1.0);
let mut aux = AuxChannels::new(display.width, display.height, 1.0);
let mut crypto: HashMap<String, (Option<String>, Option<String>)> = HashMap::new();
let mut collect_meta = |other: &HashMap<exr::meta::attribute::Text, exr::meta::attribute::AttributeValue>| {
for (k, v) in other {
let key = k.to_string();
let mut rgb = Rgb::None;
for info in infos {
for (key, value) in &info.text_attributes {
let Some(rest) = key.strip_prefix("cryptomatte/") else { continue };
let Some((id, field)) = rest.split_once('/') else { continue };
let exr::meta::attribute::AttributeValue::Text(t) = v else { continue };
let e = crypto.entry(id.to_string()).or_default();
let entry = crypto.entry(id.to_string()).or_default();
match field {
"name" => e.0 = Some(t.to_string()),
"manifest" => e.1 = Some(t.to_string()),
"name" => entry.0 = Some(value.clone()),
"manifest" => entry.1 = Some(value.clone()),
_ => {}
}
}
};
collect_meta(&img.attributes.other);
let mut rgb = Rgb::None;
for layer in img.layer_data {
collect_meta(&layer.attributes.other);
let part = layer.channel_data;
if rgb == Rgb::None && part.has_rgb {
let prefix = info.name.as_deref();
let exact = info.display_window == display;
let rgb_indices = ["R", "G", "B"].map(|wanted| info.channels.iter().position(|channel| channel.name == wanted));
let has_rgb = rgb_indices.iter().all(Option::is_some);
let alpha_index = info.channels.iter().position(|channel| channel.name == "A");
// Metadata above still belongs to the file, but an ignored part must not allocate or
// impose its pixel/deep-data limits on the selected picture channels.
if !info.channels.iter().any(|channel| keep(&channel_name(prefix, &channel.name))) {
if rgb == Rgb::None && has_rgb {
rgb = Rgb::Unsupported;
}
continue;
}
let mut part = decode::decode_display_part(
bytes,
info.index,
keep,
&|name| {
if is_depth(name) { BACKGROUND_DEPTH } else { 0.0 }
},
&limits,
)
.map_err(|error| match error {
ExrError::LimitExceeded(message) => Refused::TooLarge(message),
_ => Refused::Unreadable,
})?;
if rgb == Rgb::None && has_rgb {
let base = aux.channels.len();
rgb = match part.rgb {
Some(([r, g, b], a)) if part.exact => {
let picked = [Some(r), Some(g), Some(b), a];
let depth = picked.iter().flatten().any(|i| part.planes.get(*i).is_some_and(|(n, _)| is_depth(n)));
if depth { Rgb::Unsupported } else { Rgb::Planes([base + r, base + g, base + b], a.map(|a| base + a)) }
let locate = |raw: usize| {
let name = channel_name(prefix, &info.channels.get(raw)?.name);
part.channels.iter().position(|channel| channel.name == name)
};
rgb = match (rgb_indices[0].and_then(locate), rgb_indices[1].and_then(locate), rgb_indices[2].and_then(locate)) {
(Some(r), Some(g), Some(b)) if exact => {
let a = alpha_index.and_then(locate);
let depth =
[Some(r), Some(g), Some(b), a].into_iter().flatten().any(|i| part.channels.get(i).is_some_and(|channel| is_depth(&channel.name)));
if depth { Rgb::Unsupported } else { Rgb::Planes([base + r, base + g, base + b], a.map(|i| base + i)) }
}
_ => Rgb::Unsupported,
};
}
aux.channels.extend(part.planes);
aux.channels.extend(part.channels.drain(..).map(|channel| (channel.name, channel.samples)));
}
for (_, (name, manifest)) in crypto {
if let (Some(n), Some(m)) = (name, manifest) {
aux.manifests.push((n, parse_manifest(&m)));
if let (Some(name), Some(manifest)) = (name, manifest) {
aux.manifests.push((name, parse_manifest(&manifest)));
}
}
aux.manifests.sort();
@@ -389,11 +268,16 @@ fn picture_names(aux: &AuxChannels) -> Option<([String; 3], Option<String>)> {
/// headers, then only those channels decoded; in a multi-part file the other parts' pixels are
/// not even decompressed), with the names of its planes.
fn picture_only(bytes: &[u8]) -> Result<(Decoded, [String; 3], Option<String>), Refused> {
let meta = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false).map_err(|_| Refused::Unreadable)?;
let mut limits = craft_exr::decode::Limits::default();
limits.max_width = u32::MAX;
limits.max_height = u32::MAX;
limits.max_pixels = u64::MAX;
limits.max_bytes = u64::MAX;
let meta = craft_exr::decode::parts(bytes, &limits).map_err(|_| Refused::Unreadable)?;
let mut names = AuxChannels::new(0, 0, 1.0);
for header in meta.headers.iter() {
let prefix = part_name(header);
names.channels.extend(header.channels.list.iter().map(|ch| (channel_name(prefix.as_deref(), &ch.name.to_string()), vec![])));
for part in meta {
let prefix = part.name.as_deref();
names.channels.extend(part.channels.iter().map(|ch| (channel_name(prefix, &ch.name), vec![])));
}
let (rgb, a) = picture_names(&names).ok_or(Refused::Unreadable)?;
// Planes are looked up by name ignoring case, so every channel that could answer is kept.
@@ -679,8 +563,13 @@ impl ChannelCache {
/// Decode the file at `path` whose bytes are `bytes` for `slot` (see [`ChannelCache::get`]).
fn decode_file(&self, path: &str, slot: &Slot, bytes: &[u8]) -> Option<Arc<Decoded>> {
// The planes it will allocate count against the budget while it decodes.
if let Ok(meta) = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false)
&& let Some(need) = planes_bytes(&meta, &|_| true)
let mut limits = craft_exr::decode::Limits::default();
limits.max_width = u32::MAX;
limits.max_height = u32::MAX;
limits.max_pixels = u64::MAX;
limits.max_bytes = u64::MAX;
if let Ok(infos) = craft_exr::decode::parts(bytes, &limits)
&& let Some(need) = planes_bytes(&infos, &|_| true)
{
self.account(slot, need.min(MAX_DECODE_BYTES));
}
@@ -1264,6 +1153,69 @@ mod tests {
assert!(aux.get("ViewLayer.Combined.R").is_some());
}
#[test]
fn multipart_fallback_filters_channels_and_reserves_display_planes() {
use exr::prelude::*;
let part = |name: &str, pos: (i32, i32), size: (usize, usize), channels: Vec<AnyChannel<FlatSamples>>| {
let attrs = LayerAttributes { layer_name: Some(Text::from(name)), layer_position: Vec2(pos.0, pos.1), ..Default::default() };
Layer::new(size, attrs, Encoding::FAST_LOSSLESS, AnyChannels::sort(channels.into()))
};
let (w, h) = (2usize, 1usize);
let bytes = file(
vec![
part("Depth", (0, 0), (1, 1), vec![AnyChannel::new("Z", FlatSamples::F32(vec![7.0]))]),
part(
"Beauty",
(1, 1),
(w, h),
vec![
AnyChannel::new("R", FlatSamples::F32(vec![0.1; w * h])),
AnyChannel::new("G", FlatSamples::F32(vec![0.2; w * h])),
AnyChannel::new("B", FlatSamples::F32(vec![0.3; w * h])),
],
),
],
IntegerBounds::from_dimensions((3, 2)),
);
let mut limits = craft_exr::decode::Limits::default();
limits.max_width = u32::MAX;
limits.max_height = u32::MAX;
limits.max_pixels = u64::MAX;
limits.max_bytes = u64::MAX;
let parts = craft_exr::decode::parts(&bytes, &limits).unwrap();
assert_eq!(planes_bytes(&parts, &|name| name == "Beauty.R"), Some(3 * 2 * 4));
assert_eq!(planes_bytes(&parts, &|_| false), Some(0));
// Different display windows must not reserve every part at the first part's size.
let mut varied = parts.clone();
varied[0].display_window.width = 1;
varied[0].display_window.height = 1;
varied[1].display_window.width = 10;
varied[1].display_window.height = 20;
assert_eq!(planes_bytes(&varied, &|_| true), Some(4 + 10 * 20 * 3 * 4));
assert_eq!(planes_bytes(&varied, &|name| name == "Beauty.R"), Some(10 * 20 * 4));
varied[1].display_window.width = u32::MAX;
varied[1].display_window.height = u32::MAX;
assert_eq!(planes_bytes(&varied, &|_| true), None);
assert_eq!(planes_bytes(&varied, &|name| name == "Depth.Z"), Some(4));
// Skipping the RGB part preserves the first-RGB fallback state and depth samples.
let depth = decode(&bytes, &|name| name == "Depth.Z").unwrap();
assert_eq!(depth.rgb, Rgb::Unsupported);
assert_eq!(depth.aux.channels.len(), 1);
assert_eq!(depth.aux.get("Depth.Z").unwrap()[0], 7.0);
let selected = decode(&bytes, &|name| name == "Beauty.R").unwrap();
assert_eq!(selected.aux.channels.iter().map(|(name, _)| name.as_str()).collect::<Vec<_>>(), ["Beauty.R"]);
assert_eq!(selected.aux.get("Beauty.R").unwrap(), [0.0, 0.0, 0.0, 0.0, 0.1, 0.1]);
// The old app decoder selected the named beauty part when the first part was depth-only,
// and placed its data into the shared display window; retain that layered-picture result.
let image = layered_image(&bytes).unwrap().to_rgba32f();
assert_eq!(image.get_pixel(1, 1).0, [0.1, 0.2, 0.3, 1.0]);
assert_eq!(image.get_pixel(0, 0).0, [0.0, 0.0, 0.0, 1.0]);
}
/// Straight RGBA of [`layered_image`] at pixel 0 for a file of single-value channels.
fn picture<S: AsRef<str>>(channels: &[(S, f32)]) -> Vec<f32> {
use exr::prelude::*;
+155 -34
View File
@@ -5,11 +5,9 @@
//! delays), so uneven timing is kept exactly; [`Anim::frame_at`] looks the frame up by time.
//! Animated WebP ([`crate::webp_anim`]) shares [`Anim`] and this decode path.
use std::io::Cursor;
use effectcraft_project::{AlphaMode, Footage, FootageKind};
use effectcraft_time::{FrameRate, Tick};
use image::{AnimationDecoder, DynamicImage, codecs::gif::GifDecoder};
use image::DynamicImage;
use crate::{MediaError, Result};
@@ -26,6 +24,10 @@ pub(crate) const MAX_BYTES: usize = 256 << 20;
/// Frames read from a GIF's or animated WebP's headers when probing.
pub(crate) const MAX_FRAMES: usize = 100_000;
/// Frames kept when decoding a GIF or animated WebP: all of them. Only [`MAX_BYTES`] and the
/// canvas budget bound decoding, as before the shared frame readers; [`MAX_FRAMES`] bounds probing.
pub(crate) const DECODE_FRAMES: usize = usize::MAX;
/// Whether `f` is an animated GIF or WebP imported by [`probe`] or [`crate::webp_anim::probe`]:
/// its frames come from an [`Anim`].
pub(crate) fn is_animated(f: &Footage) -> bool {
@@ -86,43 +88,57 @@ impl Anim {
if crate::webp_anim::is_webp(bytes) {
return crate::webp_anim::load(bytes);
}
let mut dec = GifDecoder::new(Cursor::new(bytes)).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?;
// A frame may be far larger than the canvas, and the decoder allocates its buffer before
// decoding it: no single buffer may pass what all the frames may hold.
let mut limits = image::Limits::no_limits();
limits.max_alloc = Some(MAX_BYTES as u64);
image::ImageDecoder::set_limits(&mut dec, limits).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?;
// The decoder allocates the full logical canvas for the first frame, however small the
// frames themselves are: check it before decoding any.
let (w, h) = image::ImageDecoder::dimensions(&dec);
if u64::from(w) * u64::from(h) * 4 > MAX_BYTES as u64 {
return Err(MediaError::Unsupported(format!("GIF canvas of {w}×{h} is too large to hold in memory")));
}
let (mut frames, mut starts) = (Vec::new(), Vec::new());
let (mut at, mut size) = (0i64, 0usize);
for frame in dec.into_frames() {
let frame = match frame {
Ok(f) => f,
Err(e) if frames.is_empty() => return Err(MediaError::Decode(format!("GIF: {e}"))),
Err(e) => {
log::warn!("media: GIF stops after {} frames: {e}", frames.len());
break;
}
};
let (numer, denom) = frame.delay().numer_denom_ms();
let cs = delay_cs(numer.checked_div(denom).unwrap_or(0) / 10);
let buf = frame.into_buffer();
size = size.saturating_add(buf.as_raw().len());
if size > MAX_BYTES {
return Err(MediaError::Unsupported("GIF is too large to hold in memory".into()));
// The frame reader scans every image descriptor before it checks the canvas: refuse a
// logical screen too large to hold first, as the former decoder did.
if let Some(&[w0, w1, h0, h1]) = bytes.get(6..10).filter(|_| bytes.starts_with(b"GIF8")) {
let (w, h) = (u16::from_le_bytes([w0, w1]), u16::from_le_bytes([h0, h1]));
if u64::from(w) * u64::from(h) * 4 > MAX_BYTES as u64 {
return Err(MediaError::Unsupported(format!("GIF canvas of {w}×{h} is too large to hold in memory")));
}
}
let mut limits = craft_gif::frames::FrameLimits::default();
limits.max_frames = DECODE_FRAMES;
limits.max_bytes = MAX_BYTES as u64;
limits.max_canvas_bytes = MAX_BYTES as u64;
limits.max_decode_bytes = Some(MAX_BYTES as u64);
let decoded = craft_gif::frames::read_frames(bytes, &limits).map_err(|e| match e {
craft_gif::frames::FrameError::Malformed(m) => MediaError::Decode(format!("GIF: {m}")),
craft_gif::frames::FrameError::LimitExceeded(m)
if m.contains("cannot hold") || m.contains("decode-allocation limit") || m.to_ascii_lowercase().contains("memory limit") =>
{
MediaError::Unsupported("GIF is too large to hold in memory".into())
}
craft_gif::frames::FrameError::LimitExceeded(m) if m.contains("canvas is over") => {
MediaError::Unsupported("GIF canvas is too large to hold in memory".into())
}
craft_gif::frames::FrameError::LimitExceeded(m) => MediaError::Unsupported(format!("GIF: {m}")),
})?;
if matches!(&decoded.stop, craft_gif::frames::Stop::ByteLimit | craft_gif::frames::Stop::FrameLimit) {
return Err(MediaError::Unsupported("GIF is too large to hold in memory".into()));
}
if let craft_gif::frames::Stop::Damaged(ref e) = decoded.stop {
log::warn!("media: GIF stops after {} frames: {e}", decoded.frames.len());
}
let mut frames = Vec::with_capacity(decoded.frames.len());
let mut starts = Vec::with_capacity(decoded.frames.len());
let mut at = 0i64;
for frame in decoded.frames {
let cs = delay_cs(frame.delay_ms / 10);
let image = image::RgbaImage::from_raw(decoded.width, decoded.height, frame.rgba)
.ok_or_else(|| MediaError::Decode("GIF: invalid decoded frame dimensions".into()))?;
starts.push(Tick::from_units(at, 100));
at = at.saturating_add(i64::from(cs));
frames.push(DynamicImage::ImageRgba8(buf));
frames.push(DynamicImage::ImageRgba8(image));
}
Anim::new(frames, starts).ok_or_else(|| MediaError::Decode("GIF: no frames".into()))
}
/// How many frames were decoded.
#[cfg(test)]
pub(crate) fn len(&self) -> usize {
self.frames.len()
}
/// The frame shown at source time `t` (the first one before the start, the last one past the
/// end).
pub(crate) fn frame_at(&self, t: Tick) -> Option<&DynamicImage> {
@@ -133,9 +149,13 @@ impl Anim {
#[cfg(test)]
mod tests {
use std::io::Cursor;
use effectcraft_project::{Footage, FootageKind};
use effectcraft_time::{FrameRate, Tick};
use image::AnimationDecoder;
use super::MAX_FRAMES;
use crate::{MediaPool, probe_bytes};
/// A 2×2 GIF with one solid-colour frame per `(rgb, delay)`.
@@ -152,6 +172,15 @@ mod tests {
out
}
/// Preserve the old consumer path as a test oracle while the frame API is shared.
fn legacy_gif_frames(bytes: &[u8]) -> Vec<image::RgbaImage> {
image::codecs::gif::GifDecoder::new(Cursor::new(bytes)).unwrap().into_frames().map(|frame| frame.unwrap().into_buffer()).collect()
}
fn legacy_gif_frames_until_error(bytes: &[u8]) -> Vec<image::RgbaImage> {
image::codecs::gif::GifDecoder::new(Cursor::new(bytes)).unwrap().into_frames().map_while(|frame| frame.ok()).map(|frame| frame.into_buffer()).collect()
}
const RED: [u8; 3] = [255, 0, 0];
const GREEN: [u8; 3] = [0, 255, 0];
const BLUE: [u8; 3] = [0, 0, 255];
@@ -201,6 +230,62 @@ mod tests {
assert_eq!(f.duration, Tick::from_units(20, 100));
}
#[test]
fn shared_gif_frames_match_legacy_compositing_and_disposal() {
let mut bytes = Vec::new();
{
let palette = [255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0];
let mut enc = gif::Encoder::new(&mut bytes, 3, 2, &palette).unwrap();
let first = gif::Frame {
width: 3,
height: 2,
buffer: vec![0; 6].into(), // red background
delay: 5,
dispose: gif::DisposalMethod::Keep,
..gif::Frame::default()
};
enc.write_frame(&first).unwrap();
let second = gif::Frame {
left: 1,
width: 1,
height: 1,
buffer: vec![1].into(), // green patch
delay: 7,
dispose: gif::DisposalMethod::Background,
..gif::Frame::default()
};
enc.write_frame(&second).unwrap();
let third = gif::Frame {
left: 2,
width: 1,
height: 1,
buffer: vec![2].into(), // blue patch after disposal
delay: 9,
dispose: gif::DisposalMethod::Previous,
..gif::Frame::default()
};
enc.write_frame(&third).unwrap();
let fourth = gif::Frame {
width: 1,
height: 1,
buffer: vec![3].into(), // black patch after restoring the previous canvas
delay: 11,
..gif::Frame::default()
};
enc.write_frame(&fourth).unwrap();
}
let expected = legacy_gif_frames(&bytes);
let actual = super::Anim::load(&bytes).unwrap();
assert_eq!(actual.frames.len(), expected.len());
for (frame, old) in actual.frames.iter().zip(expected) {
assert_eq!(frame.to_rgba8(), old);
}
assert_eq!(actual.starts, vec![Tick::from_units(0, 100), Tick::from_units(5, 100), Tick::from_units(12, 100), Tick::from_units(21, 100)]);
}
#[test]
fn a_single_frame_gif_stays_a_still() {
let f = probe_bytes("a.gif", gif(&[(RED, 10)]).into()).unwrap();
@@ -228,6 +313,20 @@ mod tests {
let _ = probe_bytes("a.gif", bytes.into());
}
#[test]
fn a_damaged_gif_tail_keeps_frames_already_decoded() {
let mut bytes = gif(&[(RED, 5), (GREEN, 5), (BLUE, 5)]);
let info = craft_gif::scan(&bytes, MAX_FRAMES).unwrap();
assert_eq!(info.frames.len(), 3);
bytes.truncate(info.frames[1].offset);
let legacy = legacy_gif_frames_until_error(&bytes);
assert_eq!(legacy.len(), 1);
let actual = super::Anim::load(&bytes).unwrap();
assert_eq!(actual.frames.len(), legacy.len());
assert_eq!(actual.frames[0].to_rgba8(), legacy[0]);
}
#[test]
fn a_huge_canvas_is_refused_before_decoding() {
// A 65535×65535 logical screen (16 GiB of RGBA) holding one 1×1 frame.
@@ -239,6 +338,28 @@ mod tests {
}
}
let e = super::Anim::load(&out).err().unwrap();
assert!(e.to_string().contains("too large"), "{e}");
assert!(e.to_string().contains("GIF canvas of 65535×65535 is too large"), "{e}");
}
/// A 1×1 GIF of `n` frames.
fn tiny_gif(n: usize) -> Vec<u8> {
let mut out = Vec::new();
{
let mut enc = gif::Encoder::new(&mut out, 1, 1, &[]).unwrap();
let frame = gif::Frame::from_rgb(1, 1, &RED);
for _ in 0..n {
enc.write_frame(&frame).unwrap();
}
}
out
}
#[test]
fn decoding_keeps_every_frame_past_the_probe_frame_count() {
// 1×1 frames fit the byte budget however many there are: the frame count refuses none.
for n in [MAX_FRAMES, MAX_FRAMES + 1] {
let anim = super::Anim::load(&tiny_gif(n)).unwrap();
assert_eq!(anim.len(), n);
}
}
}
+125 -26
View File
@@ -9,13 +9,11 @@
//! every delay is a whole number of frames at (1000 / the greatest common divisor of the delays),
//! so it is kept exactly. A still WebP (no animation flag, or a single frame) stays a still.
use std::io::Cursor;
use effectcraft_project::{AlphaMode, Footage, FootageKind};
use effectcraft_time::{FrameRate, Tick};
use image::{AnimationDecoder, DynamicImage, codecs::webp::WebPDecoder};
use image::DynamicImage;
use crate::gif_anim::{Anim, MAX_BYTES, MAX_FRAMES, gcd};
use crate::gif_anim::{Anim, DECODE_FRAMES, MAX_BYTES, MAX_FRAMES, gcd};
use crate::{MediaError, Result};
/// The `Footage::codec` of an animated WebP.
@@ -121,29 +119,29 @@ pub(crate) fn load(bytes: &[u8]) -> Result<Anim> {
if frame_bytes.saturating_mul(h.delays.len()) > MAX_BYTES {
return Err(MediaError::Unsupported(format!("animated WebP of {} frames of {}×{} is too large to hold in memory", h.delays.len(), h.width, h.height)));
}
let dec = WebPDecoder::new(Cursor::new(bytes)).map_err(|e| MediaError::Decode(format!("WebP: {e}")))?;
// The budget above used the canvas this file's header scan found; the decoder must agree, or
// a second VP8X header could make it allocate a far larger canvas.
if image::ImageDecoder::dimensions(&dec) != (h.width, h.height) {
let mut limits = craft_webp::frames::FrameLimits::default();
limits.max_frames = DECODE_FRAMES;
limits.max_bytes = MAX_BYTES as u64;
limits.max_canvas_bytes = MAX_BYTES as u64;
let decoded = craft_webp::frames::read_frames(bytes, &limits).map_err(|e| match e {
craft_webp::frames::FrameError::Malformed(m) => MediaError::Decode(format!("WebP: {m}")),
craft_webp::frames::FrameError::LimitExceeded(m) => MediaError::Unsupported(format!("WebP: {m}")),
})?;
if matches!(&decoded.stop, craft_webp::frames::Stop::ByteLimit | craft_webp::frames::Stop::FrameLimit) {
return Err(MediaError::Unsupported("animated WebP is too large to hold in memory".into()));
}
if let craft_webp::frames::Stop::Damaged(ref e) = decoded.stop {
log::warn!("media: WebP stops after {} frames: {e}", decoded.frames.len());
}
if (decoded.width, decoded.height) != (h.width, h.height) {
return Err(MediaError::Decode("WebP: conflicting canvas sizes".into()));
}
let (mut frames, mut delays, mut size) = (Vec::new(), Vec::new(), 0usize);
for frame in dec.into_frames() {
let frame = match frame {
Ok(f) => f,
Err(e) if frames.is_empty() => return Err(MediaError::Decode(format!("WebP: {e}"))),
Err(e) => {
log::warn!("media: WebP stops after {} frames: {e}", frames.len());
break;
}
};
let (numer, denom) = frame.delay().numer_denom_ms();
delays.push(numer.checked_div(denom).unwrap_or(0));
let buf = frame.into_buffer();
size = size.saturating_add(buf.as_raw().len());
if size > MAX_BYTES {
return Err(MediaError::Unsupported("animated WebP is too large to hold in memory".into()));
}
let mut frames = Vec::with_capacity(decoded.frames.len());
let mut delays = Vec::with_capacity(decoded.frames.len());
for frame in decoded.frames {
delays.push(frame.delay_ms);
let buf = image::RgbaImage::from_raw(decoded.width, decoded.height, frame.rgba)
.ok_or_else(|| MediaError::Decode("WebP: invalid decoded frame dimensions".into()))?;
frames.push(DynamicImage::ImageRgba8(buf));
}
let starts = timing(&delays).starts;
@@ -152,9 +150,11 @@ pub(crate) fn load(bytes: &[u8]) -> Result<Anim> {
#[cfg(test)]
mod tests {
use std::io::Cursor;
use effectcraft_project::{AlphaMode, Footage, FootageKind};
use effectcraft_time::{FrameRate, Tick};
use image::{ExtendedColorType, ImageEncoder, codecs::webp::WebPEncoder};
use image::{AnimationDecoder, ExtendedColorType, ImageEncoder, codecs::webp::WebPDecoder, codecs::webp::WebPEncoder};
use super::timing;
use crate::{MediaPool, probe_bytes};
@@ -204,6 +204,43 @@ mod tests {
chunk(b"RIFF", &body)
}
/// Build an animation whose frames may cover only part of the canvas. `x` and `y` are even,
/// as required by ANMF; bit 1 of `flags` selects no-blend and bit 0 selects disposal.
fn webp_subframes(canvas: (u32, u32), frames: &[(u32, u32, u32, u32, [u8; 4], u32, u8)]) -> Vec<u8> {
let (w, h) = canvas;
let mut vp8x = vec![0x12, 0, 0, 0];
vp8x.extend_from_slice(&u24(w - 1));
vp8x.extend_from_slice(&u24(h - 1));
let mut body = b"WEBP".to_vec();
body.extend(chunk(b"VP8X", &vp8x));
body.extend(chunk(b"ANIM", &[0; 6]));
for &(x, y, fw, fh, rgba, delay, flags) in frames {
let mut anmf = Vec::new();
anmf.extend_from_slice(&u24(x / 2));
anmf.extend_from_slice(&u24(y / 2));
anmf.extend_from_slice(&u24(fw - 1));
anmf.extend_from_slice(&u24(fh - 1));
anmf.extend_from_slice(&u24(delay));
anmf.push(flags);
anmf.extend(vp8l(fw, fh, rgba));
body.extend(chunk(b"ANMF", &anmf));
}
chunk(b"RIFF", &body)
}
fn anmf_offsets(bytes: &[u8]) -> Vec<usize> {
let mut offsets = Vec::new();
let mut at = 12usize;
while at + 8 <= bytes.len() {
let size = u32::from_le_bytes(bytes[at + 4..at + 8].try_into().unwrap()) as usize;
if &bytes[at..at + 4] == b"ANMF" {
offsets.push(at);
}
at = at.saturating_add(8).saturating_add(size).saturating_add(size & 1);
}
offsets
}
const RED: [u8; 4] = [255, 0, 0, 255];
const GREEN: [u8; 4] = [0, 255, 0, 255];
const BLUE: [u8; 4] = [0, 0, 255, 128];
@@ -238,6 +275,38 @@ mod tests {
assert_eq!(pixel_at(&pool, &f, 5.0), last);
}
#[test]
fn shared_webp_frames_match_legacy_subrect_blending_and_disposal() {
// Includes a blended alpha patch, then a no-blend patch disposed to the background.
let bytes =
webp_subframes((4, 2), &[(0, 0, 4, 2, RED, 50, 0b10), (2, 0, 2, 2, BLUE, 60, 0b00), (0, 0, 2, 2, GREEN, 70, 0b11), (2, 0, 2, 2, RED, 80, 0b10)]);
let expected = WebPDecoder::new(Cursor::new(&bytes)).unwrap().into_frames().map(|frame| frame.unwrap().into_buffer()).collect::<Vec<_>>();
let actual = super::load(&bytes).unwrap();
assert_eq!(expected.len(), 4);
for (index, (start_ms, old)) in [(0, &expected[0]), (50, &expected[1]), (110, &expected[2]), (180, &expected[3])].into_iter().enumerate() {
let tick = Tick::from_units(start_ms, 1000);
let frame = actual.frame_at(tick).unwrap().to_rgba8();
assert_eq!(frame, *old, "frame {index} at {start_ms} ms");
}
}
#[test]
fn a_damaged_webp_tail_keeps_frames_already_decoded() {
let mut bytes = webp(4, 2, &[(RED, 50), (GREEN, 60), (BLUE, 70)]);
let offsets = anmf_offsets(&bytes);
assert_eq!(offsets.len(), 3);
bytes.truncate(offsets[2]);
let legacy =
WebPDecoder::new(Cursor::new(&bytes)).unwrap().into_frames().map_while(|frame| frame.ok()).map(|frame| frame.into_buffer()).collect::<Vec<_>>();
assert_eq!(legacy.len(), 2);
let actual = super::load(&bytes).unwrap();
for (index, old) in legacy.iter().enumerate() {
let tick = Tick::from_units(if index == 0 { 0 } else { 50 }, 1000);
assert_eq!(actual.frame_at(tick).unwrap().to_rgba8(), *old);
}
}
#[test]
fn delays_rounded_to_milliseconds_read_as_the_standard_rate() {
// 24 frames a second rounded to whole milliseconds, as ffmpeg writes them.
@@ -312,4 +381,34 @@ mod tests {
bytes[4..8].copy_from_slice(&riff.to_le_bytes());
assert!(super::load(&bytes).is_err());
}
/// A 1×1 animated WebP of `n` identical frames.
fn tiny_webp(n: usize) -> Vec<u8> {
let mut anmf = vec![0; 6];
anmf.extend_from_slice(&u24(0));
anmf.extend_from_slice(&u24(0));
anmf.extend_from_slice(&u24(50));
anmf.push(0b10);
anmf.extend(vp8l(1, 1, RED));
let frame = chunk(b"ANMF", &anmf);
let mut vp8x = vec![0x12, 0, 0, 0];
vp8x.extend_from_slice(&u24(0));
vp8x.extend_from_slice(&u24(0));
let mut body = b"WEBP".to_vec();
body.extend(chunk(b"VP8X", &vp8x));
body.extend(chunk(b"ANIM", &[0; 6]));
for _ in 0..n {
body.extend_from_slice(&frame);
}
chunk(b"RIFF", &body)
}
#[test]
fn decoding_keeps_every_frame_past_the_probe_frame_count() {
// 1×1 frames fit the byte budget however many there are: the frame count refuses none.
for n in [crate::gif_anim::MAX_FRAMES, crate::gif_anim::MAX_FRAMES + 1] {
let anim = super::load(&tiny_webp(n)).unwrap();
assert_eq!(anim.len(), n);
}
}
}
-15
View File
@@ -1,15 +0,0 @@
[package]
name = "effectcraft-opusenc"
description = "Clean-room pure-Rust Opus encoder (RFC 6716 SILK NB/MB/WB, hybrid SWB/FB and CELT FB modes, 48 kHz, 20 ms; RFC 7845 OpusHead) for WebM export audio"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[lints]
workspace = true
[dependencies]
[dev-dependencies]
craft-audio = { workspace = true }
-136
View File
@@ -1,136 +0,0 @@
# effectcraft-opusenc
Clean-room, pure-Rust Opus encoder for EffectCraft's WebM export audio (layer L0, `std` only,
no `unsafe`, builds for `wasm32-unknown-unknown`).
All three Opus modes, 20 ms packets (one frame per packet), 48 kHz float input, mono or stereo:
- **CELT** (fullband): MDCT transform coding, constant packet size.
- **SILK** (narrowband 8 kHz, mediumband 12 kHz, wideband 16 kHz internal rate): linear
prediction with long-term (pitch) prediction; packet size varies around the target bitrate.
- **Hybrid** (super-wideband, fullband): SILK wideband codes 0–8 kHz and CELT codes bands 17 and
up (8 kHz and above) in the same range-coded frame; constant packet size.
```rust
use effectcraft_opusenc::{Application, OpusEncoder};
let mut enc = OpusEncoder::with_application(2, 48_000, Application::Voip);
let packet = enc.encode_float(&vec![0.0; OpusEncoder::FRAME_SIZE * 2]);
// enc.mode(), enc.bandwidth(), enc.pre_skip(), enc.opus_head() ...
```
`OpusEncoder::new(channels, bitrate)` is `with_application(.., Application::Audio)`.
`OpusEncoder::with_mode(channels, bitrate, mode, bandwidth)` forces a mode/bandwidth pair
(SILK: NB/MB/WB; hybrid: SWB/FB; CELT: FB).
## Mode table
Chosen once per stream (constant-bitrate export, no in-stream switching) from the bitrate per
channel (bitrate / channels; the total is clamped to 6 kb/s per channel … 510 kb/s):
| per-channel bitrate | `Application::Voip` | `Application::Audio` (default) |
|---------------------|-------------------------|--------------------------------|
| < 10 kb/s | SILK narrowband (8 kHz) | SILK wideband |
| 10–12 kb/s | SILK mediumband (12 kHz)| SILK wideband |
| 12–16 kb/s | SILK wideband (16 kHz) | SILK wideband |
| 16–18 kb/s | SILK wideband | hybrid fullband |
| 18–28 kb/s | hybrid super-wideband | hybrid fullband |
| 28–36 kb/s | hybrid fullband | hybrid fullband |
| 36–40 kb/s | hybrid fullband | CELT fullband |
| ≥ 40 kb/s | CELT fullband | CELT fullband |
TOC configurations (RFC 6716 §3.1, Table 2): SILK NB/MB/WB 20 ms = 1/5/9, hybrid SWB/FB 20 ms
= 13/15, CELT FB 20 ms = 31; the stereo bit is set for two channels; frame-count code 0.
`pre_skip()` is 120 samples in every mode: the CELT MDCT overlap, and for SILK/hybrid the
decimator delay (83 samples at 48 kHz, 88 for narrowband) plus the decoder's SILK delay (one
internal-rate sample and the resampler delay of RFC 6716 Table 54). The SILK and CELT halves of a
hybrid frame are therefore aligned without extra buffering.
## What is implemented
**SILK** (RFC 6716 §4.2, encoder direction; own analysis and search design):
- 48 kHz → 8/12/16 kHz decimation with a linear-phase Kaiser-windowed-sinc FIR.
- Per frame: energy VAD (inactive / unvoiced / voiced), open-loop pitch search (normalised
correlation with octave checks, joint lag + contour codebook choice, §4.2.7.6.1), windowed
autocorrelation LPC (Levinson, lag window, bandwidth expansion), LPC → NLSF by root search,
NLSF quantisation (stage-1 preselection by weighted distance, backwards-predictive stage-2
scalar quantiser reproducing the decoder's dequantisation exactly, rate-distortion choice),
LTP codebook search over the three periodicity codebooks, independent + delta gain coding.
- Analysis-by-synthesis excitation: every sample is chosen by running the decoder's exact
fixed-point reconstruction (LTP from the decoder's own past output, LPC synthesis with the Q12
coefficients of the *dequantised* NLSFs, gain rescaling between subframes, quantisation
offsets, the LCG sign sequence), so encoder and decoder never drift. A noise-feedback loop
shapes the coding noise by `1/A(z/γ)` (γ 0.75–0.9, coarser quantisation → more shaping), and a
small rate penalty biases pulses towards zero.
- Excitation coding: rate level, per-block pulse counts with LSB escapes, shell coding, LSBs and
signs (§4.2.7.8).
- Rate control: per-frame search over a gain offset (integer bracket, then bisection) to the
frame budget; SILK-only streams carry a bit reservoir across frames (packet sizes vary).
- Stereo: mid/side with the decoder's stereo prediction (§4.2.7.1, §4.2.8; ridge-regularised
least-squares weights, quantised to the decoder grid, interpolation mirrored), mid-only frames
when the predicted side is negligible, side gains floored at the mid gains.
- Not implemented: LBRR (in-band FEC), NLSF interpolation, 10/40/60 ms frames, DTX.
**Hybrid** (§4.3 with start band 17): the SILK frame is written first, then the redundancy flag
(always 0) when there is room, then a CELT frame coding bands 17–18 (SWB) or 17–20 (FB) in the
same range coder with the remaining packet bytes (no silence flag or post-filter in hybrid
frames, decoder band-energy reset for uncoded bands mirrored). SILK gets 72 % (SWB) / 62 % (FB)
of the per-channel bitrate, at most the bitrate minus 6 kb/s, and never more than leaves CELT a
fifth of the packet (at least 80 bits).
**CELT** (unchanged): forward MDCT with the low-overlap window, band energies with inter/intra
coarse prediction, fine and final energy bits, the decoder's exact bit allocation, spreading,
recursive band splitting, greedy PVQ search. Not implemented: transient frames, TF changes,
pitch post-filter, intensity stereo, dynamic allocation, VBR. All-zero CELT frames become 3-byte
silence packets.
## Measured (tests in `tests/`, craft-audio's Opus decoder)
Synthetic speech (glottal pulses 100–200 Hz through three moving formants, syllable envelopes,
pauses) and music (three-note harmonic chords); waveform SNR against the input band-limited to
the mode's bandwidth (full band for hybrid/CELT), residual delay 0 (±1 for narrowband) after the
pre-skip:
| mode / bandwidth | bitrate target → actual | speech SNR | music SNR |
|--------------------|-------------------------|------------|-----------|
| SILK NB | 8 → 7.5 kb/s | 15.9 dB | 6.9 dB (9 kb/s) |
| SILK MB | 11 → 10.1 kb/s | 21.8 dB | 7.9 dB |
| SILK WB | 12 → 10.9 kb/s | 21.5 dB | 8.1 dB |
| SILK WB | 16 → 14.3 kb/s | 28.1 dB | 12.1 dB |
| SILK WB | 24 → 21.1 kb/s | 37.0 dB | 19.7 dB |
| hybrid SWB | 24 kb/s (CBR) | 28.7 dB | 13.0 dB |
| hybrid FB | 32 kb/s (CBR) | 32.1 dB | 15.6 dB |
| CELT FB | 12 kb/s (CBR) | 13.0 dB | — |
At 12 kb/s mono, SILK wideband beats the CELT path on speech by ~8 dB (21.0 vs 13.0 dB
full-band SNR). SILK bitrates land within −13 % … 0 % of the target (speech spends little in
pauses), hybrid is exact. Stereo SILK WB at 32 kb/s: 41 dB dual-mono, 40/37 dB panned, 22/19 dB
for two independent speech signals.
ffmpeg (external oracle, Ogg Opus written by the test) decodes every mode with both its native
`opus` decoder and `libopus` without errors, honouring pre-skip and end trimming, with the same
SNRs (libopus residual delay 0, ffmpeg's native decoder 0–4 samples). The only message tolerated
is ffmpeg's raw-PCM muxer timestamp complaint that its native decoder also prints for
narrowband files encoded by libopus itself.
## Specifications
- RFC 6716, *Definition of the Opus Audio Codec* (September 2012), with the RFC 8251 updates
(October 2017) as implemented by the decoder we round-trip against.
- RFC 7845, *Ogg Encapsulation for the Opus Audio Codec* (April 2016), §5.1 `OpusHead` and
§4.2 pre-skip.
## Provenance
Implemented from the RFC text. Constant tables (CELT band edges, `eMeans`, the coarse-energy
Laplace model, the allocation table, ICDFs; all SILK probability tables and codebooks: NLSF
stage-1/2 codebooks and prediction weights, gain, pitch, contour, LTP and pulse ICDFs, stereo
weights) and the decoder-side normative arithmetic the encoder must reproduce bit-exactly (CELT
allocation; SILK NLSF dequantisation and stabilisation, NLSF → LPC conversion, `log2lin`, the
fixed-point division/inverse helpers and the LPC analysis filter) are mirrored from FilmCraft's
first-party, spec-derived `filmcraft-opus` decoder (MIT OR Apache-2.0). That decoder, now
`craft_audio::opus` in craft-libs, is also the round-trip oracle in `tests/` and `crates/export/tests/it/opus_roundtrip.rs` (ffmpeg is a second,
external oracle when installed). All encoder-side analysis and search (pitch, LPC, NLSF and
LTP search, noise feedback, rate control, stereo prediction, the decimator) is our own design.
No third-party encoder or decoder source (libopus or otherwise) was consulted.
-720
View File
@@ -1,720 +0,0 @@
//! Band shape coding: PVQ search and codeword indexing, spreading, recursive band splitting
//! (RFC 6716 §4.3.4, encoder direction).
//!
//! The encoder codes 20 ms long-block frames only (no transients, no TF changes), and stereo
//! as dual (independent L/R) stereo, so a band is always coded by `quant_partition` with a single
//! block. Every bit-budget computation mirrors the decoder so both sides take the same
//! split/pulse decisions.
use crate::range::{BITRES, RangeEncoder, ilog};
use crate::rate::{Mode, QTHETA_OFFSET, get_pulses};
use crate::tables::EBANDS;
use crate::{CeltFrame, rate::Allocation};
pub const SPREAD_NORMAL: usize = 2;
#[inline]
fn frac_mul16(a: i32, b: i32) -> i32 {
(16384 + (a as i16 as i32) * (b as i16 as i32)) >> 15
}
fn bitexact_cos(x: i32) -> i32 {
let tmp = (4096 + x * x) >> 13;
let x2 = tmp;
let x2 = (32767 - x2) + frac_mul16(x2, -7651 + frac_mul16(x2, 8277 + frac_mul16(-626, x2)));
1 + x2
}
fn bitexact_log2tan(isin: i32, icos: i32) -> i32 {
let lc = ilog(icos as u32);
let ls = ilog(isin as u32);
let icos = icos << (15 - lc);
let isin = isin << (15 - ls);
(ls - lc) * (1 << 11) + frac_mul16(isin, frac_mul16(isin, -2597) + 7932) - frac_mul16(icos, frac_mul16(icos, -2597) + 7932)
}
fn compute_qn(n: i32, b: i32, offset: i32, pulse_cap: i32, stereo: bool) -> i32 {
const EXP2_TABLE8: [i32; 8] = [16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048];
let mut n2 = 2 * n - 1;
if stereo && n == 2 {
n2 -= 1;
}
let mut qb = (b + n2 * offset) / n2;
qb = qb.min(b - pulse_cap - (4 << BITRES));
qb = qb.min(8 << BITRES);
if qb < (1 << BITRES >> 1) {
1
} else {
let qn = EXP2_TABLE8[(qb & 0x7) as usize] >> (14 - (qb >> BITRES));
(qn + 1) >> 1 << 1
}
}
/// One elementary pass of the spreading rotation, inverted (the decoder applies the forward
/// pass; the encoder undoes it before the pulse search).
fn exp_rotation1_inv(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) {
if len <= stride {
return;
}
if len > 2 * stride {
for i in 0..=len - 2 * stride - 1 {
let x1 = x[i];
let x2 = x[i + stride];
x[i + stride] = c * x2 - s * x1;
x[i] = c * x1 + s * x2;
}
}
for i in (0..len - stride).rev() {
let x1 = x[i];
let x2 = x[i + stride];
x[i + stride] = c * x2 - s * x1;
x[i] = c * x1 + s * x2;
}
}
/// Inverse of the decoder's spreading rotation (§4.3.4.3) for one block of `len` samples.
pub fn exp_rotation_inv(x: &mut [f32], len: usize, k: usize, spread: usize) {
const SPREAD_FACTOR: [usize; 3] = [15, 10, 5];
if 2 * k >= len || spread == 0 {
return;
}
let factor = SPREAD_FACTOR[spread - 1];
let gain = len as f32 / (len + factor * k) as f32;
let theta = 0.5 * gain * gain;
let c = (0.5 * std::f32::consts::PI * theta).cos();
let s = (0.5 * std::f32::consts::PI * (1.0 - theta)).cos();
let mut stride2 = 0;
if len >= 8 {
stride2 = 1;
while stride2 * stride2 + stride2 < len {
stride2 += 1;
}
}
// The decoder applies (stride2: s, c) then (1: c, s); undo in reverse order.
exp_rotation1_inv(x, len, 1, c, s);
if stride2 != 0 {
exp_rotation1_inv(x, len, stride2, s, c);
}
}
/// Relative slack for the pulse-candidate filter in [`pvq_search`]: far above the f32 rounding of
/// the filter score (a few 1e-7), far below the spread of real scores.
const PVQ_SCORE_SLACK: f32 = 1e-5;
/// Greedy PVQ search: the integer vector `y` with `sum |y| = k` closest in direction to `x`.
pub fn pvq_search(x: &[f32], k: usize, y: &mut [i32]) {
let n = x.len();
y[..n].fill(0);
// `abs`, `dy` (2 y + 1, infinite in the padding) and the per-pass scores `score`, each padded to
// whole 8-lane chunks.
let np = n.next_multiple_of(8);
let mut buf = vec![0f32; 3 * np];
let (abs, rest) = buf.split_at_mut(np);
let (dy, score) = rest.split_at_mut(np);
for (a, v) in abs.iter_mut().zip(x) {
*a = v.abs();
}
let sum: f32 = abs[..n].iter().sum();
if !sum.is_finite() || sum <= 1e-15 {
y[0] = k as i32;
return;
}
let mut placed = 0usize;
let mut xy = 0f32;
let mut yy = 0f32;
if k > n / 2 {
// Pre-project onto the pyramid (rounding down) and place the rest greedily.
let r = (k as f32 - 1.0) / sum;
for j in 0..n {
// Non-negative and at most k, so the truncating cast is the floor.
let p = (abs[j] * r) as i32;
y[j] = p;
placed += p as usize;
xy += abs[j] * p as f32;
yy += (p * p) as f32;
}
}
for j in 0..np {
dy[j] = if j < n { 2.0 * y[j] as f32 + 1.0 } else { f32::INFINITY };
}
while placed < k {
// Vectorisable scores (xy + |x_j|)^2 / (yy + 2 y_j + 1) and their maximum.
for ((sc, &a), &d) in score.iter_mut().zip(abs.iter()).zip(dy.iter()) {
let t = xy + a;
*sc = t * t / (yy + d);
}
let mut lane_max = [0f32; 8];
for c in score.as_chunks::<8>().0 {
for i in 0..8 {
lane_max[i] = if c[i] > lane_max[i] { c[i] } else { lane_max[i] };
}
}
let top = lane_max.iter().fold(0f32, |m, &v| if v > m { v } else { m });
// The exact cross-multiplied test below, in index order with the first maximum winning, only
// matters for entries within rounding distance of the top score: every other entry is beaten
// by the top one whenever it is reached and never beats it afterwards. Running the test on
// those alone therefore picks the same pulse as running it on all of them.
let filter = top > 1e-30 && top.is_finite();
let lo = top * (1.0 - PVQ_SCORE_SLACK);
// Usually exactly one entry qualifies; its index falls out of a lane-wise count.
let mut count = [0u32; 8];
let mut index = [0u32; 8];
for (ci, c) in score.as_chunks::<8>().0.iter().enumerate() {
for i in 0..8 {
let hit = u32::from(c[i] >= lo);
count[i] += hit;
index[i] += hit * (ci * 8 + i) as u32;
}
}
let mut best = 0usize;
if filter && count.iter().sum::<u32>() == 1 {
best = index.iter().sum::<u32>() as usize;
} else {
let mut best_num = -1f32;
let mut best_den = 1f32;
for j in 0..n {
if !filter || score[j] >= lo {
let num = xy + abs[j];
let num = num * num;
let den = yy + 2.0 * y[j] as f32 + 1.0;
if num * best_den > best_num * den {
best_num = num;
best_den = den;
best = j;
}
}
}
}
xy += abs[best];
yy += 2.0 * y[best] as f32 + 1.0;
y[best] += 1;
dy[best] = 2.0 * y[best] as f32 + 1.0;
placed += 1;
}
for j in 0..n {
if x[j] < 0.0 {
y[j] = -y[j];
}
}
}
/// Largest band width and pulse count the encoder codes (176 coefficients, `get_pulses(40)` = 128).
const PVQ_MAX_N: usize = 176;
const PVQ_MAX_K: usize = 128;
/// Row-major `V(n, k)` for `n <= max_n`, `k <= max_k` with row stride `max_k + 1`, saturated at
/// `u32::MAX` (a codebook that large is never coded, see `rate::fits_in32`).
fn build_pvq_table(max_n: usize, max_k: usize) -> Vec<u32> {
let w = max_k + 1;
let mut v = vec![0u32; (max_n + 1) * w];
v[0] = 1;
for nn in 1..=max_n {
v[nn * w] = 1;
for kk in 1..=max_k {
let sum = v[(nn - 1) * w + kk] as u64 + v[nn * w + kk - 1] as u64 + v[(nn - 1) * w + kk - 1] as u64;
v[nn * w + kk] = sum.min(u32::MAX as u64) as u32;
}
}
v
}
fn pvq_table() -> &'static [u32] {
static TABLE: std::sync::OnceLock<Vec<u32>> = std::sync::OnceLock::new();
TABLE.get_or_init(|| build_pvq_table(PVQ_MAX_N, PVQ_MAX_K))
}
/// Encodes the PVQ codeword `y` (`sum |y| = k`) as the index the RFC 6716 §4.3.4.2 decoder maps
/// back to `y`.
pub fn encode_pulses(y: &[i32], k: usize, enc: &mut RangeEncoder) {
let n = y.len();
// Shapes outside the coded range (never produced by the band layout) get a local table.
let local;
let (v, w) = if n <= PVQ_MAX_N && k <= PVQ_MAX_K {
(pvq_table(), PVQ_MAX_K + 1)
} else {
local = build_pvq_table(n, k);
(&local[..], k + 1)
};
let row = |nn: usize| &v[nn * w..(nn + 1) * w];
let total = row(n)[k] as u64;
let mut index = 0u64;
let mut kk = k;
for j in 0..n {
if kk == 0 {
break;
}
let rem = n - j;
let t = y[j].unsigned_abs() as usize;
let below = row(rem - 1);
// Codewords with |y_j| = t start at sum_{m < kk - t} V(rem-1, m) within their sign half.
let mut base: u64 = below[..kk - t].iter().map(|&x| x as u64).sum();
if y[j] < 0 {
base += (below[kk] as u64 + row(rem)[kk] as u64) / 2;
}
index += base;
kk -= t;
}
debug_assert!(total <= u32::MAX as u64);
if total >= 2 {
enc.uint(index as u32, total as u32);
}
}
pub struct BandEnc<'m, 'e> {
pub m: &'m Mode,
pub enc: &'e mut RangeEncoder,
pub i: usize,
pub spread: usize,
pub remaining_bits: i32,
iy: Vec<i32>,
tmp: Vec<f32>,
}
impl BandEnc<'_, '_> {
/// `compute_theta` for a mono split (single block): measures the energy split between the
/// two halves and codes the quantised angle with the triangular pdf.
fn compute_theta_split(&mut self, xa: &[f32], xb: &[f32], b: &mut i32, lm: i32) -> (i32, i32, i32) {
let n = xa.len() as i32;
let i = self.i;
let pulse_cap = self.m.log_n[i] + lm * (1 << BITRES);
let offset = (pulse_cap >> 1) - QTHETA_OFFSET;
let qn = compute_qn(n, *b, offset, pulse_cap, false);
let tell = self.enc.tell_frac();
let mut itheta = 0i32;
if qn != 1 {
let mid = xa.iter().map(|v| v * v).sum::<f32>().sqrt();
let side = xb.iter().map(|v| v * v).sum::<f32>().sqrt();
let ang = (side as f64).atan2(mid as f64) * std::f64::consts::FRAC_2_PI;
let it = ((ang * qn as f64 + 0.5).floor() as i32).clamp(0, qn) as u32;
let q = qn as u32;
let half = q >> 1;
let ft = (half + 1) * (half + 1);
let (fl, fs) = if it <= half { ((it * (it + 1)) >> 1, it + 1) } else { (ft - (((q + 1 - it) * (q + 2 - it)) >> 1), q + 1 - it) };
self.enc.encode(fl, fl + fs, ft);
itheta = ((it * 16384) / q) as i32;
}
let qalloc = self.enc.tell_frac() - tell;
*b -= qalloc;
let delta = if itheta == 0 {
-16384
} else if itheta == 16384 {
16384
} else {
let imid = bitexact_cos(itheta);
let iside = bitexact_cos(16384 - itheta);
frac_mul16((n - 1) << 7, bitexact_log2tan(iside, imid))
};
(itheta, delta, qalloc)
}
/// `quant_partition` (single block, encoder side).
pub fn quant_partition(&mut self, x: &[f32], mut b: i32, mut lm: i32) {
let n = x.len();
let i = self.i;
let cache = self.m.cache(lm, i);
let cache_max = cache[cache[0] as usize] as i32;
if lm != -1 && b > cache_max + 12 && n > 2 {
let half = n >> 1;
let (xa, xb) = x.split_at(half);
lm -= 1;
let (itheta, delta, qalloc) = self.compute_theta_split(xa, xb, &mut b, lm);
let mut mbits = 0.max(b.min((b - delta) / 2));
let mut sbits = b - mbits;
self.remaining_bits -= qalloc;
let mut rebalance = self.remaining_bits;
if mbits >= sbits {
self.quant_partition(xa, mbits, lm);
rebalance = mbits - (rebalance - self.remaining_bits);
if rebalance > 3 << BITRES && itheta != 0 {
sbits += rebalance - (3 << BITRES);
}
self.quant_partition(xb, sbits, lm);
} else {
self.quant_partition(xb, sbits, lm);
rebalance = sbits - (rebalance - self.remaining_bits);
if rebalance > 3 << BITRES && itheta != 16384 {
mbits += rebalance - (3 << BITRES);
}
self.quant_partition(xa, mbits, lm);
}
return;
}
let m = self.m;
let mut q = m.bits2pulses(i, lm, b);
let mut curr_bits = m.pulses2bits(i, lm, q);
self.remaining_bits -= curr_bits;
while self.remaining_bits < 0 && q > 0 {
self.remaining_bits += curr_bits;
q -= 1;
curr_bits = m.pulses2bits(i, lm, q);
self.remaining_bits -= curr_bits;
}
if q != 0 {
let k = get_pulses(q) as usize;
let mut tmp = std::mem::take(&mut self.tmp);
tmp.clear();
tmp.extend_from_slice(x);
exp_rotation_inv(&mut tmp, n, k, self.spread);
let mut iy = std::mem::take(&mut self.iy);
iy.clear();
iy.resize(n, 0);
pvq_search(&tmp, k, &mut iy);
encode_pulses(&iy, k, self.enc);
self.iy = iy;
self.tmp = tmp;
}
}
}
/// Shape-coding controls, independent of the selected band allocation.
pub struct BandCoding {
pub spread: usize,
pub total_bits: i32,
}
/// Codes all band shapes (`quant_all_bands`, encoder side, long blocks). `x` holds
/// `frame.channels` channels of `n_total` normalised MDCT coefficients each.
pub fn quant_all_bands(m: &Mode, frame: CeltFrame, x: &[f32], n_total: usize, allocation: &Allocation, coding: BandCoding, enc: &mut RangeEncoder) {
let CeltFrame { start, end, channels: c, lm } = frame;
let Allocation { pulses, dual_stereo, intensity, coded_bands, balance, .. } = allocation;
let (dual_stereo, intensity, coded_bands, mut balance) = (*dual_stereo, *intensity, *coded_bands, *balance);
let BandCoding { spread, total_bits } = coding;
let mm = 1usize << lm;
debug_assert!(c == 1 || dual_stereo || intensity <= start, "stereo frames are coded as dual stereo");
let mut ctx = BandEnc { m, enc, i: 0, spread, remaining_bits: 0, iy: Vec::new(), tmp: Vec::new() };
for i in start..end {
ctx.i = i;
let xo = mm * EBANDS[i] as usize;
let n = mm * EBANDS[i + 1] as usize - xo;
let tell = ctx.enc.tell_frac();
if i != start {
balance -= tell;
}
let remaining_bits = total_bits - tell - 1;
ctx.remaining_bits = remaining_bits;
let b = if i < coded_bands {
let curr_balance = balance / 3.min(coded_bands as i32 - i as i32);
0.max(16383.min((remaining_bits + 1).min(pulses[i] + curr_balance)))
} else {
0
};
// Bands at and above `intensity` (= coded_bands) carry no bits, so they emit no symbols.
if i < coded_bands || c == 1 {
if c == 2 && dual_stereo && i < intensity {
ctx.quant_partition(&x[xo..xo + n], b / 2, lm as i32);
ctx.quant_partition(&x[n_total + xo..n_total + xo + n], b / 2, lm as i32);
} else {
ctx.quant_partition(&x[xo..xo + n], b, lm as i32);
}
}
balance += pulses[i] + tell;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::range::dec::RangeDecoder;
fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) {
let ms = -s;
if len <= stride {
return;
}
for i in 0..len - stride {
let x1 = x[i];
let x2 = x[i + stride];
x[i + stride] = c * x2 + s * x1;
x[i] = c * x1 + ms * x2;
}
if len > 2 * stride {
for i in (0..=len - 2 * stride - 1).rev() {
let x1 = x[i];
let x2 = x[i + stride];
x[i + stride] = c * x2 + s * x1;
x[i] = c * x1 + ms * x2;
}
}
}
/// The decoder's spreading rotation (single block).
fn exp_rotation(x: &mut [f32], len: usize, k: usize, spread: usize) {
const SPREAD_FACTOR: [usize; 3] = [15, 10, 5];
if 2 * k >= len || spread == 0 {
return;
}
let factor = SPREAD_FACTOR[spread - 1];
let gain = len as f32 / (len + factor * k) as f32;
let theta = 0.5 * gain * gain;
let c = (0.5 * std::f32::consts::PI * theta).cos();
let s = (0.5 * std::f32::consts::PI * (1.0 - theta)).cos();
let mut stride2 = 0;
if len >= 8 {
stride2 = 1;
while stride2 * stride2 + stride2 < len {
stride2 += 1;
}
}
if stride2 != 0 {
exp_rotation1(x, len, stride2, s, c);
}
exp_rotation1(x, len, 1, c, s);
}
#[test]
fn spreading_inverse_is_exact() {
for &len in &[8usize, 16, 24, 48, 96, 176] {
for k in [1usize, 2, 3, 5] {
let orig: Vec<f32> = (0..len).map(|i| ((i * 31 % 17) as f32 - 8.0) / 8.0).collect();
let mut x = orig.clone();
exp_rotation_inv(&mut x, len, k, SPREAD_NORMAL);
exp_rotation(&mut x, len, k, SPREAD_NORMAL);
for (a, b) in x.iter().zip(&orig) {
assert!((a - b).abs() < 1e-4, "len={len} k={k}");
}
}
}
}
/// PVQ decoder of RFC 6716 §4.3.4.2 (as in FilmCraft's decoder).
fn decode_pulses(n: usize, k: usize, dec: &mut RangeDecoder) -> Vec<i32> {
let w = k + 1;
let mut v = vec![0u64; (n + 1) * w];
v[0] = 1;
for nn in 1..=n {
v[nn * w] = 1;
for kk in 1..=k {
v[nn * w + kk] = v[(nn - 1) * w + kk].saturating_add(v[nn * w + kk - 1]).saturating_add(v[(nn - 1) * w + kk - 1]);
}
}
let total = v[n * w + k];
let mut i = if total >= 2 { dec.uint(total as u32) as u64 } else { 0 };
let mut y = vec![0i32; n];
let mut kk = k;
for j in 0..n {
let rem = n - j;
if kk == 0 {
break;
}
let vv = |nn: usize, kx: usize| v[nn * w + kx];
let mut p = (vv(rem - 1, kk) + vv(rem, kk)) / 2;
let neg = i >= p;
if neg {
i -= p;
}
let k0 = kk;
p -= vv(rem - 1, kk);
while p > i && kk > 0 {
kk -= 1;
p -= vv(rem - 1, kk);
}
let mag = (k0 - kk) as i32;
y[j] = if neg { -mag } else { mag };
i -= p.min(i);
}
y
}
fn all_codewords(n: usize, k: usize) -> Vec<Vec<i32>> {
if n == 0 {
return if k == 0 { vec![vec![]] } else { vec![] };
}
let mut out = Vec::new();
for t in -(k as i32)..=(k as i32) {
for mut rest in all_codewords(n - 1, k - t.unsigned_abs() as usize) {
rest.insert(0, t);
out.push(rest);
}
}
out
}
#[test]
fn pvq_index_roundtrip_exhaustive() {
for n in 1..6usize {
for k in 1..6usize {
for y in all_codewords(n, k) {
let mut e = RangeEncoder::new();
encode_pulses(&y, k, &mut e);
let buf = e.done(16);
let mut d = RangeDecoder::new(&buf);
assert_eq!(decode_pulses(n, k, &mut d), y, "n={n} k={k}");
}
}
}
}
/// The per-call table encoder this module used before the shared table.
fn encode_pulses_reference(y: &[i32], k: usize, enc: &mut RangeEncoder) {
let n = y.len();
let w = k + 1;
let mut v = vec![0u64; (n + 1) * w];
v[0] = 1;
for nn in 1..=n {
v[nn * w] = 1;
for kk in 1..=k {
let a = v[(nn - 1) * w + kk];
let b = v[nn * w + kk - 1];
let c = v[(nn - 1) * w + kk - 1];
v[nn * w + kk] = a.saturating_add(b).saturating_add(c);
}
}
let total = v[n * w + k];
let vv = |nn: usize, kx: usize| v[nn * w + kx];
let mut index = 0u64;
let mut kk = k;
for j in 0..n {
if kk == 0 {
break;
}
let rem = n - j;
let t = y[j].unsigned_abs() as usize;
let mut base: u64 = (0..kk - t).map(|m| vv(rem - 1, m)).sum();
if y[j] < 0 {
base += (vv(rem - 1, kk) + vv(rem, kk)) / 2;
}
index += base;
kk -= t;
}
if total >= 2 {
enc.uint(index as u32, total as u32);
}
}
#[test]
fn pvq_table_encode_matches_per_call_table() {
let mut seed = 0x9E37_79B9_7F4A_7C15u64;
let mut next = move |m: u64| {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
seed % m
};
let mut checked = 0;
for n in (1..=PVQ_MAX_N).step_by(5).chain([PVQ_MAX_N]) {
for k in (1..=PVQ_MAX_K).step_by(3).chain([PVQ_MAX_K]) {
if crate::rate::pvq_v(n, k) > u32::MAX as u128 {
continue;
}
for _ in 0..4 {
let mut y = vec![0i32; n];
for _ in 0..k {
let at = next(n as u64) as usize;
y[at] += if next(2) == 0 { 1 } else { -1 };
}
// Opposite pulses cancel; top up so that sum |y| = k again.
let mut have: usize = y.iter().map(|v| v.unsigned_abs() as usize).sum();
while have < k {
let at = next(n as u64) as usize;
y[at] += if y[at] < 0 { -1 } else { 1 };
have += 1;
}
let (mut a, mut b) = (RangeEncoder::new(), RangeEncoder::new());
encode_pulses(&y, k, &mut a);
encode_pulses_reference(&y, k, &mut b);
assert_eq!(a.done(16), b.done(16), "n={n} k={k}");
checked += 1;
}
}
}
assert!(checked > 100, "{checked}");
}
/// The scalar greedy search the vectorised one replaced.
fn pvq_search_ref(x: &[f32], k: usize, y: &mut [i32]) {
let n = x.len();
y[..n].fill(0);
let abs: Vec<f32> = x.iter().map(|v| v.abs()).collect();
let sum: f32 = abs.iter().sum();
if !sum.is_finite() || sum <= 1e-15 {
y[0] = k as i32;
return;
}
let mut placed = 0usize;
let mut xy = 0f32;
let mut yy = 0f32;
if k > n / 2 {
let r = (k as f32 - 1.0) / sum;
for j in 0..n {
let p = (abs[j] * r).floor() as i32;
y[j] = p;
placed += p as usize;
xy += abs[j] * p as f32;
yy += (p * p) as f32;
}
}
while placed < k {
let mut best = 0usize;
let mut best_num = -1f32;
let mut best_den = 1f32;
for j in 0..n {
let num = xy + abs[j];
let num = num * num;
let den = yy + 2.0 * y[j] as f32 + 1.0;
if num * best_den > best_num * den {
best_num = num;
best_den = den;
best = j;
}
}
xy += abs[best];
yy += 2.0 * y[best] as f32 + 1.0;
y[best] += 1;
placed += 1;
}
for j in 0..n {
if x[j] < 0.0 {
y[j] = -y[j];
}
}
}
#[test]
fn pvq_search_matches_scalar_reference() {
let mut state = 0x9e3779b97f4a7c15u64;
let mut rnd = move || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
(state >> 40) as f32 / (1u64 << 24) as f32 * 2.0 - 1.0
};
let mut a = vec![0i32; 200];
let mut b = vec![0i32; 200];
let mut cases = 0;
for round in 0..6000 {
let n = 1 + (round * 37 + (rnd().abs() * 176.0) as usize) % 176;
let k = 1 + (rnd().abs() * 127.0) as usize;
let scale = [1.0f32, 1e-9, 1e6, 3e-3][round % 4];
let x: Vec<f32> = (0..n)
.map(|_| match round % 6 {
0 => rnd(),
1 => if rnd() > 0.8 { rnd() } else { 0.0 },
2 => (rnd() * 4.0).round() / 4.0,
3 => if rnd() > 0.0 { 1.0 } else { -1.0 },
4 => rnd().powi(5),
_ => if rnd() > 0.95 { 3.0 * rnd() } else { 1e-4 * rnd() },
} * scale)
.collect();
pvq_search(&x, k, &mut a);
pvq_search_ref(&x, k, &mut b);
assert_eq!(a[..n], b[..n], "n={n} k={k} round={round}");
cases += 1;
}
assert_eq!(cases, 6000);
}
#[test]
fn pvq_search_finds_good_direction() {
let x: Vec<f32> = (0..32).map(|i| ((i as f32) * 0.7).sin()).collect();
let mut y = vec![0i32; 32];
for k in [1usize, 4, 16, 64] {
pvq_search(&x, k, &mut y);
assert_eq!(y.iter().map(|v| v.unsigned_abs() as usize).sum::<usize>(), k);
let xy: f32 = x.iter().zip(&y).map(|(a, &b)| a * b as f32).sum();
let yy: f32 = y.iter().map(|&b| (b * b) as f32).sum();
let xx: f32 = x.iter().map(|a| a * a).sum();
let corr = xy / (xx * yy).sqrt();
assert!(corr > if k >= 16 { 0.9 } else { 0.2 }, "k={k} corr={corr}");
}
}
}
-212
View File
@@ -1,212 +0,0 @@
//! Band energy quantisation: Laplace-coded coarse energy with inter/intra prediction, fine
//! energy and the final refinement bits (RFC 6716 §4.3.2, encoder direction).
//!
//! The quantised energies are updated with exactly the decoder's arithmetic so the prediction
//! state never drifts between encoder and decoder.
use crate::CeltFrame;
use crate::range::RangeEncoder;
use crate::rate::{Allocation, MAX_FINE_BITS};
use crate::tables::{BETA_COEF, BETA_INTRA, E_PROB_MODEL, NB_EBANDS, PRED_COEF, SMALL_ENERGY_ICDF};
const LAPLACE_MINP: u32 = 1;
const LAPLACE_NMIN: u32 = 16;
fn laplace_get_freq1(fs0: u32, decay: u32) -> u32 {
let ft = 32768 - LAPLACE_MINP * (2 * LAPLACE_NMIN) - fs0;
(ft * (16384 - decay)) >> 15
}
/// Encodes a Laplace-distributed integer (`fs` = probability of 0 in Q15, `decay` in Q14).
/// Returns the value actually coded (very large magnitudes are clamped to the representable
/// range).
pub fn laplace_encode(enc: &mut RangeEncoder, value: i32, mut fs: u32, decay: u32) -> i32 {
let mut fl = 0u32;
let mut coded = value;
if value != 0 {
let s: i32 = if value < 0 { -1 } else { 0 };
let val = value.abs();
fl = fs;
fs = laplace_get_freq1(fs, decay);
let mut i = 1;
while fs > 0 && i < val {
fs *= 2;
fl += fs + 2 * LAPLACE_MINP;
fs = (fs * decay) >> 15;
i += 1;
}
if fs == 0 {
let ndi_max = (32768 - fl + LAPLACE_MINP - 1) as i32;
let ndi_max = (ndi_max - s) >> 1;
let di = (val - i).min(ndi_max - 1);
fl = (fl as i32 + (2 * di + 1 + s) * LAPLACE_MINP as i32) as u32;
fs = LAPLACE_MINP.min(32768 - fl);
let mag = i + di;
coded = if s < 0 { -mag } else { mag };
} else {
fs += LAPLACE_MINP;
if s == 0 {
fl += fs;
}
}
}
enc.encode_bin(fl, fl + fs, 15);
coded
}
/// `quant_coarse_energy` for one prediction mode. `e` holds the target log2 energies (relative
/// to `E_MEANS`), `old_e` the quantised energies, updated in place. `budget` is the frame size in
/// bits.
pub fn quant_coarse_energy(frame: CeltFrame, e: &[f32; 2 * NB_EBANDS], old_e: &mut [f32; 2 * NB_EBANDS], intra: bool, enc: &mut RangeEncoder, budget: i32) {
let CeltFrame { start, end, channels: c, lm } = frame;
let prob = &E_PROB_MODEL[lm][intra as usize];
let (coef, beta) = if intra { (0.0, BETA_INTRA) } else { (PRED_COEF[lm], BETA_COEF[lm]) };
let mut prev = [0f32; 2];
for i in start..end {
for ch in 0..c {
let idx = i + ch * NB_EBANDS;
let oldc = old_e[idx].max(-9.0);
let x = e[idx];
let f = x - coef * oldc - prev[ch];
let mut qi = (f + 0.5).floor().clamp(-64.0, 64.0) as i32;
let tell = enc.tell();
// Keep a few bits per remaining band in reserve when the budget runs out.
let bits_left = budget - tell - 3 * c as i32 * (end - i) as i32;
if i != start && bits_left < 30 {
if bits_left < 24 {
qi = qi.min(1);
}
if bits_left < 16 {
qi = qi.max(-1);
}
}
if budget - tell >= 15 {
let pi = 2 * i.min(20);
qi = laplace_encode(enc, qi, (prob[pi] as u32) << 7, (prob[pi + 1] as u32) << 6);
} else if budget - tell >= 2 {
qi = qi.clamp(-1, 1);
enc.icdf(if qi < 0 { (-2 * qi - 1) as usize } else { (2 * qi) as usize }, &SMALL_ENERGY_ICDF, 2);
} else if budget - tell >= 1 {
qi = qi.clamp(-1, 0);
enc.bit_logp(qi == -1, 1);
} else {
qi = -1;
}
let q = qi as f32;
old_e[idx] = oldc;
let tmp = coef * old_e[idx] + prev[ch] + q;
old_e[idx] = tmp;
prev[ch] = prev[ch] + q - beta * q;
}
}
}
/// `quant_fine_energy`: raw bits refining each band's quantised energy.
pub fn quant_fine_energy(frame: CeltFrame, e: &[f32; 2 * NB_EBANDS], old_e: &mut [f32; 2 * NB_EBANDS], fine_quant: &[i32; NB_EBANDS], enc: &mut RangeEncoder) {
let CeltFrame { start, end, channels: c, .. } = frame;
for i in start..end {
let fq = fine_quant[i];
if fq <= 0 {
continue;
}
for ch in 0..c {
let idx = i + ch * NB_EBANDS;
let err = e[idx] - old_e[idx];
let levels = 1i32 << fq;
let q2 = (((err + 0.5) * levels as f32).floor() as i32).clamp(0, levels - 1);
enc.bits(q2 as u32, fq as u32);
let offset = (q2 as f32 + 0.5) * (1 << (14 - fq)) as f32 / 16384.0 - 0.5;
old_e[idx] += offset;
}
}
}
/// `quant_energy_finalise`: spends the bits left at the end of the frame on one more bit of
/// energy resolution per band, in priority order.
pub fn quant_energy_finalise(
frame: CeltFrame,
e: &[f32; 2 * NB_EBANDS],
old_e: &mut [f32; 2 * NB_EBANDS],
allocation: &Allocation,
mut bits_left: i32,
enc: &mut RangeEncoder,
) {
let CeltFrame { start, end, channels: c, .. } = frame;
let Allocation { fine_quant, fine_priority, .. } = allocation;
for prio in 0..2 {
let mut i = start;
while i < end && bits_left >= c as i32 {
if fine_quant[i] >= MAX_FINE_BITS || fine_priority[i] != prio {
i += 1;
continue;
}
for ch in 0..c {
let idx = i + ch * NB_EBANDS;
let q2 = (e[idx] - old_e[idx] >= 0.0) as u32;
enc.bits(q2, 1);
let offset = (q2 as f32 - 0.5) * (1 << (14 - fine_quant[i] - 1)) as f32 / 16384.0;
old_e[idx] += offset;
bits_left -= 1;
}
i += 1;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::range::dec::RangeDecoder;
/// Laplace decoder of RFC 6716 §4.3.2.1 (as in FilmCraft's decoder).
fn laplace_decode(dec: &mut RangeDecoder, mut fs: u32, decay: u32) -> i32 {
let mut val = 0i32;
let fm = dec.decode(32768);
let mut fl = 0u32;
if fm >= fs {
val += 1;
fl = fs;
fs = laplace_get_freq1(fs, decay) + LAPLACE_MINP;
while fs > LAPLACE_MINP && fm >= fl + 2 * fs {
fs *= 2;
fl += fs;
fs = (((fs - 2 * LAPLACE_MINP) * decay) >> 15) + LAPLACE_MINP;
val += 1;
}
if fs <= LAPLACE_MINP {
let di = (fm - fl) >> 1;
val += di as i32;
fl += 2 * di * LAPLACE_MINP;
}
if fm < fl + fs {
val = -val;
} else {
fl += fs;
}
}
dec.update(fl, (fl + fs).min(32768), 32768);
val
}
#[test]
fn laplace_roundtrip_all_models() {
for lm in 0..4 {
for intra in 0..2 {
for band in 0..21 {
let prob = &E_PROB_MODEL[lm][intra];
let (fs, decay) = ((prob[2 * band] as u32) << 7, (prob[2 * band + 1] as u32) << 6);
let vals: Vec<i32> = (-64..=64).collect();
let mut e = RangeEncoder::new();
let coded: Vec<i32> = vals.iter().map(|&v| laplace_encode(&mut e, v, fs, decay)).collect();
let size = (e.tell() as usize).div_ceil(8);
let buf = e.done(size);
let mut d = RangeDecoder::new(&buf);
for (&v, &c) in vals.iter().zip(&coded) {
assert!(c == v || (c.signum() == v.signum() && c.abs() < v.abs()), "clamp {v} -> {c}");
assert_eq!(laplace_decode(&mut d, fs, decay), c, "lm={lm} intra={intra} band={band} v={v}");
}
}
}
}
}
}
-553
View File
@@ -1,553 +0,0 @@
//! Clean-room pure-Rust Opus encoder (RFC 6716 SILK, hybrid and CELT modes; RFC 7845 `OpusHead`).
//!
//! Produces 20 ms Opus packets from 48 kHz float input, mono or stereo. The coding mode and
//! audio bandwidth are chosen once per stream from the per-channel bitrate and the
//! [`Application`] (see the crate README for the table):
//!
//! - **SILK** (narrowband/mediumband/wideband): linear-predictive speech coding at an internal
//! rate of 8/12/16 kHz; packet sizes vary around the target bitrate.
//! - **Hybrid** (super-wideband/fullband): SILK wideband codes 0–8 kHz and CELT codes the bands
//! above in the same range-coded frame; constant packet size.
//! - **CELT** (fullband): MDCT transform coding; constant packet size.
//!
//! Layer L0: no dependencies beyond `std`; no `unsafe`; builds for `wasm32-unknown-unknown`.
//!
//! ```
//! use effectcraft_opusenc::{Application, Mode, OpusEncoder};
//! let mut enc = OpusEncoder::new(2, 128_000);
//! let pcm = vec![0.0f32; OpusEncoder::FRAME_SIZE * 2];
//! let packet = enc.encode_float(&pcm);
//! assert_eq!(packet[0] >> 3, 31); // CELT-only, fullband, 20 ms
//! assert_eq!(enc.opus_head().len(), 19);
//! let voip = OpusEncoder::with_application(1, 16_000, Application::Voip);
//! assert_eq!(voip.mode(), Mode::Silk);
//! ```
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
mod bands;
mod energy;
mod mdct;
mod range;
mod rate;
mod resample;
mod silk;
mod tables;
use std::sync::OnceLock;
use bands::{BandCoding, SPREAD_NORMAL, quant_all_bands};
use energy::{quant_coarse_energy, quant_energy_finalise, quant_fine_energy};
use mdct::Mdct;
use range::{BITRES, RangeEncoder};
use rate::{AllocationInput, Mode as CeltMode, compute_allocation};
use resample::Downsampler;
use silk::SilkEncoder;
use tables::{E_MEANS, EBANDS, NB_EBANDS, OVERLAP, PREEMPH, SPREAD_ICDF, TF_SELECT_TABLE, TRIM_ICDF};
/// Band range, channel count and transform size shared by CELT coding stages.
#[derive(Clone, Copy)]
struct CeltFrame {
start: usize,
end: usize,
channels: usize,
lm: usize,
}
/// Samples per channel in one 20 ms frame at 48 kHz.
const N: usize = 960;
/// LM of a 20 ms frame (8 short blocks).
const LM: usize = 3;
/// Smallest CELT payload the encoder produces (bytes).
const MIN_FRAME_BYTES: usize = 16;
/// Largest payload allowed in one frame (RFC 6716 §3.4, R2).
const MAX_FRAME_BYTES: usize = 1275;
/// First CELT band coded in hybrid frames (8 kHz).
const HYBRID_START_BAND: usize = 17;
fn celt_mode() -> &'static CeltMode {
static MODE: OnceLock<CeltMode> = OnceLock::new();
MODE.get_or_init(CeltMode::new)
}
/// Intended use of the stream; selects the mode/bandwidth table.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Application {
/// General audio (music, mixed content): CELT unless the bitrate is low.
#[default]
Audio,
/// Speech: SILK and hybrid over a wider bitrate range, narrower bandwidths at low rates.
Voip,
}
/// Opus coding mode (RFC 6716 §2).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Mode {
/// MDCT transform coding only.
Celt,
/// Linear-prediction coding only.
Silk,
/// SILK below 8 kHz plus CELT above.
Hybrid,
}
/// Coded audio bandwidth (RFC 6716 §2, Table 1).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Bandwidth {
/// 4 kHz (8 kHz sampling).
Narrowband,
/// 6 kHz (12 kHz sampling).
Mediumband,
/// 8 kHz (16 kHz sampling).
Wideband,
/// 12 kHz (24 kHz sampling).
SuperWideband,
/// 20 kHz (48 kHz sampling).
Fullband,
}
/// Mode and bandwidth for a per-channel bitrate (bits per second per channel).
fn choose_mode(app: Application, per_channel: u32) -> (Mode, Bandwidth) {
match app {
Application::Voip => match per_channel {
0..10_000 => (Mode::Silk, Bandwidth::Narrowband),
10_000..12_000 => (Mode::Silk, Bandwidth::Mediumband),
12_000..18_000 => (Mode::Silk, Bandwidth::Wideband),
18_000..28_000 => (Mode::Hybrid, Bandwidth::SuperWideband),
28_000..40_000 => (Mode::Hybrid, Bandwidth::Fullband),
_ => (Mode::Celt, Bandwidth::Fullband),
},
Application::Audio => match per_channel {
0..16_000 => (Mode::Silk, Bandwidth::Wideband),
16_000..36_000 => (Mode::Hybrid, Bandwidth::Fullband),
_ => (Mode::Celt, Bandwidth::Fullband),
},
}
}
/// SILK side of a SILK-only or hybrid stream.
struct SilkPath {
enc: SilkEncoder,
down: Vec<Downsampler>,
/// Bits per frame aimed at (excluding the TOC byte in SILK-only mode).
target_bits: f64,
/// Bits saved (positive) or overspent by previous frames (SILK-only mode).
reservoir: f64,
}
/// Opus encoder for one mono or stereo stream at 48 kHz.
pub struct OpusEncoder {
channels: usize,
application: Application,
mode: Mode,
bandwidth: Bandwidth,
/// Requested bitrate (clamped), bits per second.
bitrate_bps: u32,
/// Payload bytes per packet in CELT and hybrid modes (the packet is one byte longer: the
/// TOC).
frame_bytes: usize,
mdct: Mdct,
/// Last input sample of each channel (pre-emphasis filter memory, ×32768).
preemph_mem: [f32; 2],
/// Last `OVERLAP` pre-emphasised samples of each channel.
hist: Vec<f32>,
/// Quantised band energies, exactly as the decoder holds them.
old_band_e: [f32; 2 * NB_EBANDS],
block: Vec<f32>,
freq: Vec<f32>,
silk: Option<SilkPath>,
}
impl OpusEncoder {
/// Samples per channel per packet (20 ms at 48 kHz).
pub const FRAME_SIZE: usize = N;
/// Creates a general-audio encoder ([`Application::Audio`]) for `channels` (1 or 2)
/// channels of 48 kHz audio at `bitrate_bps` (total for all channels).
pub fn new(channels: u8, bitrate_bps: u32) -> Self {
Self::with_application(channels, bitrate_bps, Application::Audio)
}
/// Creates an encoder for `channels` (1 or 2) channels of 48 kHz audio at `bitrate_bps`
/// (total for all channels; clamped to 6 kb/s per channel – 510 kb/s), choosing the mode
/// and bandwidth for `application`.
pub fn with_application(channels: u8, bitrate_bps: u32, application: Application) -> Self {
let c = channels.max(1) as u32;
let (mode, bandwidth) = choose_mode(application, bitrate_bps.clamp(6_000 * c, 510_000) / c);
Self::build(channels, bitrate_bps, application, mode, bandwidth)
}
/// Creates an encoder with an explicit mode and bandwidth instead of the application's
/// table. Valid combinations: SILK with narrowband, mediumband or wideband; hybrid with
/// super-wideband or fullband; CELT with fullband.
///
/// # Panics
/// On any other combination, or when `channels` is not 1 or 2.
pub fn with_mode(channels: u8, bitrate_bps: u32, mode: Mode, bandwidth: Bandwidth) -> Self {
let ok = match mode {
Mode::Silk => bandwidth <= Bandwidth::Wideband,
Mode::Hybrid => bandwidth >= Bandwidth::SuperWideband,
Mode::Celt => bandwidth == Bandwidth::Fullband,
};
assert!(ok, "unsupported mode/bandwidth combination {mode:?}/{bandwidth:?}");
Self::build(channels, bitrate_bps, Application::Audio, mode, bandwidth)
}
fn build(channels: u8, bitrate_bps: u32, application: Application, mode: Mode, bandwidth: Bandwidth) -> Self {
assert!(channels == 1 || channels == 2, "Opus family-0 streams have 1 or 2 channels");
let c = channels as usize;
let bitrate_bps = bitrate_bps.clamp(6_000 * c as u32, 510_000);
let packet_bytes = (bitrate_bps as usize).div_ceil(400);
let frame_bytes = packet_bytes.saturating_sub(1).clamp(MIN_FRAME_BYTES, MAX_FRAME_BYTES);
let silk = match mode {
Mode::Celt => None,
Mode::Silk | Mode::Hybrid => {
let fs_khz = match bandwidth {
Bandwidth::Narrowband => 8,
Bandwidth::Mediumband => 12,
_ => 16,
};
let target_bits = if mode == Mode::Silk {
bitrate_bps as f64 / 50.0 - 8.0
} else {
// SILK gets most of a hybrid packet; CELT codes 8 kHz and up with the rest.
let per = (bitrate_bps / c as u32) as f64;
let share = if bandwidth == Bandwidth::SuperWideband { 0.72 } else { 0.62 };
c as f64 * (per * share).min(per - 6_000.0).max(per * 0.5) / 50.0
};
Some(SilkPath {
enc: SilkEncoder::new(fs_khz, c),
down: (0..c).map(|_| Downsampler::new(fs_khz as u32 * 1000)).collect(),
target_bits,
reservoir: 0.0,
})
}
};
OpusEncoder {
channels: c,
application,
mode,
bandwidth,
bitrate_bps,
frame_bytes,
mdct: Mdct::new(N),
preemph_mem: [0.0; 2],
hist: vec![0.0; c * OVERLAP],
old_band_e: [0.0; 2 * NB_EBANDS],
block: vec![0.0; N + OVERLAP],
freq: vec![0.0; c * N],
silk,
}
}
/// Number of channels.
pub fn channels(&self) -> u8 {
self.channels as u8
}
/// The application the encoder was created for.
pub fn application(&self) -> Application {
self.application
}
/// Coding mode of every packet of the stream.
pub fn mode(&self) -> Mode {
self.mode
}
/// Coded audio bandwidth of every packet of the stream.
pub fn bandwidth(&self) -> Bandwidth {
self.bandwidth
}
/// Bitrate in bits per second: the constant packet size × 50 packets per second in CELT
/// and hybrid modes, the (average) target in SILK mode.
pub fn bitrate(&self) -> u32 {
match self.mode {
Mode::Silk => self.bitrate_bps,
_ => ((self.frame_bytes + 1) * 8 * 50) as u32,
}
}
/// Samples at 48 kHz the decoder must discard from the start of the stream (the `OpusHead`
/// pre-skip): the encoder's total delay. CELT: the 120-sample MDCT overlap. SILK/hybrid:
/// the decimator (83 samples, 88 for narrowband) plus the decoder's SILK delay (one
/// internal-rate sample and the resampler delay of RFC 6716 Table 54), which also rounds
/// to 120 samples.
pub fn pre_skip(&self) -> u16 {
let (fs, table54_ms) = match (self.mode, self.bandwidth) {
(Mode::Celt, _) => return OVERLAP as u16,
(_, Bandwidth::Narrowband) => (8_000, 0.538),
(_, Bandwidth::Mediumband) => (12_000, 0.692),
_ => (16_000, 0.706),
};
(resample::delay(fs) as f64 + 48_000.0 / fs as f64 + table54_ms * 48.0).round() as u16
}
/// The 19-byte `OpusHead` identification header (RFC 7845 §5.1): version 1, channel count,
/// pre-skip, input sample rate 48000, output gain 0, channel mapping family 0.
pub fn opus_head(&self) -> Vec<u8> {
let mut h = Vec::with_capacity(19);
h.extend_from_slice(b"OpusHead");
h.push(1);
h.push(self.channels as u8);
h.extend_from_slice(&self.pre_skip().to_le_bytes());
h.extend_from_slice(&48_000u32.to_le_bytes());
h.extend_from_slice(&0i16.to_le_bytes());
h.push(0);
h
}
/// TOC byte (RFC 6716 §3.1, Table 2) of a 20 ms single-frame packet.
fn toc(&self) -> u8 {
let config: u8 = match (self.mode, self.bandwidth) {
(Mode::Silk, Bandwidth::Narrowband) => 1,
(Mode::Silk, Bandwidth::Mediumband) => 5,
(Mode::Silk, _) => 9,
(Mode::Hybrid, Bandwidth::SuperWideband) => 13,
(Mode::Hybrid, _) => 15,
(Mode::Celt, _) => 31,
};
(config << 3) | if self.channels == 2 { 4 } else { 0 }
}
/// Encodes one 20 ms frame of interleaved f32 samples in [-1, 1] (exactly
/// `FRAME_SIZE * channels` values) and returns one Opus packet.
pub fn encode_float(&mut self, pcm: &[f32]) -> Vec<u8> {
let c = self.channels;
assert_eq!(pcm.len(), N * c, "encode_float takes exactly FRAME_SIZE samples per channel");
let mut packet = vec![self.toc()];
match self.mode {
Mode::Celt => {
let (log_e, silence) = self.celt_analysis(pcm);
let len = self.frame_bytes;
packet.extend_from_slice(&self.encode_celt(RangeEncoder::new(), len, 0..NB_EBANDS, &log_e, silence));
}
Mode::Silk => {
let x = self.silk_input(pcm);
// The SILK state exists whenever the mode uses SILK (set with the mode).
if let Some(sp) = self.silk.as_mut() {
let target = sp.target_bits + 0.25 * sp.reservoir;
let mut enc = RangeEncoder::new();
sp.enc.encode(&x, target.max(24.0), (MAX_FRAME_BYTES * 8) as i32, &mut enc);
let bytes = (enc.tell() as usize).div_ceil(8).max(2);
let lim = 4.0 * sp.target_bits;
sp.reservoir = (sp.reservoir + sp.target_bits - (bytes * 8) as f64).clamp(-lim, lim);
packet.extend_from_slice(&enc.done(bytes));
}
}
Mode::Hybrid => {
let x = self.silk_input(pcm);
let len = self.frame_bytes;
let total = (len * 8) as i32;
let celt_min = (total / 5).max(80);
let mut enc = RangeEncoder::new();
if let Some(sp) = self.silk.as_mut() {
sp.enc.encode(&x, sp.target_bits, total - celt_min, &mut enc);
}
// No redundancy (RFC 6716 §4.5.1); the flag is present when there is room.
if enc.tell() + 17 + 20 <= total {
enc.bit_logp(false, 12);
}
let (log_e, _) = self.celt_analysis(pcm);
let end = if self.bandwidth == Bandwidth::SuperWideband { 19 } else { NB_EBANDS };
packet.extend_from_slice(&self.encode_celt(enc, len, HYBRID_START_BAND..end, &log_e, false));
}
}
packet
}
/// Decimates the input to the SILK internal rate (16-bit scale), one vector per channel.
fn silk_input(&mut self, pcm: &[f32]) -> Vec<Vec<f32>> {
let c = self.channels;
let Some(sp) = self.silk.as_mut() else { return vec![vec![]; c] };
let mut out = Vec::with_capacity(c);
let mut tmp = vec![0f32; N];
for ch in 0..c {
for (j, t) in tmp.iter_mut().enumerate() {
let s = pcm[j * c + ch];
*t = if s.is_finite() { s.clamp(-1.0, 1.0) * 32768.0 } else { 0.0 };
}
let mut y = Vec::with_capacity(N / 3);
sp.down[ch].process(&tmp, &mut y);
out.push(y);
}
out
}
/// Pre-emphasis, the MDCT, band energies (log2, relative to `E_MEANS`) and normalised
/// bands; returns the energies and whether the frame is digital silence.
fn celt_analysis(&mut self, pcm: &[f32]) -> ([f32; 2 * NB_EBANDS], bool) {
let c = self.channels;
let mut log_e = [0f32; 2 * NB_EBANDS];
let mut peak = 0f32;
for ch in 0..c {
self.block[..OVERLAP].copy_from_slice(&self.hist[ch * OVERLAP..(ch + 1) * OVERLAP]);
let mut mem = self.preemph_mem[ch];
for j in 0..N {
let s = pcm[j * c + ch];
let s = if s.is_finite() { s.clamp(-2.0, 2.0) * 32768.0 } else { 0.0 };
let v = s - PREEMPH * mem;
mem = s;
self.block[OVERLAP + j] = v;
}
self.preemph_mem[ch] = mem;
self.hist[ch * OVERLAP..(ch + 1) * OVERLAP].copy_from_slice(&self.block[N..]);
peak = self.block.iter().fold(peak, |p, v| p.max(v.abs()));
let freq = &mut self.freq[ch * N..(ch + 1) * N];
self.mdct.forward(&self.block, freq);
for i in 0..NB_EBANDS {
let lo = (EBANDS[i] as usize) << LM;
let hi = (EBANDS[i + 1] as usize) << LM;
let e = (1e-27f32 + freq[lo..hi].iter().map(|v| v * v).sum::<f32>()).sqrt();
log_e[ch * NB_EBANDS + i] = e.log2() - E_MEANS[i];
let g = 1.0 / e;
for v in freq[lo..hi].iter_mut() {
*v *= g;
}
}
}
(log_e, peak < 1e-4)
}
/// Codes one CELT frame (RFC 6716 §4.3, in bitstream order) of `len` bytes into `enc`
/// (which already holds the SILK frame in hybrid mode), coding bands `start..end`, and
/// finalises the frame.
fn encode_celt(&mut self, mut enc: RangeEncoder, len: usize, bands: std::ops::Range<usize>, log_e: &[f32; 2 * NB_EBANDS], silence: bool) -> Vec<u8> {
let m = celt_mode();
let c = self.channels;
let frame = CeltFrame { start: bands.start, end: bands.end, channels: c, lm: LM };
let CeltFrame { start, end, .. } = frame;
if c == 1 {
for i in 0..NB_EBANDS {
self.old_band_e[i] = self.old_band_e[i].max(self.old_band_e[NB_EBANDS + i]);
}
}
if silence && start == 0 {
// The silence flag; the decoder treats every remaining bit as consumed.
enc.bit_logp(true, 15);
self.old_band_e = [-28.0; 2 * NB_EBANDS];
return enc.done(2);
}
let total_bits_raw = len as i32 * 8;
if enc.tell() == 1 {
enc.bit_logp(false, 15);
}
// No pitch post-filter (only signalled in CELT-only frames).
if start == 0 && enc.tell() + 16 <= total_bits_raw {
enc.bit_logp(false, 1);
}
// Long blocks only.
if enc.tell() + 3 <= total_bits_raw {
enc.bit_logp(false, 3);
}
// Coarse energy, choosing intra or inter prediction by trial (whichever is cheaper).
if enc.tell() + 3 <= total_bits_raw {
let mut enc_intra = enc.clone();
let mut old_intra = self.old_band_e;
enc_intra.bit_logp(true, 3);
quant_coarse_energy(frame, log_e, &mut old_intra, true, &mut enc_intra, total_bits_raw);
enc.bit_logp(false, 3);
let mut old_inter = self.old_band_e;
quant_coarse_energy(frame, log_e, &mut old_inter, false, &mut enc, total_bits_raw);
if enc_intra.tell_frac() < enc.tell_frac() {
enc = enc_intra;
self.old_band_e = old_intra;
} else {
self.old_band_e = old_inter;
}
} else {
quant_coarse_energy(frame, log_e, &mut self.old_band_e, false, &mut enc, total_bits_raw);
}
// TF resolution: no changes.
{
let mut budget = total_bits_raw;
let mut tell = enc.tell();
let mut logp = 4;
let tf_select_rsv = LM > 0 && tell + logp < budget;
budget -= tf_select_rsv as i32;
for _ in start..end {
if tell + logp <= budget {
enc.bit_logp(false, logp as u32);
tell = enc.tell();
}
logp = 5;
}
if tf_select_rsv && TF_SELECT_TABLE[LM][0] != TF_SELECT_TABLE[LM][2] {
enc.bit_logp(false, 1);
}
}
if enc.tell() + 4 <= total_bits_raw {
enc.icdf(SPREAD_NORMAL, &SPREAD_ICDF, 5);
}
// Dynamic allocation: no boosts.
let cap = m.init_caps(LM, c);
let offsets = [0i32; NB_EBANDS];
let total_bits = total_bits_raw << BITRES;
let mut tellf = enc.tell_frac();
for &cp in cap.iter().take(end).skip(start) {
if tellf + (6 << BITRES) < total_bits && 0 < cp {
enc.bit_logp(false, 6);
tellf = enc.tell_frac();
}
}
let alloc_trim = 5;
if tellf + (6 << BITRES) <= total_bits {
enc.icdf(alloc_trim as usize, &TRIM_ICDF, 7);
}
let bits = (total_bits_raw << BITRES) - enc.tell_frac() - 1;
let alloc = compute_allocation(m, frame, AllocationInput { offsets: &offsets, cap: &cap, trim: alloc_trim, bits }, &mut enc);
quant_fine_energy(frame, log_e, &mut self.old_band_e, &alloc.fine_quant, &mut enc);
quant_all_bands(m, frame, &self.freq, N, &alloc, BandCoding { spread: SPREAD_NORMAL, total_bits: total_bits_raw << BITRES }, &mut enc);
let bits_left = total_bits_raw - enc.tell();
quant_energy_finalise(frame, log_e, &mut self.old_band_e, &alloc, bits_left, &mut enc);
debug_assert!(enc.tell() <= total_bits_raw, "frame overflow: {} > {}", enc.tell(), total_bits_raw);
if c == 1 {
let (a, b) = self.old_band_e.split_at_mut(NB_EBANDS);
b.copy_from_slice(a);
}
// The decoder forgets the energies of bands it did not code.
for ch in 0..2 {
for i in (0..start).chain(end..NB_EBANDS) {
self.old_band_e[ch * NB_EBANDS + i] = 0.0;
}
}
enc.done(len)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn opus_head_layout() {
let e = OpusEncoder::new(2, 96_000);
let h = e.opus_head();
assert_eq!(&h[..8], b"OpusHead");
assert_eq!(h[8], 1);
assert_eq!(h[9], 2);
assert_eq!(u16::from_le_bytes([h[10], h[11]]), e.pre_skip());
assert_eq!(u32::from_le_bytes([h[12], h[13], h[14], h[15]]), 48_000);
assert_eq!(&h[16..], &[0, 0, 0]);
}
#[test]
fn packet_sizes_follow_bitrate() {
for (ch, rate) in [(1u8, 64_000u32), (2, 128_000), (2, 256_000)] {
let mut e = OpusEncoder::new(ch, rate);
let pcm: Vec<f32> = (0..N * ch as usize).map(|i| (i as f32 * 0.05).sin() * 0.5).collect();
let p = e.encode_float(&pcm);
assert_eq!(p.len(), rate as usize / 400);
assert_eq!(p[0], 0xF8 | if ch == 2 { 4 } else { 0 });
assert_eq!(e.bitrate(), rate);
}
}
#[test]
fn silence_is_a_tiny_packet() {
let mut e = OpusEncoder::new(1, 64_000);
let p = e.encode_float(&vec![0.0; N]);
assert_eq!(p.len(), 3);
}
}
-363
View File
@@ -1,363 +0,0 @@
//! Forward MDCT with the CELT low-overlap window (RFC 6716 §4.3.7, run in the analysis
//! direction).
//!
//! A 20 ms frame is a 1920-point MDCT (960 coefficients) whose window is zero for 420 samples,
//! rises over the 120-sample overlap, is flat for 840 samples, falls over the next 120 samples
//! and is zero again for the last 420. The input block is therefore the 120 overlap samples kept
//! from the previous frame followed by the 960 new samples. The MDCT is folded into a DCT-IV,
//! which is computed with an N/2-point complex FFT.
use crate::tables::OVERLAP;
#[derive(Clone, Copy, Default, Debug)]
pub struct Cpx {
pub re: f32,
pub im: f32,
}
impl Cpx {
#[inline]
fn mul(self, o: Cpx) -> Cpx {
Cpx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re }
}
#[inline]
fn add(self, o: Cpx) -> Cpx {
Cpx { re: self.re + o.re, im: self.im + o.im }
}
#[inline]
fn sub(self, o: Cpx) -> Cpx {
Cpx { re: self.re - o.re, im: self.im - o.im }
}
}
/// Mixed-radix (2, 3, 4, 5) forward complex FFT, unscaled (decimation in time, recursive).
pub struct Fft {
n: usize,
factors: Vec<usize>,
twiddle: Vec<Cpx>,
}
impl Fft {
pub fn new(n: usize) -> Fft {
let mut factors = Vec::new();
let mut m = n;
for p in [4usize, 2, 3, 5] {
while m.is_multiple_of(p) {
factors.push(p);
m /= p;
}
}
assert_eq!(m, 1, "unsupported FFT size {n}");
let twiddle = (0..n)
.map(|k| {
let ph = -2.0 * std::f64::consts::PI * k as f64 / n as f64;
Cpx { re: ph.cos() as f32, im: ph.sin() as f32 }
})
.collect();
Fft { n, factors, twiddle }
}
pub fn process(&self, data: &mut [Cpx], scratch: &mut Vec<Cpx>) {
scratch.clear();
scratch.extend_from_slice(&data[..self.n]);
self.rec(&mut data[..self.n], scratch, 1, self.n, 0, 1);
}
fn rec(&self, out: &mut [Cpx], inp: &[Cpx], in_stride: usize, n: usize, fi: usize, tw_stride: usize) {
let p = self.factors[fi];
let m = n / p;
if m == 1 {
for (i, o) in out.iter_mut().enumerate().take(p) {
*o = inp[i * in_stride];
}
} else {
for q in 0..p {
self.rec(&mut out[q * m..(q + 1) * m], &inp[q * in_stride..], in_stride * p, m, fi + 1, tw_stride * p);
}
}
// The twiddle index q * k * tw_stride is always below n, so no modulo is needed.
let tw = &self.twiddle;
match p {
2 => {
for k in 0..m {
let a0 = out[k];
let a1 = out[m + k].mul(tw[k * tw_stride]);
out[k] = a0.add(a1);
out[m + k] = a0.sub(a1);
}
}
4 => {
for k in 0..m {
let t0 = out[k];
let t1 = out[m + k].mul(tw[k * tw_stride]);
let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]);
let t3 = out[3 * m + k].mul(tw[3 * k * tw_stride]);
let (s0, d0) = (t0.add(t2), t0.sub(t2));
let (s1, d1) = (t1.add(t3), t1.sub(t3));
out[k] = s0.add(s1);
out[2 * m + k] = s0.sub(s1);
// The odd outputs combine d0 with -j * d1 = (d1.im, -d1.re) and its negative.
out[m + k] = Cpx { re: d0.re + d1.im, im: d0.im - d1.re };
out[3 * m + k] = Cpx { re: d0.re - d1.im, im: d0.im + d1.re };
}
}
3 => {
// Imaginary part of exp(-2 pi i / 3) = -sqrt(3) / 2.
let h = tw[self.n / 3].im;
for k in 0..m {
let t0 = out[k];
let t1 = out[m + k].mul(tw[k * tw_stride]);
let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]);
let (s, d) = (t1.add(t2), t1.sub(t2));
let a = Cpx { re: t0.re - 0.5 * s.re, im: t0.im - 0.5 * s.im };
out[k] = t0.add(s);
// j * h * d = (-h * d.im, h * d.re)
out[m + k] = Cpx { re: a.re - h * d.im, im: a.im + h * d.re };
out[2 * m + k] = Cpx { re: a.re + h * d.im, im: a.im - h * d.re };
}
}
_ => {
// Generic DFT butterfly (radix 5, once per transform).
let roots: [Cpx; 5] = std::array::from_fn(|q| if q < p { tw[q * (self.n / p)] } else { Cpx::default() });
let mut t = [Cpx::default(); 5];
for k in 0..m {
t[0] = out[k];
for q in 1..p {
t[q] = out[q * m + k].mul(tw[q * k * tw_stride]);
}
for q2 in 0..p {
let mut acc = t[0];
for q in 1..p {
acc = acc.add(t[q].mul(roots[(q * q2) % p]));
}
out[q2 * m + k] = acc;
}
}
}
}
}
}
/// Forward MDCT producing `n` coefficients from a block of `n + OVERLAP` samples.
pub struct Mdct {
/// Number of coefficients (half the MDCT length).
n: usize,
fft: Fft,
pre: Vec<Cpx>,
post: Vec<Cpx>,
/// Rising half of the low-overlap window (RFC 6716 §4.3.7).
pub window: [f32; OVERLAP],
scale: f32,
z: Vec<Cpx>,
scratch: Vec<Cpx>,
v: Vec<f32>,
}
impl Mdct {
pub fn new(n: usize) -> Mdct {
let n2 = n / 2;
let pi = std::f64::consts::PI;
let pre = (0..n2)
.map(|m| {
let ph = -pi * m as f64 / n as f64;
Cpx { re: ph.cos() as f32, im: ph.sin() as f32 }
})
.collect();
let post = (0..n2)
.map(|p| {
let ph = -pi * (p as f64 + 0.25) / n as f64;
Cpx { re: ph.cos() as f32, im: ph.sin() as f32 }
})
.collect();
let mut window = [0f32; OVERLAP];
for (i, w) in window.iter_mut().enumerate() {
let x = std::f64::consts::FRAC_PI_2 * (i as f64 + 0.5) / OVERLAP as f64;
let s = x.sin();
*w = (std::f64::consts::FRAC_PI_2 * s * s).sin() as f32;
}
Mdct { n, fft: Fft::new(n2), pre, post, window, scale: 2.0 / n as f32, z: Vec::new(), scratch: Vec::new(), v: vec![0.0; n] }
}
/// Transforms `block` (`n + OVERLAP` samples: the previous frame's last `OVERLAP` samples, then
/// `n` new ones) into `out[..n]`, scaled so that the RFC 6716 decoder's inverse MDCT and
/// overlap-add reproduce the input.
pub fn forward(&mut self, block: &[f32], out: &mut [f32]) {
let n = self.n;
let n2 = n / 2;
debug_assert_eq!(block.len(), n + OVERLAP);
// Place the windowed block into the (virtual) 2n-sample MDCT input u at offset `pad`.
let pad = (n - OVERLAP) / 2;
let w = &self.window;
let u = |t: usize| -> f32 {
if t < pad || t >= pad + n + OVERLAP {
return 0.0;
}
let j = t - pad;
let g = if j < OVERLAP {
w[j]
} else if j >= n {
w[OVERLAP - 1 - (j - n)]
} else {
1.0
};
block[j] * g
};
// Fold to a DCT-IV input: v = (-c_r - d, a - b_r) over the four quarters of u.
for m in 0..n2 {
self.v[m] = -u(n + n2 - 1 - m) - u(n + n2 + m);
self.v[n2 + m] = u(m) - u(n2 + n2 - 1 - m);
}
// DCT-IV via an n/2-point complex FFT.
self.z.clear();
for m in 0..n2 {
let wv = Cpx { re: self.v[2 * m], im: self.v[n - 1 - 2 * m] };
self.z.push(wv.mul(self.pre[m]));
}
self.fft.process(&mut self.z, &mut self.scratch);
for p in 0..n2 {
let y = self.z[p].mul(self.post[p]);
out[2 * p] = y.re * self.scale;
out[n - 1 - 2 * p] = -y.im * self.scale;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Direct MDCT definition X[k] = sum u[t] cos(pi/N (t + 1/2 + N/2)(k + 1/2)).
fn direct(block: &[f32], n: usize, window: &[f32; OVERLAP]) -> Vec<f64> {
let pad = (n - OVERLAP) / 2;
let mut u = vec![0f64; 2 * n];
for (j, &b) in block.iter().enumerate() {
let g = if j < OVERLAP {
window[j]
} else if j >= n {
window[OVERLAP - 1 - (j - n)]
} else {
1.0
};
u[pad + j] = (b * g) as f64;
}
(0..n)
.map(|k| {
(0..2 * n).map(|t| u[t] * (std::f64::consts::PI / n as f64 * (t as f64 + 0.5 + n as f64 / 2.0) * (k as f64 + 0.5)).cos()).sum::<f64>() * 2.0
/ n as f64
})
.collect()
}
#[test]
fn fft_matches_dft() {
// Every radix combination, including the 480-point size of the 20 ms frame.
for &n in &[2usize, 3, 4, 5, 6, 8, 12, 15, 16, 20, 24, 30, 48, 60, 120, 240, 480] {
let fft = Fft::new(n);
let x: Vec<Cpx> = (0..n).map(|i| Cpx { re: ((i * 37 % 101) as f32 - 50.0) / 7.0, im: ((i * 53 % 89) as f32 - 44.0) / 9.0 }).collect();
let mut y = x.clone();
fft.process(&mut y, &mut Vec::new());
for k in 0..n {
let (mut re, mut im) = (0f64, 0f64);
for (j, v) in x.iter().enumerate() {
let ph = -2.0 * std::f64::consts::PI * ((j * k) % n) as f64 / n as f64;
re += v.re as f64 * ph.cos() - v.im as f64 * ph.sin();
im += v.re as f64 * ph.sin() + v.im as f64 * ph.cos();
}
assert!((y[k].re as f64 - re).abs() < 2e-3 && (y[k].im as f64 - im).abs() < 2e-3, "n={n} k={k}: ({}, {}) vs ({re}, {im})", y[k].re, y[k].im);
}
}
}
#[test]
fn matches_direct_formula() {
for &n in &[240usize, 960] {
let block: Vec<f32> = (0..n + OVERLAP).map(|i| ((i * 37 % 101) as f32 - 50.0) / 7.0).collect();
let mut m = Mdct::new(n);
let mut out = vec![0f32; n];
m.forward(&block, &mut out);
let d = direct(&block, n, &m.window);
for k in 0..n {
assert!((out[k] as f64 - d[k]).abs() < 1e-2, "n={n} k={k}: {} vs {}", out[k], d[k]);
}
}
}
// ---- Replica of the RFC 6716 decoder inverse MDCT (as in FilmCraft's decoder) ----
fn imdct_backward(n_full: usize, input: &[f32], out: &mut [f32], window: &[f32]) {
let n2 = n_full >> 1;
let n4 = n_full >> 2;
let t: Vec<f32> = (0..n2).map(|i| (2.0 * std::f64::consts::PI * (i as f64 + 0.125) / n_full as f64).cos() as f32).collect();
let fft = Fft::new(n4);
let mut z = Vec::new();
for i in 0..n4 {
let x1 = input[2 * i];
let x2 = input[n2 - 1 - 2 * i];
let yr = x2 * t[i] + x1 * t[n4 + i];
let yi = x1 * t[i] - x2 * t[n4 + i];
z.push(Cpx { re: yi, im: yr });
}
fft.process(&mut z, &mut Vec::new());
let overlap = OVERLAP;
let base = overlap >> 1;
let y = &mut out[base..base + n2];
for i in 0..n4.div_ceil(2) {
let front = z[i];
let back = z[n4 - 1 - i];
let (re, im) = (front.im, front.re);
let (t0, t1) = (t[i], t[n4 + i]);
let yr0 = re * t0 + im * t1;
let yi0 = re * t1 - im * t0;
let (re, im) = (back.im, back.re);
let (t0, t1) = (t[n4 - i - 1], t[n2 - i - 1]);
let yr1 = re * t0 + im * t1;
let yi1 = re * t1 - im * t0;
y[2 * i] = yr0;
y[n2 - 1 - 2 * i] = yi0;
y[n2 - 2 - 2 * i] = yr1;
y[2 * i + 1] = yi1;
}
for i in 0..overlap / 2 {
let x1 = out[overlap - 1 - i];
let x2 = out[i];
let w1 = window[i];
let w2 = window[overlap - 1 - i];
out[i] = x2 * w2 - x1 * w1;
out[overlap - 1 - i] = x2 * w1 + x1 * w2;
}
}
/// MDCT analysis followed by the decoder's IMDCT + TDAC overlap-add reproduces the input
/// delayed by exactly `OVERLAP` samples.
#[test]
fn perfect_reconstruction_through_decoder_imdct() {
let n = 960;
let frames = 5;
let sig: Vec<f32> = (0..n * frames).map(|i| (i as f32 * 0.013).sin() * 1000.0 + ((i * 7919) % 211) as f32 - 105.0).collect();
let mut m = Mdct::new(n);
let mut hist = vec![0f32; OVERLAP];
// Decoder memory: [frame output n][folded tail OVERLAP/2] laid out like decode_mem.
let mut mem = vec![0f32; n + OVERLAP];
let mut decoded = Vec::new();
for f in 0..frames {
let mut block = hist.clone();
block.extend_from_slice(&sig[f * n..(f + 1) * n]);
hist.copy_from_slice(&block[n..]);
let mut coefs = vec![0f32; n];
m.forward(&block, &mut coefs);
// Shift: the previous folded tail (at n..n+OVERLAP/2) moves to 0..OVERLAP/2.
mem.copy_within(n..n + OVERLAP / 2, 0);
imdct_backward(2 * n, &coefs, &mut mem, &m.window);
decoded.extend_from_slice(&mem[..n]);
}
let mut err = 0f64;
let mut sum = 0f64;
for t in OVERLAP..n * frames {
let d = decoded[t] as f64 - sig[t - OVERLAP] as f64;
err += d * d;
sum += (sig[t - OVERLAP] as f64).powi(2);
}
let snr = 10.0 * (sum / err).log10();
assert!(snr > 80.0, "reconstruction SNR {snr:.1} dB");
}
}
-472
View File
@@ -1,472 +0,0 @@
//! Range encoder (RFC 6716 §5.1), the exact inverse of the §4.1 range decoder.
//!
//! Range-coded symbols are written from the front of the buffer and raw bits from the back; the
//! bit accounting ([`RangeEncoder::tell`], [`RangeEncoder::tell_frac`]) matches the decoder's, so
//! every budget decision the decoder takes can be mirrored exactly.
const SYM_BITS: u32 = 8;
const CODE_BITS: u32 = 32;
const SYM_MAX: u32 = (1 << SYM_BITS) - 1;
const CODE_SHIFT: u32 = CODE_BITS - SYM_BITS - 1;
const CODE_TOP: u32 = 1 << (CODE_BITS - 1);
const CODE_BOT: u32 = CODE_TOP >> SYM_BITS;
/// Bit resolution of [`RangeEncoder::tell_frac`] (1/8 bit).
pub const BITRES: u32 = 3;
/// Number of bits needed to represent `x` (0 for 0).
#[inline]
pub fn ilog(x: u32) -> i32 {
(32 - x.leading_zeros()) as i32
}
#[derive(Clone, Debug)]
pub struct RangeEncoder {
out: Vec<u8>,
end: Vec<u8>,
end_window: u32,
nend_bits: i32,
nbits_total: i32,
rng: u32,
low: u32,
rem: i32,
ext: u32,
}
impl Default for RangeEncoder {
fn default() -> Self {
Self::new()
}
}
impl RangeEncoder {
pub fn new() -> Self {
RangeEncoder {
out: Vec::new(),
end: Vec::new(),
end_window: 0,
nend_bits: 0,
nbits_total: (CODE_BITS + 1) as i32,
rng: CODE_TOP,
low: 0,
rem: -1,
ext: 0,
}
}
/// Current range (equals the decoder's range after the same symbols).
#[cfg(test)]
pub fn rng(&self) -> u32 {
self.rng
}
fn carry_out(&mut self, c: u32) {
if c != SYM_MAX {
let carry = c >> SYM_BITS;
if self.rem >= 0 {
self.out.push((self.rem as u32 + carry) as u8);
}
if self.ext > 0 {
let sym = (SYM_MAX + carry) & SYM_MAX;
for _ in 0..self.ext {
self.out.push(sym as u8);
}
self.ext = 0;
}
self.rem = (c & SYM_MAX) as i32;
} else {
self.ext += 1;
}
}
#[inline]
fn normalize(&mut self) {
while self.rng <= CODE_BOT {
self.carry_out(self.low >> CODE_SHIFT);
self.low = (self.low << SYM_BITS) & (CODE_TOP - 1);
self.rng <<= SYM_BITS;
self.nbits_total += SYM_BITS as i32;
}
}
/// Encodes the symbol with cumulative range `[fl, fh)` of total `ft`.
pub fn encode(&mut self, fl: u32, fh: u32, ft: u32) {
let r = self.rng / ft;
if fl > 0 {
self.low += self.rng - r * (ft - fl);
self.rng = r * (fh - fl);
} else {
self.rng -= r * (ft - fh);
}
self.normalize();
}
/// Like [`Self::encode`] with `ft = 1 << bits`.
pub fn encode_bin(&mut self, fl: u32, fh: u32, bits: u32) {
let r = self.rng >> bits;
let ft = 1u32 << bits;
if fl > 0 {
self.low += self.rng - r * (ft - fl);
self.rng = r * (fh - fl);
} else {
self.rng -= r * (ft - fh);
}
self.normalize();
}
/// Encodes a binary symbol whose probability of being 1 is `1/2^logp`.
pub fn bit_logp(&mut self, val: bool, logp: u32) {
let r = self.rng;
let s = r >> logp;
let r2 = r - s;
if val {
self.low += r2;
self.rng = s;
} else {
self.rng = r2;
}
self.normalize();
}
/// Encodes symbol `s` with an "inverse CDF" table (`ft = 1 << ftb`).
pub fn icdf(&mut self, s: usize, icdf: &[u8], ftb: u32) {
let r = self.rng >> ftb;
if s > 0 {
self.low += self.rng - r * icdf[s - 1] as u32;
self.rng = r * (icdf[s - 1] as u32 - icdf[s] as u32);
} else {
self.rng -= r * icdf[s] as u32;
}
self.normalize();
}
/// Encodes a uniformly distributed integer `fl` in `[0, ft)` (`ft >= 2`).
pub fn uint(&mut self, fl: u32, ft: u32) {
debug_assert!(ft > 1 && fl < ft);
let ft1 = ft - 1;
let ftb = ilog(ft1);
if ftb > 8 {
let ftb = (ftb - 8) as u32;
let top = (ft1 >> ftb) + 1;
let v = fl >> ftb;
self.encode(v, v + 1, top);
self.bits(fl & ((1 << ftb) - 1), ftb);
} else {
self.encode(fl, fl + 1, ft);
}
}
/// Writes `bits` raw bits (0..=25) to the end of the buffer.
pub fn bits(&mut self, val: u32, bits: u32) {
if bits == 0 {
return;
}
let mut window = self.end_window;
let mut used = self.nend_bits;
if used as u32 + bits > CODE_BITS {
while used >= SYM_BITS as i32 {
self.end.push((window & SYM_MAX) as u8);
window >>= SYM_BITS;
used -= SYM_BITS as i32;
}
}
window |= val << used;
used += bits as i32;
self.end_window = window;
self.nend_bits = used;
self.nbits_total += bits as i32;
}
/// Number of bits written so far (rounded up), identical to the decoder's `tell`.
#[inline]
pub fn tell(&self) -> i32 {
self.nbits_total - ilog(self.rng)
}
/// Number of 1/8 bits written so far (rounded up), identical to the decoder's `tell_frac`.
pub fn tell_frac(&self) -> i32 {
let nbits = (self.nbits_total as u32) << BITRES;
let mut l = ilog(self.rng) as u32;
let mut r = self.rng >> (l - 16);
for _ in 0..BITRES {
r = (r * r) >> 15;
let b = r >> 16;
l = (l << 1) | b;
r >>= b;
}
nbits.wrapping_sub(l) as i32
}
/// Finalises the stream into a buffer of exactly `size` bytes (range data at the front, raw
/// bits at the back, zero padding in between; a partial raw-bit byte is merged into the byte
/// just before the full raw-bit bytes).
pub fn done(mut self, size: usize) -> Vec<u8> {
let mut l = CODE_BITS as i32 - ilog(self.rng);
let mut msk = (CODE_TOP - 1) >> l;
let mut end = (self.low.wrapping_add(msk)) & !msk;
if (end | msk) >= self.low.wrapping_add(self.rng) {
l += 1;
msk >>= 1;
end = (self.low.wrapping_add(msk)) & !msk;
}
while l > 0 {
self.carry_out(end >> CODE_SHIFT);
end = (end << SYM_BITS) & (CODE_TOP - 1);
l -= SYM_BITS as i32;
}
if self.rem >= 0 || self.ext > 0 {
self.carry_out(0);
}
let mut window = self.end_window;
let mut used = self.nend_bits;
while used >= SYM_BITS as i32 {
self.end.push((window & SYM_MAX) as u8);
window >>= SYM_BITS;
used -= SYM_BITS as i32;
}
let mut buf = vec![0u8; size];
let full = self.end.len().min(size);
let room = size - full;
let n = self.out.len().min(room);
buf[..n].copy_from_slice(&self.out[..n]);
for (k, &b) in self.end.iter().take(full).enumerate() {
buf[size - 1 - k] = b;
}
if used > 0 && room > 0 {
buf[room - 1] |= window as u8;
}
buf
}
}
#[cfg(test)]
pub(crate) mod dec {
//! Minimal range decoder (RFC 6716 §4.1) used to verify the encoder.
use super::*;
const CODE_EXTRA: u32 = (CODE_BITS - 2) % SYM_BITS + 1;
pub struct RangeDecoder<'a> {
buf: &'a [u8],
offs: usize,
end_offs: usize,
end_window: u32,
nend_bits: i32,
nbits_total: i32,
rng: u32,
val: u32,
rem: u32,
}
impl<'a> RangeDecoder<'a> {
pub fn new(buf: &'a [u8]) -> Self {
let mut d = RangeDecoder {
buf,
offs: 0,
end_offs: 0,
end_window: 0,
nend_bits: 0,
nbits_total: (CODE_BITS + 1 - ((CODE_BITS - CODE_EXTRA) / SYM_BITS) * SYM_BITS) as i32,
rng: 1 << CODE_EXTRA,
val: 0,
rem: 0,
};
d.rem = d.read_byte();
d.val = d.rng - 1 - (d.rem >> (SYM_BITS - CODE_EXTRA));
d.normalize();
d
}
fn read_byte(&mut self) -> u32 {
if self.offs < self.buf.len() {
self.offs += 1;
self.buf[self.offs - 1] as u32
} else {
0
}
}
fn read_byte_from_end(&mut self) -> u32 {
if self.end_offs < self.buf.len() {
self.end_offs += 1;
self.buf[self.buf.len() - self.end_offs] as u32
} else {
0
}
}
fn normalize(&mut self) {
while self.rng <= CODE_BOT {
self.nbits_total += SYM_BITS as i32;
self.rng <<= SYM_BITS;
let sym = self.rem;
self.rem = self.read_byte();
let sym = ((sym << SYM_BITS) | self.rem) >> (SYM_BITS - CODE_EXTRA);
self.val = ((self.val << SYM_BITS).wrapping_add(SYM_MAX & !sym)) & (CODE_TOP - 1);
}
}
pub fn rng(&self) -> u32 {
self.rng
}
pub fn decode(&mut self, ft: u32) -> u32 {
let ext = self.rng / ft;
let s = self.val / ext;
ft - (s + 1).min(ft)
}
pub fn update(&mut self, fl: u32, fh: u32, ft: u32) {
let ext = self.rng / ft;
let s = ext * (ft - fh);
self.val = self.val.wrapping_sub(s);
self.rng = if fl > 0 { ext * (fh - fl) } else { self.rng - s };
self.normalize();
}
pub fn bit_logp(&mut self, logp: u32) -> bool {
let r = self.rng;
let d = self.val;
let s = r >> logp;
let ret = d < s;
if !ret {
self.val = d - s;
}
self.rng = if ret { s } else { r - s };
self.normalize();
ret
}
pub fn icdf(&mut self, icdf: &[u8], ftb: u32) -> usize {
let mut s = self.rng;
let d = self.val;
let r = s >> ftb;
let mut ret = 0usize;
let mut t;
loop {
t = s;
s = r * icdf[ret] as u32;
if d >= s {
break;
}
ret += 1;
}
self.val = d - s;
self.rng = t - s;
self.normalize();
ret
}
pub fn uint(&mut self, ft: u32) -> u32 {
let ft1 = ft - 1;
let ftb = ilog(ft1);
if ftb > 8 {
let ftb = (ftb - 8) as u32;
let top = (ft1 >> ftb) + 1;
let s = self.decode(top);
self.update(s, s + 1, top);
(s << ftb) | self.bits(ftb)
} else {
let s = self.decode(ft);
self.update(s, s + 1, ft);
s
}
}
pub fn bits(&mut self, bits: u32) -> u32 {
let mut window = self.end_window;
let mut available = self.nend_bits;
if (available as u32) < bits {
loop {
window |= self.read_byte_from_end() << available;
available += SYM_BITS as i32;
if available > (CODE_BITS - SYM_BITS) as i32 {
break;
}
}
}
let ret = window & ((1u32 << bits) - 1);
self.end_window = window >> bits;
self.nend_bits = available - bits as i32;
self.nbits_total += bits as i32;
ret
}
pub fn tell(&self) -> i32 {
self.nbits_total - ilog(self.rng)
}
}
}
#[cfg(test)]
mod tests {
use super::dec::RangeDecoder;
use super::*;
struct Lcg(u32);
impl Lcg {
fn next(&mut self) -> u32 {
self.0 = self.0.wrapping_mul(1664525).wrapping_add(1013904223);
self.0 >> 8
}
}
#[test]
fn starts_at_one_bit() {
let e = RangeEncoder::new();
assert_eq!(e.tell(), 1);
assert_eq!(e.tell_frac(), 8);
}
#[test]
fn roundtrip_mixed_symbols_and_tell_matches() {
let mut rng = Lcg(7);
let icdf: [u8; 4] = [200, 120, 30, 0];
for trial in 0..300 {
let n = 1 + (rng.next() % 300) as usize;
let mut ops = Vec::new();
for _ in 0..n {
match rng.next() % 5 {
0 => {
let ft = 2 + rng.next() % 1000;
ops.push((0u8, rng.next() % ft, ft));
}
1 => {
let logp = 1 + rng.next() % 15;
ops.push((1, rng.next().is_multiple_of(5) as u32, logp));
}
2 => {
let bits = 1 + rng.next() % 24;
ops.push((2, rng.next() & ((1 << bits) - 1), bits));
}
3 => {
let ft = 2 + rng.next() % 100_000_000;
ops.push((3, rng.next() % ft, ft));
}
_ => ops.push((4, rng.next() % 4, 0)),
}
}
let mut e = RangeEncoder::new();
let mut tells = Vec::new();
for &(k, v, p) in &ops {
match k {
0 => e.encode(v, v + 1, p),
1 => e.bit_logp(v != 0, p),
2 => e.bits(v, p),
3 => e.uint(v, p),
_ => e.icdf(v as usize, &icdf, 8),
}
tells.push(e.tell());
}
// Exactly the number of bytes `tell` says were used must suffice.
let size = (e.tell() as usize).div_ceil(8);
let final_rng = e.rng();
let buf = e.done(size);
assert_eq!(buf.len(), size);
let mut d = RangeDecoder::new(&buf);
for (i, &(k, v, p)) in ops.iter().enumerate() {
let got = match k {
0 => {
let s = d.decode(p);
d.update(s, s + 1, p);
s
}
1 => d.bit_logp(p) as u32,
2 => d.bits(p),
3 => d.uint(p),
_ => d.icdf(&icdf, 8) as u32,
};
assert_eq!(got, v, "trial {trial} op {i} kind {k}");
assert_eq!(d.tell(), tells[i], "tell mismatch trial {trial} op {i}");
}
assert_eq!(d.rng(), final_rng);
}
}
}
-560
View File
@@ -1,560 +0,0 @@
//! Pulse cache and bit allocation (RFC 6716 §4.3.3, §4.3.4.1).
//!
//! The allocation must be bit-identical to the decoder's, so this mirrors FilmCraft's
//! spec-derived `filmcraft-opus` decoder (first-party, MIT OR Apache-2.0) step by step; the only
//! difference is that the encoder *writes* the band-skip, intensity and dual-stereo symbols.
use crate::CeltFrame;
use crate::range::{BITRES, RangeEncoder, ilog};
use crate::tables::{ALLOC, EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS};
const MAX_PSEUDO: i32 = 40;
const LOG_MAX_PSEUDO: i32 = 6;
pub const MAX_FINE_BITS: i32 = 8;
const FINE_OFFSET: i32 = 21;
pub const QTHETA_OFFSET: i32 = 4;
pub const QTHETA_OFFSET_TWOPHASE: i32 = 16;
const ALLOC_STEPS: i32 = 6;
/// Number of pulses represented by pseudo-pulse index `i`.
pub fn get_pulses(i: i32) -> i32 {
if i < 8 { i } else { (8 + (i & 7)) << ((i >> 3) - 1) }
}
/// Conservative (rounded up) log2 of `val` with `frac` fractional bits.
pub fn log2_frac(mut val: u32, mut frac: u32) -> i32 {
let mut l = ilog(val);
if val & val.wrapping_sub(1) != 0 {
if l > 16 {
val = ((val - 1) >> (l - 16)) + 1;
} else {
val <<= 16 - l;
}
let mut l = (l - 1) << frac;
loop {
let b = (val >> 16) as i32;
l += b << frac;
val = (val + b as u32) >> b;
val = (val * val + 0x7FFF) >> 15;
if frac == 0 {
break;
}
frac -= 1;
}
l + (val > 0x8000) as i32
} else {
l -= 1;
l << frac
}
}
/// PVQ codebook size V(N, K) (saturating far above 2^32).
pub fn pvq_v(n: usize, k: usize) -> u128 {
if k == 0 {
return 1;
}
if n == 0 {
return 0;
}
let mut row = vec![0u128; k + 1];
row[0] = 1;
for _ in 0..n {
let mut prev_diag = row[0];
for kk in 1..=k {
let up = row[kk];
let v = up.saturating_add(row[kk - 1]).saturating_add(prev_diag);
prev_diag = up;
row[kk] = v.min(u128::MAX >> 8);
}
row[0] = 1;
}
row[k]
}
fn fits_in32(n: usize, k: i32) -> bool {
pvq_v(n, k as usize) <= u32::MAX as u128
}
/// Precomputed per-mode data.
pub struct Mode {
/// log2 of band width in 1/8 bits.
pub log_n: [i32; NB_EBANDS],
cache_index: Vec<i16>,
cache_bits: Vec<u8>,
caps: Vec<u8>,
}
impl Mode {
pub fn new() -> Mode {
let mut log_n = [0i32; NB_EBANDS];
for (i, l) in log_n.iter_mut().enumerate() {
*l = log2_frac((EBANDS[i + 1] - EBANDS[i]) as u32, BITRES);
}
let (cache_index, cache_bits) = compute_pulse_cache(MAX_LM as i32);
let mut m = Mode { log_n, cache_index, cache_bits, caps: Vec::new() };
m.caps = compute_caps(&m);
m
}
#[inline]
pub fn cache(&self, lm: i32, band: usize) -> &[u8] {
let idx = self.cache_index[((lm + 1) as usize) * NB_EBANDS + band];
&self.cache_bits[idx.max(0) as usize..]
}
pub fn bits2pulses(&self, band: usize, lm: i32, bits: i32) -> i32 {
let cache = self.cache(lm, band);
let mut lo = 0i32;
let mut hi = cache[0] as i32;
let bits = bits - 1;
for _ in 0..LOG_MAX_PSEUDO {
let mid = (lo + hi + 1) >> 1;
if cache[mid as usize] as i32 >= bits {
hi = mid;
} else {
lo = mid;
}
}
let lo_bits = if lo == 0 { -1 } else { cache[lo as usize] as i32 };
if bits - lo_bits <= cache[hi as usize] as i32 - bits { lo } else { hi }
}
pub fn pulses2bits(&self, band: usize, lm: i32, pulses: i32) -> i32 {
if pulses == 0 { 0 } else { self.cache(lm, band)[pulses as usize] as i32 + 1 }
}
/// Per-band allocation caps for this frame (`init_caps`).
pub fn init_caps(&self, lm: usize, c: usize) -> [i32; NB_EBANDS] {
let mut cap = [0i32; NB_EBANDS];
for (i, cp) in cap.iter_mut().enumerate() {
let n = (EBANDS[i + 1] - EBANDS[i]) << lm;
*cp = ((self.caps[NB_EBANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * n) >> 2;
}
cap
}
}
fn compute_pulse_cache(lm_max: i32) -> (Vec<i16>, Vec<u8>) {
let nb = NB_EBANDS;
let rows = (lm_max + 2) as usize;
let mut index = vec![-1i16; nb * rows];
let mut entries: Vec<(usize, i32, usize)> = Vec::new();
let mut curr = 0usize;
for i in 0..rows {
for j in 0..nb {
let n = (((EBANDS[j + 1] - EBANDS[j]) << i) >> 1) as usize;
index[i * nb + j] = -1;
'search: for k in 0..=i {
for nn in 0..nb {
if k == i && nn >= j {
break;
}
if n == (((EBANDS[nn + 1] - EBANDS[nn]) << k) >> 1) as usize {
index[i * nb + j] = index[k * nb + nn];
break 'search;
}
}
}
if index[i * nb + j] == -1 && n != 0 {
let mut k = 0;
while k < MAX_PSEUDO && fits_in32(n, get_pulses(k + 1)) {
k += 1;
}
index[i * nb + j] = curr as i16;
entries.push((n, k, curr));
curr += (k + 1) as usize;
}
}
}
let mut bits = vec![0u8; curr];
for &(n, k, off) in &entries {
let maxk = get_pulses(k) as usize;
let tmp = required_bits(n, maxk, BITRES);
for j in 1..=k {
bits[off + j as usize] = (tmp[get_pulses(j) as usize] - 1) as u8;
}
bits[off] = k as u8;
}
(index, bits)
}
fn required_bits(n: usize, maxk: usize, frac: u32) -> Vec<i32> {
let mut out = vec![0i32; maxk + 1];
if n == 1 {
for o in out.iter_mut().skip(1) {
*o = 1 << frac;
}
} else {
for (k, o) in out.iter_mut().enumerate().skip(1) {
*o = log2_frac(pvq_v(n, k) as u32, frac);
}
}
out
}
fn compute_caps(m: &Mode) -> Vec<u8> {
let nb = NB_EBANDS;
let mut caps = vec![0u8; (MAX_LM + 1) * 2 * nb];
for i in 0..=MAX_LM as i32 {
for c in 1..=2i32 {
for j in 0..nb {
let mut n0 = EBANDS[j + 1] - EBANDS[j];
let max_bits = if n0 << i == 1 {
(c * (1 + MAX_FINE_BITS)) << BITRES
} else {
let mut lm0 = 0;
if n0 > 2 {
n0 >>= 1;
lm0 -= 1;
} else if n0 <= 1 {
lm0 = i.min(1);
n0 <<= lm0;
}
let pcache = m.cache(lm0, j);
let mut mb = pcache[pcache[0] as usize] as i32 + 1;
let mut n = n0;
for k in 0..(i - lm0) {
mb <<= 1;
let offset = ((m.log_n[j] + ((lm0 + k) << BITRES)) >> 1) - QTHETA_OFFSET;
let num = 459 * ((2 * n - 1) * offset + mb);
let den = ((2 * n - 1) << 9) - 459;
let qb = ((num + (den >> 1)) / den).min(57);
mb += qb;
n <<= 1;
}
if c == 2 {
mb <<= 1;
let offset = ((m.log_n[j] + (i << BITRES)) >> 1) - if n == 2 { QTHETA_OFFSET_TWOPHASE } else { QTHETA_OFFSET };
let ndof = 2 * n - 1 - (n == 2) as i32;
let p = if n == 2 { 512 } else { 487 };
let num = p * (mb + ndof * offset);
let den = (ndof << 9) - p;
let qb = ((num + (den >> 1)) / den).min(if n == 2 { 64 } else { 61 });
mb += qb;
}
let ndof = c * n + if c == 2 && n > 2 { 1 } else { 0 };
let mut offset = ((m.log_n[j] + (i << BITRES)) >> 1) - FINE_OFFSET;
if n == 2 {
offset += (1 << BITRES) >> 2;
}
let num = mb + ndof * offset;
let den = (ndof - 1) << BITRES;
let qb = ((num + (den >> 1)) / den).min(MAX_FINE_BITS);
mb += (c * qb) << BITRES;
mb
};
let w = c * ((EBANDS[j + 1] - EBANDS[j]) << i);
let v = (4 * max_bits / w) - 64;
caps[((i * 2 + c - 1) as usize) * nb + j] = v.clamp(0, 255) as u8;
}
}
}
caps
}
/// Result of the allocation process.
pub struct Allocation {
pub coded_bands: usize,
pub intensity: usize,
pub dual_stereo: bool,
pub balance: i32,
pub pulses: [i32; NB_EBANDS],
pub fine_quant: [i32; NB_EBANDS],
pub fine_priority: [i32; NB_EBANDS],
}
/// Per-band limits, boosts and available budget for the allocation search.
pub struct AllocationInput<'a> {
pub offsets: &'a [i32; NB_EBANDS],
pub cap: &'a [i32; NB_EBANDS],
pub trim: i32,
pub bits: i32,
}
/// `clt_compute_allocation`, encoder side. Stereo frames are always coded as dual (L/R) stereo
/// with no intensity bands, and no band is skipped voluntarily.
pub fn compute_allocation(m: &Mode, frame: CeltFrame, input: AllocationInput<'_>, enc: &mut RangeEncoder) -> Allocation {
let CeltFrame { start, end, channels: c, lm } = frame;
let AllocationInput { offsets, cap, trim: alloc_trim, bits: total } = input;
let ci = c as i32;
let lmi = lm as i32;
let mut total = total.max(0);
let mut skip_start = start;
let skip_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 };
total -= skip_rsv;
let mut intensity_rsv = 0;
let mut dual_stereo_rsv = 0;
if c == 2 {
intensity_rsv = LOG2_FRAC_TABLE[end - start];
if intensity_rsv > total {
intensity_rsv = 0;
} else {
total -= intensity_rsv;
dual_stereo_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 };
total -= dual_stereo_rsv;
}
}
let mut bits1 = [0i32; NB_EBANDS];
let mut bits2 = [0i32; NB_EBANDS];
let mut thresh = [0i32; NB_EBANDS];
let mut trim_offset = [0i32; NB_EBANDS];
for j in start..end {
let w = EBANDS[j + 1] - EBANDS[j];
thresh[j] = (ci << BITRES).max(((3 * w) << lmi << BITRES) >> 4);
trim_offset[j] = (ci * w * (alloc_trim - 5 - lmi) * (end as i32 - j as i32 - 1) * (1 << (lmi + BITRES as i32))) >> 6;
if w << lmi == 1 {
trim_offset[j] -= ci << BITRES;
}
}
let nb_alloc = ALLOC.len() as i32;
let mut lo = 1;
let mut hi = nb_alloc - 1;
loop {
let mut done = false;
let mut psum = 0;
let mid = (lo + hi) >> 1;
for j in (start..end).rev() {
let n = EBANDS[j + 1] - EBANDS[j];
let mut bitsj = (ci * n * (ALLOC[mid as usize][j] as i32)) << lmi >> 2;
if bitsj > 0 {
bitsj = (bitsj + trim_offset[j]).max(0);
}
bitsj += offsets[j];
if bitsj >= thresh[j] || done {
done = true;
psum += bitsj.min(cap[j]);
} else if bitsj >= ci << BITRES {
psum += ci << BITRES;
}
}
if psum > total {
hi = mid - 1;
} else {
lo = mid + 1;
}
if lo > hi {
break;
}
}
hi = lo;
lo -= 1;
for j in start..end {
let n = EBANDS[j + 1] - EBANDS[j];
let mut bits1j = (ci * n * (ALLOC[lo as usize][j] as i32)) << lmi >> 2;
let mut bits2j = if hi >= nb_alloc { cap[j] } else { (ci * n * (ALLOC[hi as usize][j] as i32)) << lmi >> 2 };
if bits1j > 0 {
bits1j = (bits1j + trim_offset[j]).max(0);
}
if bits2j > 0 {
bits2j = (bits2j + trim_offset[j]).max(0);
}
if lo > 0 {
bits1j += offsets[j];
}
bits2j += offsets[j];
if offsets[j] > 0 {
skip_start = j;
}
bits2j = (bits2j - bits1j).max(0);
bits1[j] = bits1j;
bits2[j] = bits2j;
}
let interpolation =
AllocationInterpolation { skip_start, bits1: &bits1, bits2: &bits2, thresh: &thresh, cap, total, skip_rsv, intensity_rsv, dual_stereo_rsv };
interp_bits2pulses(m, frame, interpolation, enc)
}
/// Bracketing allocation vectors and signalling reserves for the interpolation stage.
struct AllocationInterpolation<'a> {
skip_start: usize,
bits1: &'a [i32; NB_EBANDS],
bits2: &'a [i32; NB_EBANDS],
thresh: &'a [i32; NB_EBANDS],
cap: &'a [i32; NB_EBANDS],
total: i32,
skip_rsv: i32,
intensity_rsv: i32,
dual_stereo_rsv: i32,
}
fn interp_bits2pulses(m: &Mode, frame: CeltFrame, interpolation: AllocationInterpolation<'_>, enc: &mut RangeEncoder) -> Allocation {
let CeltFrame { start, end, channels: c, lm } = frame;
let AllocationInterpolation { skip_start, bits1, bits2, thresh, cap, mut total, skip_rsv, mut intensity_rsv, mut dual_stereo_rsv } = interpolation;
let ci = c as i32;
let alloc_floor = ci << BITRES;
let stereo = (c > 1) as i32;
let log_m = (lm as i32) << BITRES;
let mut lo = 0;
let mut hi = 1 << ALLOC_STEPS;
for _ in 0..ALLOC_STEPS {
let mid = (lo + hi) >> 1;
let mut psum = 0;
let mut done = false;
for j in (start..end).rev() {
let tmp = bits1[j] + ((mid * bits2[j]) >> ALLOC_STEPS);
if tmp >= thresh[j] || done {
done = true;
psum += tmp.min(cap[j]);
} else if tmp >= alloc_floor {
psum += alloc_floor;
}
}
if psum > total {
hi = mid;
} else {
lo = mid;
}
}
let mut bits = [0i32; NB_EBANDS];
let mut psum = 0;
let mut done = false;
for j in (start..end).rev() {
let mut tmp = bits1[j] + ((lo * bits2[j]) >> ALLOC_STEPS);
if tmp < thresh[j] && !done {
tmp = if tmp >= alloc_floor { alloc_floor } else { 0 };
} else {
done = true;
}
tmp = tmp.min(cap[j]);
bits[j] = tmp;
psum += tmp;
}
let s = start;
let mut coded_bands = end;
loop {
let j = coded_bands - 1;
if j <= skip_start {
total += skip_rsv;
break;
}
let mut left = total - psum;
let span = EBANDS[coded_bands] - EBANDS[s];
let percoeff = left / span;
left -= span * percoeff;
let rem = (left - (EBANDS[j] - EBANDS[s])).max(0);
let band_width = EBANDS[coded_bands] - EBANDS[j];
let band_bits = bits[j] + percoeff * band_width + rem;
if band_bits >= thresh[j].max(alloc_floor + (1 << BITRES)) {
// Keep this band (and every band below it).
enc.bit_logp(true, 1);
break;
}
psum -= bits[j] + intensity_rsv;
if intensity_rsv > 0 {
intensity_rsv = LOG2_FRAC_TABLE[j - s];
}
psum += intensity_rsv;
if band_bits >= alloc_floor {
psum += alloc_floor;
bits[j] = alloc_floor;
} else {
bits[j] = 0;
}
coded_bands -= 1;
}
let intensity = if intensity_rsv > 0 {
// No intensity stereo: every coded band is coded in full.
enc.uint((coded_bands - s) as u32, (coded_bands + 1 - s) as u32);
coded_bands
} else {
0
};
if intensity <= s {
total += dual_stereo_rsv;
dual_stereo_rsv = 0;
}
let dual_stereo = if dual_stereo_rsv > 0 {
enc.bit_logp(true, 1);
true
} else {
false
};
let mut left = total - psum;
let span = EBANDS[coded_bands] - EBANDS[s];
let percoeff = left / span;
left -= span * percoeff;
for j in s..coded_bands {
bits[j] += percoeff * (EBANDS[j + 1] - EBANDS[j]);
}
for j in s..coded_bands {
let tmp = left.min(EBANDS[j + 1] - EBANDS[j]);
bits[j] += tmp;
left -= tmp;
}
let mut ebits = [0i32; NB_EBANDS];
let mut fine_priority = [0i32; NB_EBANDS];
let mut balance = 0;
for j in s..coded_bands {
let n0 = EBANDS[j + 1] - EBANDS[j];
let n = n0 << lm;
let bit = bits[j] + balance;
let mut excess;
if n > 1 {
excess = (bit - cap[j]).max(0);
bits[j] = bit - excess;
let den = ci * n + if c == 2 && n > 2 && !dual_stereo && j < intensity { 1 } else { 0 };
let nclogn = den * (m.log_n[j] + log_m);
let mut offset = (nclogn >> 1) - den * FINE_OFFSET;
if n == 2 {
offset += (den << BITRES) >> 2;
}
if bits[j] + offset < (den * 2) << BITRES {
offset += nclogn >> 2;
} else if bits[j] + offset < (den * 3) << BITRES {
offset += nclogn >> 3;
}
let mut eb = (bits[j] + offset + (den << (BITRES - 1))).max(0);
eb = (eb / den) >> BITRES;
if ci * eb > (bits[j] >> BITRES) {
eb = bits[j] >> stereo >> BITRES;
}
eb = eb.min(MAX_FINE_BITS);
ebits[j] = eb;
fine_priority[j] = (eb * (den << BITRES) >= bits[j] + offset) as i32;
bits[j] -= (ci * eb) << BITRES;
} else {
excess = (bit - (ci << BITRES)).max(0);
bits[j] = bit - excess;
ebits[j] = 0;
fine_priority[j] = 1;
}
if excess > 0 {
let extra_fine = (excess >> (stereo + BITRES as i32)).min(MAX_FINE_BITS - ebits[j]);
ebits[j] += extra_fine;
let extra_bits = (extra_fine * ci) << BITRES;
fine_priority[j] = (extra_bits >= excess - balance) as i32;
excess -= extra_bits;
}
balance = excess;
}
for j in coded_bands..end {
ebits[j] = bits[j] >> stereo >> BITRES;
bits[j] = 0;
fine_priority[j] = (ebits[j] < 1) as i32;
}
Allocation { coded_bands, intensity, dual_stereo, balance, pulses: bits, fine_quant: ebits, fine_priority }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn caps_match_decoder_reference() {
let m = Mode::new();
let first_rows: [u8; 42] = [
224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 160, 160, 185, 185, 185, 178, 178, 168, 134, 61, 37, 224, 224, 224, 224, 224, 224, 224, 224, 240,
240, 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40,
];
assert_eq!(&m.caps[..42], &first_rows[..]);
}
#[test]
fn pvq_counts() {
assert_eq!(pvq_v(5, 1), 10);
assert_eq!(pvq_v(1, 7), 2);
assert_eq!(pvq_v(2, 9), 36);
assert_eq!(pvq_v(3, 2), 18);
}
}
-160
View File
@@ -1,160 +0,0 @@
//! 48 kHz → 8/12/16 kHz decimator feeding the SILK layer (own design).
//!
//! A linear-phase low-pass FIR (Kaiser-windowed sinc at 48 kHz) evaluated only at the retained
//! output phases. Its group delay ([`delay`]) is a whole number of 48 kHz samples, chosen so
//! that the SILK path (this filter + the decoder's internal-rate delay and resampler, RFC 6716
//! Table 54) lands within a fraction of a sample of the CELT path's 120-sample MDCT overlap.
/// Group delay of the decimator to `fs_out` in 48 kHz samples (the filter has `2 * delay + 1`
/// taps).
pub fn delay(fs_out: u32) -> usize {
if fs_out == 8_000 { 88 } else { 83 }
}
/// Zeroth-order modified Bessel function of the first kind (power series).
fn bessel_i0(x: f64) -> f64 {
let mut sum = 1.0;
let mut term = 1.0;
let q = x * x / 4.0;
for k in 1..64 {
term *= q / (k * k) as f64;
sum += term;
if term < sum * 1e-17 {
break;
}
}
sum
}
/// Windowed-sinc low-pass with cutoff `fc` (Hz at 48 kHz), unit DC gain.
fn design(fc: f64, beta: f64, delay: usize) -> Vec<f32> {
let wc = 2.0 * std::f64::consts::PI * fc / 48_000.0;
let i0b = bessel_i0(beta);
let mut h: Vec<f64> = (0..2 * delay + 1)
.map(|k| {
let m = k as f64 - delay as f64;
let sinc = if m == 0.0 { wc / std::f64::consts::PI } else { (wc * m).sin() / (std::f64::consts::PI * m) };
let r = m / delay as f64;
sinc * bessel_i0(beta * (1.0 - r * r).max(0.0).sqrt()) / i0b
})
.collect();
let s: f64 = h.iter().sum();
for v in h.iter_mut() {
*v /= s;
}
h.iter().map(|&v| v as f32).collect()
}
/// Dot product with eight independent partial sums, which the compiler can vectorise (a single
/// running sum is bound by the latency of the float addition). The sums are combined pairwise.
#[inline]
fn dot8(a: &[f32], b: &[f32]) -> f32 {
let mut s = [0f32; 8];
let (ca, ra) = a.as_chunks::<8>();
let (cb, rb) = b.as_chunks::<8>();
for (x, y) in ca.iter().zip(cb) {
for l in 0..8 {
s[l] += x[l] * y[l];
}
}
let mut acc = ((s[0] + s[4]) + (s[1] + s[5])) + ((s[2] + s[6]) + (s[3] + s[7]));
for (x, y) in ra.iter().zip(rb) {
acc += x * y;
}
acc
}
/// Decimates one channel of 48 kHz audio by 3, 4 or 6.
#[derive(Clone)]
pub struct Downsampler {
factor: usize,
h: Vec<f32>,
taps: usize,
/// The last `taps - 1` input samples followed by the current frame.
buf: Vec<f32>,
}
impl Downsampler {
/// `fs_out` is 8000, 12000 or 16000.
pub fn new(fs_out: u32) -> Self {
let factor = (48_000 / fs_out) as usize;
// Cutoff at 95 % of the output Nyquist frequency (pass band to ~88 %, stop band from ~110 %).
let fc = 0.95 * fs_out as f64 / 2.0;
let d = delay(fs_out);
let taps = 2 * d + 1;
Downsampler { factor, h: design(fc, 6.5, d), taps, buf: vec![0.0; taps - 1] }
}
/// Decimates `input` (a multiple of the factor long), appending to `out`. Output sample `j`
/// is the filtered input at frame position `factor * j`, delayed by [`delay`].
pub fn process(&mut self, input: &[f32], out: &mut Vec<f32>) {
debug_assert!(input.len().is_multiple_of(self.factor));
let taps = self.taps;
self.buf.truncate(taps - 1);
self.buf.extend_from_slice(input);
for j in 0..input.len() / self.factor {
let t = taps - 1 + self.factor * j;
let win = &self.buf[t + 1 - taps..=t];
// h is symmetric, so the window can be taken in forward order.
out.push(dot8(win, &self.h));
}
let n = self.buf.len();
self.buf.copy_within(n - (taps - 1).., 0);
self.buf.truncate(taps - 1);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn tone(f: f64, n: usize) -> Vec<f32> {
(0..n).map(|i| (2.0 * std::f64::consts::PI * f * i as f64 / 48_000.0).sin() as f32).collect()
}
fn rms(x: &[f32]) -> f64 {
(x.iter().map(|&v| (v as f64).powi(2)).sum::<f64>() / x.len() as f64).sqrt()
}
#[test]
fn dot8_matches_sequential_sum() {
for len in [0usize, 1, 7, 8, 9, 63, 167, 251] {
let a: Vec<f32> = (0..len).map(|i| ((i * 37 % 101) as f32 - 50.0) / 50.0).collect();
let b: Vec<f32> = (0..len).map(|i| ((i * 53 % 89) as f32 - 44.0) / 44.0).collect();
let exact: f64 = a.iter().zip(&b).map(|(&x, &y)| x as f64 * y as f64).sum();
assert!((dot8(&a, &b) as f64 - exact).abs() < 1e-4, "len {len}");
}
}
#[test]
fn passes_band_and_rejects_aliases() {
for &(fs, pass, stop) in &[(16_000u32, 6_000.0, 8_600.0), (12_000, 4_500.0, 6_500.0), (8_000, 3_000.0, 4_500.0)] {
for &(f, want_pass) in &[(pass, true), (stop, false)] {
let mut d = Downsampler::new(fs);
let x = tone(f, 9600);
let mut y = Vec::new();
for c in x.chunks(960) {
d.process(c, &mut y);
}
let r = rms(&y[y.len() / 2..]) * std::f64::consts::SQRT_2;
if want_pass {
assert!((r - 1.0).abs() < 0.02, "{fs}: {f} Hz gain {r}");
} else {
assert!(r < 0.01, "{fs}: {f} Hz leaks {r}");
}
}
}
}
#[test]
fn delay_is_exact() {
let mut d = Downsampler::new(16_000);
let mut x = vec![0f32; 960];
x[300] = 1.0;
let mut y = Vec::new();
d.process(&x, &mut y);
// The impulse at 300 shows up centred at 48 kHz time 300 + 83 = 383 (output 127.67).
let peak = y.iter().enumerate().max_by(|a, b| a.1.total_cmp(b.1)).map(|(i, _)| i).unwrap_or(0);
assert!((peak as f64 - (300.0 + delay(16_000) as f64) / 3.0).abs() < 1.0, "peak at {peak}");
}
}
-858
View File
@@ -1,858 +0,0 @@
//! SILK layer encoder (RFC 6716 §4.2, encoder direction), 20 ms frames at 8, 12 or 16 kHz.
//!
//! Each frame is analysed (VAD, open-loop pitch, windowed-autocorrelation LPC, LTP codebook
//! search), its NLSFs are quantised, and the excitation is chosen sample by sample by running
//! the decoder's exact fixed-point reconstruction (LTP and LPC synthesis with the dequantised
//! Q12/Q14 coefficients, quantisation offsets and the pseudo-random sign sequence), so the
//! encoder always holds the decoder's state and there is no drift. A noise-feedback loop
//! shapes the coding noise with a bandwidth-expanded LPC envelope, and a per-frame search over
//! a gain offset hits the bit budget. Stereo input is coded as mid/side with the decoder's
//! stereo prediction (§4.2.8). No LBRR, no NLSF interpolation.
pub(crate) mod analysis;
pub(crate) mod lpc;
mod nlsf;
mod pulses;
#[allow(dead_code)]
pub(crate) mod tables;
use lpc::{MAX_LPC_ORDER, div32_varq, inverse32_varq, nlsf2a, rshift_round, sat16, smlawb, smulwb, smulww};
use tables::{
DELTA_GAIN, GAIN_MSB, LTP_FILTER, LTP_PERIODICITY, LTP_SCALE, LTP_TAPS_0, LTP_TAPS_1, LTP_TAPS_2, MID_ONLY, NLSF_EXT, NLSF_INTERP, NLSF_SEL_NBMB,
NLSF_SEL_WB, NLSF_STAGE1, NLSF_STAGE2, PITCH_CONTOUR, PITCH_HIGH, PITCH_LOW, STEREO_PRED_JOINT, STEREO_WEIGHTS_Q13, TYPE_ACTIVE, TYPE_INACTIVE, UNIFORM3,
UNIFORM4, UNIFORM5, UNIFORM8,
};
use crate::range::RangeEncoder;
const MAX_FRAME: usize = 320;
const LTP_ORDER: usize = 5;
/// Frame RMS (16-bit scale) below which a frame is coded as inactive (VAD off).
const VAD_RMS: f32 = 30.0;
/// Normalised pitch correlation above which a frame is a voicing candidate.
const VOICING_THRESHOLD: f64 = 0.45;
/// Minimum LTP prediction gain (energy ratio) for a frame to be coded as voiced.
const MIN_LTP_GAIN: f64 = 1.3;
/// Rate penalty (squared pulse units per unit magnitude) of the excitation quantiser.
const RD_LAMBDA: f64 = 0.15;
/// Extra gain offset (log2) for inactive frames.
const INACTIVE_OFFSET: f64 = 1.0;
/// Noise feedback: the coding noise is shaped by `1 / A(z/γ)` (A the short-term predictor).
/// γ = 0 leaves it white (closed-loop DPCM, best waveform SNR at high rates), γ = 1 shapes it
/// like the spectral envelope (open-loop residual coding, needed at low rates). γ grows with the
/// gain offset, i.e. as the quantisation gets coarser.
fn nf_gamma(offset: f64) -> f64 {
(0.75 + 0.05 * offset).clamp(0.75, 0.9)
}
/// `silk_log2lin` (decoder arithmetic).
fn log2lin(in_log_q7: i32) -> i32 {
if in_log_q7 < 0 {
return 0;
}
if in_log_q7 >= 3967 {
return i32::MAX;
}
let out = 1i32 << (in_log_q7 >> 7);
let frac = in_log_q7 & 0x7F;
if in_log_q7 < 2048 { out + ((out * smlawb(frac, frac * (128 - frac), -174)) >> 7) } else { out + (out >> 7) * smlawb(frac, frac * (128 - frac), -174) }
}
fn gain_q16(index: i32) -> i32 {
log2lin((smulwb(0x1D1C71, index) + 2090).min(3967))
}
/// The decoder's LPC analysis filter on 16-bit samples.
fn lpc_analysis_filter(out: &mut [i16], input: &[i16], b: &[i32], d: usize) {
let len = out.len();
for ix in d..len {
let mut acc: i32 = 0;
for j in 0..d {
acc = acc.wrapping_add((input[ix - 1 - j] as i32).wrapping_mul(b[j]));
}
let v = ((input[ix] as i32) << 12).wrapping_sub(acc);
out[ix] = sat16(rshift_round(v, 12)) as i16;
}
for v in out.iter_mut().take(d.min(len)) {
*v = 0;
}
}
/// The part of the decoder's channel state that the reconstruction depends on.
#[derive(Clone)]
struct DecState {
prev_gain_q16: i32,
s_lpc_q14: [i32; MAX_LPC_ORDER],
out_buf: [i16; MAX_FRAME],
last_gain_index: i32,
}
impl DecState {
fn new() -> Self {
DecState { prev_gain_q16: 65536, s_lpc_q14: [0; MAX_LPC_ORDER], out_buf: [0; MAX_FRAME], last_gain_index: 10 }
}
/// The decoder's reset of the side channel when it is coded again after mid-only frames.
fn reset_side(&mut self) {
self.out_buf = [0; MAX_FRAME];
self.s_lpc_q14 = [0; MAX_LPC_ORDER];
self.last_gain_index = 10;
}
}
/// Coded parameters of one frame (everything except gains and pulses).
#[derive(Clone, Default)]
struct Params {
vad: bool,
signal_type: usize,
quant_offset_type: usize,
nlsf_ix: [i32; 17],
a_q12: [i32; MAX_LPC_ORDER],
lag_index: i32,
contour: usize,
per_index: usize,
ltp_index: [usize; 4],
pitch_l: [i32; 4],
/// RMS (16-bit scale) of the open-loop prediction residual per subframe.
res_rms: [f32; 4],
}
/// Gains, seed and pulses chosen by the quantiser.
struct Coded {
gain_ix: [i32; 4],
gains_q16: [i32; 4],
seed: usize,
pulses: Vec<i32>,
}
/// Noise-feedback history (decoded − input), most recent first, as a double-length ring: each
/// sample is stored at `pos` and `pos + MAX_LPC_ORDER`, so `buf[pos..pos + MAX_LPC_ORDER]` is
/// always the contiguous window and a push costs two stores instead of a shift.
struct NfRing {
buf: [f64; 2 * MAX_LPC_ORDER],
/// Index of the most recent sample, always below `MAX_LPC_ORDER`.
pos: usize,
}
impl NfRing {
fn new(hist: &[f64; MAX_LPC_ORDER]) -> Self {
let mut buf = [0.0; 2 * MAX_LPC_ORDER];
buf[..MAX_LPC_ORDER].copy_from_slice(hist);
buf[MAX_LPC_ORDER..].copy_from_slice(hist);
NfRing { buf, pos: 0 }
}
/// The history, most recent first.
fn window(&self) -> &[f64] {
self.buf.get(self.pos..self.pos + MAX_LPC_ORDER).unwrap_or(&[])
}
/// Makes `v` the most recent sample and drops the oldest.
fn push(&mut self, v: f64) {
self.pos = self.pos.checked_sub(1).unwrap_or(MAX_LPC_ORDER - 1);
for i in [self.pos, self.pos + MAX_LPC_ORDER] {
if let Some(slot) = self.buf.get_mut(i) {
*slot = v;
}
}
}
/// The history as a plain array, most recent first.
fn to_array(&self) -> [f64; MAX_LPC_ORDER] {
let mut out = [0.0; MAX_LPC_ORDER];
for (o, v) in out.iter_mut().zip(self.window()) {
*o = *v;
}
out
}
}
#[derive(Clone)]
struct Channel {
fs_khz: usize,
len: usize,
sub: usize,
order: usize,
dec: DecState,
/// The last two frames of input (internal rate, 16-bit scale).
hist: Vec<f32>,
/// Recent output errors (decoded − input), most recent first, for noise feedback.
nf: [f64; MAX_LPC_ORDER],
}
impl Channel {
fn new(fs_khz: usize) -> Self {
let len = 20 * fs_khz;
Channel {
fs_khz,
len,
sub: 5 * fs_khz,
order: if fs_khz == 16 { 16 } else { 10 },
dec: DecState::new(),
hist: vec![0.0; 2 * len],
nf: [0.0; MAX_LPC_ORDER],
}
}
fn push_history(&mut self, x: &[f32]) {
let l = self.len;
self.hist.copy_within(l.., 0);
self.hist[l..].copy_from_slice(x);
}
/// Frame analysis: VAD, signal type, pitch, LPC/NLSF quantisation and LTP taps.
fn analyze(&self, x: &[f32]) -> Params {
let l = self.len;
let sub = self.sub;
let order = self.order;
let wb = order == 16;
let mut buf = self.hist.clone();
buf.extend_from_slice(x);
let start = 2 * l;
let rms = (x.iter().map(|v| v * v).sum::<f32>() / l as f32).sqrt();
let vad = rms > VAD_RMS;
let pitch = if vad { Some(analysis::pitch_search(&buf, start, l, self.fs_khz)) } else { None };
let mut voiced = pitch.as_ref().is_some_and(|p| p.score > VOICING_THRESHOLD);
let a = analysis::lpc(&buf[start - l / 4..], order, self.fs_khz);
let target: Vec<f64> = match analysis::a2nlsf(&a) {
Some(w) => w.iter().map(|v| v / std::f64::consts::PI * 32768.0).collect(),
None => (1..=order).map(|k| k as f64 * 32768.0 / (order + 1) as f64).collect(),
};
let quant = |signal_type: usize| {
let (ix, nlsf) = nlsf::quantize(&target, signal_type, wb);
(ix, nlsf2a(&nlsf[..order]))
};
let mut p = Params { vad, ..Default::default() };
let (mut ix, mut a_q12) = quant(if voiced { 2 } else { 1 });
// Open-loop residual with the quantised predictor.
let mut r = vec![0f64; buf.len()];
for n in order..buf.len() {
let mut acc = buf[n] as f64;
for j in 0..order {
acc -= a_q12[j] as f64 / 4096.0 * buf[n - 1 - j] as f64;
}
r[n] = acc;
}
if voiced && let Some(pitch) = &pitch {
let ltp = analysis::ltp_search(&r, start, sub, &pitch.lags);
let gain = ltp.energy.iter().sum::<f64>() / ltp.err.iter().sum::<f64>().max(1e-9);
if gain >= MIN_LTP_GAIN {
p.lag_index = pitch.lag_index;
p.contour = pitch.contour;
p.pitch_l = pitch.lags;
p.per_index = ltp.per_index;
p.ltp_index = ltp.ltp_index;
} else {
voiced = false;
(ix, a_q12) = quant(1);
}
}
// Gains follow the short-term residual level (the long-term predictor then only
// reduces the number of pulses), which keeps the noise-to-signal ratio steady.
for k in 0..4 {
let e: f64 = r[start + k * sub..start + (k + 1) * sub].iter().map(|v| v * v).sum();
p.res_rms[k] = (e / sub as f64).sqrt() as f32;
}
p.signal_type = if !vad {
0
} else if voiced {
2
} else {
1
};
p.quant_offset_type = 0;
p.nlsf_ix = ix;
p.a_q12 = a_q12;
p
}
/// Chooses gains and pulses for target `x` by running the decoder's reconstruction, and
/// advances the mirrored decoder state. `offset` scales the gains (log2), `floor` gives
/// minimum gains (Q16) per subframe. Returns the coded values and the decoded frame.
fn quantize(&mut self, p: &Params, x: &[f32], offset: f64, floor: Option<&[i32; 4]>, seed: usize) -> (Coded, Vec<i16>) {
let l = self.len;
let sub = self.sub;
let order = self.order;
let st = &mut self.dec;
let mut ring = NfRing::new(&self.nf);
let off = if p.signal_type == 0 { offset + INACTIVE_OFFSET } else { offset };
let gamma = nf_gamma(off);
let mut nf_c = [0f64; MAX_LPC_ORDER];
let mut gpow = 1.0;
for j in 0..order {
gpow *= gamma;
nf_c[j] = p.a_q12[j] as f64 / 4096.0 * gpow;
}
// Gains (independent coding of the first subframe, deltas for the rest).
let mut gain_ix = [0i32; 4];
let mut gains_q16 = [0i32; 4];
let mut prev = st.last_gain_index;
for k in 0..4 {
let mut g = p.res_rms[k] as f64 * off.exp2();
if let Some(f) = floor {
g = g.max(f[k] as f64 / 65536.0);
}
let want = (((g * 65536.0).max(1.0).log2() * 128.0 - 2090.0) / (1_907_825.0 / 65536.0)).round().clamp(0.0, 63.0) as i32;
if k == 0 {
gain_ix[0] = want;
prev = want.max(prev - 16).clamp(0, 63);
} else {
let mut best = (i32::MAX, 0i32, prev);
for ind in 0..41 {
let tmp = ind - 4;
let thr = 2 * 36 - 64 + prev;
let d = if tmp > thr { prev + (tmp << 1) - thr } else { prev + tmp }.clamp(0, 63);
let e = (d - want).abs();
if e < best.0 {
best = (e, ind, d);
}
}
gain_ix[k] = best.1;
prev = best.2;
}
gains_q16[k] = gain_q16(prev);
}
st.last_gain_index = prev;
// Analysis-by-synthesis excitation quantisation through the decoder's exact synthesis.
let voiced = p.signal_type == 2;
let offset_q10 = [[100, 240], [32, 100]][p.signal_type >> 1][p.quant_offset_type];
let off16 = offset_q10 << 4;
let mut seed_v = seed as i32;
let mut pulses = vec![0i32; l];
let mut xq = vec![0i16; l];
let mut s_lpc = [0i32; MAX_FRAME / 4 + MAX_LPC_ORDER];
s_lpc[..MAX_LPC_ORDER].copy_from_slice(&st.s_lpc_q14);
let mut s_ltp = [0i16; MAX_FRAME];
let mut s_ltp_q15 = [0i32; 2 * MAX_FRAME];
let ltp_mem = l;
let mut ltp_buf_idx = ltp_mem;
let a_q12 = &p.a_q12;
let ltp_scale_q14 = 15565;
for k in 0..4 {
let mut b_q14 = [0i32; LTP_ORDER];
if voiced {
let ix = p.ltp_index[k];
let taps = match p.per_index {
0 => LTP_TAPS_0[ix],
1 => LTP_TAPS_1[ix],
_ => LTP_TAPS_2[ix],
};
for i in 0..LTP_ORDER {
b_q14[i] = taps[i] << 7;
}
}
let gain_q10 = gains_q16[k] >> 6;
let mut inv_gain_q31 = inverse32_varq(gains_q16[k], 47);
let gain_adj_q16 = if gains_q16[k] != st.prev_gain_q16 {
let g = div32_varq(st.prev_gain_q16, gains_q16[k], 16);
for v in s_lpc[..MAX_LPC_ORDER].iter_mut() {
*v = smulww(g, *v);
}
g
} else {
1 << 16
};
st.prev_gain_q16 = gains_q16[k];
let lag = p.pitch_l[k];
if voiced {
let lag_u = lag as usize;
if k == 0 {
let start_idx = (ltp_mem as isize - lag as isize - order as isize - (LTP_ORDER / 2) as isize).max(0) as usize;
lpc_analysis_filter(&mut s_ltp[start_idx..ltp_mem], &st.out_buf[start_idx..ltp_mem], a_q12, order);
inv_gain_q31 = smulwb(inv_gain_q31, ltp_scale_q14) << 2;
for i in 0..(lag_u + LTP_ORDER / 2).min(ltp_mem) {
s_ltp_q15[ltp_buf_idx - i - 1] = smulwb(inv_gain_q31, s_ltp[ltp_mem - i - 1] as i32);
}
} else if gain_adj_q16 != 1 << 16 {
for i in 0..(lag_u + LTP_ORDER / 2).min(ltp_buf_idx) {
let q = ltp_buf_idx - i - 1;
s_ltp_q15[q] = smulww(gain_adj_q16, s_ltp_q15[q]);
}
}
}
let inv_g = 16_777_216.0 / gain_q10.max(1) as f64;
for i in 0..sub {
let n = k * sub + i;
seed_v = seed_v.wrapping_mul(196_314_165).wrapping_add(907_633_515);
let ltp_q14 = if voiced {
let base = ltp_buf_idx as isize - lag as isize + (LTP_ORDER / 2) as isize;
let g = |o: isize| -> i32 { if base - o >= 0 { s_ltp_q15[(base - o) as usize] } else { 0 } };
let mut pred = 2i32;
for o in 0..LTP_ORDER {
pred = smlawb(pred, g(o as isize), b_q14[o]);
}
pred << 1
} else {
0
};
let mut lpc_pred = (order >> 1) as i32;
for j in 0..order {
lpc_pred = smlawb(lpc_pred, s_lpc[MAX_LPC_ORDER + i - 1 - j], a_q12[j]);
}
let lpc_q14 = lpc_pred.saturating_mul(16);
// Target excitation (Q14) for this sample.
let fb: f64 = nf_c.iter().zip(ring.window()).take(order).map(|(c, e)| c * e).sum();
let tgt = (x[n] as f64 + fb).clamp(-32768.0, 32767.0) * inv_g - lpc_q14 as f64 - ltp_q14 as f64;
let sgn = if seed_v < 0 { -1.0 } else { 1.0 };
let u = sgn * tgt;
let pf = (u - off16 as f64) / 16384.0;
let p0 = if pf > 0.0 { (pf + 0.078_125).round() } else { (pf - 0.078_125).round() } as i32;
let level = |q: i32| -> i32 {
let mut e = q << 14;
if e > 0 {
e -= 80 << 4;
} else if e < 0 {
e += 80 << 4;
}
e + off16
};
let mut best = (f64::MAX, 0i32);
for q in [p0 - 1, p0, p0 + 1, 0] {
let q = q.clamp(-pulses::MAX_PULSE, pulses::MAX_PULSE);
let d = (level(q) as f64 - u) / 16384.0;
let cost = d * d + RD_LAMBDA * q.abs() as f64;
if cost < best.0 {
best = (cost, q);
}
}
let q = best.1;
pulses[n] = q;
let mut e = level(q);
if seed_v < 0 {
e = -e;
}
seed_v = seed_v.wrapping_add(q);
let res = e.wrapping_add(ltp_q14);
if voiced {
s_ltp_q15[ltp_buf_idx] = res << 1;
ltp_buf_idx += 1;
}
let v = res.saturating_add(lpc_q14);
s_lpc[MAX_LPC_ORDER + i] = v;
xq[n] = sat16(rshift_round(smulww(v, gain_q10), 8)) as i16;
ring.push(xq[n] as f64 - x[n] as f64);
}
s_lpc.copy_within(sub..sub + MAX_LPC_ORDER, 0);
}
self.nf = ring.to_array();
st.s_lpc_q14.copy_from_slice(&s_lpc[..MAX_LPC_ORDER]);
st.out_buf[..l].copy_from_slice(&xq);
(Coded { gain_ix, gains_q16, seed, pulses }, xq)
}
/// Writes the frame's indices and pulses (`decode_indices` + `decode_pulses` order, coded
/// independently).
fn write(&self, enc: &mut RangeEncoder, p: &Params, c: &Coded) {
let ix = p.signal_type * 2 + p.quant_offset_type;
if p.vad {
enc.icdf(ix - 2, &TYPE_ACTIVE, 8);
} else {
enc.icdf(ix, &TYPE_INACTIVE, 8);
}
enc.icdf((c.gain_ix[0] >> 3) as usize, GAIN_MSB[p.signal_type], 8);
enc.icdf((c.gain_ix[0] & 7) as usize, &UNIFORM8, 8);
for k in 1..4 {
enc.icdf(c.gain_ix[k] as usize, &DELTA_GAIN, 8);
}
let wb = self.order == 16;
let i1 = p.nlsf_ix[0] as usize;
enc.icdf(i1, NLSF_STAGE1[(p.signal_type >> 1) + if wb { 2 } else { 0 }], 8);
for i in 0..self.order {
let cb = if wb { 8 + NLSF_SEL_WB[i1][i] as usize } else { NLSF_SEL_NBMB[i1][i] as usize };
let v = p.nlsf_ix[i + 1] + 4;
if v <= 0 {
enc.icdf(0, NLSF_STAGE2[cb], 8);
enc.icdf((-v) as usize, &NLSF_EXT, 8);
} else if v >= 8 {
enc.icdf(8, NLSF_STAGE2[cb], 8);
enc.icdf((v - 8) as usize, &NLSF_EXT, 8);
} else {
enc.icdf(v as usize, NLSF_STAGE2[cb], 8);
}
}
// No NLSF interpolation.
enc.icdf(4, &NLSF_INTERP, 8);
if p.signal_type == 2 {
let half = (self.fs_khz >> 1) as i32;
enc.icdf((p.lag_index / half) as usize, &PITCH_HIGH, 8);
let low = match self.fs_khz {
8 => PITCH_LOW[0],
12 => PITCH_LOW[1],
_ => PITCH_LOW[2],
};
enc.icdf((p.lag_index % half) as usize, low, 8);
enc.icdf(p.contour, if self.fs_khz == 8 { PITCH_CONTOUR[1] } else { PITCH_CONTOUR[3] }, 8);
enc.icdf(p.per_index, &LTP_PERIODICITY, 8);
for k in 0..4 {
enc.icdf(p.ltp_index[k], LTP_FILTER[p.per_index], 8);
}
// LTP scaling index 0 (15565 in Q14), matching `quantize`.
enc.icdf(0, &LTP_SCALE, 8);
}
enc.icdf(c.seed, &UNIFORM4, 8);
pulses::encode(enc, &c.pulses, p.signal_type, p.quant_offset_type);
}
}
/// Quantises a stereo prediction weight (Q13) to the decoder's grid (§4.2.7.1): returns the
/// (UNIFORM3, UNIFORM5, joint) indices and the reconstructed value.
fn quant_pred(w: f64) -> ([usize; 3], i32) {
let mut best = (f64::MAX, [0usize; 3], 0i32);
for i in 0..15 {
let low = STEREO_WEIGHTS_Q13[i];
let step = smulwb(STEREO_WEIGHTS_Q13[i + 1] - low, 6554);
for j in 0..5 {
let v = low + step * (2 * j as i32 + 1);
let e = (v as f64 - w).abs();
if e < best.0 {
best = (e, [i % 3, j, i / 3], v);
}
}
}
(best.1, best.2)
}
/// Per-frame preparation shared by every rate-loop trial.
struct Prep {
x: [Vec<f32>; 2],
params: [Params; 2],
mid_only: bool,
pred_ix: [[usize; 3]; 2],
pred_q13: [i32; 2],
}
/// One rate-loop trial: the frame coded at gain offset `offset`.
struct Trial {
offset: f64,
bits: i32,
state: SilkEncoder,
coder: RangeEncoder,
}
/// SILK encoder for one mono or stereo (mid/side) stream.
#[derive(Clone)]
pub struct SilkEncoder {
fs_khz: usize,
channels: usize,
ch: [Channel; 2],
pred_prev_q13: [i32; 2],
prev_mid_only: bool,
/// Last mid sample of the previous frame (for the stereo predictor's low-pass).
prev_mid: f32,
frame: usize,
/// Gain offset chosen for the previous frame (search starting point).
pub last_offset: f64,
}
impl SilkEncoder {
pub fn new(fs_khz: usize, channels: usize) -> Self {
SilkEncoder {
fs_khz,
channels,
ch: [Channel::new(fs_khz), Channel::new(fs_khz)],
pred_prev_q13: [0; 2],
prev_mid_only: false,
prev_mid: 0.0,
frame: 0,
last_offset: 1.0,
}
}
/// Samples per channel of one 20 ms frame at the internal rate.
pub fn frame_len(&self) -> usize {
20 * self.fs_khz
}
fn prepare(&self, input: &[Vec<f32>]) -> Prep {
let l = self.frame_len();
if self.channels == 1 {
let x = input[0].clone();
let p = self.ch[0].analyze(&x);
return Prep { x: [x, vec![0.0; l]], params: [p, Params::default()], mid_only: true, pred_ix: [[0; 3]; 2], pred_q13: [0; 2] };
}
let mid: Vec<f32> = (0..l).map(|i| 0.5 * (input[0][i] + input[1][i])).collect();
let side: Vec<f32> = (0..l).map(|i| 0.5 * (input[0][i] - input[1][i])).collect();
let lp = |j: usize| -> f64 {
let a = if j == 0 { self.prev_mid } else { mid[j - 1] };
let c = if j + 1 < l { mid[j + 1] } else { mid[l - 1] };
(a as f64 + 2.0 * mid[j] as f64 + c as f64) / 4.0
};
// Least-squares prediction of the side from the low-passed mid and the mid.
let (mut r00, mut r01, mut r11, mut c0, mut c1, mut ss, mut mm) = (0f64, 0f64, 0f64, 0f64, 0f64, 0f64, 0f64);
for j in 0..l {
let (a, b, s) = (lp(j), mid[j] as f64, side[j] as f64);
r00 += a * a;
r01 += a * b;
r11 += b * b;
c0 += a * s;
c1 += b * s;
ss += s * s;
mm += b * b;
}
// Ridge regularisation: the low-passed mid and the mid are nearly collinear.
let lam = 0.02 * 0.5 * (r00 + r11) + 1e-9;
let (a00, a11) = (r00 + lam, r11 + lam);
let det = a00 * a11 - r01 * r01;
let (p0, p1) = ((c0 * a11 - c1 * r01) / det, (c1 * a00 - c0 * r01) / det);
let lim = 13732.0;
let quant = |p0: f64, p1: f64| {
let (ix0, q0) = quant_pred(((p0 + p1) * 8192.0).clamp(-lim, lim));
let (ix1, q1) = quant_pred((p1 * 8192.0).clamp(-lim, lim));
([ix0, ix1], [q0 - q1, q1])
};
// Side residual with the decoder's interpolated predictor.
let fs = self.fs_khz as i32;
let denom_q16 = (1 << 16) / (8 * fs);
let interp = 8 * self.fs_khz;
let residual = |pred_q13: [i32; 2]| -> Vec<f32> {
let d0 = rshift_round((pred_q13[0] - self.pred_prev_q13[0]) as i16 as i32 * denom_q16 as i16 as i32, 16);
let d1 = rshift_round((pred_q13[1] - self.pred_prev_q13[1]) as i16 as i32 * denom_q16 as i16 as i32, 16);
let (mut pp0, mut pp1) = (self.pred_prev_q13[0] + d0, self.pred_prev_q13[1] + d1);
(0..l)
.map(|j| {
if j + 1 < interp {
pp0 += d0;
pp1 += d1;
} else {
pp0 = pred_q13[0];
pp1 = pred_q13[1];
}
(side[j] as f64 - (pp0 as f64 * lp(j) + pp1 as f64 * mid[j] as f64) / 8192.0) as f32
})
.collect()
};
let energy = |v: &[f32]| -> f64 { v.iter().map(|&x| (x as f64).powi(2)).sum() };
let (mut pred_ix, mut pred_q13) = quant(p0, p1);
let mut res = residual(pred_q13);
let mut res_e = energy(&res);
// Never predict worse than not predicting at all.
if res_e > ss {
(pred_ix, pred_q13) = quant(0.0, 0.0);
res = residual(pred_q13);
res_e = energy(&res);
}
let res_rms = (res_e / l as f64).sqrt();
let mid_rms = (mm / l as f64).sqrt();
let mid_only = res_rms < 0.01 * mid_rms + 1.0;
let pm = self.ch[0].analyze(&mid);
let ps = if mid_only { Params::default() } else { self.ch[1].analyze(&res) };
Prep { x: [mid, res], params: [pm, ps], mid_only, pred_ix, pred_q13 }
}
/// Codes one frame into `enc` with gain offset `offset`.
fn code(&mut self, prep: &Prep, offset: f64, enc: &mut RangeEncoder) {
let stereo = self.channels == 2;
let side_vad = stereo && !prep.mid_only && prep.params[1].vad;
enc.bit_logp(prep.params[0].vad, 1);
enc.bit_logp(false, 1);
if stereo {
enc.bit_logp(side_vad, 1);
enc.bit_logp(false, 1);
let [a, b] = prep.pred_ix;
enc.icdf(5 * a[2] + b[2], &STEREO_PRED_JOINT, 8);
enc.icdf(a[0], &UNIFORM3, 8);
enc.icdf(a[1], &UNIFORM5, 8);
enc.icdf(b[0], &UNIFORM3, 8);
enc.icdf(b[1], &UNIFORM5, 8);
if !side_vad {
enc.icdf(prep.mid_only as usize, &MID_ONLY, 8);
}
}
let seed = self.frame & 3;
let (cm, _) = self.ch[0].quantize(&prep.params[0], &prep.x[0], offset, None, seed);
self.ch[0].write(enc, &prep.params[0], &cm);
if stereo {
if !prep.mid_only {
if self.prev_mid_only {
self.ch[1].dec.reset_side();
}
let (cs, _) = self.ch[1].quantize(&prep.params[1], &prep.x[1], offset, Some(&cm.gains_q16), seed);
self.ch[1].write(enc, &prep.params[1], &cs);
}
self.pred_prev_q13 = prep.pred_q13;
self.prev_mid_only = prep.mid_only;
}
}
/// Encodes one 20 ms frame (`input`: one internal-rate, 16-bit-scale vector per channel)
/// into `enc`, aiming at `target_bits` total and never exceeding `max_bits`.
pub fn encode(&mut self, input: &[Vec<f32>], target_bits: f64, max_bits: i32, enc: &mut RangeEncoder) {
let prep = self.prepare(input);
let start_bits = enc.tell();
// Every trial codes the frame into a clone of the encoder and the range coder. The result
// is kept per offset: the hard-limit check below hits the memo for the chosen offset, and
// the final step adopts that trial instead of coding the frame once more.
let mut trials: Vec<Trial> = Vec::new();
let mut trial = |s: &Self, o: f64| -> i32 {
if let Some(t) = trials.iter().find(|t| t.offset == o) {
return t.bits;
}
let mut state = s.clone();
let mut coder = enc.clone();
state.code(&prep, o, &mut coder);
let bits = coder.tell() - start_bits;
trials.push(Trial { offset: o, bits, state, coder });
bits
};
// Bisection on the gain offset: the smallest offset (finest quantisation) within budget.
let target = target_bits.min(max_bits as f64);
// Bits are not strictly monotonic in the offset (very coarse quantisation feeds large
// errors back), so walk an integer grid from the previous frame's offset to find a
// bracket [infeasible, feasible], then bisect inside it.
const MIN_O: f64 = -3.0;
const MAX_O: f64 = 6.0;
let mut best_any = (i32::MAX, MAX_O);
let mut probe = |o: f64| -> bool {
let b = trial(self, o);
if b < best_any.0 {
best_any = (b, o);
}
b as f64 <= target
};
let g0 = self.last_offset.round().clamp(MIN_O, MAX_O);
let mut bracket = None;
if probe(g0) {
let mut hi = g0;
loop {
if hi - 1.0 < MIN_O {
bracket = Some((None, hi));
break;
}
if probe(hi - 1.0) {
hi -= 1.0;
} else {
bracket = Some((Some(hi - 1.0), hi));
break;
}
}
} else {
let mut lo = g0;
while lo + 1.0 <= MAX_O {
if probe(lo + 1.0) {
bracket = Some((Some(lo), lo + 1.0));
break;
}
lo += 1.0;
}
}
let mut o = match bracket {
Some((Some(mut lo), mut hi)) => {
for _ in 0..4 {
let m = 0.5 * (lo + hi);
if probe(m) {
hi = m;
} else {
lo = m;
}
}
hi
}
Some((None, hi)) => hi,
None => best_any.1,
};
// Hard limit (hybrid packets must leave room for CELT).
while trial(self, o) > max_bits && o < 12.0 {
o += 1.0;
}
match trials.iter().position(|t| t.offset == o) {
Some(i) => {
let t = trials.swap_remove(i);
*self = t.state;
*enc = t.coder;
}
None => self.code(&prep, o, enc),
}
self.end_frame(&prep, o);
}
/// Advances the input history and the frame counter after frame `prep` was coded at `o`.
fn end_frame(&mut self, prep: &Prep, o: f64) {
self.last_offset = o;
for ch in 0..self.channels {
self.ch[ch].push_history(&prep.x[ch]);
}
if self.channels == 2 {
self.prev_mid = prep.x[0][self.frame_len() - 1];
}
self.frame += 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Deterministic speech-like test input: two harmonics with a moving pitch, a syllable
/// envelope, noise and short silences, at the internal rate and 16-bit scale.
fn signal(len: usize, fs_khz: usize, seed: u32) -> Vec<f32> {
let mut s = seed;
let fs = (fs_khz * 1000) as f64;
(0..len)
.map(|i| {
s = s.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
let noise = (s >> 8) as f64 / (1u32 << 24) as f64 - 0.5;
let t = i as f64 / fs;
let f0 = 140.0 + 40.0 * (t * 1.3).sin();
let env = ((t * 4.0).sin().max(0.0)).powf(0.7);
let voiced = (std::f64::consts::TAU * f0 * t).sin() + 0.5 * (std::f64::consts::TAU * 2.0 * f0 * t).sin();
(8000.0 * env * voiced + 600.0 * noise) as f32
})
.collect()
}
#[test]
fn nf_ring_matches_a_shifted_history() {
let mut hist = [0f64; MAX_LPC_ORDER];
for (i, h) in hist.iter_mut().enumerate() {
*h = i as f64 * 0.5 - 3.0;
}
let mut ring = NfRing::new(&hist);
assert_eq!(ring.window(), &hist[..]);
for n in 0..100 {
let v = (n as f64 * 0.37).sin() * 100.0;
hist.copy_within(0..MAX_LPC_ORDER - 1, 1);
hist[0] = v;
ring.push(v);
assert_eq!(ring.window(), &hist[..], "after push {n}");
}
assert_eq!(ring.to_array(), hist);
}
/// The rate loop adopts the memoised trial for the chosen offset. A second encoder that codes
/// every frame afresh at that offset (what the loop did before the memo) must produce the
/// same bytes, frame after frame, so the adopted encoder and range-coder states are exact.
#[test]
fn adopting_the_memoised_trial_equals_coding_the_frame_again() {
for (fs_khz, channels, target_bits, max_bits) in
[(8, 1, 200.0, 1000), (12, 1, 320.0, 1000), (16, 1, 480.0, 1000), (16, 2, 640.0, 1000), (16, 1, 900.0, 400), (8, 2, 120.0, 160)]
{
let mut memo = SilkEncoder::new(fs_khz, channels);
let mut fresh = memo.clone();
let l = memo.frame_len();
let frames = 40;
let left = signal(l * frames, fs_khz, 7);
let right: Vec<f32> = signal(l * frames + 37, fs_khz, 11)[37..].iter().map(|v| v * 0.6).collect();
for f in 0..frames {
let mut input = vec![left[f * l..(f + 1) * l].to_vec()];
if channels == 2 {
input.push(right[f * l..(f + 1) * l].to_vec());
}
let mut enc = RangeEncoder::new();
memo.encode(&input, target_bits, max_bits, &mut enc);
let o = memo.last_offset;
let prep = fresh.prepare(&input);
let mut enc_fresh = RangeEncoder::new();
fresh.code(&prep, o, &mut enc_fresh);
fresh.end_frame(&prep, o);
assert_eq!(enc.tell(), enc_fresh.tell(), "{fs_khz} kHz {channels} ch frame {f}");
assert_eq!(enc.done(1275), enc_fresh.done(1275), "{fs_khz} kHz {channels} ch frame {f}");
for c in 0..channels {
assert_eq!(memo.ch[c].nf, fresh.ch[c].nf, "noise feedback, channel {c}, frame {f}");
assert_eq!(memo.ch[c].dec.s_lpc_q14, fresh.ch[c].dec.s_lpc_q14, "LPC state, channel {c}, frame {f}");
}
}
}
}
}
-418
View File
@@ -1,418 +0,0 @@
//! Encoder-side signal analysis for SILK (own design): windowed autocorrelation LPC, LPC → NLSF
//! conversion, open-loop pitch search and LTP codebook search.
use super::tables::{CB_NB_20, CB_WB_20, LTP_FILTER, LTP_PERIODICITY, LTP_TAPS_0, LTP_TAPS_1, LTP_TAPS_2};
/// Short-term prediction coefficients `a` (prediction `x[n] ≈ Σ a[j] x[n-1-j]`) from the
/// windowed segment `x`, with a Gaussian lag window and white-noise correction.
pub fn lpc(x: &[f32], order: usize, fs_khz: usize) -> Vec<f64> {
let n = x.len();
// Asymmetric window: sin² rise over the first half, flat, cos fall over the last 1/8.
let rise = n / 2;
let fall = n / 8;
let w = |i: usize| -> f64 {
if i < rise {
let s = (std::f64::consts::FRAC_PI_2 * (i as f64 + 0.5) / rise as f64).sin();
s * s
} else if i >= n - fall {
(std::f64::consts::FRAC_PI_2 * (i + fall - n) as f64 / fall as f64 + 0.0).cos()
} else {
1.0
}
};
let xw: Vec<f64> = x.iter().enumerate().map(|(i, &v)| v as f64 * w(i)).collect();
let mut r = vec![0f64; order + 1];
for (k, rk) in r.iter_mut().enumerate() {
*rk = xw[k..].iter().zip(&xw).map(|(a, b)| a * b).sum();
}
if r[0] <= 1e-6 {
return vec![0.0; order];
}
// White-noise correction (-40 dB) and a 50 Hz Gaussian lag window.
r[0] *= 1.0 + 1e-4;
let f0 = 2.0 * std::f64::consts::PI * 50.0 / (fs_khz as f64 * 1000.0);
for (k, rk) in r.iter_mut().enumerate().skip(1) {
*rk *= (-0.5 * (f0 * k as f64).powi(2)).exp();
}
let mut a = levinson(&r, order);
// Mild bandwidth expansion keeps the NLSFs apart.
let mut g = 1.0;
for v in a.iter_mut() {
g *= 0.996;
*v *= g;
}
a
}
/// Levinson-Durbin recursion on autocorrelation `r` (predictor convention as in [`lpc`]).
pub fn levinson(r: &[f64], order: usize) -> Vec<f64> {
let mut a = vec![0f64; order];
let mut err = r[0];
let mut tmp = vec![0f64; order];
for i in 0..order {
let mut acc = r[i + 1];
for j in 0..i {
acc -= a[j] * r[i - j];
}
let k = (acc / err).clamp(-0.9999, 0.9999);
tmp[..i].copy_from_slice(&a[..i]);
for j in 0..i {
a[j] = tmp[j] - k * tmp[i - 1 - j];
}
a[i] = k;
err *= 1.0 - k * k;
if err <= 0.0 {
break;
}
}
a
}
/// Evaluates the symmetric polynomial `p` (length `d + 1`, `d` even) as the cosine series
/// `e^{jωd/2} P(e^{jω})`, with `cos_m(m)` giving `cos(m ω)`.
#[inline]
fn cos_series_with(p: &[f64], cos_m: impl Fn(usize) -> f64) -> f64 {
let h = (p.len() - 1) / 2;
let mut s = p[h];
for k in 0..h {
s += 2.0 * p[k] * cos_m(h - k);
}
s
}
fn cos_series(p: &[f64], w: f64) -> f64 {
cos_series_with(p, |m| (m as f64 * w).cos())
}
/// Number of grid intervals the root search scans over `[0, π]`.
const GRID: usize = 1024;
/// Largest multiple of ω in the series (half the LPC order, at most 16 / 2).
const MAX_H: usize = 8;
/// Bisection steps per root: the bracket shrinks to 3e-9 rad, far below one Q15 NLSF step (9.6e-5 rad).
const BISECT_STEPS: usize = 20;
/// `cos(m · π g / GRID)` for every grid point `g` and `m` in `0..=MAX_H`, computed with the same
/// expression as [`cos_series`], so the table values are bit-identical to calling `cos` there.
fn grid_cos() -> &'static [[f64; MAX_H + 1]] {
static TABLE: std::sync::OnceLock<Vec<[f64; MAX_H + 1]>> = std::sync::OnceLock::new();
TABLE.get_or_init(|| {
(0..=GRID)
.map(|g| {
let w = std::f64::consts::PI * g as f64 / GRID as f64;
std::array::from_fn(|m| (m as f64 * w).cos())
})
.collect()
})
}
fn roots(p: &[f64], out: &mut Vec<f64>) {
let grid = grid_cos();
let at_grid = |g: usize| cos_series_with(p, |m| grid[g][m]);
let mut w0 = 0.0;
let mut f0 = at_grid(0);
for g in 1..=GRID {
let w1 = std::f64::consts::PI * g as f64 / GRID as f64;
let f1 = at_grid(g);
if f0 == 0.0 {
out.push(w0);
} else if f0 * f1 < 0.0 {
let (mut lo, mut hi, mut flo) = (w0, w1, f0);
for _ in 0..BISECT_STEPS {
let mid = 0.5 * (lo + hi);
let fm = cos_series(p, mid);
if fm * flo <= 0.0 {
hi = mid;
} else {
lo = mid;
flo = fm;
}
}
out.push(0.5 * (lo + hi));
}
w0 = w1;
f0 = f1;
}
}
/// LPC → normalised line spectral frequencies in radians (ascending, `order` values), or `None`
/// when the root search fails (unstable or degenerate filter).
pub fn a2nlsf(a: &[f64]) -> Option<Vec<f64>> {
let d = a.len();
if d > 2 * MAX_H {
return None;
}
let mut c = vec![0f64; d + 2];
c[0] = 1.0;
for k in 0..d {
c[k + 1] = -a[k];
}
let mut p = vec![0f64; d + 2];
let mut q = vec![0f64; d + 2];
for k in 0..d + 2 {
p[k] = c[k] + c[d + 1 - k];
q[k] = c[k] - c[d + 1 - k];
}
// Remove the trivial roots at z = -1 (P) and z = +1 (Q).
let mut p1 = vec![0f64; d + 1];
let mut q1 = vec![0f64; d + 1];
p1[0] = p[0];
q1[0] = q[0];
for k in 1..=d {
p1[k] = p[k] - p1[k - 1];
q1[k] = q[k] + q1[k - 1];
}
let mut r = Vec::with_capacity(d);
roots(&p1, &mut r);
roots(&q1, &mut r);
if r.len() != d {
return None;
}
r.sort_by(f64::total_cmp);
Some(r)
}
/// Normalised cross-correlation of `x[n]` and `x[n - lag]` over `n` in `range`.
fn ncorr(x: &[f32], range: std::ops::Range<usize>, lag: usize) -> f64 {
let (mut xy, mut xx, mut yy) = (0f64, 0f64, 0f64);
for n in range {
let a = x[n] as f64;
let b = x[n - lag] as f64;
xy += a * b;
xx += a * a;
yy += b * b;
}
if xy <= 0.0 { 0.0 } else { xy / (xx * yy + 1e-9).sqrt() }
}
pub struct Pitch {
pub lag_index: i32,
pub contour: usize,
pub lags: [i32; 4],
pub score: f64,
}
/// Open-loop pitch search on `x` (history followed by the frame starting at `start`, `len`
/// samples, four subframes) at `fs_khz`.
pub fn pitch_search(x: &[f32], start: usize, len: usize, fs_khz: usize) -> Pitch {
let fs = fs_khz as i32;
let min_lag = 2 * fs;
let max_lag = 18 * fs - 1;
let sub = len / 4;
let nl = (max_lag - min_lag + 1) as usize;
// Whole-frame score per lag.
let full: Vec<f64> = (0..nl).map(|i| ncorr(x, start..start + len, min_lag as usize + i)).collect();
let mut best = 0usize;
for i in 0..nl {
// Slight preference for shorter lags.
if full[i] * (1.0 - 0.0004 * i as f64) > full[best] * (1.0 - 0.0004 * best as f64) {
best = i;
}
}
let mut lag = min_lag + best as i32;
// Prefer a sub-multiple when it explains the signal almost as well (octave errors).
for div in [4, 3, 2] {
let cand = (lag as f64 / div as f64).round() as i32;
let mut cbest = (0.0, 0);
for c in cand - 1..=cand + 1 {
if c >= min_lag && c <= max_lag {
let s = full[(c - min_lag) as usize];
if s > cbest.0 {
cbest = (s, c);
}
}
}
if cbest.1 != 0 && cbest.0 > 0.9 * full[(lag - min_lag) as usize] {
lag = cbest.1;
break;
}
}
let score = full[(lag - min_lag) as usize];
// Contour: per-subframe lag offsets around the chosen lag.
let cb: &[[i32; 4]] = if fs_khz == 8 { &CB_NB_20 } else { &CB_WB_20 };
// The contour search revisits the same (subframe, lag) pairs for neighbouring base lags and
// codebook entries, so each correlation is computed once.
let span = (18 * fs - min_lag + 1) as usize;
let memo = std::cell::RefCell::new(vec![f64::NAN; 4 * span]);
let sub_score = |k: usize, l: i32| -> f64 {
let l = l.clamp(min_lag, 18 * fs);
let slot = k * span + (l - min_lag) as usize;
let cached = memo.borrow()[slot];
if !cached.is_nan() {
return cached;
}
let v = ncorr(x, start + k * sub..start + (k + 1) * sub, l as usize);
memo.borrow_mut()[slot] = v;
v
};
let mut best = (f64::MIN, lag, 0usize);
for l0 in (lag - 2).max(min_lag)..=(lag + 2).min(max_lag) {
for (ci, offs) in cb.iter().enumerate() {
let s: f64 = (0..4).map(|k| sub_score(k, l0 + offs[k])).sum::<f64>() - 0.002 * ci as f64;
if s > best.0 {
best = (s, l0, ci);
}
}
}
let (_, l0, ci) = best;
let mut lags = [0i32; 4];
for k in 0..4 {
lags[k] = (l0 + cb[ci][k]).clamp(min_lag, 18 * fs);
}
Pitch { lag_index: l0 - min_lag, contour: ci, lags, score }
}
/// Result of the LTP codebook search.
pub struct Ltp {
pub per_index: usize,
pub ltp_index: [usize; 4],
/// Residual energy after LTP prediction / before, per subframe.
pub err: [f64; 4],
pub energy: [f64; 4],
}
fn icdf_bits(icdf: &[u8], s: usize) -> f64 {
let hi = if s == 0 { 256 } else { icdf[s - 1] as u32 };
let p = (hi - icdf[s] as u32).max(1) as f64 / 256.0;
-p.log2()
}
/// Searches the three LTP codebooks (RFC 6716 Tables 39–42) for the taps that best predict the
/// residual `r` (history + frame starting at `start`) from its past at the per-subframe `lags`.
pub fn ltp_search(r: &[f64], start: usize, sub: usize, lags: &[i32; 4]) -> Ltp {
let mut rr = [[0f64; 25]; 4];
let mut cv = [[0f64; 5]; 4];
let mut e = [0f64; 4];
for k in 0..4 {
let s0 = start + k * sub;
let lag = lags[k] as usize;
for n in s0..s0 + sub {
let t = r[n];
e[k] += t * t;
let base = n + 2 - lag;
for i in 0..5 {
let vi = r[base - i];
cv[k][i] += t * vi;
for j in 0..5 {
rr[k][i * 5 + j] += vi * r[base - j];
}
}
}
}
let books: [&[[i32; 5]]; 3] = [&LTP_TAPS_0, &LTP_TAPS_1, &LTP_TAPS_2];
let mut best = (f64::MAX, 0usize, [0usize; 4], [0f64; 4]);
for (p, book) in books.iter().enumerate() {
let mut total = icdf_bits(&LTP_PERIODICITY, p) * 1e-3 * e.iter().sum::<f64>() / 4.0;
let mut idx = [0usize; 4];
let mut errs = [0f64; 4];
for k in 0..4 {
let mut bk = (f64::MAX, 0usize, 0f64);
for (i, taps) in book.iter().enumerate() {
let b: [f64; 5] = std::array::from_fn(|j| taps[j] as f64 / 128.0);
let mut err = e[k];
for a in 0..5 {
err -= 2.0 * b[a] * cv[k][a];
for c in 0..5 {
err += b[a] * b[c] * rr[k][a * 5 + c];
}
}
let cost = err + icdf_bits(LTP_FILTER[p], i) * 1e-3 * e[k];
if cost < bk.0 {
bk = (cost, i, err.max(0.0));
}
}
total += bk.0;
idx[k] = bk.1;
errs[k] = bk.2;
}
if total < best.0 {
best = (total, p, idx, errs);
}
}
Ltp { per_index: best.1, ltp_index: best.2, err: best.3, energy: e }
}
#[cfg(test)]
mod tests {
use super::super::lpc::nlsf2a;
use super::*;
#[test]
fn nlsf_roundtrip_through_decoder_conversion() {
// A resonant 10th/16th-order predictor from a synthetic signal.
for &(order, fs) in &[(10usize, 8usize), (16, 16)] {
let mut st = [0f64; 4];
let mut seed = 1u32;
let x: Vec<f32> = (0..400)
.map(|_| {
seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
let w = (seed >> 9) as f64 / (1u32 << 23) as f64 - 1.0;
let y = w + 1.6 * st[0] - 0.9 * st[1] + 0.3 * st[2] - 0.2 * st[3];
st = [y, st[0], st[1], st[2]];
(y * 1000.0) as f32
})
.collect();
let a = lpc(&x, order, fs);
let nlsf = a2nlsf(&a).expect("roots");
let q15: Vec<i32> = nlsf.iter().map(|w| (w / std::f64::consts::PI * 32768.0).round() as i32).collect();
let a_q12 = nlsf2a(&q15);
for j in 0..order {
assert!((a_q12[j] as f64 / 4096.0 - a[j]).abs() < 0.02, "order {order} coef {j}: {} vs {}", a_q12[j] as f64 / 4096.0, a[j]);
}
}
}
/// The root search as it was before the grid table: `cos` called for every evaluation.
fn roots_reference(p: &[f64], out: &mut Vec<f64>) {
let mut w0 = 0.0;
let mut f0 = cos_series(p, w0);
for g in 1..=GRID {
let w1 = std::f64::consts::PI * g as f64 / GRID as f64;
let f1 = cos_series(p, w1);
if f0 == 0.0 {
out.push(w0);
} else if f0 * f1 < 0.0 {
let (mut lo, mut hi, mut flo) = (w0, w1, f0);
for _ in 0..BISECT_STEPS {
let mid = 0.5 * (lo + hi);
let fm = cos_series(p, mid);
if fm * flo <= 0.0 {
hi = mid;
} else {
lo = mid;
flo = fm;
}
}
out.push(0.5 * (lo + hi));
}
w0 = w1;
f0 = f1;
}
}
#[test]
fn grid_table_roots_are_bit_identical_to_direct_cos() {
let mut seed = 7u64;
let mut rnd = move || {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
(seed >> 11) as f64 / (1u64 << 53) as f64 * 2.0 - 1.0
};
let mut found = 0;
for len in [11usize, 17] {
for _ in 0..300 {
let mut p = vec![0f64; len];
for k in 0..len / 2 + 1 {
p[k] = rnd();
p[len - 1 - k] = p[k];
}
let (mut a, mut b) = (Vec::new(), Vec::new());
roots(&p, &mut a);
roots_reference(&p, &mut b);
assert_eq!(a.iter().map(|v| v.to_bits()).collect::<Vec<_>>(), b.iter().map(|v| v.to_bits()).collect::<Vec<_>>());
found += a.len();
}
}
assert!(found > 1000, "{found}");
}
}
-370
View File
@@ -1,370 +0,0 @@
//! Fixed-point NLSF decoding, NLSF→LPC conversion and filter stabilisation
//! (RFC 6716 §4.2.7.5, with the RFC 8251 §6/§7 overflow fixes).
//!
//! This is the decoder's normative arithmetic, mirrored from FilmCraft's first-party,
//! spec-derived `filmcraft-opus` decoder (MIT OR Apache-2.0): the encoder must reconstruct the
//! quantised NLSFs and LPC coefficients bit-exactly as the decoder does.
use super::tables::{
COS_Q12, LSF_ORDER_NBMB, LSF_ORDER_WB, NLSF_CB_NBMB, NLSF_CB_WB, NLSF_MIN_SPACING_NBMB, NLSF_MIN_SPACING_WB, NLSF_PRED_NBMB, NLSF_PRED_WB, NLSF_PSEL_NBMB,
NLSF_PSEL_WB,
};
pub const MAX_LPC_ORDER: usize = 16;
#[inline]
pub fn smulwb(a: i32, b: i32) -> i32 {
((a as i64 * (b as i16) as i64) >> 16) as i32
}
#[inline]
pub fn smlawb(a: i32, b: i32, c: i32) -> i32 {
a.wrapping_add(smulwb(b, c))
}
#[inline]
pub fn smulww(a: i32, b: i32) -> i32 {
((a as i64 * b as i64) >> 16) as i32
}
#[inline]
pub fn smmul(a: i32, b: i32) -> i32 {
((a as i64 * b as i64) >> 32) as i32
}
#[inline]
pub fn rshift_round(a: i32, s: u32) -> i32 {
if s == 1 { (a >> 1) + (a & 1) } else { ((a >> (s - 1)) + 1) >> 1 }
}
#[inline]
pub fn rshift_round64(a: i64, s: u32) -> i64 {
if s == 1 { (a >> 1) + (a & 1) } else { ((a >> (s - 1)) + 1) >> 1 }
}
#[inline]
pub fn sat16(a: i32) -> i32 {
a.clamp(-32768, 32767)
}
/// `silk_INVERSE32_varQ`: approximation of `(1 << q_res) / b32`.
pub fn inverse32_varq(b32: i32, q_res: i32) -> i32 {
let b_headrm = b32.unsigned_abs().leading_zeros() as i32 - 1;
let b32_nrm = b32.wrapping_shl(b_headrm as u32);
let b32_inv = (i32::MAX >> 2) / (b32_nrm >> 16);
let mut result = b32_inv << 16;
let err_q32 = ((1i32 << 29).wrapping_sub(smulwb(b32_nrm, b32_inv))).wrapping_shl(3);
result = result.wrapping_add(((err_q32 as i64 * b32_inv as i64) >> 16) as i32);
let lshift = 61 - b_headrm - q_res;
if lshift <= 0 {
let s = (-lshift) as u32;
let lim_hi = i32::MAX >> s;
let lim_lo = i32::MIN >> s;
result.clamp(lim_lo, lim_hi) << s
} else if lshift < 32 {
result >> lshift
} else {
0
}
}
/// `silk_DIV32_varQ`: approximation of `(a32 << q_res) / b32`.
pub fn div32_varq(a32: i32, b32: i32, q_res: i32) -> i32 {
let a_headrm = a32.unsigned_abs().leading_zeros() as i32 - 1;
let a32_nrm = a32.wrapping_shl(a_headrm as u32);
let b_headrm = b32.unsigned_abs().leading_zeros() as i32 - 1;
let b32_nrm = b32.wrapping_shl(b_headrm as u32);
let b32_inv = (i32::MAX >> 2) / (b32_nrm >> 16);
let mut result = smulwb(a32_nrm, b32_inv);
let a32_nrm = a32_nrm.wrapping_sub(((smmul(b32_nrm, result) as i64) << 3) as i32);
result = smlawb(result, a32_nrm, b32_inv);
let lshift = 29 + a_headrm - b_headrm - q_res;
if lshift < 0 {
let s = (-lshift) as u32;
let lim_hi = i32::MAX >> s;
let lim_lo = i32::MIN >> s;
result.clamp(lim_lo, lim_hi) << s
} else if lshift < 32 {
result >> lshift
} else {
0
}
}
pub fn sqrt_approx(x: i32) -> i32 {
if x <= 0 {
return 0;
}
let lz = x.leading_zeros() as i32;
let frac = (x.rotate_right((24 - lz).rem_euclid(32) as u32)) & 0x7f;
let mut y = if lz & 1 != 0 { 32768 } else { 46214 };
y >>= lz >> 1;
smlawb(y, y, 213 * frac)
}
/// Decodes the stage-1/stage-2 indices into stabilised NLSFs (Q15).
pub fn nlsf_decode(indices: &[i32], wb: bool) -> [i32; MAX_LPC_ORDER] {
let order = if wb { 16 } else { 10 };
let i1 = indices[0] as usize;
let cb1: &[i32] = if wb { &NLSF_CB_WB[i1] } else { &NLSF_CB_NBMB[i1] };
let qstep = if wb { 9830 } else { 11796 };
// Residual dequantisation with backwards prediction.
let mut res_q10 = [0i32; MAX_LPC_ORDER];
let mut out_q10 = 0i32;
for i in (0..order).rev() {
let pred = if i + 1 < order {
let sel = if wb { NLSF_PSEL_WB[i1][i] } else { NLSF_PSEL_NBMB[i1][i] } as usize;
let w = if wb { NLSF_PRED_WB[sel][i] } else { NLSF_PRED_NBMB[sel][i] };
(out_q10 * w) >> 8
} else {
0
};
let mut v = indices[i + 1] << 10;
if v > 0 {
v -= 102;
} else if v < 0 {
v += 102;
}
out_q10 = smlawb(pred, v, qstep);
res_q10[i] = out_q10;
}
// Laroia weights.
let mut nlsf = [0i32; MAX_LPC_ORDER];
for k in 0..order {
let prev = if k == 0 { 0 } else { cb1[k - 1] };
let next = if k + 1 == order { 256 } else { cb1[k + 1] };
let w = (1024 / (cb1[k] - prev).max(1) + 1024 / (next - cb1[k]).max(1)).min(32767);
let w_q9 = sqrt_approx(w << 16);
let v = (cb1[k] << 7) + (res_q10[k] << 14) / w_q9.max(1);
nlsf[k] = v.clamp(0, 32767);
}
let dmin: &[i32] = if wb { &NLSF_MIN_SPACING_WB } else { &NLSF_MIN_SPACING_NBMB };
nlsf_stabilize(&mut nlsf[..order], dmin);
nlsf
}
pub fn nlsf_stabilize(nlsf: &mut [i32], dmin: &[i32]) {
let l = nlsf.len();
for _ in 0..20 {
let mut min_diff = nlsf[0] - dmin[0];
let mut idx = 0;
for i in 1..l {
let d = nlsf[i] - (nlsf[i - 1] + dmin[i]);
if d < min_diff {
min_diff = d;
idx = i;
}
}
let d = 32768 - (nlsf[l - 1] + dmin[l]);
if d < min_diff {
min_diff = d;
idx = l;
}
if min_diff >= 0 {
return;
}
if idx == 0 {
nlsf[0] = dmin[0];
} else if idx == l {
nlsf[l - 1] = 32768 - dmin[l];
} else {
let mut min_center = dmin[..idx].iter().sum::<i32>();
min_center += dmin[idx] >> 1;
let mut max_center = 32768 - dmin[idx + 1..=l].iter().sum::<i32>();
max_center -= dmin[idx] >> 1;
let c = rshift_round(nlsf[idx - 1] + nlsf[idx], 1);
let c = if min_center > max_center { c.clamp(max_center, min_center) } else { c.clamp(min_center, max_center) };
nlsf[idx - 1] = c - (dmin[idx] >> 1);
nlsf[idx] = nlsf[idx - 1] + dmin[idx];
}
}
// Fallback.
nlsf.sort_unstable();
nlsf[0] = nlsf[0].max(dmin[0]);
for i in 1..l {
nlsf[i] = nlsf[i].max(sat16(nlsf[i - 1] + dmin[i]));
}
nlsf[l - 1] = nlsf[l - 1].min(32768 - dmin[l]);
for i in (0..l - 1).rev() {
nlsf[i] = nlsf[i].min(nlsf[i + 1] - dmin[i + 1]);
}
}
fn bwexpander_32(ar: &mut [i32], mut chirp_q16: i32) {
let d = ar.len();
let chirp_minus_one = chirp_q16 - 65536;
for v in ar.iter_mut().take(d - 1) {
*v = smulww(chirp_q16, *v);
chirp_q16 += rshift_round(chirp_q16.wrapping_mul(chirp_minus_one), 16);
}
ar[d - 1] = smulww(chirp_q16, ar[d - 1]);
}
fn lpc_inverse_pred_gain(a_q12: &[i32]) -> i32 {
const QA: u32 = 24;
const A_LIMIT: i32 = 16_773_022;
let order = a_q12.len();
let mut a = [0i32; MAX_LPC_ORDER];
let mut dc = 0;
for k in 0..order {
dc += a_q12[k];
a[k] = a_q12[k] << (QA - 12);
}
if dc >= 4096 {
return 0;
}
let mut inv_gain: i32 = 1 << 30;
let mul32_frac_q31 = |x: i32, y: i32| rshift_round64(x as i64 * y as i64, 31) as i32;
for k in (1..order).rev() {
if a[k] > A_LIMIT || a[k] < -A_LIMIT {
return 0;
}
let rc_q31 = -(a[k] << (31 - QA));
let rc_mult1 = (1i32 << 30) - smmul(rc_q31, rc_q31);
inv_gain = smmul(inv_gain, rc_mult1) << 2;
if inv_gain < 107_374 {
return 0;
}
let mult2q = 32 - rc_mult1.unsigned_abs().leading_zeros() as i32;
let rc_mult2 = inverse32_varq(rc_mult1, mult2q + 30);
for n in 0..(k + 1) >> 1 {
let t1 = a[n];
let t2 = a[k - n - 1];
let v1 = rshift_round64(t1.saturating_sub(mul32_frac_q31(t2, rc_q31)) as i64 * rc_mult2 as i64, mult2q as u32);
if v1 > i32::MAX as i64 || v1 < i32::MIN as i64 {
return 0;
}
let v2 = rshift_round64(t2.saturating_sub(mul32_frac_q31(t1, rc_q31)) as i64 * rc_mult2 as i64, mult2q as u32);
if v2 > i32::MAX as i64 || v2 < i32::MIN as i64 {
return 0;
}
a[n] = v1 as i32;
a[k - n - 1] = v2 as i32;
}
}
if a[0] > A_LIMIT || a[0] < -A_LIMIT {
return 0;
}
let rc_q31 = -(a[0] << (31 - QA));
let rc_mult1 = (1i32 << 30) - smmul(rc_q31, rc_q31);
inv_gain = smmul(inv_gain, rc_mult1) << 2;
if inv_gain < 107_374 {
return 0;
}
inv_gain
}
/// Converts NLSFs (Q15) to stable Q12 LPC coefficients (`silk_NLSF2A`).
pub fn nlsf2a(nlsf: &[i32]) -> [i32; MAX_LPC_ORDER] {
let d = nlsf.len();
let ordering: &[usize] = if d == 16 { &LSF_ORDER_WB } else { &LSF_ORDER_NBMB };
let mut c_q17 = [0i32; MAX_LPC_ORDER];
for k in 0..d {
let i = (nlsf[k] >> 8) as usize;
let f = nlsf[k] & 255;
let c0 = COS_Q12[i];
let c1 = COS_Q12[(i + 1).min(128)];
c_q17[ordering[k]] = (c0 * 256 + (c1 - c0) * f + 4) >> 3;
}
let d2 = d / 2;
let find_poly = |off: usize| -> [i64; 10] {
let mut out = [0i64; 10];
out[0] = 1 << 16;
out[1] = -(c_q17[off] as i64);
for k in 1..d2 {
let ftmp = c_q17[2 * k + off] as i64;
out[k + 1] = 2 * out[k - 1] - ((ftmp * out[k] + 32768) >> 16);
for n in (2..=k).rev() {
out[n] += out[n - 2] - ((ftmp * out[n - 1] + 32768) >> 16);
}
out[1] -= ftmp;
}
out
};
let p = find_poly(0);
let q = find_poly(1);
let mut a32 = [0i32; MAX_LPC_ORDER];
for k in 0..d2 {
let ptmp = p[k + 1] + p[k];
let qtmp = q[k + 1] - q[k];
a32[k] = (-qtmp - ptmp) as i32;
a32[d - k - 1] = (qtmp - ptmp) as i32;
}
let a32 = &mut a32[..d];
// Limit the dynamic range (silk_LPC_fit with QIN = 17, QOUT = 12).
let mut a_q12 = [0i32; MAX_LPC_ORDER];
let mut i = 0;
while i < 10 {
let mut maxabs = 0i32;
let mut idx = 0;
for (k, &v) in a32.iter().enumerate() {
let av = v.saturating_abs();
if av > maxabs {
maxabs = av;
idx = k;
}
}
let maxabs = rshift_round(maxabs, 5);
if maxabs > 32767 {
let maxabs = maxabs.min(163_838);
let chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (idx as i32 + 1)) >> 2);
bwexpander_32(a32, chirp);
} else {
break;
}
i += 1;
}
if i == 10 {
for k in 0..d {
a_q12[k] = sat16(rshift_round(a32[k], 5));
a32[k] = a_q12[k] << 5;
}
} else {
for k in 0..d {
a_q12[k] = rshift_round(a32[k], 5);
}
}
for i in 0..16 {
if lpc_inverse_pred_gain(&a_q12[..d]) == 0 {
bwexpander_32(a32, 65536 - (2 << i));
for k in 0..d {
a_q12[k] = rshift_round(a32[k], 5);
}
} else {
break;
}
}
a_q12
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flat_nlsf_gives_small_lpc() {
// Uniformly spaced NLSFs correspond to an (almost) flat spectrum.
let nlsf: Vec<i32> = (1..=16).map(|k| k * 32768 / 17).collect();
let a = nlsf2a(&nlsf);
assert!(a.iter().all(|v| v.abs() < 600), "{a:?}");
let nlsf: Vec<i32> = (1..=10).map(|k| k * 32768 / 11).collect();
let a = nlsf2a(&nlsf);
assert!(a[..10].iter().all(|v| v.abs() < 600), "{a:?}");
}
#[test]
fn stabilize_enforces_spacing() {
let mut v = [100, 90, 80, 30000, 30001, 30002, 31000, 32000, 32700, 32760];
nlsf_stabilize(&mut v, &NLSF_MIN_SPACING_NBMB);
assert!(v[0] >= NLSF_MIN_SPACING_NBMB[0]);
for i in 1..10 {
assert!(v[i] - v[i - 1] >= NLSF_MIN_SPACING_NBMB[i], "{v:?}");
}
assert!(32768 - v[9] >= NLSF_MIN_SPACING_NBMB[10]);
}
#[test]
fn inverse_approximations() {
for &b in &[65536, 100_000, 1 << 20, 81920, 1_686_110_208] {
let inv = inverse32_varq(b, 47) as f64;
let exact = 2f64.powi(47) / b as f64;
assert!((inv - exact).abs() / exact < 1e-3, "{b}");
}
let q = div32_varq(65536, 131072, 16) as f64;
assert!((q - 32768.0).abs() < 2.0);
}
}
-130
View File
@@ -1,130 +0,0 @@
//! NLSF quantisation (encoder side of RFC 6716 §4.2.7.5): stage-1 vector codebook preselection
//! and a backwards-predictive stage-2 scalar quantiser that reproduces the decoder's residual
//! dequantisation exactly. The search strategy is our own.
use super::lpc::{MAX_LPC_ORDER, nlsf_decode, smlawb, sqrt_approx};
use super::tables::{
NLSF_CB_NBMB, NLSF_CB_WB, NLSF_EXT, NLSF_PRED_NBMB, NLSF_PRED_WB, NLSF_PSEL_NBMB, NLSF_PSEL_WB, NLSF_SEL_NBMB, NLSF_SEL_WB, NLSF_STAGE1, NLSF_STAGE2,
};
/// Number of stage-1 candidates refined with stage 2.
const SURVIVORS: usize = 8;
/// Rate weight (weighted squared Q15 error per bit).
const LAMBDA: f64 = 60.0;
fn icdf_bits(icdf: &[u8], s: usize) -> f64 {
let hi = if s == 0 { 256 } else { icdf[s - 1] as u32 };
let p = (hi - icdf[s] as u32).max(1) as f64 / 256.0;
-p.log2()
}
/// Bits of a stage-2 index `q` (-10..=10) with stage-2 table `tab`.
fn stage2_bits(tab: &[u8], q: i32) -> f64 {
let v = q + 4;
if v <= 0 {
icdf_bits(tab, 0) + icdf_bits(&NLSF_EXT, (-v) as usize)
} else if v >= 8 {
icdf_bits(tab, 8) + icdf_bits(&NLSF_EXT, (v - 8) as usize)
} else {
icdf_bits(tab, v as usize)
}
}
/// Quantises target NLSFs (`target`, Q15 as f64, ascending) and returns the 1 + order indices
/// in the decoder's layout (`[stage1, stage2...]`) together with the reconstructed NLSFs.
pub fn quantize(target: &[f64], signal_type: usize, wb: bool) -> ([i32; 17], [i32; MAX_LPC_ORDER]) {
let order = target.len();
// Laroia-style weights.
let w: Vec<f64> = (0..order)
.map(|k| {
let prev = if k == 0 { 0.0 } else { target[k - 1] };
let next = if k + 1 == order { 32768.0 } else { target[k + 1] };
1.0 / (target[k] - prev).max(50.0) + 1.0 / (next - target[k]).max(50.0)
})
.collect();
let s1_tab = NLSF_STAGE1[(signal_type >> 1) + if wb { 2 } else { 0 }];
let n_cb = 32;
let mut cands: Vec<(f64, usize)> = (0..n_cb)
.map(|i1| {
let cb: &[i32] = if wb { &NLSF_CB_WB[i1] } else { &NLSF_CB_NBMB[i1] };
let d: f64 = (0..order).map(|k| w[k] * ((cb[k] << 7) as f64 - target[k]).powi(2)).sum();
(d + LAMBDA * icdf_bits(s1_tab, i1), i1)
})
.collect();
cands.sort_by(|a, b| a.0.total_cmp(&b.0));
let qstep = if wb { 9830 } else { 11796 };
let mut best = (f64::MAX, [0i32; 17], [0i32; MAX_LPC_ORDER]);
for &(_, i1) in cands.iter().take(SURVIVORS) {
let cb1: &[i32] = if wb { &NLSF_CB_WB[i1] } else { &NLSF_CB_NBMB[i1] };
let mut ix = [0i32; 17];
ix[0] = i1 as i32;
let mut bits = icdf_bits(s1_tab, i1);
// Exact decoder weights per coefficient.
let w_q9: Vec<i32> = (0..order)
.map(|k| {
let prev = if k == 0 { 0 } else { cb1[k - 1] };
let next = if k + 1 == order { 256 } else { cb1[k + 1] };
let ww = (1024 / (cb1[k] - prev).max(1) + 1024 / (next - cb1[k]).max(1)).min(32767);
sqrt_approx(ww << 16).max(1)
})
.collect();
let mut out_q10 = 0i32;
for i in (0..order).rev() {
let pred = if i + 1 < order {
let sel = if wb { NLSF_PSEL_WB[i1][i] } else { NLSF_PSEL_NBMB[i1][i] } as usize;
let wp = if wb { NLSF_PRED_WB[sel][i] } else { NLSF_PRED_NBMB[sel][i] };
(out_q10 * wp) >> 8
} else {
0
};
let cb = if wb { 8 + NLSF_SEL_WB[i1][i] as usize } else { NLSF_SEL_NBMB[i1][i] as usize };
let tab = NLSF_STAGE2[cb];
let want_q10 = (target[i] - (cb1[i] << 7) as f64) * w_q9[i] as f64 / 16384.0;
let step = qstep as f64 * 1024.0 / 65536.0;
let q0 = ((want_q10 - pred as f64) / step).round() as i32;
let mut bq = (f64::MAX, 0i32, 0i32);
for q in (q0 - 1).max(-10)..=(q0 + 1).min(10) {
let mut v = q << 10;
if v > 0 {
v -= 102;
} else if v < 0 {
v += 102;
}
let o = smlawb(pred, v, qstep);
let nl = (cb1[i] << 7) as f64 + ((o << 14) / w_q9[i]) as f64;
let cost = w[i] * (nl - target[i]).powi(2) + LAMBDA * stage2_bits(tab, q);
if cost < bq.0 {
bq = (cost, q, o);
}
}
ix[i + 1] = bq.1;
out_q10 = bq.2;
bits += stage2_bits(tab, bq.1);
}
let nlsf = nlsf_decode(&ix, wb);
let d: f64 = (0..order).map(|k| w[k] * (nlsf[k] as f64 - target[k]).powi(2)).sum();
let cost = d + LAMBDA * bits;
if cost < best.0 {
best = (cost, ix, nlsf);
}
}
(best.1, best.2)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn quantised_nlsfs_track_the_target() {
for wb in [false, true] {
let order = if wb { 16 } else { 10 };
// A smooth, plausible NLSF vector.
let target: Vec<f64> = (0..order).map(|k| 32768.0 * (k as f64 + 0.8 + 0.3 * (k as f64).sin()) / (order as f64 + 1.0)).collect();
let (ix, nlsf) = quantize(&target, 1, wb);
assert!(ix[1..=order].iter().all(|v| (-10..=10).contains(v)));
let max_err = (0..order).map(|k| (nlsf[k] as f64 - target[k]).abs()).fold(0.0, f64::max);
assert!(max_err < 1200.0, "wb={wb} max err {max_err}: {:?} vs {target:?}", &nlsf[..order]);
}
}
}
-113
View File
@@ -1,113 +0,0 @@
//! Excitation coding (encoder side of RFC 6716 §4.2.7.8): rate level, per-block pulse counts
//! with LSB escapes, the recursive shell code, LSBs and signs, written in exactly the order the
//! decoder reads them.
use super::tables::{EXC_LSB, EXC_SIGN, PULSE_COUNT, RATE_LEVEL, SHELL_2, SHELL_4, SHELL_8, SHELL_16};
use crate::range::RangeEncoder;
/// Largest pulse magnitude the quantiser produces (keeps every 16-sample block codable).
pub const MAX_PULSE: i32 = 500;
pub fn icdf_bits(icdf: &[u8], s: usize) -> f32 {
let hi = if s == 0 { 256 } else { icdf[s - 1] as u32 };
let p = (hi - icdf[s] as u32).max(1) as f32 / 256.0;
-p.log2()
}
fn count_table(nls: usize) -> &'static [u8] {
if nls == 10 { PULSE_COUNT[10] } else { PULSE_COUNT[9] }
}
fn shell(enc: &mut RangeEncoder, v: &[i32], count: i32) {
let n = v.len();
if n == 1 || count == 0 {
return;
}
let tab = match n {
16 => SHELL_16[(count - 1) as usize],
8 => SHELL_8[(count - 1) as usize],
4 => SHELL_4[(count - 1) as usize],
_ => SHELL_2[(count - 1) as usize],
};
let (a, b) = v.split_at(n / 2);
let left: i32 = a.iter().sum();
enc.icdf(left as usize, tab, 8);
shell(enc, a, left);
shell(enc, b, count - left);
}
/// Codes the frame's pulses (`len` a multiple of 16).
pub fn encode(enc: &mut RangeEncoder, pulses: &[i32], signal_type: usize, quant_offset_type: usize) {
let nblk = pulses.len() / 16;
let mut nls = vec![0usize; nblk];
let mut sums = vec![0i32; nblk];
let mut hi = vec![0i32; pulses.len()];
for b in 0..nblk {
let blk = &pulses[b * 16..(b + 1) * 16];
let mut s = 0usize;
loop {
let sum: i32 = blk.iter().map(|p| p.abs() >> s).sum();
if sum <= 16 || s == 10 {
sums[b] = sum.min(16);
break;
}
s += 1;
}
nls[b] = s;
for (h, p) in hi[b * 16..(b + 1) * 16].iter_mut().zip(blk) {
*h = p.abs() >> s;
}
}
// Rate level with the cheapest pulse-count symbols.
let rl_tab = RATE_LEVEL[signal_type >> 1];
let mut best = (f32::MAX, 0usize);
for rl in 0..9 {
let mut bits = icdf_bits(rl_tab, rl);
for b in 0..nblk {
bits += if nls[b] == 0 { icdf_bits(PULSE_COUNT[rl], sums[b] as usize) } else { icdf_bits(PULSE_COUNT[rl], 17) };
}
if bits < best.0 {
best = (bits, rl);
}
}
let rl = best.1;
enc.icdf(rl, rl_tab, 8);
for b in 0..nblk {
if nls[b] == 0 {
enc.icdf(sums[b] as usize, PULSE_COUNT[rl], 8);
} else {
enc.icdf(17, PULSE_COUNT[rl], 8);
for j in 1..nls[b] {
enc.icdf(17, count_table(j), 8);
}
enc.icdf(sums[b] as usize, count_table(nls[b]), 8);
}
}
for b in 0..nblk {
if sums[b] > 0 {
shell(enc, &hi[b * 16..(b + 1) * 16], sums[b]);
}
}
for b in 0..nblk {
if nls[b] > 0 {
for p in &pulses[b * 16..(b + 1) * 16] {
let a = p.abs();
for j in (0..nls[b]).rev() {
enc.icdf(((a >> j) & 1) as usize, &EXC_LSB, 8);
}
}
}
}
let base = 7 * (quant_offset_type + (signal_type << 1));
for b in 0..nblk {
let p = sums[b] | ((nls[b] as i32) << 5);
if p > 0 {
let tab = EXC_SIGN[base + ((p & 0x1F) as usize).min(6)];
for &q in &pulses[b * 16..(b + 1) * 16] {
if q != 0 {
enc.icdf((q > 0) as usize, tab, 8);
}
}
}
}
}
-540
View File
@@ -1,540 +0,0 @@
//! SILK probability tables and codebooks, transcribed from RFC 6716 §4.2 (Tables 4–53).
//!
//! PDFs are written as in the RFC (`{...}/256`) and converted at compile time to the inverse-CDF
//! form consumed by the range coder.
//!
//! Copied from FilmCraft's first-party, spec-derived `filmcraft-opus` decoder (MIT OR
//! Apache-2.0) so encoder and decoder share exactly the same values.
/// Converts a PDF (summing to 256) into an inverse CDF (`256 - cdf[i+1]`).
pub const fn icdf<const N: usize>(pdf: &[u8; N]) -> [u8; N] {
let mut out = [0u8; N];
let mut acc: u32 = 0;
let mut i = 0;
while i < N {
acc += pdf[i] as u32;
out[i] = (256 - acc) as u8;
i += 1;
}
out
}
pub const STEREO_PRED_JOINT: [u8; 25] = icdf(&[7, 2, 1, 1, 1, 10, 24, 8, 1, 1, 3, 23, 92, 23, 3, 1, 1, 8, 24, 10, 1, 1, 1, 2, 7]);
pub const UNIFORM3: [u8; 3] = icdf(&[85, 86, 85]);
pub const UNIFORM5: [u8; 5] = icdf(&[51, 51, 52, 51, 51]);
pub const STEREO_WEIGHTS_Q13: [i32; 16] = [-13732, -10050, -8266, -7526, -6500, -5000, -2950, -820, 820, 2950, 5000, 6500, 7526, 8266, 10050, 13732];
pub const MID_ONLY: [u8; 2] = icdf(&[192, 64]);
pub const TYPE_INACTIVE: [u8; 2] = icdf(&[26, 230]);
pub const TYPE_ACTIVE: [u8; 4] = icdf(&[24, 74, 148, 10]);
pub const GAIN_MSB: [&[u8]; 3] =
[&icdf::<8>(&[32, 112, 68, 29, 12, 1, 1, 1]), &icdf::<8>(&[2, 17, 45, 60, 62, 47, 19, 4]), &icdf::<8>(&[1, 3, 26, 71, 94, 50, 9, 2])];
pub const UNIFORM8: [u8; 8] = icdf(&[32, 32, 32, 32, 32, 32, 32, 32]);
pub const DELTA_GAIN: [u8; 41] =
icdf(&[6, 5, 11, 31, 132, 21, 8, 4, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
pub const NLSF_STAGE1: [&[u8]; 4] = [
&icdf::<32>(&[44, 34, 30, 19, 21, 12, 11, 3, 3, 2, 16, 2, 2, 1, 5, 2, 1, 3, 3, 1, 1, 2, 2, 2, 3, 1, 9, 9, 2, 7, 2, 1]),
&icdf::<32>(&[1, 10, 1, 8, 3, 8, 8, 14, 13, 14, 1, 14, 12, 13, 11, 11, 12, 11, 10, 10, 11, 8, 9, 8, 7, 8, 1, 1, 6, 1, 6, 5]),
&icdf::<32>(&[31, 21, 3, 17, 1, 8, 17, 4, 1, 18, 16, 4, 2, 3, 1, 10, 1, 3, 16, 11, 16, 2, 2, 3, 2, 11, 1, 4, 9, 8, 7, 3]),
&icdf::<32>(&[1, 4, 16, 5, 18, 11, 5, 14, 15, 1, 3, 12, 13, 14, 14, 6, 14, 12, 2, 6, 1, 12, 12, 11, 10, 3, 10, 5, 1, 1, 1, 3]),
];
pub const NLSF_STAGE2: [&[u8]; 16] = [
&icdf::<9>(&[1, 1, 1, 15, 224, 11, 1, 1, 1]),
&icdf::<9>(&[1, 1, 2, 34, 183, 32, 1, 1, 1]),
&icdf::<9>(&[1, 1, 4, 42, 149, 55, 2, 1, 1]),
&icdf::<9>(&[1, 1, 8, 52, 123, 61, 8, 1, 1]),
&icdf::<9>(&[1, 3, 16, 53, 101, 74, 6, 1, 1]),
&icdf::<9>(&[1, 3, 17, 55, 90, 73, 15, 1, 1]),
&icdf::<9>(&[1, 7, 24, 53, 74, 67, 26, 3, 1]),
&icdf::<9>(&[1, 1, 18, 63, 78, 58, 30, 6, 1]),
&icdf::<9>(&[1, 1, 1, 9, 232, 9, 1, 1, 1]),
&icdf::<9>(&[1, 1, 2, 28, 186, 35, 1, 1, 1]),
&icdf::<9>(&[1, 1, 3, 42, 152, 53, 2, 1, 1]),
&icdf::<9>(&[1, 1, 10, 49, 126, 65, 2, 1, 1]),
&icdf::<9>(&[1, 4, 19, 48, 100, 77, 5, 1, 1]),
&icdf::<9>(&[1, 1, 14, 54, 100, 72, 12, 1, 1]),
&icdf::<9>(&[1, 1, 15, 61, 87, 61, 25, 4, 1]),
&icdf::<9>(&[1, 7, 21, 50, 77, 81, 17, 1, 1]),
];
pub const NLSF_SEL_NBMB: [[u8; 10]; 32] = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 3, 1, 2, 2, 1, 2, 1, 1, 1],
[2, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 2, 1, 2, 1, 1, 1],
[2, 3, 3, 3, 3, 2, 2, 2, 2, 2],
[0, 5, 3, 3, 2, 2, 2, 2, 1, 1],
[0, 2, 2, 2, 2, 2, 2, 2, 2, 1],
[2, 3, 6, 4, 4, 4, 5, 4, 5, 5],
[2, 4, 5, 5, 4, 5, 4, 6, 4, 4],
[2, 4, 4, 7, 4, 5, 4, 5, 5, 4],
[4, 3, 3, 3, 2, 3, 2, 2, 2, 2],
[1, 5, 5, 6, 4, 5, 4, 5, 5, 5],
[2, 7, 4, 6, 5, 5, 5, 5, 5, 5],
[2, 7, 5, 5, 5, 5, 5, 6, 5, 4],
[3, 3, 5, 4, 4, 5, 4, 5, 4, 4],
[2, 3, 3, 5, 5, 4, 4, 4, 4, 4],
[2, 4, 4, 6, 4, 5, 4, 5, 5, 5],
[2, 5, 4, 6, 5, 5, 5, 4, 5, 4],
[2, 7, 4, 5, 4, 5, 4, 5, 5, 5],
[2, 5, 4, 6, 7, 6, 5, 6, 5, 4],
[3, 6, 7, 4, 6, 5, 5, 6, 4, 5],
[2, 7, 6, 4, 4, 4, 5, 4, 5, 5],
[4, 5, 5, 4, 6, 6, 5, 6, 5, 4],
[2, 5, 5, 6, 5, 6, 4, 6, 4, 4],
[4, 5, 5, 5, 3, 7, 4, 5, 5, 4],
[2, 3, 4, 5, 5, 6, 4, 5, 5, 4],
[2, 3, 2, 3, 3, 4, 2, 3, 3, 3],
[1, 1, 2, 2, 2, 2, 2, 3, 2, 2],
[4, 5, 5, 6, 6, 6, 5, 6, 4, 5],
[3, 5, 5, 4, 4, 4, 4, 3, 3, 2],
[2, 5, 3, 7, 5, 5, 4, 4, 5, 4],
[4, 4, 5, 4, 5, 6, 5, 6, 5, 4],
];
pub const NLSF_SEL_WB: [[u8; 16]; 32] = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[2, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 0, 3],
[2, 5, 5, 3, 7, 4, 4, 5, 2, 5, 4, 5, 5, 4, 3, 3],
[0, 2, 1, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1],
[0, 6, 5, 4, 6, 4, 7, 5, 4, 4, 4, 5, 5, 4, 4, 3],
[0, 3, 5, 5, 4, 3, 3, 5, 3, 3, 3, 3, 3, 3, 2, 4],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 2, 6, 3, 7, 2, 5, 3, 4, 5, 5, 4, 3, 3, 2, 3],
[0, 6, 2, 6, 6, 4, 5, 4, 6, 5, 4, 4, 5, 3, 3, 3],
[2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[2, 2, 3, 4, 5, 3, 3, 3, 3, 3, 3, 3, 2, 2, 1, 3],
[2, 2, 3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 3],
[3, 4, 4, 4, 6, 4, 4, 5, 3, 5, 4, 4, 5, 4, 3, 4],
[0, 6, 4, 5, 4, 7, 5, 2, 6, 5, 7, 4, 4, 3, 5, 3],
[0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0],
[1, 6, 5, 7, 5, 4, 5, 3, 4, 5, 4, 4, 4, 3, 3, 4],
[1, 3, 3, 4, 4, 3, 3, 5, 2, 3, 3, 5, 5, 5, 3, 4],
[2, 3, 3, 2, 2, 2, 3, 2, 1, 2, 1, 2, 1, 1, 1, 4],
[0, 2, 3, 5, 3, 3, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0],
[3, 4, 3, 5, 3, 3, 2, 2, 1, 1, 1, 1, 1, 2, 2, 4],
[2, 6, 3, 7, 7, 4, 5, 4, 5, 3, 5, 3, 3, 2, 3, 3],
[2, 3, 5, 6, 6, 3, 5, 3, 4, 4, 3, 3, 3, 3, 2, 4],
[1, 3, 3, 4, 4, 4, 4, 3, 5, 5, 5, 3, 1, 1, 1, 1],
[2, 5, 3, 6, 6, 4, 7, 4, 4, 5, 3, 4, 4, 3, 3, 3],
[0, 6, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 6, 6, 3, 5, 2, 5, 5, 3, 4, 4, 7, 7, 4, 4, 4],
[3, 3, 7, 3, 5, 4, 3, 3, 3, 2, 2, 3, 3, 3, 2, 3],
[0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 1, 2, 2, 2, 1, 1],
[0, 3, 2, 5, 3, 3, 2, 3, 2, 1, 0, 0, 1, 0, 0, 1],
[3, 5, 5, 4, 7, 5, 3, 3, 2, 3, 2, 2, 1, 0, 1, 0],
[2, 3, 5, 3, 4, 3, 3, 3, 2, 1, 2, 6, 4, 0, 0, 0],
];
pub const NLSF_EXT: [u8; 7] = icdf(&[156, 60, 24, 9, 4, 2, 1]);
pub const NLSF_PRED_NBMB: [[i32; 9]; 2] = [[179, 138, 140, 148, 151, 149, 153, 151, 163], [116, 67, 82, 59, 92, 72, 100, 89, 92]];
pub const NLSF_PRED_WB: [[i32; 15]; 2] =
[[175, 148, 160, 176, 178, 173, 174, 164, 177, 174, 196, 182, 198, 192, 182], [68, 62, 66, 60, 72, 117, 85, 90, 118, 136, 151, 142, 160, 142, 155]];
pub const NLSF_PSEL_NBMB: [[u8; 9]; 32] = [
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 0, 0, 1, 0],
[0, 1, 1, 0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 1, 0, 1, 1],
[0, 0, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 0, 1, 1, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 0, 1, 1, 0, 1, 0, 1, 0],
[0, 0, 1, 1, 1, 0, 1, 1, 1],
[0, 1, 1, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 0, 1, 0],
[0, 1, 1, 0, 0, 1, 0, 1, 0],
[0, 1, 1, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 1, 0, 1, 0],
[1, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 1, 0, 1],
[1, 0, 1, 1, 0, 1, 1, 1, 1],
];
pub const NLSF_PSEL_WB: [[u8; 15]; 32] = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
[0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0],
[0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0],
];
pub const NLSF_CB_NBMB: [[i32; 10]; 32] = [
[12, 35, 60, 83, 108, 132, 157, 180, 206, 228],
[15, 32, 55, 77, 101, 125, 151, 175, 201, 225],
[19, 42, 66, 89, 114, 137, 162, 184, 209, 230],
[12, 25, 50, 72, 97, 120, 147, 172, 200, 223],
[26, 44, 69, 90, 114, 135, 159, 180, 205, 225],
[13, 22, 53, 80, 106, 130, 156, 180, 205, 228],
[15, 25, 44, 64, 90, 115, 142, 168, 196, 222],
[19, 24, 62, 82, 100, 120, 145, 168, 190, 214],
[22, 31, 50, 79, 103, 120, 151, 170, 203, 227],
[21, 29, 45, 65, 106, 124, 150, 171, 196, 224],
[30, 49, 75, 97, 121, 142, 165, 186, 209, 229],
[19, 25, 52, 70, 93, 116, 143, 166, 192, 219],
[26, 34, 62, 75, 97, 118, 145, 167, 194, 217],
[25, 33, 56, 70, 91, 113, 143, 165, 196, 223],
[21, 34, 51, 72, 97, 117, 145, 171, 196, 222],
[20, 29, 50, 67, 90, 117, 144, 168, 197, 221],
[22, 31, 48, 66, 95, 117, 146, 168, 196, 222],
[24, 33, 51, 77, 116, 134, 158, 180, 200, 224],
[21, 28, 70, 87, 106, 124, 149, 170, 194, 217],
[26, 33, 53, 64, 83, 117, 152, 173, 204, 225],
[27, 34, 65, 95, 108, 129, 155, 174, 210, 225],
[20, 26, 72, 99, 113, 131, 154, 176, 200, 219],
[34, 43, 61, 78, 93, 114, 155, 177, 205, 229],
[23, 29, 54, 97, 124, 138, 163, 179, 209, 229],
[30, 38, 56, 89, 118, 129, 158, 178, 200, 231],
[21, 29, 49, 63, 85, 111, 142, 163, 193, 222],
[27, 48, 77, 103, 133, 158, 179, 196, 215, 232],
[29, 47, 74, 99, 124, 151, 176, 198, 220, 237],
[33, 42, 61, 76, 93, 121, 155, 174, 207, 225],
[29, 53, 87, 112, 136, 154, 170, 188, 208, 227],
[24, 30, 52, 84, 131, 150, 166, 186, 203, 229],
[37, 48, 64, 84, 104, 118, 156, 177, 201, 230],
];
pub const NLSF_CB_WB: [[i32; 16]; 32] = [
[7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239],
[13, 25, 41, 55, 69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236],
[15, 21, 34, 51, 61, 78, 92, 106, 126, 136, 152, 167, 185, 205, 225, 240],
[10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173, 189, 205, 221, 237],
[17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240],
[10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236],
[8, 21, 37, 51, 65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218],
[12, 15, 34, 55, 63, 78, 87, 108, 118, 131, 148, 167, 185, 203, 219, 236],
[16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171, 186, 204, 220, 237],
[11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241],
[6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225],
[11, 19, 30, 44, 57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234],
[12, 19, 29, 46, 57, 71, 88, 100, 120, 132, 148, 165, 182, 199, 216, 233],
[17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167, 185, 204, 222, 237],
[14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240],
[9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237],
[16, 19, 36, 48, 57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236],
[12, 17, 29, 54, 71, 81, 94, 104, 126, 136, 149, 164, 182, 201, 221, 237],
[15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180, 194, 208, 223, 238],
[8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239],
[17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235],
[14, 19, 36, 45, 61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238],
[12, 18, 31, 45, 60, 76, 91, 107, 123, 138, 154, 171, 187, 204, 221, 236],
[13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167, 185, 203, 220, 237],
[17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240],
[8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239],
[13, 16, 41, 66, 73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241],
[17, 25, 37, 52, 63, 75, 92, 102, 119, 132, 144, 160, 175, 191, 212, 231],
[19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187, 200, 213, 227, 242],
[18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239],
[16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240],
[15, 21, 35, 50, 61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237],
];
pub const NLSF_MIN_SPACING_NBMB: [i32; 11] = [250, 3, 6, 3, 3, 3, 4, 3, 3, 3, 461];
pub const NLSF_MIN_SPACING_WB: [i32; 17] = [100, 3, 40, 3, 3, 3, 5, 14, 14, 10, 11, 3, 8, 9, 7, 3, 347];
pub const NLSF_INTERP: [u8; 5] = icdf(&[13, 22, 29, 11, 181]);
pub const LSF_ORDER_NBMB: [usize; 10] = [0, 9, 6, 3, 4, 5, 8, 1, 2, 7];
pub const LSF_ORDER_WB: [usize; 16] = [0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1];
pub const COS_Q12: [i32; 129] = [
4096, 4095, 4091, 4085, 4076, 4065, 4052, 4036, 4017, 3997, 3973, 3948, 3920, 3889, 3857, 3822, 3784, 3745, 3703, 3659, 3613, 3564, 3513, 3461, 3406, 3349,
3290, 3229, 3166, 3102, 3035, 2967, 2896, 2824, 2751, 2676, 2599, 2520, 2440, 2359, 2276, 2191, 2106, 2019, 1931, 1842, 1751, 1660, 1568, 1474, 1380, 1285,
1189, 1093, 995, 897, 799, 700, 601, 501, 401, 301, 201, 101, 0, -101, -201, -301, -401, -501, -601, -700, -799, -897, -995, -1093, -1189, -1285, -1380,
-1474, -1568, -1660, -1751, -1842, -1931, -2019, -2106, -2191, -2276, -2359, -2440, -2520, -2599, -2676, -2751, -2824, -2896, -2967, -3035, -3102, -3166,
-3229, -3290, -3349, -3406, -3461, -3513, -3564, -3613, -3659, -3703, -3745, -3784, -3822, -3857, -3889, -3920, -3948, -3973, -3997, -4017, -4036, -4052,
-4065, -4076, -4085, -4091, -4095, -4096,
];
pub const PITCH_HIGH: [u8; 32] = icdf(&[3, 3, 6, 11, 21, 30, 32, 19, 11, 10, 12, 13, 13, 12, 11, 9, 8, 7, 6, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
pub const PITCH_LOW: [&[u8]; 3] = [&icdf::<4>(&[64, 64, 64, 64]), &icdf::<6>(&[43, 42, 43, 43, 42, 43]), &icdf::<8>(&[32, 32, 32, 32, 32, 32, 32, 32])];
pub const PITCH_DELTA: [u8; 21] = icdf(&[46, 2, 2, 3, 4, 6, 10, 15, 26, 38, 30, 22, 15, 10, 7, 6, 4, 4, 2, 2, 2]);
pub const PITCH_CONTOUR: [&[u8]; 4] = [
&icdf::<3>(&[143, 50, 63]),
&icdf::<11>(&[68, 12, 21, 17, 19, 22, 30, 24, 17, 16, 10]),
&icdf::<12>(&[91, 46, 39, 19, 14, 12, 8, 7, 6, 5, 5, 4]),
&icdf::<34>(&[33, 22, 18, 16, 15, 14, 14, 13, 13, 10, 9, 9, 8, 6, 6, 6, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1]),
];
pub const CB_NB_10: [[i32; 2]; 3] = [[0, 0], [1, 0], [0, 1]];
pub const CB_NB_20: [[i32; 4]; 11] = [
[0, 0, 0, 0],
[2, 1, 0, -1],
[-1, 0, 1, 2],
[-1, 0, 0, 1],
[-1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 1],
[1, 1, 0, 0],
[1, 0, 0, 0],
[0, 0, 0, -1],
[1, 0, 0, -1],
];
pub const CB_WB_10: [[i32; 2]; 12] = [[0, 0], [0, 1], [1, 0], [-1, 1], [1, -1], [-1, 2], [2, -1], [-2, 2], [2, -2], [-2, 3], [3, -2], [-3, 3]];
pub const CB_WB_20: [[i32; 4]; 34] = [
[0, 0, 0, 0],
[0, 0, 1, 1],
[1, 1, 0, 0],
[-1, 0, 0, 0],
[0, 0, 0, 1],
[1, 0, 0, 0],
[-1, 0, 0, 1],
[0, 0, 0, -1],
[-1, 0, 1, 2],
[1, 0, 0, -1],
[-2, -1, 1, 2],
[2, 1, 0, -1],
[-2, 0, 0, 2],
[-2, 0, 1, 3],
[2, 1, -1, -2],
[-3, -1, 1, 3],
[2, 0, 0, -2],
[3, 1, 0, -2],
[-3, -1, 2, 4],
[-4, -1, 1, 4],
[3, 1, -1, -3],
[-4, -1, 2, 5],
[4, 2, -1, -3],
[4, 1, -1, -4],
[-5, -1, 2, 6],
[5, 2, -1, -4],
[-6, -2, 2, 6],
[-5, -2, 2, 5],
[6, 2, -1, -5],
[-7, -2, 3, 8],
[6, 2, -2, -6],
[5, 2, -2, -5],
[8, 3, -2, -7],
[-9, -3, 3, 9],
];
pub const LTP_PERIODICITY: [u8; 3] = icdf(&[77, 80, 99]);
pub const LTP_FILTER: [&[u8]; 3] = [
&icdf::<8>(&[185, 15, 13, 13, 9, 9, 6, 6]),
&icdf::<16>(&[57, 34, 21, 20, 15, 13, 12, 13, 10, 10, 9, 10, 9, 8, 7, 8]),
&icdf::<32>(&[15, 16, 14, 12, 12, 12, 11, 11, 11, 10, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 5, 4, 4, 4, 3, 4, 3, 2]),
];
pub const LTP_TAPS_0: [[i32; 5]; 8] = [
[4, 6, 24, 7, 5],
[0, 0, 2, 0, 0],
[12, 28, 41, 13, -4],
[-9, 15, 42, 25, 14],
[1, -2, 62, 41, -9],
[-10, 37, 65, -4, 3],
[-6, 4, 66, 7, -8],
[16, 14, 38, -3, 33],
];
pub const LTP_TAPS_1: [[i32; 5]; 16] = [
[13, 22, 39, 23, 12],
[-1, 36, 64, 27, -6],
[-7, 10, 55, 43, 17],
[1, 1, 8, 1, 1],
[6, -11, 74, 53, -9],
[-12, 55, 76, -12, 8],
[-3, 3, 93, 27, -4],
[26, 39, 59, 3, -8],
[2, 0, 77, 11, 9],
[-8, 22, 44, -6, 7],
[40, 9, 26, 3, 9],
[-7, 20, 101, -7, 4],
[3, -8, 42, 26, 0],
[-15, 33, 68, 2, 23],
[-2, 55, 46, -2, 15],
[3, -1, 21, 16, 41],
];
pub const LTP_TAPS_2: [[i32; 5]; 32] = [
[-6, 27, 61, 39, 5],
[-11, 42, 88, 4, 1],
[-2, 60, 65, 6, -4],
[-1, -5, 73, 56, 1],
[-9, 19, 94, 29, -9],
[0, 12, 99, 6, 4],
[8, -19, 102, 46, -13],
[3, 2, 13, 3, 2],
[9, -21, 84, 72, -18],
[-11, 46, 104, -22, 8],
[18, 38, 48, 23, 0],
[-16, 70, 83, -21, 11],
[5, -11, 117, 22, -8],
[-6, 23, 117, -12, 3],
[3, -8, 95, 28, 4],
[-10, 15, 77, 60, -15],
[-1, 4, 124, 2, -4],
[3, 38, 84, 24, -25],
[2, 13, 42, 13, 31],
[21, -4, 56, 46, -1],
[-1, 35, 79, -13, 19],
[-7, 65, 88, -9, -14],
[20, 4, 81, 49, -29],
[20, 0, 75, 3, -17],
[5, -9, 44, 92, -8],
[1, -3, 22, 69, 31],
[-6, 95, 41, -12, 5],
[39, 67, 16, -4, 1],
[0, -6, 120, 55, -36],
[-13, 44, 122, 4, -24],
[81, 5, 11, 3, 7],
[2, 0, 9, 10, 88],
];
pub const LTP_SCALE: [u8; 3] = icdf(&[128, 64, 64]);
pub const UNIFORM4: [u8; 4] = icdf(&[64, 64, 64, 64]);
pub const RATE_LEVEL: [&[u8]; 2] = [&icdf::<9>(&[15, 51, 12, 46, 45, 13, 33, 27, 14]), &icdf::<9>(&[33, 30, 36, 17, 34, 49, 18, 21, 18])];
pub const PULSE_COUNT: [&[u8]; 11] = [
&icdf::<18>(&[131, 74, 25, 8, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
&icdf::<18>(&[58, 93, 60, 23, 7, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
&icdf::<18>(&[43, 51, 46, 33, 24, 16, 11, 8, 6, 3, 3, 3, 2, 1, 1, 2, 1, 2]),
&icdf::<18>(&[17, 52, 71, 57, 31, 12, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
&icdf::<18>(&[6, 21, 41, 53, 49, 35, 21, 11, 6, 3, 2, 2, 1, 1, 1, 1, 1, 1]),
&icdf::<18>(&[7, 14, 22, 28, 29, 28, 25, 20, 17, 13, 11, 9, 7, 5, 4, 4, 3, 10]),
&icdf::<18>(&[2, 5, 14, 29, 42, 46, 41, 31, 19, 11, 6, 3, 2, 1, 1, 1, 1, 1]),
&icdf::<18>(&[1, 2, 4, 10, 19, 29, 35, 37, 34, 28, 20, 14, 8, 5, 4, 2, 2, 2]),
&icdf::<18>(&[1, 2, 2, 5, 9, 14, 20, 24, 27, 28, 26, 23, 20, 15, 11, 8, 6, 15]),
&icdf::<18>(&[1, 1, 1, 6, 27, 58, 56, 39, 25, 14, 10, 6, 3, 3, 2, 1, 1, 2]),
&icdf::<18>(&[2, 1, 6, 27, 58, 56, 39, 25, 14, 10, 6, 3, 3, 2, 1, 1, 2, 0]),
];
pub const SHELL_16: [&[u8]; 16] = [
&icdf::<2>(&[126, 130]),
&icdf::<3>(&[56, 142, 58]),
&icdf::<4>(&[25, 101, 104, 26]),
&icdf::<5>(&[12, 60, 108, 64, 12]),
&icdf::<6>(&[7, 35, 84, 87, 37, 6]),
&icdf::<7>(&[4, 20, 59, 86, 63, 21, 3]),
&icdf::<8>(&[3, 12, 38, 72, 75, 42, 12, 2]),
&icdf::<9>(&[2, 8, 25, 54, 73, 59, 27, 7, 1]),
&icdf::<10>(&[2, 5, 17, 39, 63, 65, 42, 18, 4, 1]),
&icdf::<11>(&[1, 4, 12, 28, 49, 63, 54, 30, 11, 3, 1]),
&icdf::<12>(&[1, 4, 8, 20, 37, 55, 57, 41, 22, 8, 2, 1]),
&icdf::<13>(&[1, 3, 7, 15, 28, 44, 53, 48, 33, 16, 6, 1, 1]),
&icdf::<14>(&[1, 2, 6, 12, 21, 35, 47, 48, 40, 25, 12, 5, 1, 1]),
&icdf::<15>(&[1, 1, 4, 10, 17, 27, 37, 47, 43, 33, 21, 9, 4, 1, 1]),
&icdf::<16>(&[1, 1, 1, 8, 14, 22, 33, 40, 43, 38, 28, 16, 8, 1, 1, 1]),
&icdf::<17>(&[1, 1, 1, 1, 13, 18, 27, 36, 41, 41, 34, 24, 14, 1, 1, 1, 1]),
];
pub const SHELL_8: [&[u8]; 16] = [
&icdf::<2>(&[127, 129]),
&icdf::<3>(&[53, 149, 54]),
&icdf::<4>(&[22, 105, 106, 23]),
&icdf::<5>(&[11, 61, 111, 63, 10]),
&icdf::<6>(&[6, 35, 86, 88, 36, 5]),
&icdf::<7>(&[4, 20, 59, 87, 62, 21, 3]),
&icdf::<8>(&[3, 13, 40, 71, 73, 41, 13, 2]),
&icdf::<9>(&[3, 9, 27, 53, 70, 56, 28, 9, 1]),
&icdf::<10>(&[3, 8, 19, 37, 57, 61, 44, 20, 6, 1]),
&icdf::<11>(&[3, 7, 15, 28, 44, 54, 49, 33, 17, 5, 1]),
&icdf::<12>(&[1, 7, 13, 22, 34, 46, 48, 38, 28, 14, 4, 1]),
&icdf::<13>(&[1, 1, 11, 22, 27, 35, 42, 47, 33, 25, 10, 1, 1]),
&icdf::<14>(&[1, 1, 6, 14, 26, 37, 43, 43, 37, 26, 14, 6, 1, 1]),
&icdf::<15>(&[1, 1, 4, 10, 20, 31, 40, 42, 40, 31, 20, 10, 4, 1, 1]),
&icdf::<16>(&[1, 1, 3, 8, 16, 26, 35, 38, 38, 35, 26, 16, 8, 3, 1, 1]),
&icdf::<17>(&[1, 1, 2, 6, 12, 21, 30, 36, 38, 36, 30, 21, 12, 6, 2, 1, 1]),
];
pub const SHELL_4: [&[u8]; 16] = [
&icdf::<2>(&[127, 129]),
&icdf::<3>(&[49, 157, 50]),
&icdf::<4>(&[20, 107, 109, 20]),
&icdf::<5>(&[11, 60, 113, 62, 10]),
&icdf::<6>(&[7, 36, 84, 87, 36, 6]),
&icdf::<7>(&[6, 24, 57, 82, 60, 23, 4]),
&icdf::<8>(&[5, 18, 39, 64, 68, 42, 16, 4]),
&icdf::<9>(&[6, 14, 29, 47, 61, 52, 30, 14, 3]),
&icdf::<10>(&[1, 15, 23, 35, 51, 50, 40, 30, 10, 1]),
&icdf::<11>(&[1, 1, 21, 32, 42, 52, 46, 41, 18, 1, 1]),
&icdf::<12>(&[1, 6, 16, 27, 36, 42, 42, 36, 27, 16, 6, 1]),
&icdf::<13>(&[1, 5, 12, 21, 31, 38, 40, 38, 31, 21, 12, 5, 1]),
&icdf::<14>(&[1, 3, 9, 17, 26, 34, 38, 38, 34, 26, 17, 9, 3, 1]),
&icdf::<15>(&[1, 3, 7, 14, 22, 29, 34, 36, 34, 29, 22, 14, 7, 3, 1]),
&icdf::<16>(&[1, 2, 5, 11, 18, 25, 31, 35, 35, 31, 25, 18, 11, 5, 2, 1]),
&icdf::<17>(&[1, 1, 4, 9, 15, 21, 28, 32, 34, 32, 28, 21, 15, 9, 4, 1, 1]),
];
pub const SHELL_2: [&[u8]; 16] = [
&icdf::<2>(&[128, 128]),
&icdf::<3>(&[42, 172, 42]),
&icdf::<4>(&[21, 107, 107, 21]),
&icdf::<5>(&[12, 60, 112, 61, 11]),
&icdf::<6>(&[8, 34, 86, 86, 35, 7]),
&icdf::<7>(&[8, 23, 55, 90, 55, 20, 5]),
&icdf::<8>(&[5, 15, 38, 72, 72, 36, 15, 3]),
&icdf::<9>(&[6, 12, 27, 52, 77, 47, 20, 10, 5]),
&icdf::<10>(&[6, 19, 28, 35, 40, 40, 35, 28, 19, 6]),
&icdf::<11>(&[4, 14, 22, 31, 37, 40, 37, 31, 22, 14, 4]),
&icdf::<12>(&[3, 10, 18, 26, 33, 38, 38, 33, 26, 18, 10, 3]),
&icdf::<13>(&[2, 8, 13, 21, 29, 36, 38, 36, 29, 21, 13, 8, 2]),
&icdf::<14>(&[1, 5, 10, 17, 25, 32, 38, 38, 32, 25, 17, 10, 5, 1]),
&icdf::<15>(&[1, 4, 7, 13, 21, 29, 35, 36, 35, 29, 21, 13, 7, 4, 1]),
&icdf::<16>(&[1, 2, 5, 10, 17, 25, 32, 36, 36, 32, 25, 17, 10, 5, 2, 1]),
&icdf::<17>(&[1, 2, 4, 7, 13, 21, 28, 34, 36, 34, 28, 21, 13, 7, 4, 2, 1]),
];
pub const EXC_LSB: [u8; 2] = icdf(&[136, 120]);
pub const EXC_SIGN: [&[u8]; 42] = [
&icdf::<2>(&[2, 254]),
&icdf::<2>(&[207, 49]),
&icdf::<2>(&[189, 67]),
&icdf::<2>(&[179, 77]),
&icdf::<2>(&[174, 82]),
&icdf::<2>(&[163, 93]),
&icdf::<2>(&[157, 99]),
&icdf::<2>(&[58, 198]),
&icdf::<2>(&[245, 11]),
&icdf::<2>(&[238, 18]),
&icdf::<2>(&[232, 24]),
&icdf::<2>(&[225, 31]),
&icdf::<2>(&[220, 36]),
&icdf::<2>(&[211, 45]),
&icdf::<2>(&[1, 255]),
&icdf::<2>(&[210, 46]),
&icdf::<2>(&[190, 66]),
&icdf::<2>(&[178, 78]),
&icdf::<2>(&[169, 87]),
&icdf::<2>(&[162, 94]),
&icdf::<2>(&[152, 104]),
&icdf::<2>(&[48, 208]),
&icdf::<2>(&[242, 14]),
&icdf::<2>(&[235, 21]),
&icdf::<2>(&[224, 32]),
&icdf::<2>(&[214, 42]),
&icdf::<2>(&[205, 51]),
&icdf::<2>(&[190, 66]),
&icdf::<2>(&[1, 255]),
&icdf::<2>(&[162, 94]),
&icdf::<2>(&[152, 104]),
&icdf::<2>(&[147, 109]),
&icdf::<2>(&[144, 112]),
&icdf::<2>(&[141, 115]),
&icdf::<2>(&[138, 118]),
&icdf::<2>(&[8, 248]),
&icdf::<2>(&[203, 53]),
&icdf::<2>(&[187, 69]),
&icdf::<2>(&[176, 80]),
&icdf::<2>(&[168, 88]),
&icdf::<2>(&[161, 95]),
&icdf::<2>(&[154, 102]),
];
pub const LBRR_FLAGS: [&[u8]; 2] = [&icdf::<3>(&[53, 53, 150]), &icdf::<7>(&[41, 20, 29, 41, 15, 28, 82])];
-122
View File
@@ -1,122 +0,0 @@
//! Constant tables of the 48 kHz CELT mode (RFC 6716 §4.3, Tables 55–58 and §4.3.2).
//!
//! Transcribed from RFC 6716; copied from FilmCraft's spec-derived `filmcraft-opus` decoder
//! (first-party, MIT OR Apache-2.0) so both sides share the exact same values.
/// Number of energy bands.
pub const NB_EBANDS: usize = 21;
/// Band edges in units of 2.5 ms MDCT bins (Table 55).
pub const EBANDS: [i32; NB_EBANDS + 1] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100];
/// Overlap of the low-overlap window (samples at 48 kHz).
pub const OVERLAP: usize = 120;
/// Largest LM (20 ms frames).
pub const MAX_LM: usize = 3;
/// Pre-emphasis coefficient.
pub const PREEMPH: f32 = 0.850_006_1;
/// Static allocation table (Table 57), indexed `[quality][band]`, in 1/32 bit per MDCT bin.
pub const ALLOC: [[u8; NB_EBANDS]; 11] = transpose([
[0, 90, 110, 118, 126, 134, 144, 152, 162, 172, 200],
[0, 80, 100, 110, 119, 127, 137, 145, 155, 165, 200],
[0, 75, 90, 103, 112, 120, 130, 138, 148, 158, 200],
[0, 69, 84, 93, 104, 114, 124, 132, 142, 152, 200],
[0, 63, 78, 86, 95, 103, 113, 123, 133, 143, 200],
[0, 56, 71, 80, 89, 97, 107, 117, 127, 137, 200],
[0, 49, 65, 75, 83, 91, 101, 111, 121, 131, 200],
[0, 40, 58, 70, 78, 85, 95, 105, 115, 125, 200],
[0, 34, 51, 65, 72, 78, 88, 98, 108, 118, 198],
[0, 29, 45, 59, 66, 72, 82, 92, 102, 112, 193],
[0, 20, 39, 53, 60, 66, 76, 86, 96, 106, 188],
[0, 18, 32, 47, 54, 60, 70, 80, 90, 100, 183],
[0, 10, 26, 40, 47, 54, 64, 74, 84, 94, 178],
[0, 0, 20, 31, 39, 47, 57, 67, 77, 87, 173],
[0, 0, 12, 23, 32, 41, 51, 61, 71, 81, 168],
[0, 0, 0, 15, 25, 35, 45, 55, 65, 75, 163],
[0, 0, 0, 4, 17, 29, 39, 49, 59, 69, 158],
[0, 0, 0, 0, 12, 23, 33, 43, 53, 63, 153],
[0, 0, 0, 0, 1, 16, 26, 36, 46, 56, 148],
[0, 0, 0, 0, 0, 10, 15, 20, 30, 45, 129],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 20, 104],
]);
const fn transpose(t: [[u8; 11]; NB_EBANDS]) -> [[u8; NB_EBANDS]; 11] {
let mut out = [[0u8; NB_EBANDS]; 11];
let mut b = 0;
while b < NB_EBANDS {
let mut q = 0;
while q < 11 {
out[q][b] = t[b][q];
q += 1;
}
b += 1;
}
out
}
/// Mean band energy (log2 domain) removed before coarse quantisation.
pub const E_MEANS: [f32; 25] = [
6.4375, 6.25, 5.75, 5.3125, 5.0625, 4.8125, 4.5, 4.375, 4.875, 4.6875, 4.5625, 4.4375, 4.875, 4.625, 4.3125, 4.5, 4.375, 4.625, 4.75, 4.4375, 3.75, 3.75,
3.75, 3.75, 3.75,
];
/// Inter-frame prediction coefficient (alpha) per LM, Q15.
pub const PRED_COEF: [f32; 4] = [29440.0 / 32768.0, 26112.0 / 32768.0, 21248.0 / 32768.0, 16384.0 / 32768.0];
/// Inter-band prediction coefficient (beta) per LM for inter frames, Q15.
pub const BETA_COEF: [f32; 4] = [30147.0 / 32768.0, 22282.0 / 32768.0, 12124.0 / 32768.0, 6554.0 / 32768.0];
/// Beta for intra frames.
pub const BETA_INTRA: f32 = 4915.0 / 32768.0;
/// Laplace model parameters for coarse energy: `[LM][intra][2*band + {0: prob, 1: decay}]`.
pub const E_PROB_MODEL: [[[u8; 42]; 2]; 4] = [
[
[
72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40, 114, 40, 132, 26, 132,
26, 145, 17, 161, 12, 176, 10, 177, 11,
],
[
24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, 91, 67, 100, 59, 108, 50,
120, 40, 122, 37, 97, 43, 78, 50,
],
],
[
[
83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, 146, 19, 162, 12, 165, 10,
178, 7, 189, 6, 190, 8, 177, 9,
],
[
23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60, 104, 60, 117, 52, 123, 44,
138, 35, 133, 31, 97, 38, 77, 45,
],
],
[
[
61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16, 158, 18, 170, 13, 177,
10, 187, 8, 192, 6, 175, 9, 159, 10,
],
[
21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, 112, 56, 129, 51, 132, 40,
150, 33, 140, 29, 98, 35, 77, 42,
],
],
[
[
42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, 154, 26, 166, 21, 173,
16, 184, 13, 184, 10, 150, 13, 139, 15,
],
[
22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52, 117, 55, 135, 49, 137, 39,
157, 32, 145, 29, 97, 33, 77, 40,
],
],
];
/// Inverse CDF for coarse energy when only a few bits remain.
pub const SMALL_ENERGY_ICDF: [u8; 3] = [2, 1, 0];
/// Conservative log2 in 1/8 bits of the number of coded bands (intensity reservation).
pub const LOG2_FRAC_TABLE: [i32; 24] = [0, 8, 13, 16, 19, 21, 23, 24, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 34, 35, 36, 36, 37, 37];
/// TF resolution changes: `[LM][4*transient + 2*tf_select + tf_flag]`.
pub const TF_SELECT_TABLE: [[i8; 8]; 4] = [[0, -1, 0, -1, 0, -1, 0, -1], [0, -1, 0, -2, 1, 0, 1, -1], [0, -2, 0, -3, 2, 0, 1, -1], [0, -2, 0, -3, 3, 0, 1, -1]];
/// Spreading decision `{7, 2, 21, 2}/32`.
pub const SPREAD_ICDF: [u8; 4] = [25, 23, 2, 0];
/// Allocation trim (Table 58).
pub const TRIM_ICDF: [u8; 11] = [126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0];
-285
View File
@@ -1,285 +0,0 @@
//! Shared helpers for the SILK/hybrid round-trip tests: synthetic signals, encoding, decoding
//! with FilmCraft's spec-derived decoder, delay-searched SNR and an Ogg Opus writer for ffmpeg.
#![allow(dead_code)]
use effectcraft_opusenc::OpusEncoder;
pub const SR: f64 = 48_000.0;
pub const FRAME: usize = OpusEncoder::FRAME_SIZE;
/// Two-pole resonator state.
struct Reson {
y1: f64,
y2: f64,
}
impl Reson {
fn run(&mut self, x: f64, f: f64, bw: f64) -> f64 {
let r = (-std::f64::consts::PI * bw / SR).exp();
let a1 = 2.0 * r * (2.0 * std::f64::consts::PI * f / SR).cos();
let a2 = -r * r;
let g = 1.0 - r;
let y = g * x + a1 * self.y1 + a2 * self.y2;
self.y2 = self.y1;
self.y1 = y;
y
}
}
/// Speech-like test signal: a glottal pulse train (100–200 Hz, gliding pitch) through three
/// formant resonators whose frequencies change per syllable, with syllable envelopes, short
/// pauses and a little aspiration noise.
pub fn speech(len: usize, seed: u32) -> Vec<f32> {
let vowels: [[f64; 3]; 5] = [[730.0, 1090.0, 2440.0], [270.0, 2290.0, 3010.0], [530.0, 1840.0, 2480.0], [570.0, 840.0, 2410.0], [300.0, 870.0, 2240.0]];
let syl = (0.24 * SR) as usize;
let mut out = Vec::with_capacity(len);
let mut res = [Reson { y1: 0.0, y2: 0.0 }, Reson { y1: 0.0, y2: 0.0 }, Reson { y1: 0.0, y2: 0.0 }];
let mut phase = 0.0f64;
let mut s = seed;
let mut prev_glottal = 0.0;
for n in 0..len {
let t = n as f64 / SR;
let k = n / syl;
let pos = (n % syl) as f64 / syl as f64;
// Every fourth syllable is a pause.
let pause = k % 4 == 3;
let env = if pause { 0.0 } else { (std::f64::consts::PI * pos).sin().powf(0.7) };
let f0 = 100.0 + 50.0 * (1.0 + (2.0 * std::f64::consts::PI * 0.9 * t).sin()) + 15.0 * ((k * 7) % 5) as f64 - 30.0 * pos;
phase += f0 / SR;
if phase >= 1.0 {
phase -= 1.0;
}
// Rosenberg-like glottal flow; its derivative excites the vocal tract.
let g = if phase < 0.4 {
0.5 * (1.0 - (std::f64::consts::PI * phase / 0.4).cos())
} else if phase < 0.56 {
(std::f64::consts::FRAC_PI_2 * (phase - 0.4) / 0.16).cos()
} else {
0.0
};
let dg = (g - prev_glottal) * 40.0;
prev_glottal = g;
s = s.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
let noise = ((s >> 8) as f64 / (1u32 << 24) as f64 - 0.5) * 0.02;
let v = vowels[(k * 3 + 1) % 5];
let w = vowels[(k * 3 + 4) % 5];
let mix = pos;
let mut x = dg + noise;
let mut y = 0.0;
for (i, r) in res.iter_mut().enumerate() {
let f = v[i] * (1.0 - mix) + w[i] * mix;
y += r.run(x, f, 60.0 + 40.0 * i as f64) * [1.0, 0.6, 0.3][i];
}
x = y * env * 1.2;
out.push(x as f32);
}
let peak = out.iter().fold(0f32, |m, v| m.max(v.abs()));
let g = 0.5 / peak.max(1e-9);
out.iter().map(|v| v * g).collect()
}
/// Music-like test signal: three-note chords of harmonic tones (six partials each), changing
/// every half second with soft attacks and releases.
pub fn music(len: usize) -> Vec<f32> {
let chords: [[f64; 3]; 4] = [[261.63, 329.63, 392.0], [220.0, 277.18, 329.63], [174.61, 220.0, 261.63], [196.0, 246.94, 293.66]];
let seg = (0.5 * SR) as usize;
(0..len)
.map(|n| {
let t = n as f64 / SR;
let k = n / seg;
let pos = (n % seg) as f64 / seg as f64;
let env = (pos * 20.0).min(1.0) * ((1.0 - pos) * 10.0).min(1.0);
let mut x = 0.0;
for (j, &f) in chords[k % 4].iter().enumerate() {
for h in 1..=6 {
x += (2.0 * std::f64::consts::PI * f * h as f64 * t + j as f64).sin() / h as f64;
}
}
(0.12 * env * x) as f32
})
.collect()
}
pub fn interleave(planar: &[Vec<f32>]) -> Vec<f32> {
let c = planar.len();
let len = planar[0].len();
let mut pcm = vec![0f32; len * c];
for (ch, p) in planar.iter().enumerate() {
for (i, &v) in p.iter().enumerate() {
pcm[i * c + ch] = v;
}
}
pcm
}
/// Encodes planar input (padding the tail so the encoder delay is flushed).
pub fn encode(mut enc: OpusEncoder, planar: &[Vec<f32>]) -> (OpusEncoder, Vec<Vec<u8>>) {
let c = planar.len();
let len = planar[0].len();
let total = len + enc.pre_skip() as usize + 16;
let frames = total.div_ceil(FRAME);
let mut pcm = interleave(planar);
pcm.resize(frames * FRAME * c, 0.0);
let packets = pcm.chunks(FRAME * c).map(|f| enc.encode_float(f)).collect();
(enc, packets)
}
/// Decodes with craft-audio, drops the pre-skip and returns planar channels (full length).
pub fn decode(head: &[u8], packets: &[Vec<u8>]) -> Vec<Vec<f32>> {
let mut dec = craft_audio::opus::Decoder::new(head).expect("OpusHead");
dec.set_trim_pre_skip(true);
let mut out: Vec<Vec<f32>> = vec![Vec::new(); dec.channels()];
for p in packets {
let planes = dec.decode(Some(p)).expect("decode");
for (o, p) in out.iter_mut().zip(planes) {
o.extend_from_slice(&p);
}
}
out
}
/// Waveform SNR (dB) of `dec[i + shift]` against `orig[i]`, excluding the first and last 20 ms.
pub fn snr_at(orig: &[f32], dec: &[f32], shift: isize) -> f64 {
let (mut s, mut e) = (0f64, 0f64);
for i in FRAME..orig.len() - FRAME {
let j = i as isize + shift;
let d = if j >= 0 && (j as usize) < dec.len() { dec[j as usize] as f64 } else { 0.0 };
s += (orig[i] as f64).powi(2);
e += (orig[i] as f64 - d).powi(2);
}
10.0 * (s / e.max(1e-30)).log10()
}
/// Best SNR over residual delays of ±`range` samples: (SNR dB, delay).
pub fn snr_search(orig: &[f32], dec: &[f32], range: isize) -> (f64, isize) {
(-range..=range).map(|d| (snr_at(orig, dec, d), d)).fold((f64::MIN, 0), |a, b| if b.0 > a.0 { b } else { a })
}
/// Segmental SNR (dB): mean over 20 ms segments holding signal (each clamped to [-10, 40]).
pub fn seg_snr(orig: &[f32], dec: &[f32], shift: isize) -> f64 {
let mut acc = 0f64;
let mut n = 0;
let mut i = FRAME;
while i + FRAME <= orig.len() - FRAME {
let (mut s, mut e) = (0f64, 0f64);
for k in i..i + FRAME {
let j = k as isize + shift;
let d = if j >= 0 && (j as usize) < dec.len() { dec[j as usize] as f64 } else { 0.0 };
s += (orig[k] as f64).powi(2);
e += (orig[k] as f64 - d).powi(2);
}
if s / FRAME as f64 > 1e-5 {
acc += (10.0 * (s / e.max(1e-30)).log10()).clamp(-10.0, 40.0);
n += 1;
}
i += FRAME;
}
acc / n.max(1) as f64
}
/// Average bitrate (bits per second) of a packet sequence of 20 ms packets.
pub fn bitrate(packets: &[Vec<u8>]) -> f64 {
packets.iter().map(|p| p.len() * 8).sum::<usize>() as f64 / (packets.len() as f64 * 0.02)
}
/// Low-pass filtered copy (FFT brick wall) used to compare band-limited modes fairly.
pub fn lowpass(x: &[f32], fc: f64) -> Vec<f32> {
let n = x.len().next_power_of_two();
let mut re: Vec<f64> = x.iter().map(|&v| v as f64).collect();
re.resize(n, 0.0);
let mut im = vec![0f64; n];
fft(&mut re, &mut im, false);
for k in 0..n {
let f = (k.min(n - k)) as f64 * SR / n as f64;
if f > fc {
re[k] = 0.0;
im[k] = 0.0;
}
}
fft(&mut re, &mut im, true);
re[..x.len()].iter().map(|&v| (v / n as f64) as f32).collect()
}
pub fn fft(re: &mut [f64], im: &mut [f64], inverse: bool) {
let n = re.len();
let mut j = 0;
for i in 1..n {
let mut bit = n >> 1;
while j & bit != 0 {
j ^= bit;
bit >>= 1;
}
j |= bit;
if i < j {
re.swap(i, j);
im.swap(i, j);
}
}
let mut len = 2;
while len <= n {
let ang = if inverse { 2.0 } else { -2.0 } * std::f64::consts::PI / len as f64;
for s in (0..n).step_by(len) {
for k in 0..len / 2 {
let (wr, wi) = ((ang * k as f64).cos(), (ang * k as f64).sin());
let (a, b) = (s + k, s + k + len / 2);
let tr = re[b] * wr - im[b] * wi;
let ti = re[b] * wi + im[b] * wr;
re[b] = re[a] - tr;
im[b] = im[a] - ti;
re[a] += tr;
im[a] += ti;
}
}
len <<= 1;
}
}
// ---- Ogg Opus (RFC 3533 + RFC 7845) for the ffmpeg oracle ----
fn ogg_crc(data: &[u8]) -> u32 {
let mut crc = 0u32;
for &b in data {
crc ^= (b as u32) << 24;
for _ in 0..8 {
crc = if crc & 0x8000_0000 != 0 { (crc << 1) ^ 0x04C1_1DB7 } else { crc << 1 };
}
}
crc
}
fn ogg_page(out: &mut Vec<u8>, packet: &[u8], header_type: u8, granule: u64, seq: u32) {
let mut lacing = vec![255u8; packet.len() / 255];
lacing.push((packet.len() % 255) as u8);
let start = out.len();
out.extend_from_slice(b"OggS");
out.push(0);
out.push(header_type);
out.extend_from_slice(&granule.to_le_bytes());
out.extend_from_slice(&0x4543_4F50u32.to_le_bytes());
out.extend_from_slice(&seq.to_le_bytes());
out.extend_from_slice(&0u32.to_le_bytes());
out.push(lacing.len() as u8);
out.extend_from_slice(&lacing);
out.extend_from_slice(packet);
let crc = ogg_crc(&out[start..]);
out[start + 22..start + 26].copy_from_slice(&crc.to_le_bytes());
}
/// An Ogg Opus file holding `packets` for `len` samples of audio.
pub fn ogg_opus(enc: &OpusEncoder, packets: &[Vec<u8>], len: usize) -> Vec<u8> {
let mut out = Vec::new();
ogg_page(&mut out, &enc.opus_head(), 0x02, 0, 0);
let vendor = b"effectcraft-opusenc";
let mut tags = b"OpusTags".to_vec();
tags.extend_from_slice(&(vendor.len() as u32).to_le_bytes());
tags.extend_from_slice(vendor);
tags.extend_from_slice(&0u32.to_le_bytes());
ogg_page(&mut out, &tags, 0, 0, 1);
let pre_skip = enc.pre_skip() as u64;
for (i, p) in packets.iter().enumerate() {
let last = i + 1 == packets.len();
let granule = if last { pre_skip + len as u64 } else { ((i + 1) * FRAME) as u64 };
ogg_page(&mut out, p, if last { 0x04 } else { 0 }, granule, i as u32 + 2);
}
out
}
-81
View File
@@ -1,81 +0,0 @@
//! ffmpeg as a second, external oracle: every mode/bandwidth written as Ogg Opus must decode
//! without errors with both ffmpeg's native `opus` decoder and `libopus`, honour the pre-skip
//! and end granule, and reproduce the input. Skipped when ffmpeg is not installed.
use crate::common;
use std::process::Command;
use common::*;
use effectcraft_opusenc::{Bandwidth, Mode, OpusEncoder};
const FFMPEG: &str = "/opt/homebrew/bin/ffmpeg";
fn ffmpeg() -> Option<&'static str> {
[FFMPEG, "ffmpeg"].into_iter().find(|exe| Command::new(exe).arg("-version").output().is_ok_and(|o| o.status.success()))
}
/// Decodes an Ogg Opus file with ffmpeg's decoder `codec`; returns interleaved f32 output.
fn ffmpeg_decode(exe: &str, codec: &str, file: &[u8], channels: usize, tag: &str) -> Option<Vec<f32>> {
let has = Command::new(exe).args(["-hide_banner", "-decoders"]).output().ok()?;
if !String::from_utf8_lossy(&has.stdout).lines().any(|l| l.split_whitespace().nth(1) == Some(codec)) {
eprintln!("ffmpeg has no {codec} decoder; skipping it");
return None;
}
let path = std::env::temp_dir().join(format!("effectcraft-opusenc-{}-{tag}.opus", std::process::id()));
std::fs::write(&path, file).unwrap();
let out = Command::new(exe)
.args(["-v", "error", "-c:a", codec, "-i"])
.arg(&path)
.args(["-f", "f32le", "-ac", &channels.to_string(), "-ar", "48000", "-"])
.output()
.expect("run ffmpeg");
let _ = std::fs::remove_file(&path);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(out.status.success(), "ffmpeg ({codec}) failed: {stderr}");
// ffmpeg's native decoder also makes its raw-PCM muxer print a timestamp complaint for
// narrowband SILK streams written by libopus itself; that line is about ffmpeg's own
// timestamping, not the bitstream, so it is the one message tolerated.
let errors: Vec<&str> = stderr.lines().filter(|l| !l.trim().is_empty() && !l.contains("non monotonically increasing dts to muxer")).collect();
assert!(errors.is_empty(), "ffmpeg ({codec}) reported errors: {errors:?}");
Some(out.stdout.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect())
}
#[test]
fn ffmpeg_decodes_every_mode() {
let Some(exe) = ffmpeg() else {
eprintln!("ffmpeg not installed; skipping the oracle test");
return;
};
let len = 48_000;
let sp = speech(len + 30_000, 3);
let left = sp[..len].to_vec();
let right: Vec<f32> = sp[30_000..].iter().map(|v| v * 0.7).collect();
let cases = [
(1usize, Mode::Silk, Bandwidth::Narrowband, 8_000u32, 3_400.0, 8.0),
(1, Mode::Silk, Bandwidth::Mediumband, 11_000, 5_000.0, 10.0),
(1, Mode::Silk, Bandwidth::Wideband, 16_000, 7_000.0, 12.0),
(2, Mode::Silk, Bandwidth::Wideband, 32_000, 7_000.0, 12.0),
(1, Mode::Hybrid, Bandwidth::SuperWideband, 24_000, 24_000.0, 10.0),
(1, Mode::Hybrid, Bandwidth::Fullband, 32_000, 24_000.0, 10.0),
(2, Mode::Hybrid, Bandwidth::Fullband, 64_000, 24_000.0, 10.0),
(2, Mode::Celt, Bandwidth::Fullband, 96_000, 24_000.0, 10.0),
];
for (c, mode, bw, rate, fc, min_snr) in cases {
let planar: Vec<Vec<f32>> = if c == 1 { vec![left.clone()] } else { vec![left.clone(), right.clone()] };
let (enc, packets) = encode(OpusEncoder::with_mode(c as u8, rate, mode, bw), &planar);
let file = ogg_opus(&enc, &packets, len);
for codec in ["opus", "libopus"] {
let Some(dec) = ffmpeg_decode(exe, codec, &file, c, &format!("{mode:?}{bw:?}{c}{codec}")) else {
continue;
};
assert_eq!(dec.len(), len * c, "{codec}: pre-skip and end granule must be honoured");
for (ch, x) in planar.iter().enumerate() {
let d: Vec<f32> = (0..len).map(|i| dec[i * c + ch]).collect();
let (snr, delay) = snr_search(&lowpass(x, fc), &d, 6);
eprintln!("ffmpeg {codec:7} {mode:?}/{bw:?} {c} ch @ {} kb/s, channel {ch}: SNR {snr:.1} dB (residual delay {delay})", rate / 1000);
assert!(snr > min_snr, "{codec} {mode:?}/{bw:?}: SNR {snr:.1} dB");
}
}
}
}
-5
View File
@@ -1,5 +0,0 @@
//! The crate's integration tests, one binary (each module is a former `tests/<name>.rs`).
mod common;
mod ffmpeg;
mod modes;
-272
View File
@@ -1,272 +0,0 @@
//! SILK, hybrid and CELT round trips through FilmCraft's spec-derived Opus decoder: every
//! mode/bandwidth decodes, waveform quality on speech-like and music-like signals, bitrate
//! tracking, stereo, the application tables and delay alignment.
use crate::common;
use common::*;
use effectcraft_opusenc::{Application, Bandwidth, Mode, OpusEncoder};
struct Run {
snr: f64,
seg: f64,
delay: isize,
kbps: f64,
packets: Vec<Vec<u8>>,
}
/// Encodes `planar`, decodes, and measures each channel against `reference` (searching a
/// residual delay of up to ±6 samples).
fn run(enc: OpusEncoder, planar: &[Vec<f32>], reference: &[Vec<f32>]) -> Vec<Run> {
let (enc, packets) = encode(enc, planar);
let dec = decode(&enc.opus_head(), &packets);
let kbps = bitrate(&packets) / 1000.0;
reference
.iter()
.zip(&dec)
.map(|(r, d)| {
assert!(d.len() >= r.len(), "decoded {} < {}", d.len(), r.len());
assert!(d.iter().all(|v| v.is_finite()));
let (snr, delay) = snr_search(r, d, 6);
Run { snr, seg: seg_snr(r, d, delay), delay, kbps, packets: packets.clone() }
})
.collect()
}
fn mono(mode: Mode, bw: Bandwidth, rate: u32, x: &[f32], fc: f64) -> Run {
let reference = if fc < 20_000.0 { lowpass(x, fc) } else { x.to_vec() };
run(OpusEncoder::with_mode(1, rate, mode, bw), &[x.to_vec()], &[reference]).remove(0)
}
/// Upper edge of the band a SILK bandwidth keeps (for band-limited references).
fn band_edge(bw: Bandwidth) -> f64 {
match bw {
Bandwidth::Narrowband => 3_400.0,
Bandwidth::Mediumband => 5_000.0,
Bandwidth::Wideband => 7_000.0,
_ => 24_000.0,
}
}
const LEN: usize = 96_000;
#[test]
fn every_mode_and_bandwidth_decodes() {
let sp = speech(LEN + 40_000, 11);
let left = sp[..LEN].to_vec();
let right: Vec<f32> = sp[40_000..].iter().map(|v| v * 0.6).collect();
for (mode, bw, config, rate) in [
(Mode::Silk, Bandwidth::Narrowband, 1u8, 8_000u32),
(Mode::Silk, Bandwidth::Mediumband, 5, 11_000),
(Mode::Silk, Bandwidth::Wideband, 9, 16_000),
(Mode::Hybrid, Bandwidth::SuperWideband, 13, 24_000),
(Mode::Hybrid, Bandwidth::Fullband, 15, 32_000),
(Mode::Celt, Bandwidth::Fullband, 31, 64_000),
] {
for c in [1usize, 2] {
let planar = if c == 1 { vec![left.clone()] } else { vec![left.clone(), right.clone()] };
let reference: Vec<Vec<f32>> = planar.iter().map(|x| lowpass(x, band_edge(bw))).collect();
let enc = OpusEncoder::with_mode(c as u8, rate * c as u32, mode, bw);
assert_eq!((enc.mode(), enc.bandwidth()), (mode, bw));
let res = run(enc, &planar, &reference);
for p in &res[0].packets {
assert_eq!(p[0], (config << 3) | if c == 2 { 4 } else { 0 }, "TOC of {mode:?}/{bw:?}");
assert!(p.len() >= 3 && p.len() <= 1276);
}
for (ch, r) in res.iter().enumerate() {
eprintln!(
"{mode:?}/{bw:?} {c} ch @ {} kb/s: {:.1} kb/s, channel {ch} SNR {:.1} dB, segSNR {:.1} dB, residual delay {}",
rate * c as u32 / 1000,
r.kbps,
r.snr,
r.seg,
r.delay
);
assert!(r.snr > 8.0, "{mode:?}/{bw:?} {c} ch channel {ch}: SNR {:.1} dB", r.snr);
assert!(r.delay.abs() <= 1, "{mode:?}/{bw:?}: residual delay {} (pre-skip mismatch)", r.delay);
}
}
}
}
#[test]
fn silk_speech_quality() {
let x = speech(LEN, 7);
for (bw, rate, min_snr) in [
(Bandwidth::Narrowband, 8_000u32, 10.0),
(Bandwidth::Mediumband, 11_000, 14.0),
(Bandwidth::Wideband, 12_000, 14.0),
(Bandwidth::Wideband, 16_000, 18.0),
(Bandwidth::Wideband, 24_000, 24.0),
] {
let r = mono(Mode::Silk, bw, rate, &x, band_edge(bw));
eprintln!("SILK {bw:?} speech @ {} kb/s: {:.1} kb/s, SNR {:.1} dB, segSNR {:.1} dB", rate / 1000, r.kbps, r.snr, r.seg);
assert!(r.snr > min_snr && r.seg > min_snr - 3.0, "SILK {bw:?} @ {rate}: SNR {:.1} / seg {:.1}", r.snr, r.seg);
}
}
#[test]
fn silk_beats_celt_on_speech_at_low_rate() {
let x = speech(LEN, 5);
// Both measured against the full-band input.
let silk = mono(Mode::Silk, Bandwidth::Wideband, 12_000, &x, 24_000.0);
let celt = mono(Mode::Celt, Bandwidth::Fullband, 12_000, &x, 24_000.0);
eprintln!(
"12 kb/s mono speech: SILK WB {:.1} dB (seg {:.1}) at {:.1} kb/s vs CELT {:.1} dB (seg {:.1}) at {:.1} kb/s",
silk.snr, silk.seg, silk.kbps, celt.snr, celt.seg, celt.kbps
);
assert!(silk.snr > celt.snr + 3.0, "SILK {:.1} dB vs CELT {:.1} dB", silk.snr, celt.snr);
assert!(silk.seg > celt.seg + 3.0, "SILK seg {:.1} dB vs CELT seg {:.1} dB", silk.seg, celt.seg);
}
#[test]
fn hybrid_quality() {
let sp = speech(LEN, 9);
let mu = music(LEN);
for (name, x, bw, rate, min_snr) in [
("speech", &sp, Bandwidth::SuperWideband, 24_000u32, 18.0),
("speech", &sp, Bandwidth::Fullband, 32_000, 20.0),
("music", &mu, Bandwidth::SuperWideband, 24_000, 9.0),
("music", &mu, Bandwidth::Fullband, 32_000, 10.0),
] {
let r = mono(Mode::Hybrid, bw, rate, x, 24_000.0);
eprintln!("hybrid {bw:?} {name} @ {} kb/s: {:.1} kb/s, SNR {:.1} dB, segSNR {:.1} dB", rate / 1000, r.kbps, r.snr, r.seg);
assert!(r.snr > min_snr, "hybrid {bw:?} {name}: SNR {:.1} dB", r.snr);
// Constant packet size.
assert!(r.packets.iter().all(|p| p.len() == rate as usize / 400));
}
}
#[test]
fn silk_music_quality() {
let x = music(LEN);
for (rate, min_snr) in [(12_000u32, 5.0), (16_000, 8.0), (24_000, 13.0)] {
let r = mono(Mode::Silk, Bandwidth::Wideband, rate, &x, 7_000.0);
eprintln!("SILK WB music @ {} kb/s: {:.1} kb/s, SNR {:.1} dB", rate / 1000, r.kbps, r.snr);
assert!(r.snr > min_snr, "SILK music @ {rate}: SNR {:.1} dB", r.snr);
}
}
#[test]
fn bitrate_tracking() {
let sp = speech(LEN, 13);
let mu = music(LEN);
for (name, x) in [("speech", &sp), ("music", &mu)] {
for (mode, bw, rate) in [
(Mode::Silk, Bandwidth::Narrowband, 6_000u32),
(Mode::Silk, Bandwidth::Narrowband, 9_000),
(Mode::Silk, Bandwidth::Mediumband, 11_000),
(Mode::Silk, Bandwidth::Wideband, 12_000),
(Mode::Silk, Bandwidth::Wideband, 20_000),
(Mode::Silk, Bandwidth::Wideband, 32_000),
(Mode::Hybrid, Bandwidth::SuperWideband, 20_000),
(Mode::Hybrid, Bandwidth::Fullband, 36_000),
] {
let r = mono(mode, bw, rate, x, band_edge(bw));
let ratio = r.kbps * 1000.0 / rate as f64;
eprintln!("{name} {mode:?}/{bw:?} target {} kb/s: actual {:.2} kb/s ({:+.0} %), SNR {:.1} dB", rate / 1000, r.kbps, (ratio - 1.0) * 100.0, r.snr);
assert!((0.75..=1.25).contains(&ratio), "{name} {mode:?}/{bw:?} @ {rate}: {:.2} kb/s", r.kbps);
}
}
}
#[test]
fn stereo_silk_and_hybrid() {
let sp = speech(LEN + 40_000, 21);
let a = sp[..LEN].to_vec();
let b: Vec<f32> = sp[40_000..].iter().map(|v| v * 0.7).collect();
let mu = music(LEN);
let cases: Vec<(&str, Vec<f32>, Vec<f32>)> = vec![
("dual mono", a.clone(), a.clone()),
("panned", a.iter().map(|v| v * 0.9).collect(), a.iter().map(|v| v * 0.35).collect()),
("independent", a.clone(), b),
("speech+music", a.iter().zip(&mu).map(|(x, y)| 0.8 * x + 0.3 * y).collect(), a.iter().zip(&mu).map(|(x, y)| 0.3 * x + 0.8 * y).collect()),
];
for (name, l, r) in &cases {
for (mode, bw, rate, min_snr) in [(Mode::Silk, Bandwidth::Wideband, 32_000u32, 9.0), (Mode::Hybrid, Bandwidth::Fullband, 64_000, 9.0)] {
let fc = band_edge(bw);
let res = run(OpusEncoder::with_mode(2, rate, mode, bw), &[l.clone(), r.clone()], &[lowpass(l, fc), lowpass(r, fc)]);
eprintln!("stereo {name} {mode:?}/{bw:?} @ {} kb/s: {:.1} kb/s, L {:.1} dB, R {:.1} dB", rate / 1000, res[0].kbps, res[0].snr, res[1].snr);
assert!(res[0].snr > min_snr && res[1].snr > min_snr, "stereo {name} {mode:?}: L {:.1} R {:.1}", res[0].snr, res[1].snr);
}
}
}
#[test]
fn application_tables_and_pre_skip() {
let cases = [
(Application::Voip, 1u8, 8_000u32, Mode::Silk, Bandwidth::Narrowband),
(Application::Voip, 1, 11_000, Mode::Silk, Bandwidth::Mediumband),
(Application::Voip, 1, 16_000, Mode::Silk, Bandwidth::Wideband),
(Application::Voip, 1, 24_000, Mode::Hybrid, Bandwidth::SuperWideband),
(Application::Voip, 1, 32_000, Mode::Hybrid, Bandwidth::Fullband),
(Application::Voip, 1, 48_000, Mode::Celt, Bandwidth::Fullband),
(Application::Voip, 2, 32_000, Mode::Silk, Bandwidth::Wideband),
(Application::Audio, 1, 12_000, Mode::Silk, Bandwidth::Wideband),
(Application::Audio, 1, 24_000, Mode::Hybrid, Bandwidth::Fullband),
(Application::Audio, 2, 48_000, Mode::Hybrid, Bandwidth::Fullband),
(Application::Audio, 1, 64_000, Mode::Celt, Bandwidth::Fullband),
(Application::Audio, 2, 128_000, Mode::Celt, Bandwidth::Fullband),
];
for (app, c, rate, mode, bw) in cases {
let e = OpusEncoder::with_application(c, rate, app);
assert_eq!((e.mode(), e.bandwidth(), e.application()), (mode, bw, app), "{app:?} {c} ch @ {rate}");
assert_eq!(e.pre_skip(), 120, "every mode is aligned to a 120-sample delay");
}
assert_eq!(OpusEncoder::new(2, 128_000).mode(), Mode::Celt);
assert_eq!(OpusEncoder::new(1, 12_000).application(), Application::Audio);
}
#[test]
#[should_panic(expected = "unsupported mode/bandwidth")]
fn invalid_mode_bandwidth_panics() {
let _ = OpusEncoder::with_mode(1, 16_000, Mode::Silk, Bandwidth::Fullband);
}
#[test]
fn extreme_inputs_stay_decodable() {
let len = 24_000;
let mut s = 1u32;
let noise: Vec<f32> = (0..len)
.map(|_| {
s = s.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
(s >> 8) as f32 / (1u32 << 23) as f32 - 1.0
})
.collect();
let square: Vec<f32> = (0..len).map(|i| if (i / 60) % 2 == 0 { 0.999 } else { -0.999 }).collect();
let clicks: Vec<f32> = (0..len).map(|i| if i % 4_801 == 0 { 1.0 } else { 0.0 }).collect();
let dc = vec![0.7f32; len];
let nan: Vec<f32> = (0..len).map(|i| if i % 1000 == 0 { f32::NAN } else { 0.1 * (i as f32 * 0.01).sin() }).collect();
for (name, x) in [("noise", &noise), ("square", &square), ("clicks", &clicks), ("dc", &dc), ("nan", &nan)] {
for (mode, bw, rate) in
[(Mode::Silk, Bandwidth::Narrowband, 6_000u32), (Mode::Silk, Bandwidth::Wideband, 40_000), (Mode::Hybrid, Bandwidth::Fullband, 24_000)]
{
for c in [1usize, 2] {
let planar: Vec<Vec<f32>> = if c == 1 { vec![x.clone()] } else { vec![x.clone(), x.iter().map(|v| -0.5 * v).collect()] };
let (enc, packets) = encode(OpusEncoder::with_mode(c as u8, rate * c as u32, mode, bw), &planar);
assert!(packets.iter().all(|p| p.len() >= 3 && p.len() <= 1276), "{name} {mode:?}: packet sizes");
let dec = decode(&enc.opus_head(), &packets);
let peak = dec.iter().flatten().fold(0f32, |m, v| m.max(v.abs()));
let snr = if name == "nan" { f64::NAN } else { snr_search(&planar[0], &dec[0], 2).0 };
eprintln!("{name} {mode:?}/{bw:?} {c} ch: peak {peak:.2}, SNR {snr:.1} dB, {:.1} kb/s", bitrate(&packets) / 1000.0);
assert!(dec.iter().all(|ch| ch.iter().all(|v| v.is_finite() && v.abs() <= 4.0)), "{name} {mode:?}/{bw:?} {c} ch: bad output");
}
}
}
}
#[test]
fn silk_and_hybrid_silence() {
for (mode, bw, rate) in [(Mode::Silk, Bandwidth::Wideband, 16_000u32), (Mode::Hybrid, Bandwidth::Fullband, 32_000)] {
let x = vec![0f32; 48_000];
let (enc, packets) = encode(OpusEncoder::with_mode(1, rate, mode, bw), std::slice::from_ref(&x));
let dec = decode(&enc.opus_head(), &packets);
let peak = dec[0].iter().fold(0f32, |m, v| m.max(v.abs()));
let kbps = bitrate(&packets) / 1000.0;
eprintln!("{mode:?} silence: {kbps:.2} kb/s, decoded peak {peak:.2e}");
assert!(peak < 1e-3, "{mode:?} silence decodes to peak {peak}");
if mode == Mode::Silk {
assert!(kbps < 4.0, "SILK silence costs {kbps:.2} kb/s");
}
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-eps = { workspace = true }
craft-ps = { workspace = true }
craft-pdf.workspace = true
# The PDF a VectorCraft document carries (Create PDF-Compatible File).
craft-doc = { workspace = true }
+95 -5
View File
@@ -10,6 +10,9 @@ pub(crate) const MAX_PATH: usize = 1 << 20;
/// The deepest the open groups nest (the render tree and its consumers recurse through them).
const MAX_DEPTH: usize = 256;
/// The most characters of a logical text label kept in a group name.
pub(crate) const MAX_LABEL: usize = 256;
#[derive(Clone, Copy, PartialEq, Eq)]
enum Kind {
Plain,
@@ -19,6 +22,9 @@ enum Kind {
pub(crate) struct Builder {
stack: Vec<(Group, Kind)>,
/// Logical text labels never share the graphics-state group stack.
text_labels: Vec<String>,
text_run: bool,
/// Which of the finished top-level nodes are optional-content layers.
pub top_layers: Vec<bool>,
pub skipped: Vec<String>,
@@ -34,14 +40,14 @@ pub(crate) fn group(name: &str) -> Group {
impl Builder {
pub fn new() -> Builder {
Builder { stack: vec![(group("page"), Kind::Plain)], top_layers: vec![], skipped: vec![], shapes: 0, base: 0 }
Builder { stack: vec![(group("page"), Kind::Plain)], text_labels: vec![], text_run: false, top_layers: vec![], skipped: vec![], shapes: 0, base: 0 }
}
/// A builder for content that will be placed inside this one's open groups and `wrap` more
/// groups (a pattern's `Pattern` and `Tile`, a soft mask's two), so its nesting limit counts
/// them.
pub fn nested(&self, wrap: usize) -> Builder {
Builder { base: self.base.saturating_add(self.stack.len()).saturating_add(wrap), ..Builder::new() }
Builder { base: self.base.saturating_add(self.stack.len()).saturating_add(self.text_labels.len()).saturating_add(wrap), ..Builder::new() }
}
pub fn depth(&self) -> usize {
@@ -50,7 +56,7 @@ impl Builder {
/// `wrap` more groups fit under the open ones within [`MAX_DEPTH`].
pub fn has_room(&self, wrap: usize) -> bool {
self.base.saturating_add(self.stack.len()).saturating_add(wrap) <= MAX_DEPTH
self.base.saturating_add(self.stack.len()).saturating_add(self.text_labels.len()).saturating_add(wrap) <= MAX_DEPTH
}
pub fn skip(&mut self, what: &str) {
@@ -60,6 +66,7 @@ impl Builder {
}
fn add(&mut self, n: Node, layer: bool) {
self.text_run = false;
if self.stack.len() == 1 {
self.top_layers.push(layer);
}
@@ -70,6 +77,9 @@ impl Builder {
/// Close every group opened above `depth`.
pub fn close_to(&mut self, depth: usize) {
if self.stack.len() > depth.max(1) {
self.text_run = false;
}
while self.stack.len() > depth.max(1) {
let Some((g, k)) = self.stack.pop() else { break };
// Empty clipping groups draw nothing.
@@ -83,6 +93,7 @@ impl Builder {
/// Open `g` as the group drawn into from now on; past [`MAX_DEPTH`] the group is dropped
/// (what would have been drawn inside it goes to the open group) and noted as skipped.
fn push_group(&mut self, g: Group, kind: Kind) {
self.text_run = false;
if self.base.saturating_add(self.stack.len()) >= MAX_DEPTH {
self.skip("group nesting limit");
return;
@@ -109,9 +120,74 @@ impl Builder {
self.push_group(g, Kind::Plain);
}
/// Start a bounded logical annotation independently of graphics-state nesting.
pub fn begin_text(&mut self, text: &str) -> bool {
self.text_run = false;
if self.text_labels.len() >= MAX_DEPTH {
self.skip("logical text nesting limit");
return false;
}
// Every paint wrapped by the annotation carries its own copy of the label, so a long
// /ActualText is cut to MAX_LABEL characters rather than copied whole into each.
let text: String = text.chars().take(MAX_LABEL).collect();
self.text_labels.push(format!("Text: {text}"));
true
}
pub fn end_text(&mut self) {
self.text_run = false;
self.text_labels.pop();
}
/// Add a finished node (an image, pattern tiles).
pub fn push(&mut self, n: Node) {
pub fn push(&mut self, mut n: Node) {
// Marked content is metadata, not a transparency boundary. Wrap only the
// painted node, without closing, splitting or reopening any live state group.
// Finished forms may already contain groups: metadata cannot extend the cap.
fn group_depth(n: &Node, at: usize) -> usize {
match n {
Node::Group(g) => {
if at >= MAX_DEPTH {
return MAX_DEPTH;
}
1 + g.children.iter().map(|n| group_depth(n, at + 1)).max().unwrap_or(0)
}
_ => 0,
}
}
let contained = if self.text_labels.is_empty() { 0 } else { group_depth(&n, 0) };
let room = MAX_DEPTH.saturating_sub(self.base.saturating_add(self.stack.len()).saturating_add(contained));
if self.text_labels.len() > room {
self.skip("group nesting limit");
}
// Keep consecutive glyphs of one annotation together only inside the same
// graphics-state container. A knockout parent's children must stay separate.
let coalesce = !self.text_labels.is_empty() && self.text_labels.len() <= room;
if self.text_run
&& coalesce
&& let Some((parent, _)) = self.stack.last_mut()
&& !parent.knockout
{
let mut target = parent.children.last_mut();
for _ in 1..self.text_labels.len() {
target = match target {
Some(Node::Group(g)) => g.children.first_mut(),
_ => None,
};
}
if let Some(Node::Group(g)) = target {
g.children.push(n);
return;
}
}
for label in self.text_labels.iter().take(room).rev() {
let mut g = group(label);
g.isolated = false;
g.children.push(n);
n = Node::Group(g);
}
self.add(n, false);
self.text_run = coalesce;
}
pub fn fill_stroke(&mut self, path: BezPath, transform: Affine, fill: Option<(Paint, f64, FillRule)>, stroke: Option<Stroke>) {
@@ -128,7 +204,7 @@ impl Builder {
None => (None, 1.0, FillRule::NonZero),
};
let s = Shape { name: name.into(), geom: Geom::Path(path), transform, fill: f, fill_opacity: fo, fill_rule: rule, stroke, opacity: 1.0 };
self.add(Node::Shape(s), false);
self.push(Node::Shape(s));
}
/// The finished top-level nodes and their layer flags.
@@ -139,3 +215,17 @@ impl Builder {
(root, flags, self.skipped)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_long_logical_label_is_cut_once_at_begin_text() {
let mut b = Builder::new();
assert!(b.begin_text(&"é".repeat(MAX_LABEL * 4)));
assert_eq!(b.text_labels[0].chars().count(), "Text: ".len() + MAX_LABEL);
assert!(b.begin_text("short"));
assert_eq!(b.text_labels[1], "Text: short");
}
}
-8
View File
@@ -366,14 +366,6 @@ pub struct Cff {
pub encoding: [u16; 256],
}
/// The first font name in a CFF program's Name INDEX.
pub fn font_name(data: &[u8]) -> Option<String> {
let hdr = *data.get(2)? as usize;
let (names, _) = index(data, hdr)?;
let r = names.first()?.clone();
Some(String::from_utf8_lossy(data.get(r)?).into_owned())
}
impl Cff {
pub fn parse(data: &[u8]) -> Option<Cff> {
let d = data;
+25 -2
View File
@@ -779,7 +779,24 @@ impl Func {
}
}
/// PDF text string → Rust string (UTF-16BE with a byte-order mark, else PDFDocEncoding ≈ Latin-1).
/// PDFDocEncoding (ISO 32000-2 Annex D) where it differs from Latin-1: bytes 0x18–0x1F and
/// 0x80–0xA0 (0x9F is undefined).
const PDFDOC_LOW: [char; 8] = ['˘', 'ˇ', 'ˆ', '˙', '˝', '˛', '˚', '˜'];
const PDFDOC_HIGH: [char; 33] = [
'•', '†', '‡', '…', '—', '–', 'ƒ', '⁄', '‹', '›', '−', '‰', '„', '“', '”', '‘', '’', '‚', '™', 'fi', 'fl', 'Ł', 'Œ', 'Š', 'Ÿ', 'Ž', 'ı', 'ł', 'œ', 'š', 'ž',
'\u{FFFD}', '€',
];
fn pdfdoc_char(c: u8) -> char {
match c {
0x18..=0x1F => PDFDOC_LOW[usize::from(c - 0x18)],
0x80..=0xA0 => PDFDOC_HIGH[usize::from(c - 0x80)],
_ => char::from(c),
}
}
/// PDF text string → Rust string (UTF-16BE with a byte-order mark, UTF-8 with one, else
/// PDFDocEncoding).
pub fn text_string(b: &[u8]) -> String {
if b.starts_with(&[0xFE, 0xFF]) {
let u: Vec<u16> = b[2..].as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect();
@@ -788,13 +805,19 @@ pub fn text_string(b: &[u8]) -> String {
if let Some(rest) = b.strip_prefix(&[0xEF, 0xBB, 0xBF]) {
return String::from_utf8_lossy(rest).into_owned();
}
b.iter().map(|&c| c as char).collect()
b.iter().map(|&c| pdfdoc_char(c)).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_strings_without_a_bom_are_pdfdocencoding() {
assert_eq!(text_string(b"a\x80b\x92\xa0\x18\xe9"), "a•b™€˘é");
assert_eq!(text_string(b"\xfe\xff\x00A\x20\x22"), "A•");
}
#[test]
fn sample_count_at_and_over_the_cap() {
// 4 Mi entries × 4 outputs fill the cap exactly; a fifth output passes it.
-30
View File
@@ -350,36 +350,6 @@ impl Font {
}
}
/// A simple font outside a PDF (EPS text): `program` is an embedded font program (CFF when
/// the flag is set, else Type 1), otherwise the bundled stand-in for `name` is used;
/// `names` is the encoding (code → glyph name), else the program's built-in encoding or
/// StandardEncoding. Advances are the program's.
pub fn standalone(name: &str, program: Option<(&[u8], bool)>, names: Option<Vec<Option<String>>>) -> Font {
let mut f = Font::blank(name, false);
f.program = match program {
Some((data, true)) => Cff::parse(data).map(|c| Program::Cff(Box::new(c))),
Some((data, false)) => Type1::parse(data).map(|t| Program::Type1(Box::new(t))),
None => None,
}
.unwrap_or_else(|| Program::Fallback(fallback(name)));
match &f.program {
Program::Cff(c) => f.matrix = Affine::new(c.font_matrix),
Program::Type1(t) => f.matrix = t.font_matrix.map(Affine::new).unwrap_or(f.matrix),
_ => {}
}
let symbol = matches!(f.program, Program::Fallback(_)) && (name.contains("Symbol") || name.contains("Dingbats"));
f.names = match names {
// Unencoded codes draw .notdef (not the built-in encoding).
Some(n) => (0..256).map(|c| Some(n.get(c).cloned().flatten().unwrap_or_else(|| ".notdef".into()))).collect(),
None => match &f.program {
Program::Type1(t) if !t.encoding.is_empty() => (0..256).map(|c| t.encoding.get(&(c as u8)).cloned()).collect(),
_ if symbol => vec![None; 256],
_ => (0..256).map(|c| Base::Standard.name(c as u8)).collect(),
},
};
f
}
pub fn load(file: &File, d: &Dict, skipped: &mut Vec<String>) -> Font {
let subtype = file.get(d, "Subtype").and_then(Obj::name).unwrap_or("Type1").to_string();
let base_name = file.get(d, "BaseFont").and_then(Obj::name).unwrap_or("").to_string();
+51 -7
View File
@@ -18,7 +18,6 @@ mod font;
mod image;
mod object;
mod page;
mod ps;
mod read;
mod shading;
mod type1;
@@ -229,13 +228,58 @@ fn parse_pdf(bytes: &[u8], index: usize, framing: Framing) -> Result<Doc, Error>
Ok(finish(nodes, flags, skipped, dw.max(1.0), dh.max(1.0), to_doc))
}
fn eps_fallback_font(name: &[u8]) -> Option<&'static [u8]> {
use effectcraft_text::fonts::{INTER_BOLD, INTER_ITALIC, INTER_REGULAR, INTER_SEMIBOLD, JETBRAINS_MONO_REGULAR, NOTO_SERIF_REGULAR};
let name = String::from_utf8_lossy(name).to_ascii_lowercase();
Some(if name.contains("courier") || name.contains("mono") || name.contains("consol") {
JETBRAINS_MONO_REGULAR
} else if name.contains("times")
|| name.contains("serif") && !name.contains("sans")
|| name.contains("georgia")
|| name.contains("garamond")
|| name.contains("minion")
|| name.contains("roman")
{
NOTO_SERIF_REGULAR
} else if name.contains("semibold") || name.contains("demi") {
INTER_SEMIBOLD
} else if name.contains("bold") || name.contains("black") || name.contains("heavy") {
INTER_BOLD
} else if name.contains("italic") || name.contains("oblique") {
INTER_ITALIC
} else {
INTER_REGULAR
})
}
fn parse_eps(bytes: &[u8]) -> Result<Doc, Error> {
let (ps, bb) = ps::eps_parts(bytes).ok_or(Error::NoBoundingBox)?;
let mut it = ps::Interp::new(bb);
it.run(ps);
let (nodes, flags, skipped) = it.b.finish();
let to_doc = Affine::new([1.0, 0.0, 0.0, -1.0, -bb[0], bb[3]]);
Ok(finish(nodes, flags, skipped, (bb[2] - bb[0]).max(1.0), (bb[3] - bb[1]).max(1.0), to_doc))
let limits = craft_ps::read::Limits {
// Preserve EffectCraft's established 8,000,000-unit PostScript execution budget.
steps: 8_000_000,
stack_overflow_noop: true,
skip_images: true,
keep_unpainted: true,
zero_division_zero: true,
fallback_font: Some(eps_fallback_font),
..Default::default()
};
let converted = craft_ps::read::eps_to_pdf(bytes, limits).map_err(|message| match message.as_str() {
"not PostScript" | "the EPS has no bounding box" => Error::NoBoundingBox,
_ => Error::Read(message),
})?;
let mut doc = parse_pdf(&converted.pdf, 0, Framing::Placed)?;
for skipped in &mut doc.skipped {
if skipped == "content (path limit)" {
*skipped = "PostScript (path limit)".to_string();
}
}
doc.skipped.extend(converted.warnings.into_iter().map(|warning| match warning.as_str() {
"the program runs too long" | "out of memory" => "PostScript (operation limit)".to_string(),
"executable name resolution limit" => "PostScript (recursion limit)".to_string(),
"content (path limit)" => "PostScript (path limit)".to_string(),
_ => warning,
}));
Ok(doc)
}
/// The document's layer names (the root's children), bottom first.
+1
View File
@@ -516,6 +516,7 @@ fn ascii_hex(data: &[u8], max: usize) -> Option<Vec<u8>> {
Some(out)
}
#[cfg(test)]
pub fn ascii85(data: &[u8]) -> Vec<u8> {
// The PostScript reader uses this unbounded entry point. No Vec can hold more
// than usize::MAX bytes, so its output cannot exceed this bound.
+34 -10
View File
@@ -209,7 +209,7 @@ pub(crate) struct Interp<'a> {
cur: Option<Point>,
start: Option<Point>,
pending_clip: Option<FillRule>,
marked: Vec<Option<usize>>,
marked: Vec<Marked>,
/// The page box in default user space (for `sh`).
page_box: [f64; 4],
budget: usize,
@@ -237,6 +237,12 @@ enum TextItem {
Adjust(f64),
}
enum Marked {
None,
Layer(usize),
Text,
}
impl<'a> Interp<'a> {
pub fn new(file: &'a File, page_box: [f64; 4]) -> Self {
Interp {
@@ -488,7 +494,7 @@ impl<'a> Interp<'a> {
}
"d1" => self.gs.lock_color = true,
"BI" => self.inline_image(&mut lx, res),
"BMC" => self.marked.push(None),
"BMC" => self.marked.push(Marked::None),
"BDC" => {
let layer = match (args.first().and_then(Obj::name), args.get(1)) {
(Some("OC"), Some(Obj::Name(prop))) => self
@@ -499,19 +505,37 @@ impl<'a> Interp<'a> {
(Some("OC"), Some(Obj::Dict(ocg))) => self.ocg_name(ocg),
_ => None,
};
match layer {
Some(name) => {
self.marked.push(Some(self.b.depth()));
let props = match args.get(1) {
Some(Obj::Name(name)) => self.file.get_dict(res, "Properties").and_then(|p| self.file.get_dict(p, name)),
Some(obj) => self.file.resolve(obj).dict(),
None => None,
};
let actual_text =
(args.first().and_then(Obj::name) == Some("Span")).then_some(props).flatten().and_then(|p| self.file.get(p, "ActualText")).and_then(
|v| match v {
Obj::Str(bytes) => Some(text_string(bytes)),
_ => None,
},
);
match (layer, actual_text) {
(Some(name), _) => {
self.marked.push(Marked::Layer(self.b.depth()));
self.b.open(&name, true);
}
None => self.marked.push(None),
(None, Some(text)) => {
let mark = if self.b.begin_text(&text) { Marked::Text } else { Marked::None };
self.marked.push(mark);
}
(None, None) => self.marked.push(Marked::None),
}
}
"EMC" => {
if let Some(Some(d)) = self.marked.pop() {
self.close_to(d);
"EMC" => match self.marked.pop() {
Some(Marked::Layer(d)) => self.close_to(d),
Some(Marked::Text) => {
self.b.end_text();
}
}
_ => {}
},
_ => {}
}
// Colour operands set by the device-space shorthands.
-49
View File
@@ -1,49 +0,0 @@
//! Encapsulated PostScript: a small PostScript interpreter (PostScript Language Reference, 3rd
//! edition, and the EPSF 3.0 specification) covering what exported vector EPS files use: the
//! operand and dictionary stacks, procedures and `def`, arithmetic, control flow, arrays and
//! dictionaries, the graphics state, path construction (including arcs) and painting, clipping
//! and device colours, and text (`findfont` / `selectfont`, `show` and its variants, `charpath`,
//! `stringwidth`, re-encoded fonts) with embedded Type 1 and CFF programs or the bundled
//! stand-in fonts. Images are skipped (their inline data is stepped over), as is
//! Level 3 smooth shading (`shfill`).
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use effectcraft_svg::{Affine, BezPath, Cap, FillRule, Join, Paint, Stroke};
use kurbo::{PathEl, Point};
use crate::build::{Builder, MAX_PATH};
use crate::font::Font;
use crate::object::{ascii85, is_white, parse_num};
mod builtin;
mod eps;
mod exec;
mod paths;
mod scanner;
mod text;
mod value;
pub use eps::eps_parts;
use value::{DictRef, GState, V};
pub(crate) struct Interp {
pub b: Builder,
st: Vec<V>,
dicts: Vec<DictRef>,
gs: GState,
saved: Vec<GState>,
budget: usize,
/// Nested procedure calls (recursion is cut off at [`MAX_CALL_DEPTH`]).
calls: usize,
/// Fonts by name: `definefont`d dictionaries.
font_dir: HashMap<String, DictRef>,
/// Embedded font programs found in the file (name → (program, CFF)).
embedded: HashMap<String, (Rc<Vec<u8>>, bool)>,
/// Fonts built for drawing, by base font and encoding.
font_cache: HashMap<String, Rc<Font>>,
page: [f64; 4],
pub unknown: Vec<String>,
}
File diff suppressed because it is too large. Load diff
-100
View File
@@ -1,100 +0,0 @@
use super::{HashMap, Rc, is_white};
/// The PostScript section of an EPS file (DOS EPS binary header handled) and its bounding box
/// (`%%HiResBoundingBox` preferred), read with `craft-eps`: the header comments, and the trailer
/// for a box deferred with `(atend)`. A binary header that points outside the file is refused.
pub fn eps_parts(bytes: &[u8]) -> Option<(&[u8], [f64; 4])> {
let ps = craft_eps::sections(bytes).ok()?.postscript;
Some((ps, craft_eps::Comments::read(ps).page_box()?))
}
pub(super) fn find_from(hay: &[u8], needle: &[u8], from: usize) -> Option<usize> {
hay.get(from..)?.windows(needle.len()).position(|w| w == needle).map(|p| p + from)
}
/// Every position of `needle` in `hay`, in order (one pass over the file).
fn positions(hay: &[u8], needle: &[u8]) -> Vec<usize> {
let mut out = vec![];
let mut from = 0;
while let Some(p) = find_from(hay, needle, from) {
out.push(p);
from = p + needle.len();
}
out
}
/// The name after `key` (`/FontName /Name`).
fn name_after(b: &[u8], key: &[u8]) -> Option<String> {
let p = find_from(b, key, 0)? + key.len();
let rest = &b[p..];
let s = rest.iter().position(|c| *c == b'/')? + 1;
let e = rest[s..].iter().position(|c| is_white(*c) || matches!(c, b'/' | b'[' | b'(' | b'{')).map_or(rest.len(), |q| s + q);
(e > s).then(|| String::from_utf8_lossy(&rest[s..e]).into_owned())
}
/// Embedded font programs of an EPS file: Type 1 fonts (`… /FontName /X … currentfile eexec
/// … cleartomark`, hexadecimal or binary) and CFF font sets (`… <length> StartData <bytes>`).
pub(super) fn embedded_fonts(ps: &[u8]) -> HashMap<String, (Rc<Vec<u8>>, bool)> {
let mut out = HashMap::new();
// The headers are found once, in one pass each; every `eexec` then takes the last header
// before it that no earlier `eexec` has used, so no prefix of the file is searched again.
let mut heads = positions(ps, b"%!PS-AdobeFont");
heads.extend(positions(ps, b"%!FontType1"));
heads.sort_unstable();
let names = positions(ps, b"/FontName");
let mut from = 0;
let mut used = 0;
while let Some(ee) = find_from(ps, b"eexec", from) {
from = ee + 5;
let last_before = |v: &[usize]| v[..v.partition_point(|&p| p < ee)].last().copied().filter(|&p| p >= used);
// The fallback look-back stops at the previous font's end, so it cannot pick up its header.
let start = last_before(&heads).or_else(|| last_before(&names).map(|p| p.saturating_sub(512).max(used)));
let Some(start) = start else { continue };
let end = find_from(ps, b"cleartomark", ee).map_or(ps.len(), |p| p + 11);
if let Some(name) = name_after(&ps[start..ee], b"/FontName") {
out.insert(name, (Rc::new(ps[start..end].to_vec()), false));
}
from = end;
used = end;
}
let mut from = 0;
while let Some(sd) = find_from(ps, b"StartData", from) {
from = sd + 9;
let head = String::from_utf8_lossy(&ps[sd.saturating_sub(64)..sd]).into_owned();
let Some(len) = head.split_whitespace().last().and_then(|t| t.parse::<usize>().ok()) else { continue };
let s = sd + 10;
let Some(data) = ps.get(s..s.saturating_add(len)) else { continue };
if let Some(name) = crate::cff::font_name(data) {
out.insert(name, (Rc::new(data.to_vec()), true));
}
from = s + len;
}
out
}
#[cfg(test)]
mod tests {
use super::embedded_fonts;
#[test]
fn font_discovery_looks_back_once_per_header() {
// 20,000 `eexec` words before any font header: each used to search the whole prefix
// for a header again. The font after them is still found.
let mut ps = b"%!PS-Adobe-3.0 EPSF-3.0\n".to_vec();
ps.extend_from_slice("eexec ".repeat(20_000).as_bytes());
ps.extend_from_slice(b"\n%!PS-AdobeFont-1.0: Real\n/FontName /Real def\ncurrentfile eexec\n0000\ncleartomark\n");
let fonts = embedded_fonts(&ps);
assert_eq!(fonts.len(), 1);
assert!(fonts.contains_key("Real"));
}
#[test]
fn header_less_font_after_a_short_one_does_not_take_its_header() {
let a = "%!PS-AdobeFont-1.0: A\n/FontName /A def\ncurrentfile eexec\n0000\ncleartomark\n";
let b = "/FontName /B def\ncurrentfile eexec\n0000\ncleartomark\n";
let ps = format!("%!PS-Adobe-3.0 EPSF-3.0\n{a}{b}");
let fonts = embedded_fonts(ps.as_bytes());
let (data, _) = fonts.get("B").expect("second font found");
assert!(!String::from_utf8_lossy(data).contains("AdobeFont"), "{}", String::from_utf8_lossy(data));
}
}
-182
View File
@@ -1,182 +0,0 @@
use super::eps::embedded_fonts;
use super::scanner::Src;
use super::value::{Flow, GState, V, new_dict};
use super::{Affine, BezPath, Builder, Cap, HashMap, Interp, Join};
/// The deepest procedure nesting run (deeper recursion stops the program).
const MAX_CALL_DEPTH: usize = 256;
/// The most values the operand stack holds: a push past it is dropped, and `copy` copies
/// nothing rather than grow the stack past it.
pub(super) const MAX_STACK: usize = 100_000;
impl Interp {
pub fn new(page: [f64; 4]) -> Interp {
Interp {
b: Builder::new(),
st: vec![],
dicts: vec![new_dict()],
gs: GState {
ctm: Affine::IDENTITY,
color: [0.0; 3],
lw: 1.0,
cap: Cap::Butt,
join: Join::Miter,
miter: 10.0,
dash: None,
path: BezPath::new(),
cur: None,
start: None,
depth: 1,
font: None,
},
saved: vec![],
budget: 8_000_000,
calls: 0,
font_dir: HashMap::new(),
embedded: HashMap::new(),
font_cache: HashMap::new(),
page,
unknown: vec![],
}
}
pub fn run(&mut self, data: &[u8]) {
self.embedded = embedded_fonts(data);
let mut src = Src { d: data, pos: 0 };
while let Some(t) = src.token(0) {
if let Err(Flow::Quit) = self.exec_token(t, &mut src) {
break;
}
}
}
pub(super) fn pop(&mut self) -> V {
self.st.pop().unwrap_or(V::Null)
}
pub(super) fn popn(&mut self) -> f64 {
self.pop().num().unwrap_or(0.0)
}
pub(super) fn push(&mut self, v: V) {
if self.st.len() < MAX_STACK {
self.st.push(v);
}
}
/// Count `n` units of work (values copied, storage allocated) against the budget without
/// stopping the program here: once it is spent, the next operator stops it.
pub(super) fn charge(&mut self, n: usize) {
self.budget = self.budget.saturating_sub(n);
}
pub(super) fn lookup(&self, k: &str) -> Option<V> {
self.dicts.iter().rev().find_map(|d| d.borrow().get(k).cloned())
}
/// A token read from the file: procedures are pushed, names executed.
fn exec_token(&mut self, t: V, src: &mut Src) -> Result<(), Flow> {
match t {
V::Name(n) => self.exec_name(&n, src),
other => {
self.push(other);
Ok(())
}
}
}
/// Execute a value as an object (procedures run).
pub(super) fn exec_obj(&mut self, v: V, src: &mut Src) -> Result<(), Flow> {
match v {
V::Proc(p) => {
if self.calls >= MAX_CALL_DEPTH {
self.b.skip("PostScript (recursion limit)");
return Err(Flow::Quit);
}
self.calls += 1;
let mut r = Ok(());
for x in p.iter() {
match x {
V::Name(n) => {
r = self.exec_name(n, src);
if r.is_err() {
break;
}
}
V::Op(op) => {
r = self.spend(1).and_then(|()| self.builtin(op, src));
if r.is_err() {
break;
}
}
// Other items count against the budget as names do.
other => {
r = self.spend(1);
if r.is_err() {
break;
}
self.push(other.clone());
}
}
}
self.calls -= 1;
r
}
// An executable name can run another (`exec` of `/exec cvx`), so names count too.
V::Name(n) => {
if self.calls >= MAX_CALL_DEPTH {
self.b.skip("PostScript (recursion limit)");
return Err(Flow::Quit);
}
self.calls += 1;
let r = self.exec_name(&n, src);
self.calls -= 1;
r
}
V::Op(op) => {
self.spend(1)?;
self.builtin(&op, src)
}
other => {
self.push(other);
Ok(())
}
}
}
fn exec_name(&mut self, n: &str, src: &mut Src) -> Result<(), Flow> {
self.spend(1)?;
if let Some(v) = self.lookup(n) {
return match v {
V::Proc(_) => self.exec_obj(v, src),
// An operator stored under another name (`/mo /moveto load def`, as Illustrator's
// EPS prolog does for every path operator) runs that operator. It pushed the
// operator's name instead, so such files drew nothing (#955).
V::Op(op) => self.builtin(&op, src),
// An executable name (`/y /x cvx def`) runs what that name is bound to.
V::Name(_) => self.exec_obj(v, src),
other => {
self.push(other);
Ok(())
}
};
}
self.builtin(n, src)
}
/// Count `n` operations against the budget, and stop the program when fewer are left. An
/// operator that makes, reads or copies path elements counts each one as an operation.
pub(super) fn spend(&mut self, n: usize) -> Result<(), Flow> {
match self.budget.checked_sub(n) {
Some(left) => {
self.budget = left;
Ok(())
}
None => {
self.b.skip("PostScript (operation limit)");
Err(Flow::Quit)
}
}
}
}
-187
View File
@@ -1,187 +0,0 @@
use super::value::{Flow, V};
use super::{Affine, BezPath, FillRule, Interp, MAX_PATH, Paint, PathEl, Point, Stroke};
/// The angle from `a` counterclockwise to `b` (degrees, 0 to 360), worked out from what is left
/// of each after whole turns.
fn ccw_turn(a: f64, b: f64) -> f64 {
(b % 360.0 - a % 360.0).rem_euclid(360.0)
}
/// The most turns one `arc` or `arcn` draws.
const MAX_ARC_TURNS: f64 = 1024.0;
impl Interp {
fn device_scale(&self) -> f64 {
self.gs.ctm.determinant().abs().sqrt()
}
pub(super) fn user_point(&mut self) -> Point {
let y = self.popn();
let x = self.popn();
Point::new(x, y)
}
/// Stop the program when a path of `len` elements would be longer than [`MAX_PATH`].
pub(super) fn path_fits(&mut self, len: usize) -> Result<(), Flow> {
if len <= MAX_PATH {
return Ok(());
}
self.b.skip("PostScript (path limit)");
Err(Flow::Quit)
}
/// [`Self::path_fits`] for the current path with `n` more elements.
pub(super) fn room(&mut self, n: usize) -> Result<(), Flow> {
self.path_fits(self.gs.path.elements().len().saturating_add(n))
}
pub(super) fn move_to(&mut self, p: Point) -> Result<(), Flow> {
// A moveto right after a moveto replaces it (PostScript Language Reference §8.2, `moveto`).
if matches!(self.gs.path.elements().last(), Some(PathEl::MoveTo(_))) {
self.gs.path.pop();
}
self.room(1)?;
let q = self.gs.ctm * p;
self.gs.path.move_to(q);
self.gs.cur = Some(q);
self.gs.start = Some(q);
Ok(())
}
pub(super) fn line_to(&mut self, p: Point) -> Result<(), Flow> {
self.room(1 + usize::from(self.gs.cur.is_none()))?;
let q = self.gs.ctm * p;
if self.gs.cur.is_none() {
self.gs.path.move_to(q);
self.gs.start = Some(q);
}
self.gs.path.line_to(q);
self.gs.cur = Some(q);
Ok(())
}
pub(super) fn curve_to(&mut self, a: Point, b: Point, c: Point) -> Result<(), Flow> {
self.room(1 + usize::from(self.gs.cur.is_none()))?;
let m = self.gs.ctm;
if self.gs.cur.is_none() {
self.gs.path.move_to(m * a);
}
self.gs.path.curve_to(m * a, m * b, m * c);
self.gs.cur = Some(m * c);
Ok(())
}
pub(super) fn current_user(&self) -> Point {
self.gs.ctm.inverse() * self.gs.cur.unwrap_or_default()
}
pub(super) fn arc(&mut self, neg: bool) -> Result<(), Flow> {
let a2 = self.popn();
let a1 = self.popn();
let r = self.popn();
let c = self.user_point();
// `arc` raises `a2` by multiples of 360 until it is at least `a1`, and `arcn` lowers it
// until it is at most `a1` (PostScript Language Reference §8.2, `arc` and `arcn`). Those
// sweeps, and the first endpoint, are worked out from what is left of each angle after
// whole turns.
let sweep = if !neg && a2 < a1 {
ccw_turn(a1, a2)
} else if neg && a2 > a1 {
-ccw_turn(a2, a1)
} else {
a2 - a1
};
// A sweep of more than `MAX_ARC_TURNS` turns is shortened by whole pairs of turns to
// between `MAX_ARC_TURNS - 2` and `MAX_ARC_TURNS` turns, with a skip note. The arc still
// ends at `a2`, and an even-odd fill or clip of it covers the same area as the full sweep.
let max = MAX_ARC_TURNS * 360.0;
let sweep = if sweep.abs() > max {
self.b.skip("arc (too many turns)");
let s = sweep.signum();
s * (max - 720.0 + (s * (a2 % 720.0 - a1 % 720.0)).rem_euclid(720.0))
} else {
sweep
};
let (a1, sweep) = ((a1 % 360.0).to_radians(), sweep.to_radians());
let start = Point::new(c.x + r * a1.cos(), c.y + r * a1.sin());
if self.gs.cur.is_some() {
self.line_to(start)?;
} else {
self.move_to(start)?;
}
let arc = kurbo::Arc::new(c, (r, r), a1, sweep, 0.0);
let m = self.gs.ctm;
// kurbo divides an arc into more curves the larger the radius (about 3.7 × 10^49 for a
// quarter turn at a radius of 10^300, more than a usize can count), so each one counts
// against the budget and the path limit.
let mut curves = arc.append_iter(0.1);
while let Some(PathEl::CurveTo(p1, p2, p3)) = curves.next() {
self.room(1)?;
self.spend(1)?;
self.gs.path.curve_to(m * p1, m * p2, m * p3);
self.gs.cur = Some(m * p3);
}
Ok(())
}
pub(super) fn paint(&mut self, fill: Option<FillRule>, stroke: bool) {
let path = std::mem::take(&mut self.gs.path);
self.gs.cur = None;
self.gs.start = None;
let s = self.device_scale();
let st = stroke.then(|| Stroke {
paint: Paint::Color(self.gs.color),
opacity: 1.0,
width: if self.gs.lw <= 0.0 { 1.0 } else { self.gs.lw * s },
cap: self.gs.cap,
join: self.gs.join,
miter: self.gs.miter.max(1.0),
dash: self.gs.dash.as_ref().map(|(a, o)| (a.iter().map(|v| v * s).collect(), o * s)),
});
self.b.fill_stroke(path, Affine::IDENTITY, fill.map(|r| (Paint::Color(self.gs.color), 1.0, r)), st);
}
pub(super) fn rect_path(&mut self) -> Result<Vec<BezPath>, Flow> {
// x y w h, or an array / string of numbers.
let top = self.pop();
let nums: Vec<f64> = match &top {
V::Array(_) | V::Proc(_) => {
let items = top.items().unwrap_or_default();
// An array's rectangles count against the budget, five path elements each.
self.spend(items.len() / 4 * 5)?;
items.iter().filter_map(V::num).collect()
}
V::Num(h) => {
let w = self.popn();
let y = self.popn();
let x = self.popn();
vec![x, y, w, *h]
}
_ => vec![],
};
self.path_fits(nums.len() / 4 * 5)?;
let m = self.gs.ctm;
Ok(nums
.as_chunks::<4>()
.0
.iter()
.map(|r| {
let mut p = BezPath::new();
p.move_to(m * Point::new(r[0], r[1]));
p.line_to(m * Point::new(r[0] + r[2], r[1]));
p.line_to(m * Point::new(r[0] + r[2], r[1] + r[3]));
p.line_to(m * Point::new(r[0], r[1] + r[3]));
p.close_path();
p
})
.collect())
}
pub(super) fn grestore(&mut self) {
if let Some(g) = self.saved.pop() {
let d = g.depth;
self.gs = g;
self.b.close_to(d);
}
}
}
-223
View File
@@ -1,223 +0,0 @@
use super::value::V;
use super::{Rc, RefCell, ascii85, is_white, parse_num};
/// Source tokenizer (the data source for `currentfile`).
pub(super) struct Src<'a> {
pub(super) d: &'a [u8],
pub(crate) pos: usize,
}
impl Src<'_> {
fn skip_ws(&mut self) {
while self.pos < self.d.len() {
let c = self.d[self.pos];
if is_white(c) {
self.pos += 1;
} else if c == b'%' {
let s = self.pos;
while self.pos < self.d.len() && !matches!(self.d[self.pos], b'\n' | b'\r') {
self.pos += 1;
}
// DSC: skip embedded binary / data sections.
let line = &self.d[s..self.pos];
if line.starts_with(b"%%BeginData:") || line.starts_with(b"%%BeginBinary:") || line.starts_with(b"%%BeginPreview") {
let end: &[u8] = if line.starts_with(b"%%BeginData:") {
b"%%EndData"
} else if line.starts_with(b"%%BeginBinary:") {
b"%%EndBinary"
} else {
b"%%EndPreview"
};
match self.d[self.pos..].windows(end.len()).position(|w| w == end) {
Some(p) => self.pos += p,
None => self.pos = self.d.len(),
}
}
} else {
break;
}
}
}
fn word(&mut self) -> &[u8] {
let s = self.pos;
while self.pos < self.d.len() {
let c = self.d[self.pos];
if is_white(c) || matches!(c, b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%') {
break;
}
self.pos += 1;
}
&self.d[s..self.pos]
}
/// The next token; `{ … }` comes back as a whole procedure.
pub(super) fn token(&mut self, depth: usize) -> Option<V> {
self.skip_ws();
let c = *self.d.get(self.pos)?;
Some(match c {
b'{' => {
self.pos += 1;
let mut body = vec![];
loop {
self.skip_ws();
match self.d.get(self.pos) {
None => break,
Some(b'}') => {
self.pos += 1;
break;
}
_ => {
if depth > 100 {
return None;
}
match self.token(depth + 1) {
Some(t) => body.push(t),
None => break,
}
}
}
}
V::Proc(Rc::new(body))
}
b'}' => {
self.pos += 1;
V::Null
}
b'/' => {
self.pos += 1;
let imm = self.d.get(self.pos) == Some(&b'/');
if imm {
self.pos += 1;
}
let w = String::from_utf8_lossy(self.word()).into_owned();
// `//name` (immediately evaluated) is treated like a plain name.
if imm { V::Name(w.into()) } else { V::Lit(w.into()) }
}
b'(' => {
self.pos += 1;
let mut out = vec![];
let mut depth = 1;
while self.pos < self.d.len() {
let c = self.d[self.pos];
self.pos += 1;
match c {
b'\\' => {
let e = self.d.get(self.pos).copied().unwrap_or(b'\\');
self.pos += 1;
match e {
b'n' => out.push(b'\n'),
b'r' => out.push(b'\r'),
b't' => out.push(b'\t'),
b'0'..=b'7' => {
let mut v = (e - b'0') as u32;
for _ in 0..2 {
match self.d.get(self.pos) {
Some(d @ b'0'..=b'7') => {
v = v * 8 + (d - b'0') as u32;
self.pos += 1;
}
_ => break,
}
}
out.push(v as u8);
}
b'\n' | b'\r' => {}
o => out.push(o),
}
}
b'(' => {
depth += 1;
out.push(c);
}
b')' => {
depth -= 1;
if depth == 0 {
break;
}
out.push(c);
}
_ => out.push(c),
}
}
V::Str(Rc::new(RefCell::new(out)))
}
b'<' if self.d.get(self.pos + 1) == Some(&b'<') => {
self.pos += 2;
V::Name("<<".into())
}
b'>' if self.d.get(self.pos + 1) == Some(&b'>') => {
self.pos += 2;
V::Name(">>".into())
}
b'<' if self.d.get(self.pos + 1) == Some(&b'~') => {
let s = self.pos;
while self.pos + 1 < self.d.len() && !(self.d[self.pos] == b'~' && self.d[self.pos + 1] == b'>') {
self.pos += 1;
}
self.pos = (self.pos + 2).min(self.d.len());
V::Str(Rc::new(RefCell::new(ascii85(&self.d[s..self.pos]))))
}
b'<' => {
self.pos += 1;
let mut hex = vec![];
while self.pos < self.d.len() && self.d[self.pos] != b'>' {
if self.d[self.pos].is_ascii_hexdigit() {
hex.push(self.d[self.pos]);
}
self.pos += 1;
}
self.pos += 1;
if hex.len() % 2 == 1 {
hex.push(b'0');
}
let h = |c: u8| (c as char).to_digit(16).unwrap_or(0) as u8;
V::Str(Rc::new(RefCell::new(hex.chunks(2).map(|p| (h(p[0]) << 4) | h(p[1])).collect())))
}
b'[' | b']' => {
self.pos += 1;
V::Name(if c == b'[' { "[" } else { "]" }.into())
}
b')' | b'>' => {
self.pos += 1;
V::Null
}
_ => {
let w = self.word();
if w.is_empty() {
self.pos += 1;
return Some(V::Null);
}
match parse_num(w) {
Some(n) => V::Num(n),
None => {
// Radix numbers (16#FF).
let s = String::from_utf8_lossy(w).into_owned();
match s.split_once('#').and_then(|(r, v)| i64::from_str_radix(v, r.parse::<u32>().ok().filter(|r| (2..=36).contains(r))?).ok()) {
Some(n) => V::Num(n as f64),
None => V::Name(s.into()),
}
}
}
}
})
}
/// Step over inline image data that follows an `image` operator reading `currentfile`.
pub(super) fn skip_image_data(&mut self) {
self.skip_ws();
if self.d[self.pos..].starts_with(b"<~") || self.d.get(self.pos).is_some_and(|c| (b'!'..=b'u').contains(c) && !c.is_ascii_hexdigit()) {
match self.d[self.pos..].windows(2).position(|w| w == b"~>") {
Some(p) => self.pos += p + 2,
None => self.pos = self.d.len(),
}
return;
}
while self.pos < self.d.len() && (self.d[self.pos].is_ascii_hexdigit() || is_white(self.d[self.pos])) {
self.pos += 1;
}
if self.d.get(self.pos) == Some(&b'>') {
self.pos += 1;
}
}
}
-329
View File
@@ -1,329 +0,0 @@
use super::scanner::Src;
use super::value::{DictRef, Flow, V, array, matrix_of, matrix_val, new_dict};
use super::{Affine, BezPath, FillRule, Font, HashMap, Interp, Paint, Point, Rc, RefCell};
// ------------------------------------------------------------------ text
/// An encoding array of glyph names (`StandardEncoding`; `ISOLatin1Encoding` and others are
/// approximated by WinAnsi, which agrees with ISO Latin-1 on the printable codes).
pub(super) fn encoding_array(name: &str) -> V {
let base = if name == "StandardEncoding" { crate::encoding::Base::Standard } else { crate::encoding::Base::WinAnsi };
array((0..256).map(|c| V::Lit(base.name(c as u8).unwrap_or_else(|| ".notdef".into()).into())).collect())
}
fn dict_matrix(d: &DictRef, key: &str) -> Affine {
d.borrow().get(key).and_then(matrix_of).unwrap_or(Affine::IDENTITY)
}
/// A copy of a font dictionary with `m` applied after its matrix (`scalefont`, `makefont`).
fn transformed(font: &DictRef, m: Affine) -> DictRef {
let copy: HashMap<String, V> = font.borrow().clone();
let d = Rc::new(RefCell::new(copy));
let old = dict_matrix(&d, "__m");
d.borrow_mut().insert("__m".into(), matrix_val(m * old));
d
}
impl Interp {
/// A font dictionary by name: one `definefont` made, else a new dictionary for an
/// embedded program or a bundled stand-in.
pub(super) fn find_font(&mut self, key: &str) -> DictRef {
if let Some(d) = self.font_dir.get(key) {
return d.clone();
}
let d = new_dict();
{
let mut m = d.borrow_mut();
m.insert("FontName".into(), V::Lit(key.into()));
m.insert("FontType".into(), V::Num(1.0));
m.insert("FontMatrix".into(), matrix_val(Affine::scale(0.001)));
m.insert("FID".into(), V::Null);
m.insert("__base".into(), V::Lit(key.into()));
m.insert("__m".into(), matrix_val(Affine::IDENTITY));
}
self.font_dir.insert(key.to_string(), d.clone());
d
}
/// The drawable font of a font dictionary (its base program and encoding) and the matrix
/// from text space (font size 1) to user space.
fn font_of(&mut self, d: &DictRef) -> Option<(Rc<Font>, Affine)> {
if matches!(d.borrow().get("FontType"), Some(V::Num(t)) if *t == 3.0) {
// Type 3 fonts (glyph procedures) are not run.
self.b.skip("EPS Type 3 font");
return None;
}
let (base, names) = {
let m = d.borrow();
let base = m.get("__base").or_else(|| m.get("FontName")).map(V::key)?;
let names: Option<Vec<Option<String>>> = m.get("Encoding").and_then(V::items).map(|a| {
a.iter()
.map(|v| match v {
V::Lit(n) | V::Name(n) if &**n != ".notdef" => Some(n.to_string()),
_ => None,
})
.collect()
});
(base, names)
};
let key = match &names {
Some(n) => format!("{base}\u{0}{}", n.iter().map(|x| x.as_deref().unwrap_or("")).collect::<Vec<_>>().join(" ")),
None => base.clone(),
};
let font = match self.font_cache.get(&key) {
Some(f) => f.clone(),
None => {
// Not embedded: a bundled stand-in, as for PDF.
let prog = self.embedded.get(&base).cloned();
let f = Rc::new(Font::standalone(&base, prog.as_ref().map(|(d, cff)| (d.as_slice(), *cff)), names));
self.font_cache.insert(key, f.clone());
f
}
};
Some((font, dict_matrix(d, "__m")))
}
/// A string's glyph outlines (device space) from the current point, with an extra
/// displacement `extra(index, code)` (user space) after each glyph; moves the current
/// point.
fn text_path(&mut self, bytes: &[u8], mut extra: impl FnMut(usize, u32) -> (f64, f64)) -> Result<Option<(BezPath, String)>, Flow> {
let Some(d) = self.gs.font.clone() else { return Ok(None) };
let Some((font, m)) = self.font_of(&d) else { return Ok(None) };
let mut p = self.current_user();
let mut path = BezPath::new();
let mut text = String::new();
let ctm = self.gs.ctm;
for (i, &code) in bytes.iter().enumerate() {
let code = code as u32;
// Every input character costs at least one unit, so empty outlines are not free.
self.spend(1)?;
if let Some(g) = font.glyph(code) {
// Each outline element counts against the path limit and the budget before it
// is copied, so a long string cannot expand into unbounded geometry.
let n = g.elements().len();
self.path_fits(path.elements().len().saturating_add(n))?;
self.spend(n)?;
path.extend(ctm * Affine::translate(p.to_vec2()) * m * (*g).clone());
}
text.push_str(&font.unicode(code));
let adv = m * Point::new(font.width(code), 0.0) - m * Point::ZERO;
let (ex, ey) = extra(i, code);
p += adv + kurbo::Vec2::new(ex, ey);
}
self.move_to(p)?;
Ok(Some((path, text)))
}
fn fill_text(&mut self, path: BezPath, text: &str) {
let label: String = text.chars().filter(|c| !c.is_control()).take(40).collect();
let name = if label.trim().is_empty() { "Text".to_string() } else { format!("Text: {}", label.trim()) };
self.b.fill_stroke_named(&name, path, Affine::IDENTITY, Some((Paint::Color(self.gs.color), 1.0, FillRule::NonZero)), None);
}
fn pop_string(&mut self) -> Vec<u8> {
match self.pop() {
V::Str(s) => s.borrow().clone(),
_ => vec![],
}
}
fn show_with(&mut self, s: &[u8], extra: impl FnMut(usize, u32) -> (f64, f64)) -> Result<(), Flow> {
match self.text_path(s, extra)? {
Some((path, text)) => self.fill_text(path, &text),
None => self.b.skip("text (no font)"),
}
Ok(())
}
/// Text and font operators (PostScript Language Reference §5.1, §8.2).
pub(super) fn text_op(&mut self, n: &str, src: &mut Src) -> Result<(), Flow> {
match n {
"scalefont" | "makefont" => {
let m = if n == "scalefont" {
Affine::scale(self.popn())
} else {
let v = self.pop();
matrix_of(&v).unwrap_or(Affine::IDENTITY)
};
let font = match self.pop() {
V::Dict(d) => d,
_ => self.find_font("Helvetica"),
};
self.push(V::Dict(transformed(&font, m)));
}
"selectfont" => {
let size = self.pop();
let key = self.pop().key();
let font = self.find_font(&key);
let m = match &size {
V::Num(s) => Affine::scale(*s),
other => matrix_of(other).unwrap_or(Affine::IDENTITY),
};
self.gs.font = Some(transformed(&font, m));
}
"setfont" => {
if let V::Dict(d) = self.pop() {
self.gs.font = Some(d);
}
}
"currentfont" | "rootfont" => {
let d = self.gs.font.clone().unwrap_or_else(new_dict);
self.push(V::Dict(d));
}
"definefont" => {
let v = self.pop();
let key = self.pop().key();
if let V::Dict(d) = &v {
if !d.borrow().contains_key("__base") {
// A font the file defines itself: its embedded program, by name.
d.borrow_mut().insert("__base".into(), V::Lit(key.as_str().into()));
d.borrow_mut().insert("__m".into(), matrix_val(Affine::IDENTITY));
}
self.font_dir.insert(key, d.clone());
}
self.push(v);
}
"undefinefont" => {
let key = self.pop().key();
self.font_dir.remove(&key);
}
"StandardEncoding" | "ISOLatin1Encoding" => self.push(encoding_array(n)),
"show" => {
let s = self.pop_string();
self.show_with(&s, |_, _| (0.0, 0.0))?;
}
"ashow" => {
let s = self.pop_string();
let ay = self.popn();
let ax = self.popn();
self.show_with(&s, |_, _| (ax, ay))?;
}
"widthshow" => {
let s = self.pop_string();
let ch = self.popn() as u32;
let cy = self.popn();
let cx = self.popn();
self.show_with(&s, |_, c| if c == ch { (cx, cy) } else { (0.0, 0.0) })?;
}
"awidthshow" => {
let s = self.pop_string();
let ay = self.popn();
let ax = self.popn();
let ch = self.popn() as u32;
let cy = self.popn();
let cx = self.popn();
self.show_with(&s, |_, c| if c == ch { (cx + ax, cy + ay) } else { (ax, ay) })?;
}
"kshow" | "cshow" => {
// The procedure between characters is not run.
let s = self.pop_string();
self.pop();
self.show_with(&s, |_, _| (0.0, 0.0))?;
}
"xshow" | "yshow" | "xyshow" => {
let disp: Vec<f64> = self.pop().items().unwrap_or_default().iter().filter_map(V::num).collect();
let s = self.pop_string();
self.explicit_show(&s, &disp, n)?;
}
"glyphshow" => {
let name = self.pop().key();
let Some(d) = self.gs.font.clone() else { return Ok(()) };
// Drawn through a one-glyph encoding.
let mut e: Vec<V> = (0..256).map(|_| V::Lit(".notdef".into())).collect();
e[65] = V::Lit(name.as_str().into());
let tmp = transformed(&d, Affine::IDENTITY);
tmp.borrow_mut().insert("Encoding".into(), array(e));
self.gs.font = Some(tmp);
self.show_with(b"A", |_, _| (0.0, 0.0))?;
self.gs.font = Some(d);
}
"charpath" => {
self.pop();
let s = self.pop_string();
let saved = std::mem::take(&mut self.gs.path);
let start = self.gs.start;
match self.text_path(&s, |_, _| (0.0, 0.0))? {
Some((glyphs, _)) => {
// The outlines join the path: each element counts against the budget and
// the path limit.
self.gs.path = saved;
self.room(glyphs.elements().len() + 1)?;
self.spend(glyphs.elements().len())?;
self.gs.path.extend(glyphs);
if let Some(c) = self.gs.cur {
self.gs.path.move_to(c);
}
self.gs.start = start;
}
None => self.gs.path = saved,
}
}
"stringwidth" => {
let s = self.pop_string();
let (mut w, mut h) = (0.0, 0.0);
if let Some(d) = self.gs.font.clone()
&& let Some((font, m)) = self.font_of(&d)
{
for &c in &s {
let v = m * Point::new(font.width(c as u32), 0.0) - m * Point::ZERO;
w += v.x;
h += v.y;
}
}
self.push(V::Num(w));
self.push(V::Num(h));
}
"setcachedevice" => {
for _ in 0..6 {
self.pop();
}
}
"setcharwidth" => {
self.pop();
self.pop();
}
// An embedded Type 1 program's encrypted part (read by `embedded_fonts`).
"eexec" => {
if matches!(self.pop(), V::File) {
let rest = &src.d[src.pos.min(src.d.len())..];
src.pos += rest.windows(11).position(|w| w == b"cleartomark").map_or(rest.len(), |p| p + 11);
}
}
// A CFF font set's binary data (read by `embedded_fonts`).
"StartData" => {
let len = self.popn().max(0.0) as usize;
self.pop();
src.pos = src.pos.saturating_add(1).saturating_add(len).min(src.d.len());
}
_ => {}
}
Ok(())
}
/// `xshow` / `yshow` / `xyshow`: glyph displacements from a number array instead of the
/// advances.
fn explicit_show(&mut self, s: &[u8], disp: &[f64], n: &str) -> Result<(), Flow> {
let step = if n == "xyshow" { 2 } else { 1 };
let d: Vec<(f64, f64)> = (0..s.len())
.map(|k| {
let i = k * step;
let v = |j: usize| disp.get(j).copied().unwrap_or(0.0);
match n {
"xshow" => (v(i), 0.0),
"yshow" => (0.0, v(i)),
_ => (v(i), v(i + 1)),
}
})
.collect();
let Some(fd) = self.gs.font.clone() else {
self.b.skip("text (no font)");
return Ok(());
};
let Some((font, m)) = self.font_of(&fd) else { return Ok(()) };
self.show_with(s, move |k, c| {
let adv = m * Point::new(font.width(c), 0.0) - m * Point::ZERO;
let (dx, dy) = d.get(k).copied().unwrap_or((0.0, 0.0));
(dx - adv.x, dy - adv.y)
})
}
}
-88
View File
@@ -1,88 +0,0 @@
use super::{Affine, BezPath, Cap, HashMap, Join, Point, Rc, RefCell};
pub(crate) type DictRef = Rc<RefCell<HashMap<String, V>>>;
#[derive(Clone)]
pub(crate) enum V {
Num(f64),
Bool(bool),
/// An executable name.
Name(Rc<str>),
/// A literal name (`/name`).
Lit(Rc<str>),
/// A built-in operator, as `load` returns it for a name no dictionary defines.
Op(Rc<str>),
Str(Rc<RefCell<Vec<u8>>>),
Proc(Rc<Vec<V>>),
Array(Rc<RefCell<Vec<V>>>),
Dict(DictRef),
Mark,
Null,
/// `currentfile`.
File,
}
impl V {
pub(super) fn num(&self) -> Option<f64> {
match self {
V::Num(n) => Some(*n),
_ => None,
}
}
pub(super) fn key(&self) -> String {
match self {
V::Name(n) | V::Lit(n) | V::Op(n) => n.to_string(),
V::Str(s) => String::from_utf8_lossy(&s.borrow()).into_owned(),
V::Num(n) => format!("{n}"),
V::Bool(b) => format!("{b}"),
_ => String::new(),
}
}
pub(super) fn items(&self) -> Option<Vec<V>> {
match self {
V::Array(a) => Some(a.borrow().clone()),
V::Proc(p) => Some(p.as_ref().clone()),
_ => None,
}
}
}
pub(super) enum Flow {
Exit,
Stop,
Quit,
}
#[derive(Clone)]
pub(super) struct GState {
pub(crate) ctm: Affine,
pub(crate) color: [f64; 3],
pub(crate) lw: f64,
pub(crate) cap: Cap,
pub(crate) join: Join,
pub(crate) miter: f64,
pub(crate) dash: Option<(Vec<f64>, f64)>,
pub(crate) path: BezPath,
pub(crate) cur: Option<Point>,
pub(crate) start: Option<Point>,
pub(crate) depth: usize,
/// The current font dictionary.
pub(crate) font: Option<DictRef>,
}
pub(super) fn new_dict() -> DictRef {
Rc::new(RefCell::new(HashMap::new()))
}
pub(super) fn array(v: Vec<V>) -> V {
V::Array(Rc::new(RefCell::new(v)))
}
pub(super) fn matrix_of(v: &V) -> Option<Affine> {
let a: Vec<f64> = v.items()?.iter().filter_map(V::num).collect();
(a.len() == 6).then(|| Affine::new([a[0], a[1], a[2], a[3], a[4], a[5]]))
}
pub(super) fn matrix_val(m: Affine) -> V {
array(m.as_coeffs().iter().map(|c| V::Num(*c)).collect())
}
+100
View File
@@ -83,6 +83,28 @@ fn pdf_paths_clip_gradient_and_layers() {
}
}
#[test]
fn pdf_actual_text_is_exposed_as_a_logical_group() {
let content = "/Span << /ActualText (Logical text) >> BDC\n1 0 0 rg 10 10 20 20 re f\nEMC";
let objs = vec![
(1, "<< /Type /Catalog /Pages 2 0 R >>".to_string(), None),
(2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(), None),
(3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Resources << >> /Contents 4 0 R >>".to_string(), None),
(4, "<< >>".to_string(), Some(content.as_bytes().to_vec())),
];
let doc = parse(&pdf(&objs, 1)).unwrap();
assert_eq!(shape_names(&doc), vec!["Text: Logical text"]);
}
#[test]
fn pdf_actual_text_without_a_bom_is_pdfdocencoding_in_the_label() {
// 0x80 is a bullet in PDFDocEncoding, 0x92 a trade mark sign, not C1 control characters.
let data = page_pdf("/Span << /ActualText <80204192> >> BDC 1 0 0 rg 10 10 20 20 re f EMC", "", vec![]);
assert_eq!(shape_names(&parse(&data).unwrap()), vec!["Text: \u{2022} A\u{2122}"]);
}
#[test]
fn pdf_forms_rotation_cmyk_and_alpha() {
let content = "q 2 0 0 2 0 0 cm /Fm0 Do Q\n/GS0 gs 0 0 0 1 k 0 0 10 10 re f\n";
@@ -232,6 +254,12 @@ fn eps_postscript_subset() {
assert_eq!(parse(b"hello"), Err(Error::NotVector));
}
#[test]
fn eps_without_a_bounding_box_keeps_the_public_error_variant() {
let eps = b"%!PS-Adobe-3.0 EPSF-3.0\n%%EndComments\n0 0 10 10 rectfill\n";
assert_eq!(parse(eps), Err(Error::NoBoundingBox));
}
#[test]
fn filters_decode() {
assert_eq!(object::ascii85(b"<~87cURD]i,\"Ebo80~>"), b"Hello World!".to_vec());
@@ -451,6 +479,9 @@ pub(crate) fn shape_names(doc: &Doc) -> Vec<String> {
fn walk(g: &effectcraft_svg::Group, out: &mut Vec<String>) {
for c in &g.children {
match c {
// Marked text from the shared PostScript adapter is a logical text object whose
// outlined glyph paths remain children for rendering and editing.
Node::Group(s) if s.name.starts_with("Text: ") => out.push(s.name.clone()),
Node::Group(s) => walk(s, out),
Node::Shape(s) => out.push(s.name.clone()),
Node::Image(i) => out.push(i.name.clone()),
@@ -1335,3 +1366,72 @@ fn clip_groups_nested_without_end_are_capped() {
assert!(inside[2] > 0.99 && inside[3] > 0.99, "the artwork draws inside the clips: {inside:?}");
assert!(outside[3] < 0.01, "and is clipped: {outside:?}");
}
#[test]
fn actual_text_named_properties_and_fallback_weights() {
let data =
page_pdf("/Span /P BDC 1 0 0 rg 10 10 20 20 re f EMC", "/Properties << /P 10 0 R >>", vec![(10, "<< /ActualText (Logical text) >>".into(), None)]);
assert_eq!(shape_names(&parse(&data).unwrap()), vec!["Text: Logical text"]);
use effectcraft_text::fonts::{INTER_BOLD, INTER_SEMIBOLD};
for name in [b"Inter-Semibold".as_slice(), b"Inter-Demi"] {
assert_eq!(eps_fallback_font(name), Some(INTER_SEMIBOLD));
}
for name in [b"Inter-Bold".as_slice(), b"Inter-Black", b"Inter-Heavy"] {
assert_eq!(eps_fallback_font(name), Some(INTER_BOLD));
}
}
#[test]
fn actual_text_does_not_change_graphics_state_or_compositing() {
let resources = "/ExtGState << /M << /BM /Multiply >> /N << /BM /Normal >> >>";
// Compare every pixel to the same operators without marked content. In particular,
// Multiply and the clip survive EMC, and a later graphics-state change still works.
for (marked, plain) in [
(
"/Span << /ActualText (A) >> BDC /M gs 0 1 1 rg 0 0 60 100 re f EMC 50 0 60 100 re f /N gs 0 0 1 rg 150 0 50 100 re f",
"/M gs 0 1 1 rg 0 0 60 100 re f 50 0 60 100 re f /N gs 0 0 1 rg 150 0 50 100 re f",
),
(
"/Span << /ActualText (A) >> BDC 0 0 100 100 re W n /M gs 0 1 1 rg 0 0 60 100 re f EMC 50 0 150 100 re f",
"0 0 100 100 re W n /M gs 0 1 1 rg 0 0 60 100 re f 50 0 150 100 re f",
),
(
"/Span << /ActualText (A) >> BDC q /M gs 0 1 1 rg 0 0 60 100 re f Q EMC 0 0 1 rg 100 0 100 100 re f",
"q /M gs 0 1 1 rg 0 0 60 100 re f Q 0 0 1 rg 100 0 100 100 re f",
),
] {
let draw = |ops| render(&parse(&page_pdf(&format!("1 1 0 rg 0 0 200 100 re f {ops}"), resources, vec![])).unwrap());
let (a, b) = (draw(marked), draw(plain));
for y in 0..100 {
for x in 0..200 {
assert_eq!(a.get(x, y), b.get(x, y), "pixel {x},{y}");
}
}
}
}
#[test]
fn logical_text_cannot_extend_finished_node_depth_limit() {
use crate::build::Builder;
use effectcraft_svg::Group;
let mut b = Builder::new();
assert!(b.begin_text("label"));
assert!(!b.nested(0).has_room(254));
let mut node = Node::Group(Group::new("leaf"));
for _ in 1..255 {
let mut parent = Group::new("nested");
parent.children.push(node);
node = Node::Group(parent);
}
b.push(node);
b.end_text();
let (nodes, _, skipped) = b.finish();
assert!(skipped.iter().any(|s| s == "group nesting limit"));
let mut current = nodes.first();
let mut depth = 1;
while let Some(Node::Group(g)) = current {
depth += 1;
current = g.children.first();
}
assert_eq!(depth, 256);
}
+37
View File
@@ -151,3 +151,40 @@ fn vertical_metrics_w2_and_dw2() {
let dw2 = doc("/DW2 [780 -1000]");
assert!(((base.1 - dw2.1) - 2).abs() <= 1, "vy 780 → 2 pt higher: {base:?} {dw2:?}");
}
#[test]
fn actual_text_does_not_combine_knockout_children() {
let plain = "1 0 0 rg 0 0 100 100 re f /H gs 0 0 1 rg 50 0 100 100 re f";
let marked = format!("/Span << /ActualText (Label) >> BDC {plain} EMC");
let a = render(&group_page("/Fm Do", "", "/Group << /S /Transparency /I true /K true >>", plain));
let b = render(&group_page("/Fm Do", "", "/Group << /S /Transparency /I true /K true >>", &marked));
for y in 0..100 {
for x in 0..200 {
assert!(near(a.get(x, y), b.get(x, y)), "pixel {x},{y}");
}
}
}
#[test]
fn actual_text_end_keeps_soft_mask_and_overlapping_paints() {
let plain = "/GS1 gs 1 0 0 rg 0 0 100 100 re f 0 0 1 rg 50 0 100 100 re f";
let marked = "/Span << /ActualText (Label) >> BDC /GS1 gs 1 0 0 rg 0 0 100 100 re f EMC 0 0 1 rg 50 0 100 100 re f";
let draw = |ops| {
let bytes = page_pdf(
ops,
"/ExtGState << /GS1 << /SMask << /S /Luminosity /G 10 0 R >> >> >>",
vec![(
10,
"<< /Type /XObject /Subtype /Form /BBox [0 0 200 100] /Group << /S /Transparency /CS /DeviceGray >> >>".into(),
Some(b"0.5 g 0 0 200 100 re f".to_vec()),
)],
);
render(&parse(&bytes).unwrap())
};
let (a, b) = (draw(plain), draw(marked));
for y in 0..100 {
for x in 0..200 {
assert!(near(a.get(x, y), b.get(x, y)), "pixel {x},{y}");
}
}
}
+16
View File
@@ -627,3 +627,19 @@ fn postscript_idiv_and_mod_of_i64_min_by_minus_one_give_zero() {
assert_eq!(shape_names(&doc).len(), 1, "{prog}");
}
}
#[test]
fn eps_unembedded_reencoded_accent_uses_bundled_glyph() {
let doc = eps(b"/Helvetica findfont dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall /Encoding 256 array def 0 1 255 {Encoding exch /.notdef put} for Encoding 65 /eacute put currentdict end /Accent exch definefont 40 scalefont setfont 10 30 moveto (A) show\n");
assert_eq!(shape_names(&doc), vec!["Text: é"]);
let reference = parse(&page_pdf(
"BT /F1 40 Tf 10 30 Td <E9> Tj ET",
"/Font << /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >> >>",
vec![],
))
.unwrap();
let (a, b) = (render(&doc), render(&reference));
let (ia, ib) = (coverage(&a, 0, 0, 200, 100), coverage(&b, 0, 0, 200, 100));
assert!(ia > 50.0 && (ia - ib).abs() < 1.0, "EPS accent {ia} vs PDF accent {ib}");
assert_eq!(ink_x(&a), ink_x(&b));
}
+21 -1
View File
@@ -459,7 +459,9 @@ fn microsoft_families_resolve_to_metric_compatible_substitutes() {
let resolved = resolve(requested, style);
let family = &face(resolved.face).info.family;
if family.eq_ignore_ascii_case(requested) {
let installed = all_faces().iter().any(|f| f.info.family.eq_ignore_ascii_case(requested));
if installed {
assert!(family.eq_ignore_ascii_case(requested), "an installed {requested} wins over {family}");
assert!(!resolved.missing, "an installed original family is available");
} else {
assert_eq!(family, substitute);
@@ -471,6 +473,24 @@ fn microsoft_families_resolve_to_metric_compatible_substitutes() {
assert!(unknown.missing);
}
/// An installed Microsoft family is used as is and not reported missing, even when its
/// substitute is also available (#1677). It runs in a child process so the Calibri fixture does
/// not reach the substitute test above.
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn an_installed_microsoft_family_wins_over_its_substitute() {
const CHILD: &str = "EFFECTCRAFT_TEST_ISOLATED_CALIBRI";
if std::env::var_os(CHILD).is_none() {
run_in_child(CHILD, "fonts::tests::an_installed_microsoft_family_wins_over_its_substitute");
return;
}
add_font_data(test_font("Carlito"));
add_font_data(test_font("Calibri"));
let resolved = resolve("Calibri", "Regular");
assert_eq!(face(resolved.face).info.family, "Calibri");
assert!(!resolved.missing, "the installed family is available");
}
#[test]
fn the_han_order_follows_the_cjk_locale() {
// Chinese first for any Chinese tag, whether or not it names a script.
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-mime = { workspace = true }
effectcraft-raster = { workspace = true }
effectcraft-segment = { workspace = true }
rayon = { workspace = true }
+1 -12
View File
@@ -1,19 +1,8 @@
//! Compact text encodings of mattes for project storage: run-length coding (LEB128 varints)
//! wrapped in standard base64 (RFC 4648 §4).
const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
pub fn base64_encode(data: &[u8]) -> String {
let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
for c in data.chunks(3) {
let b = [c[0], *c.get(1).unwrap_or(&0), *c.get(2).unwrap_or(&0)];
let n = (b[0] as u32) << 16 | (b[1] as u32) << 8 | b[2] as u32;
out.push(B64[(n >> 18) as usize & 63] as char);
out.push(B64[(n >> 12) as usize & 63] as char);
out.push(if c.len() > 1 { B64[(n >> 6) as usize & 63] as char } else { '=' });
out.push(if c.len() > 2 { B64[n as usize & 63] as char } else { '=' });
}
out
craft_mime::base64::STANDARD.encode(data)
}
pub fn base64_decode(s: &str) -> Option<Vec<u8>> {
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-mime = { workspace = true }
craft-ui = { workspace = true }
craft-quit = { workspace = true, features = ["egui"] }
effectcraft-time = { workspace = true }
+1 -13
View File
@@ -803,19 +803,7 @@ fn render_frame(app: &EffectcraftApp, p: &Value) -> Outcome {
/// Standard base64 (RFC 4648, padded).
pub fn base64(data: &[u8]) -> String {
const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut s = String::with_capacity(data.len().div_ceil(3) * 4);
for c in data.chunks(3) {
let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32;
for i in 0..4 {
if i <= c.len() {
s.push(A[(n >> (18 - 6 * i) & 63) as usize] as char);
} else {
s.push('=');
}
}
}
s
craft_mime::base64::STANDARD.encode(data)
}
/// Builds the `ui.inspect` reply: window size and pixels per point, fps, UI state with the
+77
View File
@@ -224,6 +224,60 @@ fn workspace_layout_autosave_writes_on_a_hidden_tick() {
assert_eq!(app.ui_passes(), 0);
}
/// Whether a frame running `f` asked the root window to close.
#[cfg(not(target_arch = "wasm32"))]
fn asks_to_close(ctx: &egui::Context, mut f: impl FnMut(&egui::Context)) -> bool {
let mut out = ctx.run_ui(raw(egui::vec2(800.0, 480.0)), |ui| f(ui.ctx()));
out.textures_delta.clear();
out.viewport_output.get(&egui::ViewportId::ROOT).is_some_and(|v| v.commands.contains(&egui::ViewportCommand::Close))
}
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn restart_to_update_with_unsaved_changes_opens_the_unsaved_changes_prompt() {
let ctx = egui::Context::default();
let frame = eframe::Frame::_new_kittest();
let mut app = app_with_comp();
assert!(app.session.is_dirty(), "creating a comp and a layer leaves unsaved changes");
app.updates_rt.manual_window_open = true;
assert!(asks_to_close(&ctx, |ctx| updates_ui::restart_to_update(&mut app.updates_rt, ctx, false)), "the window is asked to close");
assert!(!app.updates_rt.manual_window_open, "the modal result dialog closes");
let mut raw = raw(egui::vec2(800.0, 480.0));
raw.viewports.insert(egui::ViewportId::ROOT, egui::ViewportInfo { events: vec![egui::ViewportEvent::Close], ..Default::default() });
let out = tick_with(&mut app, &ctx, &frame, &raw);
assert!(app.dialog.is_some(), "the close opens the unsaved-changes prompt");
assert!(out.viewport_commands.values().flatten().any(|c| matches!(c, egui::ViewportCommand::CancelClose)));
}
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn restart_to_update_with_nothing_unsaved_leaves_the_close_to_the_updater() {
let ctx = egui::Context::default();
let mut app = app_with_comp();
app.updates_rt.manual_window_open = true;
assert!(!asks_to_close(&ctx, |ctx| updates_ui::restart_to_update(&mut app.updates_rt, ctx, true)));
assert!(app.updates_rt.manual_window_open, "the result dialog stays until the updater closes the app");
}
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn restart_to_update_with_the_startup_prompt_open_leaves_the_window_open() {
let ctx = egui::Context::default();
let mut app = app_with_comp();
app.updates_rt.updater.offer(craft_update::Release {
tag: "v99.0.0".into(),
version: craft_update::parse_version("99.0.0"),
name: "EffectCraft 99.0.0".into(),
html_url: "https://github.com/storytold/effectcraft/releases/tag/v99.0.0".into(),
..Default::default()
});
assert!(app.updates_rt.updater.prompt_open());
app.updates_rt.manual_window_open = true;
assert!(!asks_to_close(&ctx, |ctx| updates_ui::restart_to_update(&mut app.updates_rt, ctx, false)), "the prompt carries the restart");
assert!(!app.updates_rt.manual_window_open, "the result dialog closes");
assert!(app.updates_rt.updater.prompt_open(), "the prompt stays");
}
#[test]
fn the_close_guard_answers_a_close_request_without_a_pass() {
let ctx = egui::Context::default();
@@ -239,6 +293,29 @@ fn the_close_guard_answers_a_close_request_without_a_pass() {
assert_eq!(app.ui_passes(), 0);
}
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn the_update_result_dialog_shows_every_status_but_idle() {
use craft_update::updater::Status;
let shown = [
Status::Checking,
Status::UpToDate,
Status::Failed("offline".into()),
Status::Installing,
Status::Ready,
Status::Managed,
Status::Updated("1.2.3".into()),
Status::InstallFailed("access denied".into()),
];
for status in &shown {
assert!(updates_ui::result_window_open(false, status, false), "{status:?} opens the dialog without a requested check");
}
assert!(!updates_ui::result_window_open(false, &Status::Idle, false));
assert!(!updates_ui::result_window_open(true, &Status::Idle, false), "a check that found a release closes it");
assert!(updates_ui::result_window_open(true, &Status::Idle, true), "a queued check keeps it open");
assert!(!updates_ui::result_window_open(false, &Status::Idle, true), "a queued check does not open it");
}
// ---------------------------------------------------------------- screenshots
fn screenshot_request(app: &mut EffectcraftApp, ctx: &egui::Context, path: Option<&str>, target: ShotTarget) -> std::sync::mpsc::Receiver<serde_json::Value> {
@@ -128,6 +128,18 @@ impl PropCx<'_> {
}
}
/// Shows property `uid` of layer `layer` in the Timeline: its layer and groups twirled open, the
/// property scrolled to and the Timeline brought forward (After Effects' Reveal in Timeline,
/// which a double-click on the property's name does; #1497).
fn reveal_in_timeline(app: &mut EffectcraftApp, ctx: &egui::Context, layer: u64, uid: u64) {
app.ui.timeline.search.clear();
app.ui.timeline.scroll_to = Some(crate::state::ScrollTo::Property(uid));
if let Err(e) = crate::menus::frontend(app, ctx, "timeline.revealProps", json!({"props": [{"layer": layer, "prop": uid}]})) {
app.ui.status = e;
}
app.show_panel(crate::dock::PanelKind::Timeline);
}
/// One property row: stopwatch, name, value control (and the keyframe navigator). Returns the
/// value fields of a vector, one per dimension, for the pick whip.
#[allow(clippy::too_many_arguments)]
@@ -167,7 +179,16 @@ fn prop_row(
},
);
app.auto.add(&format!("effectControls.prop.{uid}.stopwatch"), swr, &prop.name);
if resp.clicked() {
if resp.clicked() && ui.input(|i| i.modifiers.alt) {
// Alt+click adds an expression (or removes it), as in the Timeline. Effect Controls
// has no expression field: the property shows in the Timeline with its new
// expression's editor taking the keyboard, as After Effects does (#1460).
actions.push(("prop.setExpression".into(), json!({"layer": layer.id.0, "prop": uid})));
if prop.expr.is_none() {
reveal_in_timeline(app, ui.ctx(), layer.id.0, uid);
}
crate::panels::timeline::expression_toggled(app, ui.ctx(), prop);
} else if resp.clicked() {
actions.push(("prop.toggleAnimation".into(), json!({"layer": layer.id.0, "prop": uid, "selected": true})));
}
}
@@ -201,12 +222,7 @@ fn prop_row(
p.with_clip_rect(name_clip.intersect(p.clip_rect())).text(pos2(swr.max.x + 6.0, cy), Align2::LEFT_CENTER, &prop.name, Tokens::ui(12.0), t.text);
let name_response = ui.interact(name_clip, egui::Id::unique(("ec-prop-name", uid)), Sense::click());
if name_response.double_clicked() {
app.ui.timeline.search.clear();
app.ui.timeline.scroll_to = Some(crate::state::ScrollTo::Property(uid));
if let Err(e) = crate::menus::frontend(app, ui.ctx(), "timeline.revealProps", json!({"props": [{"layer": layer.id.0, "prop": uid}]})) {
app.ui.status = e;
}
app.show_panel(crate::dock::PanelKind::Timeline);
reveal_in_timeline(app, ui.ctx(), layer.id.0, uid);
}
app.auto.add(&format!("effectControls.prop.{uid}.name"), name_clip, &prop.name);
// With several layers selected the edit applies to the same effect property of each (#491).
+16 -20
View File
@@ -42,15 +42,15 @@ pub fn parse_angle(s: &str) -> Option<f64> {
s.parse().ok()
}
/// The angle after the degrees field of `v` (which shows `shown`, the remainder's size) went to
/// The angle after the degrees field of `v` (which shows `shown`, the signed remainder) went to
/// `new`. A drag moves the angle by the pointer's distance, so it runs through zero into negative
/// angles; a typed number is a size, so the remainder keeps its sign.
/// angles; a typed number is the new remainder, its sign included (#3367).
fn degrees_edit(v: f64, shown: f64, new: f64, dragging: bool) -> f64 {
if dragging {
return v + (new - shown);
}
let (rev, deg) = split_angle(v);
rev as f64 * 360.0 + if deg < 0.0 || (deg == 0.0 && v < 0.0) { -new } else { new }
let (rev, _) = split_angle(v);
rev as f64 * 360.0 + new
}
/// An angle value, `0x+45.0°`, at `at` (top left): the revolutions and the degrees
@@ -67,21 +67,16 @@ pub fn angle_field(ui: &mut egui::Ui, at: Pos2, id: egui::Id, v: f64, decimals:
pub fn angle_field_step(ui: &mut egui::Ui, at: Pos2, id: egui::Id, v: f64, decimals: usize, t: &Tokens) -> (Rect, Rect, Option<f64>, Option<Step>) {
let (rev, deg) = split_angle(v);
let (rr, new_rev, _) = widgets::hot_int_at(ui, at, id.with("rev"), rev, 0.1, "x", t);
let sign = ui.painter().text(pos2(rr.max.x - 2.0, rr.center().y), Align2::LEFT_CENTER, if deg < 0.0 { "-" } else { "+" }, Tokens::ui(12.0), t.hot_text);
let (dr, new_deg, _, step) = widgets::hot_number_step_at(
ui,
pos2(sign.max.x - 2.0, at.y),
id,
deg.abs(),
&widgets::HotSpec { speed: 0.5, range: (-1e9, 1e9), decimals, suffix: "°" },
t,
);
// A negative remainder's minus sign is part of the degrees' text, so typing replaces it (#3367).
let sign = ui.painter().text(pos2(rr.max.x - 2.0, rr.center().y), Align2::LEFT_CENTER, if deg < 0.0 { "" } else { "+" }, Tokens::ui(12.0), t.hot_text);
let (dr, new_deg, _, step) =
widgets::hot_number_step_at(ui, pos2(sign.max.x - 2.0, at.y), id, deg, &widgets::HotSpec { speed: 0.5, range: (-1e9, 1e9), decimals, suffix: "°" }, t);
let dragging = new_deg.is_some() && ui.ctx().is_being_dragged(id);
let out = match (new_rev, new_deg, step) {
(Some(r), _, _) => Some(r as f64 * 360.0 + deg),
// (The field shows the remainder's size; the step goes from the signed remainder.)
// A typed step goes from the signed remainder the field shows.
(None, Some(_), Some(s)) => s.apply(deg).map(|d| rev as f64 * 360.0 + d),
(None, Some(d), None) => Some(degrees_edit(v, deg.abs(), d, dragging)),
(None, Some(d), None) => Some(degrees_edit(v, deg, d, dragging)),
(None, None, _) => None,
};
let step = step.filter(|s| matches!(s, Step::Add(_)) && new_rev.is_none() && out.is_some());
@@ -642,18 +637,19 @@ mod tests {
#[test]
fn dragging_degrees_crosses_zero_into_negative_angles() {
// The field shows the size (0.3), so a 1-degree drag left lands on -0.7, and keeps going.
// A 1-degree drag left from 0.3 lands on -0.7, and keeps going.
let mut v = 0.3;
for _ in 0..3 {
let (_, deg) = split_angle(v);
v = degrees_edit(v, deg.abs(), deg.abs() - 1.0, true);
v = degrees_edit(v, deg, deg - 1.0, true);
}
assert!((v + 2.7).abs() < 1e-9, "{v}");
// And back up through zero.
let (_, deg) = split_angle(v);
assert!((degrees_edit(v, deg.abs(), deg.abs() + 3.0, true) - 0.3).abs() < 1e-9);
// Typing keeps the sign shown.
assert_eq!(degrees_edit(-30.0, 30.0, 45.0, false), -45.0);
assert!((degrees_edit(v, deg, deg + 3.0, true) - 0.3).abs() < 1e-9);
// A typed number is the remainder with its sign; the revolutions stay (#3367).
assert_eq!(degrees_edit(-30.0, -30.0, 45.0, false), 45.0);
assert_eq!(degrees_edit(-30.0, -30.0, -45.0, false), -45.0);
assert_eq!(degrees_edit(405.0, 45.0, 10.0, false), 370.0);
}
+196 -5
View File
@@ -208,6 +208,94 @@ fn speed_at(pr: &Property, spatial: bool, d: usize, lt: Tick) -> f64 {
}
}
/// How far either side of the key after a Hold key the step is sampled (seconds; far below a
/// pixel, far above the tick a stretched layer's time conversion can be off by).
const HOLD_EDGE: f64 = 1e-6;
/// How many key samples one grid step of [`sample_times`] keeps as they are (a key and both
/// sides of a Hold step fit); past that only its earliest and latest are kept.
const BUCKET_KEEP: usize = 4;
/// The key samples falling in one grid step of [`sample_times`].
#[derive(Clone)]
struct Bucket {
few: Vec<f64>,
first: f64,
last: f64,
/// More than [`BUCKET_KEEP`] samples fell here.
full: bool,
}
impl Default for Bucket {
fn default() -> Self {
Bucket { few: Vec::new(), first: f64::INFINITY, last: f64::NEG_INFINITY, full: false }
}
}
impl Bucket {
fn add(&mut self, t: f64) {
self.first = self.first.min(t);
self.last = self.last.max(t);
if self.full {
return;
}
if self.few.len() < BUCKET_KEEP {
self.few.push(t);
} else {
self.full = true;
self.few = Vec::new();
}
}
}
/// The comp times (seconds) a curve is sampled at between `t0` and `t1`: `n` evenly spaced
/// times, plus every key time `keys` puts inside the span, and around the key after a key that
/// holds (its out side is Hold) the instants just before and after it. `keys` are the active
/// keys in key order, as (comp seconds, holds); on a reversed layer their comp times descend.
/// Without the key times the polyline only passes near the keys: keys closer together than a
/// sample step (a one-frame jump zoomed out) got cut across, missing their squares, and a Hold
/// step was drawn as a slope across one step instead of a vertical edge (#2720). With no keys
/// this is the plain even grid. Keys denser than the grid (baked keys zoomed out) add nothing
/// visible and would multiply the evaluations: in a grid step with more than [`BUCKET_KEEP`]
/// key samples only the earliest and latest are kept, so a curve has at most `5 * n` samples
/// and sparse keys elsewhere in view keep every sample.
fn sample_times(t0: f64, t1: f64, n: usize, keys: &[(f64, bool)]) -> Vec<f64> {
let n = n.max(2);
let mut ts: Vec<f64> = (0..n).map(|i| t0 + (t1 - t0) * i as f64 / (n - 1) as f64).collect();
let (lo, hi) = (t0.min(t1), t0.max(t1));
let inside = |t: f64| t > lo && t < hi;
// One bucket per grid step.
let steps = n - 1;
let mut buckets = vec![Bucket::default(); steps];
let mut add = |t: f64| {
if !inside(t) {
return;
}
let b = (((t - lo) / (hi - lo) * steps as f64) as usize).min(steps - 1);
if let Some(bucket) = buckets.get_mut(b) {
bucket.add(t);
}
};
for (i, &(kt, holds)) in keys.iter().enumerate() {
add(kt);
if holds && let Some(&(next, _)) = keys.get(i + 1) {
add(next - HOLD_EDGE);
add(next + HOLD_EDGE);
}
}
for b in &buckets {
if b.full {
ts.extend([b.first, b.last]);
} else {
ts.extend_from_slice(&b.few);
}
}
ts.retain(|t| t.is_finite());
ts.sort_by(f64::total_cmp);
ts.dedup();
ts
}
/// Build the curves for the visible time span.
fn curves<'a>(app: &EffectcraftApp, comp: &'a Comp, ectx: &EvalCtx, tm: TMap, plot: Rect, speed: bool, pal: &GraphPalette) -> Vec<Curve<'a>> {
let sel = &app.session.state.selected_keys;
@@ -218,12 +306,14 @@ fn curves<'a>(app: &EffectcraftApp, comp: &'a Comp, ectx: &EvalCtx, tm: TMap, pl
let Some(pr) = l.props.find(uid) else { continue };
let spatial = pr.spatial && matches!(pr.value, Value::Vec2(_) | Value::Vec3(_));
let dims = if speed && spatial { 1 } else { shown_dims(l, pr) };
// The active keys (muted ones are skipped by evaluation) in comp seconds, and whether each
// holds its value up to the next one.
let active: Vec<(f64, bool)> = pr.keys.iter().filter(|k| !k.muted).map(|k| (l.comp_time(k.time).seconds(), k.out_interp == Interp::Hold)).collect();
let times = sample_times(tm.t(plot.min.x), tm.t(plot.max.x), n, &active);
for d in 0..dims {
let color = if dims > 1 { pal.dims[d % pal.dims.len()] } else { pal.props[pi % pal.props.len()] };
let mut pts = Vec::with_capacity(n);
for i in 0..n {
let x = plot.min.x + plot.width() * i as f32 / (n - 1) as f32;
let ct = tm.t(x);
let mut pts = Vec::with_capacity(times.len());
for &ct in &times {
let tt = Tick::from_seconds_f64(ct);
let v = if speed { speed_at(pr, spatial, d, l.layer_time(tt)) } else { ectx.at(tt).value(l, pr).components().get(d).copied().unwrap_or(0.0) };
pts.push((ct, v));
@@ -1044,7 +1134,108 @@ mod theme_tests {
#[cfg(test)]
mod tests {
use super::shift_axes;
use super::{HOLD_EDGE, sample_times, shift_axes};
use effectcraft_engine::keyframe::{Keyframe, Value, evaluate};
use effectcraft_engine::time::Tick;
/// The value-graph polyline of `keys` (layer time = comp time) sampled at `times`.
fn polyline(keys: &[Keyframe], times: &[f64]) -> Vec<(f64, f64)> {
times
.iter()
.map(|&t| {
let v = evaluate(keys, Tick::from_seconds_f64(t), false).map(|v| v.components().first().copied().unwrap_or(f64::NAN)).unwrap_or(f64::NAN);
(t, v)
})
.collect()
}
fn active(keys: &[Keyframe]) -> Vec<(f64, bool)> {
keys.iter().map(|k| (k.time.seconds(), k.out_interp == effectcraft_engine::keyframe::Interp::Hold)).collect()
}
/// #2720: a step one frame wide, zoomed out so it is narrower than a sample step. The curve
/// has to pass through every key square: the old even grid (`sample_times` with no keys)
/// never sampled 0.2 s or 0.2 s + 1 frame and drew a slope from 0 to 1 across the step.
#[test]
fn value_graph_passes_through_keys_narrower_than_a_sample_step() {
let keys = [
Keyframe::new(Tick::from_seconds_f64(0.0), Value::Scalar(0.0)),
Keyframe::new(Tick::from_seconds_f64(0.2), Value::Scalar(0.0)),
Keyframe::new(Tick::from_seconds_f64(0.2 + 1.0 / 30.0), Value::Scalar(1.0)),
Keyframe::new(Tick::from_seconds_f64(0.9), Value::Scalar(1.0)),
];
let times = sample_times(0.0, 1.0, 4, &active(&keys));
let pts = polyline(&keys, &times);
for k in &keys {
let (t, v) = (k.time.seconds(), k.value.components()[0]);
assert!(pts.iter().any(|p| p.0 == t && (p.1 - v).abs() < 1e-12), "no curve point on the key at {t} s ({v}): {pts:?}");
}
// The spans between equal keys are flat: no sample between 0 and 0.2 s leaves 0, none
// between 0.2 s + 1 frame and 0.9 s leaves 1.
for &(t, v) in &pts {
if t <= 0.2 {
assert!(v.abs() < 1e-12, "{t} s: {v}");
} else if (0.2 + 1.0 / 30.0..=0.9).contains(&t) {
assert!((v - 1.0).abs() < 1e-12, "{t} s: {v}");
}
}
// Times are sorted and unique, the even grid is still there.
assert!(times.windows(2).all(|w| w[0] < w[1]), "{times:?}");
assert!([0.0, 1.0 / 3.0, 2.0 / 3.0, 1.0].iter().all(|g| times.iter().any(|t| (t - g).abs() < 1e-12)), "{times:?}");
}
/// #2720: a Hold key draws a vertical step at the next key, not a slope across a sample step.
#[test]
fn hold_step_is_vertical() {
let keys = [Keyframe::new(Tick::from_seconds_f64(0.1), Value::Scalar(0.0)).hold(), Keyframe::new(Tick::from_seconds_f64(0.5), Value::Scalar(1.0))];
let pts = polyline(&keys, &sample_times(0.0, 1.0, 4, &active(&keys)));
let at = pts.iter().position(|p| p.0 == 0.5).unwrap_or_else(|| panic!("no sample on the key at 0.5 s: {pts:?}"));
let before = pts[at - 1];
assert!((0.5 - before.0 - HOLD_EDGE).abs() < 1e-12 && before.1.abs() < 1e-12, "the held value ends right before the next key: {pts:?}");
assert!((pts[at].1 - 1.0).abs() < 1e-12, "{pts:?}");
}
#[test]
fn sample_times_keep_keys_inside_the_span_only() {
let times = sample_times(1.0, 2.0, 3, &[(0.5, true), (1.25, false), (2.5, false)]);
// 0.5 s and 2.5 s are off screen; the hold from 0.5 s ends at 1.25 s, inside it.
assert_eq!(times, vec![1.0, 1.25 - HOLD_EDGE, 1.25, 1.25 + HOLD_EDGE, 1.5, 2.0]);
// Degenerate input never panics: an empty span is one point, a NaN span has no points.
assert_eq!(sample_times(0.0, 0.0, 0, &[]), vec![0.0]);
assert!(sample_times(f64::NAN, 1.0, 4, &[(0.5, true)]).iter().all(|t| t.is_finite()));
}
/// A reversed layer: its keys run backwards in comp time, and both sides of each step after
/// a Hold key are still sampled.
#[test]
fn sample_times_cover_hold_steps_on_a_reversed_layer() {
let times = sample_times(1.0, 2.0, 10, &[(1.8, true), (1.4, true), (1.1, false)]);
for t in [1.8, 1.4 - HOLD_EDGE, 1.4, 1.4 + HOLD_EDGE, 1.1 - HOLD_EDGE, 1.1, 1.1 + HOLD_EDGE] {
assert!(times.contains(&t), "{t} missing: {times:?}");
}
assert!(times.windows(2).all(|w| w[0] < w[1]), "{times:?}");
}
/// Dense keys zoomed out (100 000 in one second, half of them Hold) don't multiply the samples.
#[test]
fn sample_times_stay_bounded_with_dense_keys() {
let keys: Vec<(f64, bool)> = (0..100_000).map(|i| (i as f64 / 100_000.0, i % 2 == 0)).collect();
let times = sample_times(0.0, 1.0, 100, &keys);
assert!(times.len() <= 300, "{} samples", times.len());
}
/// A dense cluster of keys only thins its own grid steps: a Hold step elsewhere in view keeps
/// its key and both sides of the step.
#[test]
fn sample_times_keep_sparse_keys_beside_a_dense_cluster() {
let mut keys: Vec<(f64, bool)> = (0..100_000).map(|i| (0.1 * i as f64 / 100_000.0, i % 2 == 0)).collect();
keys.extend([(0.5, true), (0.8, false)]);
let times = sample_times(0.0, 1.0, 100, &keys);
assert!(times.len() <= 500, "{} samples", times.len());
for t in [0.5, 0.8 - HOLD_EDGE, 0.8, 0.8 + HOLD_EDGE] {
assert!(times.contains(&t), "{t} missing");
}
}
#[test]
fn shift_locks_drag_to_dominant_axis() {
+22 -14
View File
@@ -325,9 +325,27 @@ fn expr_focus_id() -> egui::Id {
egui::Id::unique("tl-expr-focus")
}
/// Asks the expression editor of property `prop` to take the keyboard when the Timeline next
/// draws it, selecting `select` (counts of characters before the end).
fn focus_expression(ctx: &egui::Context, prop: u64, select: [usize; 2]) {
let pass = ctx.cumulative_pass_nr();
ctx.data_mut(|d| d.insert_temp(expr_focus_id(), ExprFocus { prop, select, pass }));
// The editor takes it in a later pass: make sure one comes without waiting for input.
ctx.request_repaint();
}
/// After a `prop.setExpression` toggle of `prop` was queued (an Alt-click on its stopwatch, in the
/// Timeline or in Effect Controls): adding one, its editor opens, comes into view and takes the
/// keyboard with the new expression selected (#1072, #1460); removing it, a pending request for
/// its editor goes, as nothing is left to take the keyboard.
pub(crate) fn expression_toggled(app: &mut EffectcraftApp, ctx: &egui::Context, prop: &Property) {
let uid = prop.uid;
if prop.expr.is_none() {
app.ui.timeline.expr_closed.remove(&uid);
focus_expression(ctx, uid, [usize::MAX, 0]);
} else if ctx.data(|d| d.get_temp::<ExprFocus>(expr_focus_id())).is_some_and(|f| f.prop == uid) {
ctx.data_mut(|d| d.remove::<ExprFocus>(expr_focus_id()));
}
}
#[derive(Clone, Debug, PartialEq)]
@@ -4054,7 +4072,10 @@ fn expr_editor_cell(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc:
gp.rect_filled(Rect::from_min_max(pos2(graph_x0, r.min.y), r.max), 0.0, t.expr_bg);
let er = Rect::from_min_max(pos2(graph_x0 + 8.0, r.min.y + 3.0), pos2(rect.max.x - 14.0, r.max.y - 3.0));
let mut buf: String = ctx.data(|d| d.get_temp(buf_id)).unwrap_or_else(|| ex.text.clone());
if let Some(f) = ctx.data(|d| d.get_temp::<ExprFocus>(expr_focus_id())).filter(|f| f.prop == *uid) {
// Not in the pass it was asked for in: a click that asked for it from another panel (an
// Alt-click on a stopwatch in Effect Controls, drawn before the Timeline) would take the
// keyboard straight back as a click elsewhere (#1460).
if let Some(f) = ctx.data(|d| d.get_temp::<ExprFocus>(expr_focus_id())).filter(|f| f.prop == *uid && ctx.cumulative_pass_nr() > f.pass) {
// Asked for once (a pick whip put a reference in, or an Alt-click added the
// expression): editing starts in the expression as it is now.
ctx.data_mut(|d| d.remove::<ExprFocus>(expr_focus_id()));
@@ -4277,19 +4298,6 @@ fn prop_context_menu(
}
}
/// An expression was just added to `prop` (it had none) or removed: adding one opens its
/// editor, which comes into view and takes the keyboard with the new expression selected
/// (#1072); removing it leaves nothing to take the keyboard.
fn expression_toggled(app: &mut EffectcraftApp, ctx: &egui::Context, prop: &Property) {
let uid = prop.uid;
if prop.expr.is_none() {
app.ui.timeline.expr_closed.remove(&uid);
focus_expression(ctx, uid, [usize::MAX, 0]);
} else if ctx.data(|d| d.get_temp::<ExprFocus>(expr_focus_id())).is_some_and(|f| f.prop == uid) {
ctx.data_mut(|d| d.remove::<ExprFocus>(expr_focus_id()));
}
}
/// The value editors and the property pick whip.
fn prop_value_cell(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: &RowCtx<'_>, ectx: &EvalCtx<'_>, prop: &Property, name_x: f32) {
let vis = g.tl.vis;
+10 -3
View File
@@ -1252,16 +1252,23 @@ fn phase_dragstart(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx,
})
}
Tool::MaskFeather => ov::begin_feather(app, ectx, &paths, &map, press).map(Gesture::Overlay),
Tool::Rotate => pick(app, ectx, cpt, mods.shift).map(|l| {
// The layer under the press is the one that turns.
Tool::Rotate => pick(app, ectx, map.to_comp(press), mods.shift).map(|l| {
let layer = comp.layer(l).cloned();
let sign = layer.as_ref().map_or(1.0, |l| rotation_sign(ectx, l));
let others = selected_others(app, comp, l)
.into_iter()
.filter_map(|o| Some((o.id, ectx.f(o, o.transform()?, "rotation", 0.0), rotation_sign(ectx, o) * sign)))
.collect();
let pivot = layer.as_ref().and_then(|l| anchor_in_comp(ectx, l)).unwrap_or(cpt);
let anchor = layer.as_ref().and_then(|l| anchor_in_comp(ectx, l));
let pivot = anchor.unwrap_or(cpt);
let rot = layer.as_ref().and_then(|l| l.transform().map(|tr| ectx.f(l, tr, "rotation", 0.0))).unwrap_or(0.0);
let last_angle = (cpt[1] - pivot[1]).atan2(cpt[0] - pivot[0]).to_degrees();
// The turn counts from where the button went down, as in After Effects: by the
// time the drag registers the pointer has already swept some way round the anchor.
// A press on the anchor (or with no anchor to turn about) has no direction, so
// the turn then counts from where the drag registered.
let seed = if anchor.is_some() && press.distance(map.to_screen(pivot)) > 3.0 { map.to_comp(press) } else { cpt };
let last_angle = (seed[1] - pivot[1]).atan2(seed[0] - pivot[0]).to_degrees();
Gesture::Rotate { layer: l, pivot, sign, last_angle, turned: 0.0, start_rot: rot, others }
}),
Tool::PanBehind => pick(app, ectx, cpt, false).and_then(|l| {
+32 -6
View File
@@ -9,7 +9,7 @@ use craft_update::updater::{Action, Status, Updater};
pub(crate) struct Runtime {
#[cfg(not(target_arch = "wasm32"))]
updater: Updater,
pub(crate) updater: Updater,
/// The desktop app turns the check at start on; tests and the web build leave it off.
#[cfg(not(target_arch = "wasm32"))]
startup_check: bool,
@@ -44,6 +44,26 @@ impl EffectcraftApp {
}
}
/// Restart to Update. With unsaved changes the result dialog closes and the window is asked to
/// close, so the unsaved-changes prompt opens. Cancel there leaves the update ready. An open
/// startup prompt keeps its own Restart to Update and save-first note, so the window stays open.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn restart_to_update(rt: &mut Runtime, ctx: &egui::Context, clean: bool) {
if clean {
rt.updater.restart_to_update();
return;
}
rt.manual_window_open = false;
rt.updater.dismiss_status();
if rt.updater.prompt_open() {
return;
}
// Without a prompt this only takes the close a finished installation queued, so a save after
// Cancel does not close the app.
let _ = rt.updater.show_if(ctx, true);
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
}
pub(crate) fn check(app: &mut EffectcraftApp) {
#[cfg(not(target_arch = "wasm32"))]
{
@@ -72,6 +92,15 @@ pub(crate) fn tick(app: &mut EffectcraftApp, ctx: &egui::Context) {
let _ = (app, ctx);
}
/// Whether the check result dialog shows this frame. It answers Help ▸ Check for Updates until the
/// requested check ends, and it shows whenever the status is not idle: at the first start after an
/// update, the check at start leaves `Updated` or `InstallFailed`. A check that finds a release
/// leaves the status idle and the startup prompt takes over, so the dialog closes then.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn result_window_open(open: bool, status: &Status, check_requested: bool) -> bool {
!matches!(status, Status::Idle) || (open && check_requested)
}
pub(crate) fn show(app: &mut EffectcraftApp, ctx: &egui::Context) {
#[cfg(not(target_arch = "wasm32"))]
{
@@ -83,6 +112,7 @@ pub(crate) fn show(app: &mut EffectcraftApp, ctx: &egui::Context) {
Action::None => {}
}
let rt = &mut app.updates_rt;
rt.manual_window_open = result_window_open(rt.manual_window_open, rt.updater.status(), rt.check_requested);
if rt.manual_window_open {
match CheckResultDialog::new(&crate::UPDATE_APP, env!("CARGO_PKG_VERSION")).show(ctx, rt.updater.status()) {
CheckResultResponse::None => {}
@@ -91,11 +121,7 @@ pub(crate) fn show(app: &mut EffectcraftApp, ctx: &egui::Context) {
rt.updater.dismiss_status();
}
CheckResultResponse::CheckAgain => rt.updater.check_now(ctx),
CheckResultResponse::RestartToUpdate => rt.updater.restart_to_update(),
}
// A check that found a release leaves the status idle and the startup prompt takes over.
if matches!(rt.updater.status(), Status::Idle) && !rt.check_requested {
rt.manual_window_open = false;
CheckResultResponse::RestartToUpdate => restart_to_update(rt, ctx, clean),
}
}
}
+22
View File
@@ -206,3 +206,25 @@ fn commit_edits(click_away: bool) {
}
}
}
/// #3367: on a negative angle the minus sign is part of the degrees typing replaces, so typing
/// 50 into `0x-20.0°` sets +50°. It used to keep the sign (-50°). Committing the text as it
/// opened keeps the angle.
#[test]
fn typing_into_a_negative_angle_replaces_its_sign() {
for panel in [Panel::EffectControls, Panel::Timeline, Panel::Properties] {
for (start, text, expected) in
[(-20.0, Some("50"), 50.0), (-20.0, Some("-50"), -50.0), (-20.0, None, -20.0), (-400.0, None, -400.0), (45.0, Some("-30"), -30.0)]
{
let (mut h, layer, uid) = harness(panel, start);
let p = component(&h, panel, uid, false);
click(&mut h, p);
if let Some(text) = text {
h.input_mut().events.push(Event::Text(text.into()));
h.step();
}
key(&mut h, Key::Enter, Modifiers::NONE);
assert_eq!(angle(&h, layer, uid), expected, "{panel:?}: {start} then {text:?}");
}
}
}
@@ -635,6 +635,7 @@ fn clicks_wait_for_the_probe_and_edits_popups_dialogs_and_comp_switches_end_or_b
/// each Resolution. Skips without a GPU adapter.
#[test]
fn picks_on_the_gpu_viewer() {
let _gpu = crate::gpu_test::lock();
let Some(probe) = effectcraft_gpu::Gpu::headless() else {
eprintln!("no GPU adapter: skipping");
return;
+48
View File
@@ -725,6 +725,54 @@ fn an_expression_added_at_the_bottom_of_the_timeline_comes_into_view() {
assert_eq!(expression(&h, layer, opacity).as_deref(), Some("25"));
}
/// #1460: Alt-clicking an effect property's stopwatch in Effect Controls adds an expression, as in
/// the Timeline (it toggled the animation instead). Effect Controls has no expression field, so,
/// as in After Effects, the property shows in the Timeline (its layer and effect twirled open,
/// scrolled into view) with the new expression's editor taking the keyboard: typing replaces
/// it. Another Alt-click removes it and leaves nothing holding the keyboard.
#[test]
fn alt_clicking_a_stopwatch_in_effect_controls_edits_the_expression_in_the_timeline() {
let (mut h, _) = harness();
let (layer, slider) = {
let s = &mut h.state_mut().session;
let l = s.execute("layer.newSolid", json!({"name": "Plate", "color": "#406080"})).unwrap()["layer"].as_u64().unwrap();
s.execute("effect.apply", json!({"layers": [l], "effect": "Slider Control"})).unwrap();
// Layers above push it below the bottom of the Timeline.
for i in 0..40 {
s.execute("layer.newSolid", json!({"name": format!("Layer {i}"), "color": "#204060"})).unwrap();
}
s.execute("layer.select", json!({"layers": [l]})).unwrap();
let slider = s.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(l)).unwrap().props.prop("effects/#1/slider").unwrap().uid;
(l, slider)
};
h.state_mut().show_panel(effectcraft_ui_egui::dock::PanelKind::EffectControls);
h.run_steps(3);
let editor = format!("timeline.prop.{slider}.expression");
assert!(h.state().auto.find(&format!("timeline.prop.{slider}.row")).is_none(), "the property is not in the Timeline yet");
let alt = Modifiers { alt: true, ..Modifiers::NONE };
let at = center(&h, &format!("effectControls.prop.{slider}.stopwatch"));
click_with(&mut h, at, alt);
assert!(expression(&h, layer, slider).is_some(), "an expression was added");
let key = h.state().session.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(layer)).unwrap().props.find(slider).unwrap().is_animated();
assert!(!key, "Alt-click doesn't turn on keyframing");
assert!(h.state().ui.timeline.reveal_props.contains(&slider), "the property is revealed in the Timeline");
let rows = h.state().auto.find("timeline.scroll").expect("the Timeline is shown").rect;
let e = h.state().auto.find(&editor).expect("the Timeline draws the expression editor").rect;
assert!(e[1] >= rows[1] - 0.5 && e[1] + e[3] <= rows[1] + rows[3] + 0.5, "the editor is in view: {e:?} in {rows:?}");
type_text(&mut h, "42");
commit_expression(&mut h);
assert_eq!(expression(&h, layer, slider).as_deref(), Some("42"), "typing replaced the new expression");
// Alt-click again removes the expression and its editor with it (the keyboard stays free;
// the editor's focus request was taken when it was added, so this doesn't exercise the
// cancel path).
let at = center(&h, &format!("effectControls.prop.{slider}.stopwatch"));
click_with(&mut h, at, alt);
h.run_steps(5);
assert_eq!(expression(&h, layer, slider), None);
assert!(h.state().auto.find(&editor).is_none(), "the expression editor is gone");
assert!(!h.ctx.egui_wants_keyboard_input(), "nothing has the keyboard");
}
fn right_click(h: &mut Harness<'_, EffectcraftApp>, p: Pos2) {
h.input_mut().events.push(Event::PointerMoved(p));
h.step();
+61
View File
@@ -3218,6 +3218,67 @@ fn rotate_tool_counts_revolutions_both_ways() {
assert_eq!(h.state().session.history.undo.len(), undo_before + 1, "one drag, one undo step");
}
/// #2769: the Rotation tool counts the turn from where the button went down, as in After Effects,
/// not from where the drag registered: a first move that sweeps 60° round the anchor turns the
/// layer 60° (it used to drop that sweep, so near the anchor the tool lagged behind the pointer).
#[test]
fn rotate_tool_counts_the_turn_from_the_press() {
let mut h = harness();
let box_id = h.state().session.active_comp().unwrap().layers[0].id;
h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
h.state_mut().ui.tool = Tool::Rotate;
h.run_steps(2);
let center = screen(&h, [320.0, 180.0]);
let r = (screen(&h, [350.0, 180.0]) - center).x;
// Press on the box right of its anchor (0°), then one move to 60° clockwise, still on the box.
let press = center + vec2(r * 0.3, 0.0);
let to = center + vec2(r * 60f32.to_radians().cos(), r * 60f32.to_radians().sin());
h.input_mut().events.push(Event::PointerMoved(press));
h.input_mut().events.push(Event::PointerButton { pos: press, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
h.step();
h.input_mut().events.push(Event::PointerMoved(to));
h.step();
// The gesture applies the previous frame's pointer: hold a frame.
h.step();
h.input_mut().events.push(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
let rot = h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/rotation").unwrap().value.as_f64();
assert!((rot - 60.0).abs() < 1.0, "the whole sweep from the press turns the layer: {rot}");
}
/// #2769: a press right on the anchor point has no direction to count the turn from, so the layer
/// doesn't jump on the first move; the turn counts from where the drag registered.
#[test]
fn rotate_tool_pressed_on_the_anchor_does_not_jump() {
let mut h = harness();
let box_id = h.state().session.active_comp().unwrap().layers[0].id;
h.state_mut().session.execute("layer.select", json!({"layers": [box_id.0]})).unwrap();
h.state_mut().ui.tool = Tool::Rotate;
h.run_steps(2);
let rotation = |h: &Harness<'_, EffectcraftApp>| {
h.state().session.active_comp().unwrap().layer(box_id).unwrap().props.prop("transform/rotation").unwrap().value.as_f64()
};
let center = screen(&h, [320.0, 180.0]);
let r = (screen(&h, [350.0, 180.0]) - center).x;
let at = |deg: f32| center + vec2(r * deg.to_radians().cos(), r * deg.to_radians().sin());
h.input_mut().events.push(Event::PointerMoved(center));
h.input_mut().events.push(Event::PointerButton { pos: center, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
h.step();
h.input_mut().events.push(Event::PointerMoved(at(60.0)));
h.step();
// The gesture applies the previous frame's pointer: hold a frame.
h.step();
let first = rotation(&h);
assert!(first.abs() < 1.0, "no jump on the first move: {first}");
h.input_mut().events.push(Event::PointerMoved(at(90.0)));
h.step();
h.step();
let next = rotation(&h);
assert!((next - 30.0).abs() < 1.0, "then turns with the pointer: {next}");
h.input_mut().events.push(Event::PointerButton { pos: at(90.0), button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
h.run_steps(2);
}
/// A harness whose Box is a child of Plate (anchor 320,180) with `plate` set on Plate first, at
/// parent-space position (200, 120). Returns the two layers' ids.
fn rotate_child_harness(plate: serde_json::Value) -> (Harness<'static, EffectcraftApp>, u64, u64) {
-1
View File
@@ -18,7 +18,6 @@ cpal or muda. Everything in L0 to L4, the egui UI and the web app also build for
| L0 | `time` | `Tick` (254 016 000 000 per second), rational frame rates incl. NTSC, SMPTE and drop-frame timecode |
| L0 | `geom` | Vectors, matrices, quaternions, the layer transform (anchor, position, scale, orientation, rotation) |
| L0 | `color` | sRGB and linear, HSL/HSV, luminance, the 38 blend modes, label colors |
| L0 | `opusenc` | Opus encoder: RFC 6716 SILK (NB/MB/WB), hybrid (SWB/FB) and CELT (FB) modes chosen by bitrate and application (audio / voice), 48 kHz 20 ms packets, mono/stereo, and the RFC 7845 `OpusHead`, for WebM export audio |
| L1 | `raster` | Premultiplied float images, sampling, affine and projective warps, blurs, compositing (parallel with rayon) |
| L1 | `keyframe` | Animated values, keyframes with temporal ease and spatial Bezier, roving, hold, velocity |
| L1 | `segment` | Swappable trained models for Roto Brush and face tracking: the `MaskModel` and `FaceModel` interfaces, a registry of open-licensed models (authors, licence, URL, size, SHA-256), PyTorch checkpoint and TensorFlow Lite readers (with an interpreter), SHA-256, MobileSAM (TinyViT + Segment Anything decoder) and MediaPipe Face Landmarker (BlazeFace + Face Mesh V2) on rayon and ndarray's safe GEMM |
+11 -2
View File
@@ -388,12 +388,21 @@ package can't read files, open the network or crash the app.
[`storytold/effectcraft-plugins`](https://github.com/storytold/effectcraft-plugins) repository;
packages are added by pull request and reviewed there. The app has no field for another
catalog; `library.refresh {catalog}` (for plug-in authors testing an entry) takes an `index.json`
on GitHub or a local file, and the page then says it shows a test catalog until Refresh.
on GitHub or a local file, and the page then says it shows a test catalog until Refresh. A file
URL that names a network path (`file:////server/share/…`) is refused.
* **Nothing is downloaded until you ask.** Fetch Catalog (`library.refresh`) reads the catalog;
Install (`library.install`) downloads the package's `.wasm`, checks its SHA-256 (and its size, when the
catalog gives one) against the catalog and loads it. Downloads use the system's `curl`,
`https://` only and starting on GitHub's hosts (redirects stay on `https://`; the SHA-256 check
is what guarantees the file).
is what guarantees the file). A package's `url` must be a release asset
(`https://github.com/<owner>/<repo>/releases/download/…`). When the package names a `repo`, the
asset must be in that repository. A `repo` that isn't `https://github.com/<owner>/<repo>` (with
an optional trailing `/` or `.git`) or a folder in one (`https://github.com/<owner>/<repo>/tree/<ref>/…`)
makes Install and Preview refuse every `https://` release of the package. These rules don't apply
to a release whose `url` is a local `file://` path, which only a local catalog may name (to test
an entry before its release is published). A catalog other than the community one is read only when
`library.refresh {catalog}` names it: an `index.json` on GitHub's hosts over `https://`, or a
local `file://` path.
* **Where it goes.** `<Plug-ins>/Library/<id>/<version>/<id>.wasm` (the `Plug-ins` folder next to
the settings), listed in `Library/installed.json`. Installed packages load at start-up, each
checked against the SHA-256 it was installed with. The catalog last fetched is kept as
+5 -3
View File
@@ -181,7 +181,7 @@ Package fields:
| `author` | no | You, or your group. |
| `description` | no, but reviewed | One or two plain sentences that say what the effect does. |
| `license` | no, but reviewed | The licence's SPDX id (`MIT`, `Apache-2.0`, `MIT OR Apache-2.0`…). |
| `repo` | no, but reviewed | The public source repository. The details' "Open on GitHub" opens it. |
| `repo` | no, but reviewed | The public source repository, as `https://github.com/<owner>/<repo>` (a trailing `/` or `.git` is allowed), or a folder in it, `https://github.com/<owner>/<repo>/tree/<ref>/<path>`. The details' "Open on GitHub" opens it. When `repo` is given in any other form, Install and Preview refuse every release of the package. |
| `tags` | no | Lower-case words for the tag filter (`blur`, `stylize`, `light`, `distort`…). Reuse tags the catalog already has. |
| `preview.video` | no | An `https://` link to a short demo video on YouTube, Vimeo or GitHub. "Watch video" opens it in the browser. A link that isn't `https://` (or is longer than 500 characters) is left out. The live preview doesn't need it. |
| `versions` | yes | One entry per release, newest or oldest first. |
@@ -193,7 +193,7 @@ Release fields (one per entry in `versions`):
| `version` | yes | Dotted numbers only, at most 32 characters: `1.0.0`, `1.2`. No `v` and no suffixes such as `-beta`. |
| `api` | yes | The plug-in API the module implements: `1`. |
| `minApp` | no | The oldest EffectCraft version the release needs (`0.17.0`). Leave it out if any version with this API works. |
| `url` | yes | The release asset's `https://` URL on GitHub. |
| `url` | yes | The release asset's `https://` URL on GitHub: `https://github.com/<owner>/<repo>/releases/download/<tag>/<file>`. When the package has a `repo`, the asset must be in that repository. Install and Preview refuse any other `https://` URL. |
| `sha256` | yes | The file's SHA-256, 64 hex digits. |
| `size` | no, but reviewed | The file's size in bytes. When it is given, a download of another size is refused. |
| `effects` | yes | The effect ids the module registers (its manifest's `id`). EffectCraft uses them to find the package that provides an effect a project needs. |
@@ -226,7 +226,9 @@ Make a folder with an `index.json` that holds only your package:
```
File URLs are absolute: `file:///home/me/glow-rings/index.json` on Linux and macOS,
`file:///C:/Users/me/glow-rings/index.json` on Windows. Write a space as `%20`.
`file:///C:/Users/me/glow-rings/index.json` on Windows. Write a space as `%20`. Separate folders
with `/`: a file URL that holds a `\` or a percent-encoded `/` or `\`, or names a network path
(`file:////server/share/…`), is refused.
Use a separate settings folder, so the test doesn't touch your real installation:
Loaded 100 of 105 files, more files were not shown because too many files have changed in this diff. Show more