diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 873f4f7aa..000000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[lints] -cargo.unused_workspace_dependencies = "allow" diff --git a/.config/nextest.toml b/.config/nextest.toml new file mode 100644 index 000000000..1037b2310 --- /dev/null +++ b/.config/nextest.toml @@ -0,0 +1,44 @@ +# cargo-nextest (https://nexte.st) runs every test in its own process, in parallel across test +# binaries. The Windows leg of the Workspace gate uses it (.github/workflows/workspace.yml); every +# other leg, and local runs, keep `cargo test`. Nextest does not run doctests, so the Windows leg +# also runs `cargo test --doc`. +# +# cargo nextest run --workspace --profile ci +# cargo test --workspace --doc +# +# Refuse an older nextest: CI installs exactly this version (GHSA-vc25-qg58-w57c is fixed in it). +nextest-version = "0.9.150" + +# Some tests share a resource that an in-process mutex keeps to one test at a time. With one +# process per test those mutexes no longer serialize anything, so these groups do it instead, one +# test at a time, as under `cargo test`. +[test-groups] +# craft-ui: `gpu_test_lock()` / `gpu_lock()`. Each renderer creates its own wgpu instance and +# device, and creating or destroying one while another test uses its device can crash. +gpu = { max-threads = 1 } +# craft-video h264_decode: `FIXTURE_GENERATION` keeps a second producer from replacing a fixture +# file another test is reading. +video-fixtures = { max-threads = 1 } +# craft-print: `PRINT_TICKET_PROVIDER` serializes the Windows PrintTicket provider, and the tests +# share the one test print queue the gate installs. +print = { max-threads = 1 } + +[[profile.default.overrides]] +filter = 'package(craft-ui) & (binary(visual_regression) | test(/^combo::tests::|selected_tab_erases_a_baseline_centered_on_its_bottom_edge/))' +test-group = 'gpu' + +[[profile.default.overrides]] +filter = 'package(craft-video) & binary(h264_decode)' +test-group = 'video-fixtures' + +[[profile.default.overrides]] +filter = 'package(craft-print)' +test-group = 'print' + +# The gate (`--profile ci`). Run every test and list all failures at the end, as `cargo test` +# reports a whole binary. Slow tests are named every 60 seconds; none is stopped for being slow, +# so a hang is still ended by the gate's own timeout, as before. +[profile.ci] +fail-fast = false +slow-timeout = { period = "60s" } +failure-output = "immediate-final" diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index 63feb5d86..3e7f07d8a 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -25,7 +25,8 @@ jobs: with: os: ${{ github.ref == 'refs/heads/next' && '["ubuntu-latest"]' || '["ubuntu-latest","windows-latest","macos-latest"]' }} timeout-minutes: 120 - tools: "" + # The Windows leg runs its tests with nextest (.config/nextest.toml). + tools: cargo-nextest@0.9.150 packages: mesa-vulkan-drivers libvulkan1 craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 linux-env: WGPU_BACKEND=vulkan @@ -58,8 +59,12 @@ jobs: cargo fmt --check cargo clippy --workspace --all-targets --locked --keep-going -- -D warnings cargo clippy --workspace --all-targets --all-features --locked --keep-going -- -D warnings - cargo test --workspace --locked - cargo test --workspace --all-features --locked + # The same tests as `cargo test`, run in parallel across test binaries instead of one + # binary at a time; nextest skips doctests, so `cargo test --doc` runs them. + cargo nextest run --workspace --locked --profile ci + cargo test --workspace --doc --locked + cargo nextest run --workspace --all-features --locked --profile ci + cargo test --workspace --doc --all-features --locked msrv: name: Rust 1.95 diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 92e6a9a7d..c42bc8168 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -93,7 +93,7 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso | `crates/psd/tests/fixtures/atn/*.atn` (seven synthetic action sets, encoded field by field; `crates/psd/tests/fixtures/atn/README.md`) | PhotoCraft contributors | [PhotoCraft `crates/psd/tests/fixtures/atn`](https://github.com/storytold/photocraft/tree/63b641b55e54bf8f5c83efd977fdb16a2c255431/crates/psd/tests/fixtures/atn) | MIT OR Apache-2.0 | | `crates/lottie/tests/fixtures/` | EffectCraft contributors (original work: four hand-written Lottie JSON documents, written from the public Lottie format description; not exported from any Adobe product or third-party file) | [EffectCraft `crates/lottie/tests/fixtures`](https://github.com/storytold/effectcraft/tree/e18f48fb/crates/lottie/tests/fixtures) | MIT OR Apache-2.0 | | `crates/pkcs7/tests/data/` | PdfCraft contributors (original work: throw-away test keys and certificates made with OpenSSL 3 for these tests, as seven PKCS #12 files with the password `test` and two PEM certificates; they protect nothing; commands in `crates/pkcs7/tests/data/README.md`) | [PdfCraft `crates/sign/tests/data`](https://github.com/storytold/pdfcraft/tree/1d7a9f9f/crates/sign/tests/data) | MIT OR Apache-2.0 | -| `crates/affinity/tests/support/corpus.tsv`, `crates/affinity/tests/support/affinity3-signatures.txt` (manifest and measured signatures only; the 51 documents are not bundled, `scripts/fetch-affinity-corpus.sh` downloads them outside the checkout) | The authors of each document: samuel-etver (vector-art), Seth A. Robinson (Patchy fixtures), Nick Beeuwsaert (AFDesignLoad), Jeremy Cantu (Branding), eviltwo (AssetStoreTemplate), Square (leakcanary) | each file's pinned upstream URL and SHA-256 in the manifest; list carried over from [VectorCraft `xtask/src/affinity_corpus.rs`](https://github.com/storytold/vectorcraft/blob/6e873b806c9d5b3f2b55f651df57bd97933b579a/xtask/src/affinity_corpus.rs) | CC0-1.0, MIT or Apache-2.0 (per file, in the manifest) | +| `crates/affinity/tests/support/corpus.tsv`, `crates/affinity/tests/support/affinity3-signatures.txt` (manifest and measured signatures only; the 113 files, 90 of them documents, are not bundled, `scripts/fetch-affinity-corpus.sh` downloads them outside the checkout) | The authors of each document: samuel-etver (vector-art), satoshoe-dev (affinity-samples), Seth A. Robinson (Patchy fixtures), Nick Beeuwsaert (AFDesignLoad), Jeremy Cantu (Branding), eviltwo (AssetStoreTemplate), Square (leakcanary) | each file's pinned upstream URL and SHA-256 in the manifest; every name from [VectorCraft `xtask/src/affinity_corpus.rs`](https://github.com/storytold/vectorcraft/blob/bada8e17103db9917de44e40a1cead5a7fc71c3c/xtask/src/affinity_corpus.rs) and [PhotoCraft `xtask/affinity-corpus.sha256`](https://github.com/storytold/photocraft/blob/629751503165118d5fec5f70e64fe2d6592f7b53/xtask/affinity-corpus.sha256) | CC0-1.0, MIT or Apache-2.0 (per file, in the manifest) | | `crates/raw/docs/coverage/adobe-cameras.tsv` | Facts (maker, model, extension, profile, minimum Camera Raw version) parsed by LightCraft contributors from Adobe's published list | [Adobe Camera Raw supported cameras](https://helpx.adobe.com/camera-raw/desktop/dng-and-file-formats/camera-raw-plug-supported-cameras.html), Wayback capture 20261003133011; moved from [LightCraft `docs/raw`](https://github.com/storytold/lightcraft/tree/5ff567a4a2e067bef1b6bce152c11e7f727ccbaf/docs/raw) | Facts, not copyrightable expression; the file is MIT OR Apache-2.0 | | `crates/raw/docs/coverage/{coverage,coverage-sweep,evidence,usage-weights}.tsv`, `coverage.py` | LightCraft contributors | [LightCraft `docs/raw`](https://github.com/storytold/lightcraft/tree/5ff567a4a2e067bef1b6bce152c11e7f727ccbaf/docs/raw) | MIT OR Apache-2.0 | | `crates/raw/docs/raf-corpus.sha256` (hashes only; the files are not bundled) | The photographers who contributed each file to raw.pixls.us | [raw.pixls.us](https://raw.pixls.us/) | CC0-1.0 | @@ -106,3 +106,25 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso `crates/tga/src/metadata_tests.rs` and metadata-boundary helpers in `crates/tga/src/lib.rs` are adapted from nuclearspike's FilmCraft #1932 at `e7842f76f887005c460c64a8ab51d59d086e6f27` (MIT OR Apache-2.0). Synthetic fixtures are original contributor test data; primary Git author retained. The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate. + +## Score serializers + +`crates/score` extracts SoundCraft MusicXML and SVG score writing from `crates/engine/src/score.rs` at main `648ed9db9a0e98cc55643babffac381521a556c8`, with per-pitch ties adapted from [SoundCraft #182](https://github.com/storytold/soundcraft/pull/182) at `21cb618d93ec0e8d4771ac534994de8d5996ff10`. Source is MIT OR Apache-2.0. Import commits preserve Brandon Thomas, NetterSilver, Samuel Bacaro and Vikram Bhaskaran as primary authors of their respective changes. The consumer retains session mapping and layout. Shared API wiring, validation and focused tests are subsequent Dylan M. Taylor changes. + +## Source-timed MIDI compatibility + +`crates/midi/src/source_timed.rs` is extracted from LoopCraft `crates/midi/src/lib.rs` at `884e67c24b9b68badf6a4a4f7d62492f3673d354` (MIT OR Apache-2.0). The recorded source history for the extracted file is Dylan M. Taylor (`0d150c1f`, `440bc7ed`, `9e50523d`); Git authorship is preserved. It includes the original hostile-file, SMPTE, timing and round-trip tests. UTF-8 truncation and event-budget suffix repairs are subsequent changes. The module is an application-independent source-timed model; it does not depend on LoopCraft. +## Non-seeking WAV primitives + +`crates/audio/src/wav_stream.rs` is extracted from AniCraft `crates/audio/src/wav.rs` at `d875a7feb250e6ab06aa28a37fb78a0699e88330` (MIT OR Apache-2.0). Tanish Manku is retained as the primary import author, from original commit `30c121d269a49d9abe54a3834ed4aa0201542535`. Odd-data padding accounting, shared API documentation and regression tests are subsequent Dylan M. Taylor changes. + +## GIMP palette reader + +`crates/palette` extracts the GPL text parser from AniCraft `crates/doc/src/swatch.rs` at `d875a7feb250e6ab06aa28a37fb78a0699e88330` (MIT OR Apache-2.0), introduced by Tanish Manku in `a64130a9da9bdde545463b852d95d6a6774b1dfd` (AniCraft #270). The import preserves Tanish as primary author. Neutral RGB/name adaptation separates application display-name sanitization and the `Untitled` policy; validation tests and integration are subsequent Dylan M. Taylor changes. + +The raw gradient wire ordering in `craft-lottie::Gradient::flat_values` follows +AniCraft's Lottie exporter by Tanish Manku (`63b39355`, `b9258b7b`), MIT OR Apache-2.0. +Sanitization, animation fitting and numeric output policy remain caller-owned. +## Screen Video key frames + +`crates/video/src/screen_video.rs` extracts AniCraft `crates/xfl/src/video.rs` codec-3 decoding and its synthetic partial-block fixture, introduced by Tanish Manku in `1f3e603b7ff53a3da364e41ec4da8d43dc83aab0` with pixel-loop cleanup `2b7571363b4b647266df758f95b6e397021cfd4d` (MIT OR Apache-2.0). Import commits preserve Tanish as primary author. Caller-owned bounds and neutral RGBA output separate codec work from the consuming app's FLV first-frame/report policy. Boundary tests and integration are subsequent Dylan M. Taylor changes. diff --git a/Cargo.lock b/Cargo.lock index 08911ee5b..7a78a19ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -114,7 +114,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" dependencies = [ "cfg-if", + "getrandom 0.3.4", "once_cell", + "serde", "version_check", "zerocopy", ] @@ -579,7 +581,7 @@ dependencies = [ "log", "num-rational", "num-traits", - "pastey", + "pastey 0.1.1", "rayon", "thiserror 2.0.21", "v_frame", @@ -645,6 +647,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd307490d624467aa6f74b0eabb77633d1f758a7b25f12bceb0b22e08d9726f6" +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + [[package]] name = "base64" version = "0.22.1" @@ -682,6 +690,15 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "bit-set" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec 0.8.0", +] + [[package]] name = "bit-set" version = "0.10.0" @@ -700,6 +717,12 @@ dependencies = [ "bit-vec 0.10.1", ] +[[package]] +name = "bit-vec" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + [[package]] name = "bit-vec" version = "0.9.1" @@ -884,6 +907,12 @@ dependencies = [ "cipher 0.4.4", ] +[[package]] +name = "borrow-or-share" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0b364ead1874514c8c2855ab558056ebfeb775653e7ae45ff72f28f8f3166c" + [[package]] name = "borsh" version = "1.8.1" @@ -995,6 +1024,12 @@ version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "bytecount" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175812e0be2bccb6abe50bb8d566126198344f707e304f45c648fd8f2cc0365e" + [[package]] name = "bytemuck" version = "1.25.2" @@ -1033,6 +1068,16 @@ version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" +[[package]] +name = "bzip2-rs" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "beeb59e7e4c811ab37cc73680c798c7a5da77fc9989c62b09138e31ee740f735" +dependencies = [ + "crc32fast", + "tinyvec", +] + [[package]] name = "calendrical_calculations" version = "0.2.4" @@ -1067,6 +1112,64 @@ dependencies = [ "cipher 0.4.4", ] +[[package]] +name = "candle-core" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c15b675b80d994b2eadb20a4bbe434eabeb454eac3ee5e2b4cf6f147ee9be091" +dependencies = [ + "byteorder", + "float8", + "gemm", + "half", + "libm", + "memmap2", + "num-traits", + "num_cpus", + "rand 0.9.5", + "rand_distr", + "rayon", + "safetensors", + "thiserror 2.0.21", + "yoke", + "zip 7.4.0", +] + +[[package]] +name = "candle-nn" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3045fa9e7aef8567d209a27d56b692f60b96f4d0569f4c3011f8ca6715c65e03" +dependencies = [ + "candle-core", + "half", + "libc", + "num-traits", + "rayon", + "safetensors", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "candle-transformers" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b538ec4aa807c416a2ddd3621044888f188827862e2a6fcacba4738e89795d01" +dependencies = [ + "byteorder", + "candle-core", + "candle-nn", + "fancy-regex 0.17.0", + "num-traits", + "rand 0.9.5", + "rayon", + "serde", + "serde_json", + "serde_plain", + "tracing", +] + [[package]] name = "cap-primitives" version = "4.0.3" @@ -1100,6 +1203,15 @@ dependencies = [ "cipher 0.4.4", ] +[[package]] +name = "castaway" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a" +dependencies = [ + "rustversion", +] + [[package]] name = "cbc" version = "0.2.1" @@ -1410,6 +1522,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "compact_str" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dfdd1c2274d9aa354115b09dc9a901d6c5576818cdf70d14cae2bdb47df00ab" +dependencies = [ + "castaway", + "cfg-if", + "itoa", + "rustversion", + "ryu", + "serde", + "static_assertions", +] + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -1446,12 +1573,41 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "cookie" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a373e3602691c3cdea496d2f0ee5935151e6168fe87739483c463db1b2f2f87" +dependencies = [ + "percent-encoding", + "time", + "version_check", +] + [[package]] name = "cookie-factory" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9885fa71e26b8ab7855e2ec7cae6e9b380edff76cd052e07c683a0319d51b3a2" +[[package]] +name = "cookie_store" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15b2c103cf610ec6cae3da84a766285b42fd16aad564758459e6ecf128c75206" +dependencies = [ + "cookie", + "document-features", + "idna", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "time", + "url", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -1561,6 +1717,7 @@ dependencies = [ "craft-aaf", "craft-aep", "craft-affinity", + "craft-ai", "craft-ai-core", "craft-anicraft-format", "craft-assuan", @@ -1575,11 +1732,13 @@ dependencies = [ "craft-cms", "craft-codepage", "craft-color", + "craft-control", "craft-control-protocol", "craft-crs", "craft-crw", "craft-csv", "craft-dav", + "craft-dds", "craft-deckcraft-format", "craft-designcraft-format", "craft-dialogs", @@ -1612,6 +1771,7 @@ dependencies = [ "craft-font-store", "craft-font-subset", "craft-format", + "craft-frame", "craft-gainmap", "craft-gcode", "craft-gif", @@ -1628,6 +1788,7 @@ dependencies = [ "craft-image-codecs", "craft-imaging", "craft-indd", + "craft-ipc", "craft-jmap", "craft-jpeg", "craft-jsonrpc", @@ -1646,6 +1807,7 @@ dependencies = [ "craft-mcp", "craft-mcp-server", "craft-media", + "craft-media-source", "craft-menu", "craft-mesh", "craft-metadata", @@ -1659,6 +1821,7 @@ dependencies = [ "craft-openpgp", "craft-ora", "craft-pages", + "craft-palette", "craft-pcraft-bundle", "craft-pcraft-format", "craft-pdf 0.1.0", @@ -1672,6 +1835,7 @@ dependencies = [ "craft-pptx", "craft-prc", "craft-prepress", + "craft-press-print", "craft-print", "craft-proof", "craft-proto", @@ -1683,6 +1847,7 @@ dependencies = [ "craft-raw", "craft-review", "craft-rtf", + "craft-score", "craft-scraft-format", "craft-settings", "craft-sf2", @@ -1692,6 +1857,7 @@ dependencies = [ "craft-sketch", "craft-smime", "craft-sniff", + "craft-ssip", "craft-stencilcraft-document", "craft-stencilcraft-format", "craft-stream-codecs", @@ -1756,6 +1922,23 @@ dependencies = [ "sha2 0.10.9", ] +[[package]] +name = "craft-ai" +version = "0.1.0" +dependencies = [ + "candle-core", + "candle-nn", + "candle-transformers", + "dirs", + "serde", + "serde_json", + "sha2 0.11.1", + "sysinfo", + "tempfile", + "tokenizers", + "ureq", +] + [[package]] name = "craft-ai-agents" version = "0.1.0" @@ -1933,6 +2116,16 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-control" +version = "0.1.0" +dependencies = [ + "getrandom 0.3.4", + "proptest", + "serde", + "serde_json", +] + [[package]] name = "craft-control-protocol" version = "0.1.0" @@ -1975,6 +2168,13 @@ dependencies = [ "proptest", ] +[[package]] +name = "craft-dds" +version = "0.1.0" +dependencies = [ + "rayon", +] + [[package]] name = "craft-deckcraft-format" version = "0.1.0" @@ -1983,7 +2183,7 @@ dependencies = [ "serde", "serde_json", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -2079,7 +2279,7 @@ dependencies = [ "craft-zip", "serde_json", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -2103,7 +2303,7 @@ dependencies = [ "thiserror 2.0.21", "unicode-bidi", "zeroize", - "zip", + "zip 9.1.3", ] [[package]] @@ -2196,7 +2396,7 @@ dependencies = [ "craft-xml", "craft-zip", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -2239,7 +2439,7 @@ dependencies = [ "ruzstd", "serde_json", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -2250,7 +2450,7 @@ dependencies = [ "serde", "serde_json", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -2408,6 +2608,16 @@ dependencies = [ "sha2 0.10.9", ] +[[package]] +name = "craft-frame" +version = "0.1.0" +dependencies = [ + "craft-color", + "craft-time", + "rayon", + "serde", +] + [[package]] name = "craft-gainmap" version = "0.1.0" @@ -2533,7 +2743,7 @@ dependencies = [ "craft-xml", "craft-zip", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -2541,6 +2751,7 @@ name = "craft-image-codecs" version = "0.1.0" dependencies = [ "avif-serialize", + "craft-dds", "craft-heif", "craft-jpeg", "craft-media-testkit", @@ -2595,6 +2806,21 @@ dependencies = [ "indd", ] +[[package]] +name = "craft-ipc" +version = "0.1.0" +dependencies = [ + "chrono", + "getrandom 0.2.17", + "interprocess", + "jsonschema", + "libc", + "serde", + "serde_json", + "sha2 0.10.9", + "widestring", +] + [[package]] name = "craft-jmap" version = "0.1.0" @@ -2657,7 +2883,7 @@ dependencies = [ "craft-xml", "craft-zip", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -2769,9 +2995,26 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-media-source" +version = "0.1.0" +dependencies = [ + "craft-audio", + "craft-color", + "craft-frame", + "craft-media", + "craft-time", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-media-testkit" version = "0.1.0" +dependencies = [ + "craft-testkit", +] [[package]] name = "craft-menu" @@ -2872,7 +3115,7 @@ dependencies = [ "sha2 0.11.1", "thiserror 2.0.21", "zeroize", - "zip", + "zip 9.1.3", ] [[package]] @@ -2909,7 +3152,7 @@ dependencies = [ "craft-zip", "log", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -2934,7 +3177,7 @@ dependencies = [ "craft-xml", "craft-zip", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -2951,6 +3194,10 @@ dependencies = [ "serde", ] +[[package]] +name = "craft-palette" +version = "0.1.0" + [[package]] name = "craft-pcraft-bundle" version = "0.1.0" @@ -3043,7 +3290,7 @@ dependencies = [ "serde", "serde_json", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -3175,6 +3422,16 @@ dependencies = [ "ttf-parser", ] +[[package]] +name = "craft-press-print" +version = "0.1.0" +dependencies = [ + "craft-cms", + "craft-prepress", + "craft-print", + "thiserror 2.0.21", +] + [[package]] name = "craft-print" version = "0.1.0" @@ -3243,6 +3500,7 @@ dependencies = [ name = "craft-pst" version = "0.1.0" dependencies = [ + "craft-pst", "proptest", ] @@ -3325,6 +3583,13 @@ dependencies = [ "proptest", ] +[[package]] +name = "craft-score" +version = "0.1.0" +dependencies = [ + "craft-xml", +] + [[package]] name = "craft-scraft-format" version = "0.1.0" @@ -3387,7 +3652,7 @@ dependencies = [ "kurbo", "serde_json", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] [[package]] @@ -3425,6 +3690,10 @@ dependencies = [ name = "craft-sniff" version = "0.1.0" +[[package]] +name = "craft-ssip" +version = "0.1.0" + [[package]] name = "craft-stencilcraft-document" version = "0.1.0" @@ -3514,6 +3783,10 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-testkit" +version = "0.1.0" + [[package]] name = "craft-text" version = "0.1.0" @@ -3546,6 +3819,7 @@ version = "0.1.0" dependencies = [ "bytemuck", "dds", + "half", "rayon", "thiserror 2.0.21", ] @@ -3871,7 +4145,7 @@ dependencies = [ "verifysign", "winreg", "winsafe", - "zip", + "zip 9.1.3", ] [[package]] @@ -3917,6 +4191,7 @@ dependencies = [ "craft-jpeg", "craft-media", "craft-media-testkit", + "miniz_oxide 0.8.9", "rav1e", "rayon", "thiserror 2.0.21", @@ -4025,6 +4300,7 @@ version = "0.1.0" dependencies = [ "craft-cfb", "craft-sheet", + "craft-xls", "md-5 0.10.6", "miniz_oxide 0.9.1", "sha1 0.10.7", @@ -4038,6 +4314,7 @@ dependencies = [ "craft-drawingml", "craft-opc", "craft-sheet", + "craft-xlsx", "craft-xml", "thiserror 2.0.21", ] @@ -4051,6 +4328,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xtask" +version = "0.1.0" +dependencies = [ + "craft-atomic", + "serde_json", + "tempfile", +] + [[package]] name = "craft-y4m" version = "0.1.0" @@ -4062,12 +4348,33 @@ dependencies = [ name = "craft-zip" version = "0.1.0" dependencies = [ + "bzip2-rs", "craft-zip", "flate2", + "lzma-rs", + "miniz_oxide 0.8.9", + "ruzstd", + "serde", + "serde_json", "thiserror 2.0.21", - "zip", + "zip 9.1.3", ] +[[package]] +name = "crc" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eb8a2a1cd12ab0d987a5d5e825195d372001a4094a0376319d5a0ad71c1ba0d" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "217698eaf96b4a3f0bc4f3662aaa55bdf913cd54d7204591faa790070c6d0853" + [[package]] name = "crc24" version = "0.1.6" @@ -4316,6 +4623,15 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "dary_heap" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b1e3a325bc115f096c8b77bbf027a7c2592230e70be2d985be950d3d5e60ebe" +dependencies = [ + "serde", +] + [[package]] name = "data-encoding" version = "2.11.1" @@ -4573,6 +4889,12 @@ dependencies = [ "libloading", ] +[[package]] +name = "doctest-file" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2db04e74f0a9a93103b50e90b96024c9b2bdca8bce6a632ec71b88736d3d359" + [[package]] name = "document-features" version = "0.2.12" @@ -4671,6 +4993,22 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" +[[package]] +name = "dyn-stack" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c4713e43e2886ba72b8271aa66c93d722116acf7a75555cce11dcde84388fe8" +dependencies = [ + "bytemuck", + "dyn-stack-macros", +] + +[[package]] +name = "dyn-stack-macros" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d926b4d407d372f141f93bb444696142c29d32962ccbd3531117cf3aa0bfa9" + [[package]] name = "eax" version = "0.5.0" @@ -4943,6 +5281,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "email_address" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e079f19b08ca6239f47f8ba8509c11cf3ea30095831f7fed61441475edd8c449" +dependencies = [ + "serde", +] + [[package]] name = "emath" version = "0.37.0" @@ -4983,6 +5330,18 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" +[[package]] +name = "enum-as-inner" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1e6a265c649f3f5979b601d26f1d05ada116434c87741c9493cb56218f76cbc" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "enumflags2" version = "0.7.12" @@ -5077,6 +5436,12 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b5343afd4a8365a643ac588dab4cf234a190c7f6c88c9f6dd6ffe00837661b7" +[[package]] +name = "esaxx-rs" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d817e038c30374a4bcb22f94d0a8a0e216958d4c3dcde369b1439fec4bdda6e6" + [[package]] name = "escape8259" version = "0.5.4" @@ -5134,6 +5499,28 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af9673d8203fcb076b19dfd17e38b3d4ae9f44959416ea532ce72415a6020365" +[[package]] +name = "fancy-regex" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72cf461f865c862bb7dc573f643dd6a2b6842f7c30b07882b56bd148cc2761b8" +dependencies = [ + "bit-set 0.8.0", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "fancy-regex" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d301f5bf187b3c295fce6468d3875037a0bccc5f6b151c63cac2f85babf21912" +dependencies = [ + "bit-set 0.8.0", + "regex-automata", + "regex-syntax", +] + [[package]] name = "fastrand" version = "2.5.0" @@ -5277,6 +5664,29 @@ dependencies = [ "num-traits", ] +[[package]] +name = "float8" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719a903cc23e4a89e87962c2a80fdb45cdaad0983a89bd150bb57b4c8571a7d5" +dependencies = [ + "half", + "num-traits", + "rand 0.9.5", + "rand_distr", +] + +[[package]] +name = "fluent-uri" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc74ac4d8359ae70623506d512209619e5cf8f347124910440dbc221714b328e" +dependencies = [ + "borrow-or-share", + "ref-cast", + "serde", +] + [[package]] name = "fmt-derive" version = "0.1.2" @@ -5428,6 +5838,16 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fraction" +version = "0.15.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076045bb43dac435333ed5f04caf35c7463631d0dae2deb2638d94dd0a5b872" +dependencies = [ + "lazy_static", + "num", +] + [[package]] name = "fs-set-times" version = "0.20.3" @@ -5537,6 +5957,125 @@ dependencies = [ "libc", ] +[[package]] +name = "gemm" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa0673db364b12263d103b68337a68fbecc541d6f6b61ba72fe438654709eacb" +dependencies = [ + "dyn-stack", + "gemm-c32", + "gemm-c64", + "gemm-common", + "gemm-f16", + "gemm-f32", + "gemm-f64", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + +[[package]] +name = "gemm-c32" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "086936dbdcb99e37aad81d320f98f670e53c1e55a98bee70573e83f95beb128c" +dependencies = [ + "dyn-stack", + "gemm-common", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + +[[package]] +name = "gemm-c64" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20c8aeeeec425959bda4d9827664029ba1501a90a0d1e6228e48bef741db3a3f" +dependencies = [ + "dyn-stack", + "gemm-common", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + +[[package]] +name = "gemm-common" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88027625910cc9b1085aaaa1c4bc46bb3a36aad323452b33c25b5e4e7c8e2a3e" +dependencies = [ + "bytemuck", + "dyn-stack", + "half", + "libm", + "num-complex", + "num-traits", + "once_cell", + "paste", + "pulp", + "raw-cpuid", + "rayon", + "seq-macro", + "sysctl", +] + +[[package]] +name = "gemm-f16" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3df7a55202e6cd6739d82ae3399c8e0c7e1402859b30e4cb780e61525d9486e" +dependencies = [ + "dyn-stack", + "gemm-common", + "gemm-f32", + "half", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "rayon", + "seq-macro", +] + +[[package]] +name = "gemm-f32" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e0b8c9da1fbec6e3e3ab2ce6bc259ef18eb5f6f0d3e4edf54b75f9fd41a81c" +dependencies = [ + "dyn-stack", + "gemm-common", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + +[[package]] +name = "gemm-f64" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "056131e8f2a521bfab322f804ccd652520c79700d81209e9d9275bbdecaadc6a" +dependencies = [ + "dyn-stack", + "gemm-common", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -5832,9 +6371,12 @@ version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" dependencies = [ + "bytemuck", "cfg-if", "crunchy", "num-traits", + "rand 0.9.5", + "rand_distr", "zerocopy", ] @@ -5889,6 +6431,8 @@ dependencies = [ "allocator-api2", "equivalent", "foldhash 0.2.0", + "serde", + "serde_core", ] [[package]] @@ -5897,6 +6441,8 @@ version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" dependencies = [ + "allocator-api2", + "equivalent", "foldhash 0.2.0", ] @@ -6594,6 +7140,19 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "interprocess" +version = "2.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb02ffbf25e8ef0f4a95bff054eef75f6d1bf16919474cf2caa63b21997478a" +dependencies = [ + "doctest-file", + "libc", + "recvmsg", + "widestring", + "windows-sys 0.61.2", +] + [[package]] name = "inventory" version = "0.3.25" @@ -6776,6 +7335,58 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "jsonschema" +version = "0.49.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ec8a241beed129f06114aa68007e905ca350e7baeb6e17a7631bb7978d91b2" +dependencies = [ + "ahash", + "bytecount", + "data-encoding", + "email_address", + "fancy-regex 0.19.2", + "fraction", + "getrandom 0.3.4", + "idna", + "itoa", + "jsonschema-regex", + "jsonschema-value", + "num-cmp", + "num-traits", + "percent-encoding", + "referencing", + "regex", + "serde", + "serde_json", + "strum", + "unicode-general-category", + "uuid-simd", +] + +[[package]] +name = "jsonschema-regex" +version = "0.49.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91994f45017ed5e66aa8e59b8415f4cb033a6380d7200387b7cf117595fbdf85" +dependencies = [ + "regex-syntax", +] + +[[package]] +name = "jsonschema-value" +version = "0.49.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ec7637f83e510868ae6ed625f7ebfbbde4554ee8ce49854caa5126a8b9b9ecb" +dependencies = [ + "ahash", + "bytecount", + "fraction", + "num-cmp", + "num-traits", + "serde_json", +] + [[package]] name = "jxl-bitstream" version = "1.1.0" @@ -7285,6 +7896,32 @@ version = "0.11.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "373f5eceeeab7925e0c1098212f2fbc4d416adec9d35051a6ab251e824c1854a" +[[package]] +name = "lzma-rs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "297e814c836ae64db86b36cf2a557ba54368d03f6afcd7d947c266692f71115e" +dependencies = [ + "byteorder", + "crc", +] + +[[package]] +name = "macro_rules_attribute" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3ae8f6d608c795738406608304d30a2dfbdc8e58e44f7ba43236da5208ded3c" +dependencies = [ + "macro_rules_attribute-proc_macro", + "pastey 0.2.3", +] + +[[package]] +name = "macro_rules_attribute-proc_macro" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc04a4c58212d57930a24bf47d3fa87485264a3a054e9c10e042eb373573ad3c" + [[package]] name = "maybe-owned" version = "0.3.4" @@ -7334,6 +7971,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b0ca1155a9ecc25aa12e8313a119f32edf0255fea2f44839c4ac0a12b2ac157" dependencies = [ "libc", + "stable_deref_trait", ] [[package]] @@ -7345,6 +7983,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "micromap" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a86d3146ed3995b5913c414f6664344b9617457320782e64f0bb44afd49d74" + [[package]] name = "mime" version = "0.3.17" @@ -7398,6 +8042,28 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "monostate" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3341a273f6c9d5bef1908f17b7267bbab0e95c9bf69a0d4dcf8e9e1b2c76ef67" +dependencies = [ + "monostate-impl", + "serde", + "serde_core", +] + +[[package]] +name = "monostate-impl" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4db6d5580af57bf992f59068d4ea26fd518574ff48d7639b255a36f9de6e7e9" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "moxcms" version = "0.8.1" @@ -7598,6 +8264,20 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + [[package]] name = "num-bigint" version = "0.4.8" @@ -7625,12 +8305,19 @@ dependencies = [ "zeroize", ] +[[package]] +name = "num-cmp" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63335b2e2c34fae2fb0aa2cecfd9f0832a1e24b3b32ecec612c3426d46dc8aaa" + [[package]] name = "num-complex" version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ + "bytemuck", "num-traits", ] @@ -7691,6 +8378,16 @@ dependencies = [ "libm", ] +[[package]] +name = "num_cpus" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "num_enum" version = "0.7.6" @@ -8105,6 +8802,17 @@ dependencies = [ "objc2-foundation 0.3.2", ] +[[package]] +name = "objc2-open-directory" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb82bed227edf5201dfedf072bba4015a33d3d4a98519837295a90f0a23f676d" +dependencies = [ + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-foundation 0.3.2", +] + [[package]] name = "objc2-quartz-core" version = "0.2.2" @@ -8241,6 +8949,28 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "onig" +version = "6.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cc3cbf698f9438986c11a880c90a6d04b9de27575afd28bbf45b154b6c709e2" +dependencies = [ + "bitflags 2.13.2", + "libc", + "once_cell", + "onig_sys", +] + +[[package]] +name = "onig_sys" +version = "69.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e68317604e77e53b85896388e1a803c1d21b74c899ec9e5e1112db90735edd7" +dependencies = [ + "cc", + "pkg-config", +] + [[package]] name = "oorandom" version = "11.1.5" @@ -8313,6 +9043,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "outref" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a80800c0488c3a21695ea981a54918fbb37abf04f4d0720c453632255e2ff0e" + [[package]] name = "p256" version = "0.13.2" @@ -8450,6 +9186,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec" +[[package]] +name = "pastey" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" + [[package]] name = "pbkdf2" version = "0.13.0" @@ -9069,6 +9811,29 @@ dependencies = [ "unarray", ] +[[package]] +name = "pulp" +version = "0.22.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "046aa45b989642ec2e4717c8e72d677b13edd831a4d3b6cf37d9a3e54912496a" +dependencies = [ + "bytemuck", + "cfg-if", + "libm", + "num-complex", + "paste", + "pulp-wasm-simd-flag", + "raw-cpuid", + "reborrow", + "version_check", +] + +[[package]] +name = "pulp-wasm-simd-flag" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d8f70e07b9c3962945a74e59ca1c511bba65b6419468acc217c457d93f3c740" + [[package]] name = "pxfm" version = "0.1.30" @@ -9274,6 +10039,16 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" +[[package]] +name = "rand_distr" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8615d50dcf34fa31f7ab52692afec947c4dd0ab803cc87cb3b0b4570ff7463" +dependencies = [ + "num-traits", + "rand 0.9.5", +] + [[package]] name = "rand_pcg" version = "0.10.2" @@ -9348,6 +10123,15 @@ dependencies = [ "rgb", ] +[[package]] +name = "raw-cpuid" +version = "11.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "498cd0dc59d73224351ee52a95fee0f1a617a2eae0e7d9d720cc622c73a54186" +dependencies = [ + "bitflags 2.13.2", +] + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -9376,6 +10160,17 @@ dependencies = [ "rayon-core", ] +[[package]] +name = "rayon-cond" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2964d0cf57a3e7a06e8183d14a8b527195c706b7983549cd5462d5aa3747438f" +dependencies = [ + "either", + "itertools 0.14.0", + "rayon", +] + [[package]] name = "rayon-core" version = "1.13.0" @@ -9478,6 +10273,18 @@ dependencies = [ "yoke", ] +[[package]] +name = "reborrow" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03251193000f4bd3b042892be858ee50e8b3719f2b08e5833ac4353724632430" + +[[package]] +name = "recvmsg" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3edd4d5d42c92f0a659926464d4cce56b562761267ecf0f469d85b7de384175" + [[package]] name = "redox_syscall" version = "0.4.1" @@ -9515,6 +10322,43 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "ref-cast" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e440fb4e4b4147295338efb76001ab9e4efc0e5839df2c47fc5ac2381d365c3" +dependencies = [ + "ref-cast-impl", +] + +[[package]] +name = "ref-cast-impl" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecd8964f8453721699a1ed72037b0db49ce2f5a5138486ee89bed6f67cdf3a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "referencing" +version = "0.49.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6efa2154ea6f5ce0fdecdd2a8d18f2fa1a39a8fbba91564f555a592e4dce8278" +dependencies = [ + "ahash", + "fluent-uri", + "getrandom 0.3.4", + "hashbrown 0.17.1", + "itoa", + "micromap", + "parking_lot", + "percent-encoding", + "serde_json", +] + [[package]] name = "regex" version = "1.13.1" @@ -9983,6 +10827,17 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04d056b875a9d2e6cb9a61d127afee9ac5999b9f87bcb32079d1318e505be714" +[[package]] +name = "safetensors" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "675656c1eabb620b921efea4f9199f97fc86e36dd6ffd1fbbe48d0f59a4987f5" +dependencies = [ + "hashbrown 0.16.1", + "serde", + "serde_json", +] + [[package]] name = "same-file" version = "1.0.6" @@ -10087,6 +10942,12 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73" +[[package]] +name = "seq-macro" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc711410fbe7399f390ca1c3b60ad0f53f80e95c5eb935e52268a0e2cd49acc" + [[package]] name = "serde" version = "1.0.229" @@ -10141,6 +11002,15 @@ dependencies = [ "serde_json", ] +[[package]] +name = "serde_plain" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce1fc6db65a611022b23a0dec6975d63fb80a302cb3388835ff02c097258d50" +dependencies = [ + "serde", +] + [[package]] name = "serde_repr" version = "0.1.21" @@ -10669,6 +11539,18 @@ dependencies = [ "der 0.8.3", ] +[[package]] +name = "spm_precompiled" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5851699c4033c63636f7ea4cf7b7c1f1bf06d0cc03cfb42e711de5a5c46cf326" +dependencies = [ + "base64 0.13.1", + "nom 7.1.3", + "serde", + "unicode-segmentation", +] + [[package]] name = "sponge-cursor" version = "0.1.0" @@ -11055,6 +11937,20 @@ dependencies = [ "web-sys", ] +[[package]] +name = "sysctl" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01198a2debb237c62b6826ec7081082d951f46dbb64b0e8c7649a452230d1dfc" +dependencies = [ + "bitflags 2.13.2", + "byteorder", + "enum-as-inner", + "libc", + "thiserror 1.0.69", + "walkdir", +] + [[package]] name = "sysinfo" version = "0.39.6" @@ -11066,6 +11962,7 @@ dependencies = [ "ntapi", "objc2-core-foundation", "objc2-io-kit", + "objc2-open-directory", "windows", ] @@ -11311,6 +12208,39 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "tokenizers" +version = "0.22.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b238e22d44a15349529690fb07bd645cf58149a1b1e44d6cb5bd1641ff1a6223" +dependencies = [ + "ahash", + "aho-corasick", + "compact_str", + "dary_heap", + "derive_builder", + "esaxx-rs", + "getrandom 0.3.4", + "itertools 0.14.0", + "log", + "macro_rules_attribute", + "monostate", + "onig", + "paste", + "rand 0.9.5", + "rayon", + "rayon-cond", + "regex", + "regex-syntax", + "serde", + "serde_json", + "spm_precompiled", + "thiserror 2.0.21", + "unicode-normalization-alignments", + "unicode-segmentation", + "unicode_categories", +] + [[package]] name = "tokio" version = "1.53.2" @@ -11535,6 +12465,12 @@ dependencies = [ "rustc-hash 2.1.3", ] +[[package]] +name = "typed-path" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54ec2755506471f54baf5e4bb5499d8e510cc1caf271270e778a736bd2d7336c" + [[package]] name = "typed-path" version = "0.13.0" @@ -11609,6 +12545,15 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-normalization-alignments" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43f613e4fa046e69818dd287fdc4bc78175ff20331479dab6e1b0f98d57062de" +dependencies = [ + "smallvec", +] + [[package]] name = "unicode-properties" version = "0.1.4" @@ -11645,6 +12590,12 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "unicode_categories" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" + [[package]] name = "universal-hash" version = "0.5.1" @@ -11674,12 +12625,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cde93ff05758a36a851a398a772b0b79a82fbe3c186a46f2696baaeaf84c966b" dependencies = [ "base64 0.23.1", + "cookie_store", "flate2", "log", "percent-encoding", "rustls", "rustls-pki-types", "rustls-platform-verifier", + "serde", + "serde_json", "ureq-proto", "utf8-zero", "webpki-roots", @@ -11799,6 +12753,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "uuid-simd" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b082222b4f6619906941c17eb2297fff4c2fb96cb60164170522942a200bd8" +dependencies = [ + "outref", + "vsimd", +] + [[package]] name = "v_frame" version = "0.3.9" @@ -12127,6 +13091,12 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "vsimd" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c3082ca00d5a5ef149bb8b555a72ae84c9c59f7250f013ac822ac2e49b19c64" + [[package]] name = "wait-timeout" version = "0.2.1" @@ -13507,6 +14477,18 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "zip" +version = "7.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc12baa6db2b15a140161ce53d72209dacea594230798c24774139b54ecaa980" +dependencies = [ + "crc32fast", + "indexmap", + "memchr", + "typed-path 0.12.4", +] + [[package]] name = "zip" version = "9.1.3" @@ -13519,7 +14501,7 @@ dependencies = [ "indexmap", "memchr", "nix", - "typed-path", + "typed-path 0.13.0", "windows-sys 0.61.2", ] diff --git a/Cargo.toml b/Cargo.toml index 1ad19f7aa..5c615a8a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,10 +11,13 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-palette = { path = "crates/palette" } +craft-score = { path = "crates/score" } craft-shx = { path = "crates/shx" } craft-http-url = { path = "crates/http-url" } craft-usd = { path = "crates/usd" } craft-smime = { path = "crates/smime" } +craft-ssip = { path = "crates/ssip" } craft-ber = { path = "crates/ber" } craft-proto = { path = "crates/proto" } craft-render-parity = { path = "crates/render-parity" } @@ -26,6 +29,7 @@ craft-platform = { path = "crates/platform" } craft-aep = { path = "crates/aep" } craft-affinity = { path = "crates/affinity" } craft-win32 = { path = "crates/win32" } +craft-ai = { path = "crates/ai" } craft-ai-core = { path = "crates/ai-core" } craft-anicraft-format = { path = "crates/anicraft-format" } craft-pdf = { path = "crates/pdf", version = "=0.1.0" } @@ -49,10 +53,13 @@ craft-gridcraft-format = { path = "crates/gridcraft-format" } craft-aaf = { path = "crates/aaf" } craft-behavior = { path = "crates/behavior" } craft-atomic = { path = "crates/atomic" } +craft-xtask = { path = "crates/xtask" } craft-captions = { path = "crates/captions" } craft-cfb = { path = "crates/cfb" } craft-codepage = { path = "crates/codepage" } craft-cms = { path = "crates/cms" } +craft-color = { path = "crates/color" } +craft-frame = { path = "crates/frame" } craft-crs = { path = "crates/crs" } craft-crw = { path = "crates/crw" } craft-csv = { path = "crates/csv" } @@ -60,7 +67,6 @@ craft-dav = { path = "crates/dav" } craft-automation = { path = "crates/automation" } craft-browse = { path = "crates/browse" } craft = { path = "crates/craft" } -craft-color = { path = "crates/color" } craft-design-lint = { path = "crates/design-lint" } craft-dialogs = { path = "crates/dialogs" } craft-doc-binary = { path = "crates/doc-binary" } @@ -109,12 +115,14 @@ craft-font-subset = { path = "crates/font-subset" } harfbuzz-subset-sys = { git = "https://github.com/storytold/harfbuzz.git", rev = "2659dd807f9a48f75604ee7c76cb709f7b70b950" } craft-host = { path = "crates/host" } craft-i18n = { path = "crates/i18n" } +craft-ipc = { path = "crates/ipc" } craft-idml = { path = "crates/idml" } craft-indd = { path = "crates/indd" } craft-jmap = { path = "crates/jmap" } craft-ical = { path = "crates/ical" } craft-imaging = { path = "crates/imaging" } craft-jpeg = { path = "crates/jpeg" } +craft-control = { path = "crates/control" } craft-control-protocol = { path = "crates/control-protocol" } craft-jsonrpc = { path = "crates/jsonrpc" } craft-mcp = { path = "crates/mcp" } @@ -131,6 +139,7 @@ craft-macho = { path = "crates/macho" } craft-math = { path = "crates/math" } # No default features: craft-audio turns on the craft-media modules each of its features needs. craft-media = { path = "crates/media", default-features = false } +craft-media-source = { path = "crates/media-source" } craft-materialx = { path = "crates/materialx" } craft-media-testkit = { path = "crates/media-testkit" } craft-midi = { path = "crates/midi" } @@ -154,10 +163,12 @@ craft-pkcs7 = { path = "crates/pkcs7" } craft-plotter = { path = "crates/plotter" } craft-prepress = { path = "crates/prepress" } craft-pages = { path = "crates/pages" } +craft-press-print = { path = "crates/press-print" } craft-print = { path = "crates/print" } craft-proof = { path = "crates/proof" } craft-raster-codecs = { path = "crates/raster-codecs" } craft-image-codecs = { path = "crates/image-codecs" } +craft-dds = { path = "crates/dds" } craft-psd = { path = "crates/psd" } craft-pst = { path = "crates/pst" } craft-quit = { path = "crates/quit" } @@ -176,6 +187,7 @@ craft-stencilcraft-format = { path = "crates/stencilcraft-format" } craft-stencilcraft-document = { path = "crates/stencilcraft-document" } craft-svg-paths = { path = "crates/svg-paths" } craft-stream-codecs = { path = "crates/stream-codecs" } +craft-testkit = { path = "crates/testkit" } craft-text = { path = "crates/text" } craft-tcraft = { path = "crates/tcraft" } craft-texture = { path = "crates/texture" } @@ -240,6 +252,9 @@ chrono = { version = "0.4.45", default-features = false, features = ["std"] } unsafe_code = "forbid" unexpected_cfgs = { level = "warn", check-cfg = ["cfg(craft_pdf_unicode_fixtures)"] } +[workspace.lints.cargo] +unused_workspace_dependencies = "allow" + [workspace.lints.clippy] cognitive_complexity = "deny" too_many_arguments = "deny" diff --git a/NOTICE b/NOTICE index f42fa7a87..ac101996d 100644 --- a/NOTICE +++ b/NOTICE @@ -260,11 +260,10 @@ applications retain their own document-font selection and licensing policy. linesweeper linesweeper 0.5.0 (https://crates.io/crates/linesweeper/0.5.0), MIT OR Apache-2.0, is stored in -vendor/linesweeper with its upstream license files and local patches, to src/sweep/sweep_line.rs and -docs/sweep.typ. Its manifest names Joe Neeman as author, and its LICENSE-MIT reads "Copyright (c) 2018 -Raph Levien". See vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for -provenance, the local patches -and the files left out. +vendor/linesweeper with its upstream license files and local patches, to src/sweep/sweep_line.rs, +src/curve/mod.rs, src/curve/line.rs and docs/sweep.typ. Its manifest names Joe Neeman as author, and +its LICENSE-MIT reads "Copyright (c) 2018 Raph Levien". See vendor/linesweeper/README.craft.md and +vendor/linesweeper/SOURCE.json for provenance, the local patches and the files left out. Platform lifecycle source provenance diff --git a/README.md b/README.md index 761127975..4eb955add 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit of compositions, layers, properties, keyframes, effects, masks, shapes, text and footage. Clean room, from EffectCraft. Mapping onto an app's document stays in the app. +- [`craft-ai`](crates/ai/README.md) *(spec)*: AI provider abstraction — BYO API keys (OpenAI-compatible) and one-click curated local models with capability probing, resumable checksummed downloads, and document segmentation that protects markup while translating prose; - [`craft-ai-core`](crates/ai-core/README.md): the data model and contract for AI in the Craft Apps (connections, models, tools, requests, jobs, consent, history, route spec). Pure, no I/O, wasm-safe. Contract in [docs/ai-shared-layer.md](docs/ai-shared-layer.md). - [`craft-pe`](crates/pe/src/lib.rs): PE/COFF headers (`.exe`, `.dll`, `.sys`, `.efi`) from untrusted bytes, from Craft Anything: the COFF header, PE32/PE32+ optional header with its data directories bounded by the header's real size, the section table, RVA-to-file mapping and the decoded Rich header. - [`craft-pdf`](crates/pdf/README.md): independently optional PDF parsing, interpretation, rasterization and guarded authoring. Apps retain layout, fonts, editing and export policy; writer-only consumers do not enable the shared reader. @@ -70,6 +71,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit - [`craft-automation`](crates/automation/README.md): drive a running app from tests, agents and the CLI: control channel, screenshot queue, frame profiler and trace (native only); recorded actions, their playback and the Actions panel (every target). - [`craft-atomic`](crates/atomic/README.md): Crash-safe atomic file writing and replacement with same-directory staging, directory fsync, Apple `F_FULLFSYNC` fallback, transient lock backoff, and fault injection for the Craft suite. +- [`craft-xtask`](crates/xtask/README.md): a library for the apps' `cargo xtask` binaries: the gate's step runner (steps in order, stop at the first failure, the ok, FAIL and skip summary, workspace tests that exclude the running xtask package), an optional slot lock held across processes and an optional per-step JSON report. Each app keeps its step list and its own policy. Planned for DesignCraft and LightCraft. - [`craft-captions`](crates/captions/README.md): caption and subtitle files behind one neutral model (cues with exact tick times): SubRip, WebVTT, Scenarist SCC, MacCaption MCC, EBU STL and TTML / IMSC1 / DFXP readers and writers, and CEA-608 / CEA-708 byte-pair coding. Carved from @@ -96,6 +98,11 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit black and white levels and as-shot white balance. The complete derivation record (plan, sources log, scripts, verification) is in `crates/crw/clean-room/`. Colour stays in each app. - [`craft-csv`](crates/csv/README.md): delimited text (CSV, TSV) and SYLK: encoding detection (UTF-8, UTF-16, Windows-1252 or Windows-1256), delimiter sniffing, a record parser with a cap on fields per record, field quoting for writers, and a SYLK (`.slk`) reader with R1C1 formulas rewritten to A1. +- [`craft-dds`](crates/dds/README.md): DirectDraw Surface (DDS) textures, read and written: the + top mip level of uncompressed, BC1 to BC3 (DXT1/3/5) and BC4/BC5 (grayscale data and normal + maps) textures, legacy and DX10 headers, and our own block encoder with optional mip chains. No dependencies (rayon behind `parallel`), so an + app that only exports textures needn't take the whole `craft-image-codecs` codec set and its + licences; `craft-image-codecs` reads and writes DDS through it. - [`craft-dialogs`](crates/dialogs/README.md): native file and message dialogs that never block the event loop: queued requests shown at the end of the frame and answered on a later frame, free-standing Open and Folder panels and Save sheets on macOS, case-insensitive filters. What @@ -249,6 +256,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit primitives (`.abr`, `.asl`, `.aco`, `.act`, `.ase`, `.grd`, `.pat`, `.atn`, `.csh`, `.shc`) and the Photoshop blocks of a layered TIFF. Bounded and panic-free on hostile input. Compositing and the mapping onto a document stay in each app. +- [`craft-ipc`](crates/ipc/README.md): Craft apps talking on one machine: per-user local sockets (Unix socket, Windows named pipe), token auth, instance discovery, launch on demand, focus hand-off, frame transfer and linked-file watching. Native only: it does not build for `wasm32`. Protocol in [PROTOCOL.md](crates/ipc/PROTOCOL.md). - [`craft-jmap`](crates/jmap/README.md): the JMAP protocol core (RFC 8620), from MailCraft, without sockets: session, requests of method calls with back-references, responses, ids, address templates, credentials and the judgement of HTTP answers, over any HTTP implementation. Bounded @@ -258,7 +266,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit encoder, from PhotoCraft; includes a frame probe for routing. Metadata interpretation stays in each app. - [`craft-raster-codecs`](crates/raster-codecs/README.md): small raster formats, one module each, - from PhotoCraft: DDS (BC1 to BC3, read and write), DICOM, FITS (read and write), IFF/ILBM (read + from PhotoCraft: DDS (BC1 to BC3 read and write, BC4 and BC5 read), DICOM, FITS (read and write), IFF/ILBM (read and write), PCX, PNM/PAM/PFM (read and write) and XPM, into one flat image type. Bounded and panic-free on hostile input. Colour, orientation and the document mapping stay in each app. - [`craft-kiwi`](crates/kiwi/README.md): clean-room decoder and encoder for the kiwi binary schema @@ -293,9 +301,8 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit stay in each app. - [`craft-mesh`](crates/mesh/README.md): 3D mesh file formats behind one neutral model (meshes of polygons with per-corner indices, materials, a node hierarchy): STL, OBJ with MTL, PLY, - glTF 2.0 / GLB and 3MF, one feature each. Readers are bounded by `Limits`, never open a file themselves - glTF 2.0 / GLB and FBX, one feature each. Readers are bounded by `Limits`, never open a file themselves - and never panic. Carved from EffectCraft, SolidCraft, TextureCraft and MaterialCraft. + glTF 2.0 / GLB, FBX and 3MF, one feature each. Readers are bounded by `Limits`, never open a file + themselves and never panic. Carved from EffectCraft, SolidCraft, TextureCraft and MaterialCraft. - [`craft-midi`](crates/midi/README.md): Standard MIDI Files: a bounded reader for formats 0, 1 and 2 and a format-1 writer, over a neutral model of notes and channel events in ticks at 960 per quarter note. From SoundCraft. @@ -337,6 +344,9 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit - [`craft-pptx`](crates/pptx/README.md): PresentationML, the model of a `.pptx` presentation: masters, layouts, slides, shapes, placeholders and their inheritance, transitions and animations, comments, sections, media and kept parts. From DeckCraft. DrawingML comes from `craft-drawingml`. +- [`craft-press-print`](crates/press-print/README.md): press-accurate printing: a PDF page separated + into plates by `craft-prepress`, composited into the printer's RGB profile and printed through + `craft-print`'s RGB transport, application-managed. - [`craft-print`](crates/print/README.md): PDF submission to CUPS and Windows, printer discovery, duplex/copies/colour and driver preferences. PDF generation stays in each app. - [`craft-proof`](crates/proof/README.md): proofing per language (spelling, suggestions, grammar, @@ -381,6 +391,13 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit - [`craft-table`](crates/table/README.md): spreadsheet widgets: the cell grid with Excel's keys, outline bands, the PivotTable field list, the condition builder, chart painting, slicers, reference picking, selection statistics, error tags and filter buttons (`ui` feature). +- [`craft-testkit`](crates/testkit/README.md): test support for the apps' own tests. Its `guards` module + reports source patterns that broke platforms before: muda's predefined Quit item, WGSL float literals + out of range, and `std::time::Instant` in code the web build runs. Its `deadline` module fails a test + whose body runs past a limit, or whose condition does not hold in time; a variable the caller names + replaces each limit. Its `hostile` module runs a parser on every truncation and on seeded damaged + copies of a valid input, and fails the test with the case that panicked or did not finish. Not + published. - [`craft-text`](crates/text/README.md): text processing below the composer. Its `hyphen` module finds hyphenation points, with a bundled US English dictionary and patterns and optional hypher patterns for 47 more languages (one cargo feature each). With `ui`, the canvas text editor, Character and diff --git a/crates/affinity/README.md b/crates/affinity/README.md index 8567507e8..4a15037a5 100644 --- a/crates/affinity/README.md +++ b/crates/affinity/README.md @@ -115,11 +115,15 @@ text-frame fixture. against the limits below: grids of 16 million cells, grids and stories shared by many layers, 200 000 paragraphs, a document embedded hundreds of times, compounds that fan out or contain themselves, lists of a million null slots linked many times. -* `scripts/fetch-affinity-corpus.sh` fetches 51 public CC0/MIT/Apache-2.0 documents at pinned - commits, SHA-256 verified against `tests/support/corpus.tsv` (which names each file's source and - licence), with the upstream licence notices. Of these, 33 are current `.af`: four CC0 vector - illustrations and 28 original Affinity 3.2.3 feature probes plus one separately identified - derived regression input from [SethRobinson/Patchy](https://github.com/SethRobinson/Patchy) +* `scripts/fetch-affinity-corpus.sh` fetches 113 files at pinned commits, SHA-256 verified + against `tests/support/corpus.tsv` (which names each file's source and license): 90 public + CC0/MIT/Apache-2.0 documents, the 21 PNG files published with the + [satoshoe-dev/affinity-samples](https://github.com/satoshoe-dev/affinity-samples) documents, and + two upstream license notices. The manifest holds every name from the PhotoCraft and VectorCraft + corpus lists, so 21 files are fetched under two names. Of the documents, 55 are current `.af`: + four CC0 vector illustrations (each under two names), 18 CC0 Affinity 3.1.0 samples from + satoshoe-dev/affinity-samples, and 28 original Affinity 3.2.3 feature probes plus one separately + identified derived regression input from [SethRobinson/Patchy](https://github.com/SethRobinson/Patchy) (MIT). Only document data and provenance notices were inspected, never that project's importer or authoring scripts. The files are never committed. @@ -129,11 +133,14 @@ text-frame fixture. ``` Without `AFFINITY_CORPUS` the corpus tests are skipped. `AFFINITY_SAMPLES` names any other - folder of documents to parse. -* `tests/it/real_files.rs` parses every file and freezes the modelled features of all 33 current - `.af` files (`tests/support/affinity3-signatures.txt`: counts, fonts, artboards, warnings and a - CRC of the whole model). Native raster tests compare decoded pixels with independent embedded - images; text tests assert Unicode run boundaries, fallback fonts and feature retention. + folder of documents to parse. A corpus folder fetched for an older manifest fails the tests + until the script is run on it again. +* `tests/it/real_files.rs` checks the shape of every manifest row (four fields, a SHA-256, a + relative name listed once, a license and an `https` URL), parses every file and freezes the + modeled features of all 55 `.af` files (`tests/support/affinity3-signatures.txt`: counts, + fonts, artboards, warnings and a CRC of the whole model). Native raster tests compare decoded + pixels with independent embedded images; text tests assert Unicode run boundaries, fallback + fonts and feature retention. * `fuzz/` has `cargo-fuzz` targets for the whole reader (`container`) and the object stream (`stream`): diff --git a/crates/affinity/tests/it/real_files.rs b/crates/affinity/tests/it/real_files.rs index 18228bfc2..673173c80 100644 --- a/crates/affinity/tests/it/real_files.rs +++ b/crates/affinity/tests/it/real_files.rs @@ -22,8 +22,55 @@ fn files(dir: &Path, out: &mut Vec) { } fn samples() -> Option { - let pinned = corpus::pinned(); - std::env::var_os("AFFINITY_SAMPLES").map(PathBuf::from).or(pinned) + folder(std::env::var_os("AFFINITY_SAMPLES"), corpus::pinned) +} + +/// The `AFFINITY_SAMPLES` folder when one is given, without reading the pinned corpus; otherwise +/// the pinned corpus. +fn folder(samples: Option, pinned: impl FnOnce() -> Option) -> Option { + samples.map(PathBuf::from).or_else(pinned) +} + +#[test] +fn a_samples_folder_is_used_without_the_pinned_corpus() { + assert_eq!(folder(Some("samples".into()), || panic!("the pinned corpus was read")), Some(PathBuf::from("samples"))); + assert_eq!(folder(None, || Some(PathBuf::from("pinned"))), Some(PathBuf::from("pinned"))); + assert_eq!(folder(None, || None), None); +} + +#[test] +fn the_pinned_manifest_is_well_formed() { + let rows = corpus::rows(corpus::MANIFEST).unwrap(); + assert!(rows.iter().filter(|r| r.name.ends_with(".af")).count() >= 4); +} + +#[test] +fn the_manifest_check_rejects_malformed_rows() { + let digest = "0".repeat(64); + let url = "https://example.com/a.af"; + let row = |name: &str| format!("{digest}\t{name}\tMIT\t{url}\n"); + // Two names may share a digest. + assert!(corpus::rows(&(row("a.af") + &row("dir/b.af"))).is_ok()); + for (bad, why) in [ + (row("a.af") + &row("a.af"), "listed twice"), + (format!("{digest}\ta.af\tMIT\n"), "four tab-separated fields"), + (format!("{digest}\ta.af\tMIT\t{url}\textra\n"), "four tab-separated fields"), + (format!("{}\ta.af\tMIT\t{url}\n", "0".repeat(63)), "invalid SHA-256"), + (format!("{}\ta.af\tMIT\t{url}\n", "A".repeat(64)), "invalid SHA-256"), + (row("../a.af"), "relative path"), + (row("/a.af"), "relative path"), + (row("dir//a.af"), "relative path"), + (row("dir\\a.af"), "relative path"), + (row("C:a.af"), "relative path"), + (format!("{digest}\ta.af\t \t{url}\n"), "no license"), + (format!("{digest}\ta.af\tMIT\thttp://example.com/a.af\n"), "https"), + (format!("{digest}\ta.af\tMIT\thttps://\n"), "https"), + ] { + match corpus::rows(&bad) { + Ok(_) => panic!("accepted {bad:?}"), + Err(e) => assert!(e.contains(why), "{bad:?}: {e}"), + } + } } #[test] diff --git a/crates/affinity/tests/support/affinity3-signatures.txt b/crates/affinity/tests/support/affinity3-signatures.txt index 79ed6df5e..f6b20d056 100644 --- a/crates/affinity/tests/support/affinity3-signatures.txt +++ b/crates/affinity/tests/support/affinity3-signatures.txt @@ -1,3 +1,21 @@ +affinity-samples/01-layer-effects.af dpi=72;spreads=1;model_crc32=9a2e65d6;counts={"depth": 2, "fills": 13, "gradients": 0, "layers": 12, "nodes": 38, "segments": 51, "shapes": 13, "strokes": 0, "subpaths": 13, "text_chars": 166, "text_runs": 13, "texts": 13};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["blur effects (left out)", "layer effects of other kinds (left out) (2×)", "paragraph leading of an unknown kind (imported as automatic) (13×)", "unknown layers attached to other layers (imported without them) (2×)"] +affinity-samples/02-adjustment-layers.af dpi=72;spreads=1;model_crc32=fc8b239f;counts={"depth": 2, "fills": 17, "gradients": 16, "layers": 8, "nodes": 42, "segments": 51, "shapes": 17, "strokes": 0, "subpaths": 17, "text_chars": 96, "text_runs": 9, "texts": 9, "unsupported": 8};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["adjustment layers (8×)", "paragraph leading of an unknown kind (imported as automatic) (9×)"] +affinity-samples/03-brush-strokes.af dpi=72;spreads=1;model_crc32=a492c413;counts={"depth": 1, "fills": 1, "gradients": 0, "hidden": 4, "images": 2, "nodes": 14, "segments": 15, "shapes": 7, "strokes": 6, "subpaths": 7, "text_chars": 107, "text_runs": 5, "texts": 5};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (5×)"] +affinity-samples/04-master-page.af dpi=300;spreads=3;model_crc32=ba260d8d;counts={"depth": 3, "fills": 1, "gradients": 0, "layers": 6, "master_instances": 3, "nodes": 30, "segments": 18, "shapes": 9, "strokes": 0, "subpaths": 9, "text_chars": 31, "text_runs": 3, "texts": 3, "unsupported": 9};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (3×)", "text fields such as page numbers (left out of the text)", "text whose story could not be read (master-page text, or an unknown layout) (9×)"] +affinity-samples/05-symbols.af dpi=72;spreads=1;model_crc32=fe74faa6;counts={"depth": 2, "fills": 5, "gradients": 0, "groups": 1, "layers": 3, "nodes": 40, "segments": 129, "shapes": 32, "strokes": 1, "subpaths": 32, "text_chars": 179, "text_runs": 4, "texts": 4};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (4×)"] +affinity-samples/06-special-shapes.af dpi=72;spreads=1;model_crc32=7e9a9eb3;counts={"depth": 1, "fills": 17, "gradients": 0, "nodes": 50, "segments": 268, "shapes": 33, "strokes": 16, "subpaths": 34, "text_chars": 182, "text_runs": 17, "texts": 17};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (17×)"] +affinity-samples/07-transparency.af dpi=72;spreads=1;model_crc32=6595042f;counts={"depth": 1, "fills": 18, "gradients": 1, "nodes": 26, "opacity": 1, "segments": 57, "shapes": 19, "strokes": 0, "subpaths": 19, "text_chars": 151, "text_runs": 7, "texts": 7};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["bitmap and pattern fills (left empty)", "paragraph leading of an unknown kind (imported as automatic) (7×)", "transparency gradients (imported without them) (2×)"] +affinity-samples/08-gradients.af dpi=72;spreads=1;model_crc32=5638db4d;counts={"depth": 1, "fills": 7, "gradients": 6, "nodes": 14, "segments": 27, "shapes": 7, "strokes": 0, "subpaths": 7, "text_chars": 97, "text_runs": 7, "texts": 7};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (7×)"] +affinity-samples/09-corners-stars.af dpi=72;spreads=1;model_crc32=e3528065;counts={"depth": 2, "fills": 9, "gradients": 0, "layers": 8, "nodes": 26, "segments": 81, "shapes": 9, "strokes": 0, "subpaths": 9, "text_chars": 122, "text_runs": 9, "texts": 9};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (9×)"] +affinity-samples/10-multiple-fills.af dpi=72;spreads=1;model_crc32=07d2e434;counts={"depth": 2, "fills": 7, "gradients": 3, "layers": 6, "nodes": 20, "segments": 21, "shapes": 7, "strokes": 3, "subpaths": 7, "text_chars": 140, "text_runs": 7, "texts": 7};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (7×)", "several fills or strokes on one object (only the active one is imported) (6×)"] +affinity-samples/11-text.af dpi=72;spreads=1;model_crc32=de077247;counts={"depth": 2, "fills": 13, "gradients": 0, "groups": 1, "layers": 1, "nodes": 28, "segments": 198, "shapes": 13, "strokes": 0, "subpaths": 18, "text_chars": 452, "text_runs": 11, "texts": 11, "unsupported": 2};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)", "text fields such as page numbers (left out of the text) (2×)", "text frames with several columns (imported as one column)", "unknown layers (2×)"] +affinity-samples/12-cmyk8.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"] +affinity-samples/12-grey8.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"] +affinity-samples/12-lab16.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"] +affinity-samples/12-rgb16.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"] +affinity-samples/12-rgb32.af dpi=72;spreads=1;model_crc32=a9aeb9ab;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"] +affinity-samples/12-rgb8.af dpi=72;spreads=1;model_crc32=f40c0d8e;counts={"depth": 1, "fills": 31, "gradients": 1, "nodes": 41, "segments": 93, "shapes": 31, "strokes": 8, "subpaths": 31, "text_chars": 148, "text_runs": 10, "texts": 10};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (10×)"] +affinity-samples/13-blend-modes.af dpi=72;spreads=1;model_crc32=13f9c6d7;counts={"depth": 2, "fills": 13, "gradients": 6, "layers": 6, "nodes": 26, "segments": 45, "shapes": 13, "strokes": 0, "subpaths": 13, "text_chars": 169, "text_runs": 7, "texts": 7};boards=[];fonts={"Fira Sans:FiraSans-Regular:400:false"};warnings=["paragraph leading of an unknown kind (imported as automatic) (7×)"] affinity3-mexican-guy.af dpi=300;spreads=1;model_crc32=32b7cf3f;counts={"depth": 8, "fills": 169, "gradients": 0, "groups": 23, "nodes": 283, "opacity": 1, "segments": 1491, "shapes": 260, "strokes": 114, "subpaths": 264};boards=[];fonts={};warnings=["blur effects (left out) (156×)"] affinity3-mexican-man.af dpi=300;spreads=1;model_crc32=c10daaea;counts={"depth": 7, "fills": 277, "gradients": 2, "groups": 29, "nodes": 903, "segments": 3225, "shapes": 874, "strokes": 540, "subpaths": 881};boards=[];fonts={};warnings=["blur effects (left out) (244×)"] affinity3-mexican-woman.af dpi=300;spreads=1;model_crc32=3bc13374;counts={"depth": 9, "fills": 527, "gradients": 0, "groups": 28, "nodes": 595, "segments": 3562, "shapes": 567, "strokes": 60, "subpaths": 570};boards=[];fonts={};warnings=["blur effects (left out) (278×)"] @@ -31,3 +49,7 @@ patchy-text-runs.af dpi=72;spreads=1;model_crc32=7820b881;counts={"depth": 1, "n patchy-transform.af dpi=72;spreads=1;model_crc32=8d56df84;counts={"depth": 1, "images": 1, "nodes": 1};boards=[];fonts={};warnings=[] patchy-vector-mask.af dpi=72;spreads=1;model_crc32=80ca946a;counts={"depth": 1, "fills": 2, "gradients": 0, "nodes": 2, "segments": 7, "shapes": 2, "strokes": 0, "subpaths": 2, "vector_masks": 1};boards=[];fonts={};warnings=["unknown layers attached to other layers (imported without them)"] patchy-vector.af dpi=72;spreads=1;model_crc32=3b16c7ed;counts={"depth": 1, "fills": 3, "gradients": 0, "nodes": 4, "segments": 17, "shapes": 4, "strokes": 2, "subpaths": 5};boards=[];fonts={};warnings=[] +vector-art/mexican-guy.af dpi=300;spreads=1;model_crc32=32b7cf3f;counts={"depth": 8, "fills": 169, "gradients": 0, "groups": 23, "nodes": 283, "opacity": 1, "segments": 1491, "shapes": 260, "strokes": 114, "subpaths": 264};boards=[];fonts={};warnings=["blur effects (left out) (156×)"] +vector-art/mexican-man.af dpi=300;spreads=1;model_crc32=c10daaea;counts={"depth": 7, "fills": 277, "gradients": 2, "groups": 29, "nodes": 903, "segments": 3225, "shapes": 874, "strokes": 540, "subpaths": 881};boards=[];fonts={};warnings=["blur effects (left out) (244×)"] +vector-art/mexican-woman.af dpi=300;spreads=1;model_crc32=3bc13374;counts={"depth": 9, "fills": 527, "gradients": 0, "groups": 28, "nodes": 595, "segments": 3562, "shapes": 567, "strokes": 60, "subpaths": 570};boards=[];fonts={};warnings=["blur effects (left out) (278×)"] +vector-art/playing-cards.af dpi=300;spreads=1;model_crc32=bc0c838d;counts={"depth": 5, "fills": 363, "gradients": 0, "groups": 758, "nodes": 1350, "segments": 4864, "shapes": 592, "strokes": 17, "subpaths": 656};boards=[];fonts={};warnings=["blur effects (left out) (309×)", "brush strokes (imported as plain strokes)"] diff --git a/crates/affinity/tests/support/corpus.rs b/crates/affinity/tests/support/corpus.rs index 21458a6f8..425aa7632 100644 --- a/crates/affinity/tests/support/corpus.rs +++ b/crates/affinity/tests/support/corpus.rs @@ -10,9 +10,56 @@ use sha2::{Digest, Sha256}; /// `sha256 name licence url` rows. pub const MANIFEST: &str = include_str!("corpus.tsv"); +/// One manifest row. +pub struct Row<'a> { + pub digest: &'a str, + pub name: &'a str, + pub license: &'a str, + pub url: &'a str, +} + +/// The rows of `manifest`. A row has exactly four tab-separated fields: a lowercase hexadecimal +/// SHA-256, a relative `/`-separated name without empty, `.` or `..` parts, a license and an +/// `https://` URL. Each name is listed once; several names may share a digest. +pub fn rows(manifest: &str) -> Result>, String> { + let mut seen = std::collections::BTreeSet::new(); + let mut rows = Vec::new(); + for line in manifest.lines().filter(|l| !l.trim().is_empty()) { + let cols: Vec<&str> = line.split('\t').collect(); + let &[digest, name, license, url] = cols.as_slice() else { + return Err(format!("corpus row needs four tab-separated fields: {line}")); + }; + if digest.len() != 64 || !digest.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) { + return Err(format!("{name}: invalid SHA-256 {digest:?}")); + } + if name.starts_with('/') + || name.contains(['\\', ':']) + || name.chars().any(char::is_control) + || name.split('/').any(|part| part.is_empty() || part == "." || part == "..") + { + return Err(format!("{name:?}: the name must be a relative path inside the corpus folder")); + } + if license.trim().is_empty() { + return Err(format!("{name}: no license")); + } + if !url.starts_with("https://") || url.len() == "https://".len() { + return Err(format!("{name}: the URL must use https: {url:?}")); + } + if !seen.insert(name) { + return Err(format!("{name}: listed twice")); + } + rows.push(Row { digest, name, license, url }); + } + Ok(rows) +} + +fn manifest() -> Vec> { + rows(MANIFEST).unwrap_or_else(|e| panic!("malformed corpus.tsv: {e}")) +} + /// The names of the pinned files. pub fn names() -> impl Iterator { - MANIFEST.lines().filter_map(|l| l.split('\t').nth(1)) + manifest().into_iter().map(|row| row.name) } pub fn pinned() -> Option { @@ -21,10 +68,7 @@ pub fn pinned() -> Option { return None; }; let mut count = 0; - for line in MANIFEST.lines().filter(|l| !l.trim().is_empty()) { - let mut cols = line.split('\t'); - let (Some(digest), Some(name)) = (cols.next(), cols.next()) else { panic!("malformed corpus row: {line}") }; - assert_eq!(digest.len(), 64, "invalid SHA256 for {name}"); + for Row { digest, name, .. } in manifest() { let bytes = std::fs::read(dir.join(name)).unwrap_or_else(|e| panic!("missing pinned Affinity file {name}: {e}")); let hex: String = Sha256::digest(&bytes).iter().map(|b| format!("{b:02x}")).collect(); assert_eq!(hex, digest, "{name}: pinned corpus modified; run scripts/fetch-affinity-corpus.sh"); diff --git a/crates/affinity/tests/support/corpus.tsv b/crates/affinity/tests/support/corpus.tsv index bf6bc4e22..2fab83b8f 100644 --- a/crates/affinity/tests/support/corpus.tsv +++ b/crates/affinity/tests/support/corpus.tsv @@ -7,10 +7,59 @@ e56c35f876277b101790ff4384cfde103a093ed6c81790a2d62176de21da339f afdesignload-re 08946c60c0da1c8aeea92d65acfb244721eeb3b480ed358609f4a48bbecfc5b9 afdesignload-shape_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/shape_test.afdesign 7306ab4fcbdc225d816f32a0d2aca15ac887053f60170459ac947e70f4ef343d afdesignload-slice_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/slice_test.afdesign e5d04bac0235af9386aea251cdd6903ed7810d335fdb6194d0483ea30b14e24a afdesignload-test_path.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/test_path.afdesign +cc8ca0db0d8fc545eb29153be900f153fa6b7b7eac068d05e54a502aba1264fe afdesignload/color.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/color.afdesign +9a17f500d09fb3b1a70d24aabdc2882955ec3eac3700a474549e6100be3a8484 afdesignload/layer_mode.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/layer_mode.afdesign +77ae60c02749224fe2c52691c951568138eaed5f8584ebc5da83715fae0234dd afdesignload/layer_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/layer_test.afdesign +04944918ff8e29c5c1b466b7e43c232f7ab80db15fb934080bbcd7baaf093e22 afdesignload/margins.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/margins.afdesign +35afa31b89f2b9970eb3701ee64e2a19476383637e8b07689334d6ffe556b593 afdesignload/raster_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/raster_test.afdesign +e56c35f876277b101790ff4384cfde103a093ed6c81790a2d62176de21da339f afdesignload/revision_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/revision_test.afdesign +08946c60c0da1c8aeea92d65acfb244721eeb3b480ed358609f4a48bbecfc5b9 afdesignload/shape_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/shape_test.afdesign +7306ab4fcbdc225d816f32a0d2aca15ac887053f60170459ac947e70f4ef343d afdesignload/slice_test.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/slice_test.afdesign +e5d04bac0235af9386aea251cdd6903ed7810d335fdb6194d0483ea30b14e24a afdesignload/test_path.afdesign MIT https://raw.githubusercontent.com/NickBeeuwsaert/AFDesignLoad/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns/test_path.afdesign +5f5b98f0ab2121f75f24362792c00cd5b610767820eb754b5660668f478b0c86 affinity-samples/01-layer-effects.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/01-layer-effects.af +33c1ad5022abc14e6136f4cb9fa0751c9778a0a564bc3abc2e0e6b051f7e00bb affinity-samples/01-layer-effects.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/01-layer-effects.png +4d19e95abe3423692f5bdfd0ede5bf3aebdd4bf16c32eaa304cead91a7a2e9b7 affinity-samples/02-adjustment-layers.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/02-adjustment-layers.af +726b7939e23a745f7578fa84188b56c2eb845cde28493499f1b18b4ede9b8a6e affinity-samples/02-adjustment-layers.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/02-adjustment-layers.png +c6ca86439c89d83ba43c76e9d783865ac09ca0ff524d5b347caa77f473caac98 affinity-samples/03-brush-strokes.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/03-brush-strokes.af +e57e36ffffdd88d3a62ebdb142435d7d3056286b22130f459c404a3ef529f61f affinity-samples/03-brush-strokes.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/03-brush-strokes.png +e3bf04f7cc02e1d49ec79503f2b4044f385de1e890bd461013a5a6fca09b5223 affinity-samples/04-master-page.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/04-master-page.af +246279f00d1f08917409b34e60359bd3bba158bcf4e8d5847a806f575d8eb98f affinity-samples/04-master-page_1.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/04-master-page_1.png +0e023792ce89f724b1dfedb6360556348309e483c23e32b91d87afab5d64c9cc affinity-samples/04-master-page_2.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/04-master-page_2.png +f431b15027e20ab760c4d241aa6981130560409cb270bbc6d4dd9241d1883f0a affinity-samples/04-master-page_3.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/04-master-page_3.png +ef779b58a6721001ee961b42eacb96fb47dcaa35ccb7e6ccf2ec6f33106070b2 affinity-samples/05-symbols.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/05-symbols.af +6675fc4a1563f61b7fcaa64978447e0fa6e1b0bb8073b4e63f958db1cf726fef affinity-samples/05-symbols.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/05-symbols.png +9e463b4d1ca8e7a1b67b3c30351a28af8ed05b9cd11f3cea2159319c9974bc67 affinity-samples/06-special-shapes.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/06-special-shapes.af +a897c607631acbea31c5a926af6ca20c0acb2744636003b8c9ef07861752d947 affinity-samples/06-special-shapes.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/06-special-shapes.png +3aba0d83055f09da4dba465a3abf66c77d4362dc6060548e54db943029b3996e affinity-samples/07-transparency.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/07-transparency.af +3b73945b7302627caf679a961053f30a9593bab7d709e27acb1df54dadc53968 affinity-samples/07-transparency.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/07-transparency.png +4292c1571b6c8c19b80c530602ca21d5996df7b99221639a4cde76d8307ffbcb affinity-samples/08-gradients.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/08-gradients.af +f6fb427c9715ea193e595d0c441b462224971fcda2aa2b4d8b71631d0f59f992 affinity-samples/08-gradients.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/08-gradients.png +c913882b3bea7a4218aa88e96890181cdc08ce37f0859196a1ad1828be78b347 affinity-samples/09-corners-stars.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/09-corners-stars.af +068bcc7599336a174d7ce87962da414ccf429d549484ccbe8f68f669779d84d0 affinity-samples/09-corners-stars.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/09-corners-stars.png +f03e797d6dbf8a31eab9d0b9cf9a8bddcc8440c6003863f31d97fe23c195c4b2 affinity-samples/10-multiple-fills.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/10-multiple-fills.af +e1951aa6d8acdbeccc281b6a20a03df9b257c0d739aa5f6aec27c105151fef3f affinity-samples/10-multiple-fills.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/10-multiple-fills.png +6da8b59b40d0e082435c5a2004c50fa90e3c4f15c3a02763b8edf84b8b9f4506 affinity-samples/11-text.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/11-text.af +bb2040cda017cb753d0594416ac0501a1049bc333a7cf2c1c832356e2bbe1a45 affinity-samples/11-text.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/11-text.png +0c13cfe0f716902199775251b0983f28d60a64c1f79d3c6f496f2c019365bdb5 affinity-samples/12-cmyk8.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-cmyk8.af +cfef257c50201968121332e8ae12a7fe2ecdcc3bce1faeb5e581751662098c05 affinity-samples/12-cmyk8.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-cmyk8.png +03d4cfe5ce4b80833d83de89848e88639056e8984c049865c8f29b9ef41b40e5 affinity-samples/12-grey8.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-grey8.af +855e4f08f21c60f1d2080c9533b98f99c7a635c5804abf8ec9d6ee1a29029cef affinity-samples/12-grey8.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-grey8.png +9adc0d4242b5448ac84aef886bd7afc165e1959015125ce21bf402ddb65caba1 affinity-samples/12-lab16.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-lab16.af +6c30cef8304fd08539d631988280a6b3bc8570db3f1425fa771d83c1591dfc8d affinity-samples/12-lab16.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-lab16.png +26b47861275b2f83517e7d42ec051efc3cd22956440784b6df0bc016ecb0e008 affinity-samples/12-rgb16.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb16.af +967ba0cb7d93496a7e454dcc37013c1b19089cc283c7e08f2dea7023ad330e80 affinity-samples/12-rgb16.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb16.png +36e62b0de8488712c04831ba3ef466da68bf080ec3caf9fbe6646905d3fb87e8 affinity-samples/12-rgb32.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb32.af +0ebcf7211447d64158c21c27eeb19b8737cb2cdcc708dcdf3b4fed5041b449e9 affinity-samples/12-rgb32.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb32.png +4d210ce4724409fb6c9715046ad9efa0049442f8324a21174068a02c6df10632 affinity-samples/12-rgb8.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb8.af +4e4dda966c337119165ba5d88cb3d4af10bf6507b9a69986d33060a8eeb37c21 affinity-samples/12-rgb8.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/12-rgb8.png +b84815dbb5e3a7f7842d860acb888f93dbd1f949bcb6a3bf858515b85ea3c3b7 affinity-samples/13-blend-modes.af CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/13-blend-modes.af +a49d2fe6043002aef4f4c6b8cf64a04838d21cf29795324c02fb1772d5519a21 affinity-samples/13-blend-modes.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/13-blend-modes.png +b73eceb73a45df1a424d60164b7e1b521b9f01f56fe86159e477b49c9cd90f78 affinity-samples/source/07-texture.png CC0-1.0 https://raw.githubusercontent.com/satoshoe-dev/affinity-samples/96aca6bd86bbe7a677f1cf0630bd832d23943be8/source/07-texture.png c499cf3ca73cf7cd3e2e65bed048d1b8ecf03e7167b74d54f0ae6db31868cab8 affinity3-mexican-guy.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-guy.af 730e3fae51d9044ada58f51c7b5f9327d99e8d62376453bc251d0d26cda47743 affinity3-mexican-man.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-man.af 5f5fccc0bd8be2bb3b2a07461ec77a012a61eef1883485d7c80f72d4383231ce affinity3-mexican-woman.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-woman.af 0a52bb923c949c566a4339b996bce1a67b683f61def1a73dfefb910effaa450d affinity3-playing-cards.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/playing-cards.af +e1b121d7c3d02356baa5dea512f5aa1fe77249c28d7b227e29cab2bc67ec323a asset-store-template/AssetStore.aftemplate MIT https://raw.githubusercontent.com/eviltwo/AssetStoreTemplate/f671981ee89d15526285b3cc87b4393d3defe644/AssetStoreTemplate/AssetStore.aftemplate 87a7bd93de16c404f6c6dd9cdebff96ec47832a3b342d2ce0af73d9a87e209e0 designer-beer.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/beer.afdesign 09ba790c428e8e09dc0b9ddebb4f3db6daefaa2af1da492ee34e73fba1b8af69 designer-cactus.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/cactus.afdesign c3bcd0395b407493bc9d2877d424029b70f0e4c817a56e893f7ac74973df52e5 designer-car.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/car.afdesign @@ -19,6 +68,8 @@ c3bcd0395b407493bc9d2877d424029b70f0e4c817a56e893f7ac74973df52e5 designer-car.af e1b121d7c3d02356baa5dea512f5aa1fe77249c28d7b227e29cab2bc67ec323a eviltwo-AssetStore.aftemplate MIT https://raw.githubusercontent.com/eviltwo/AssetStoreTemplate/f671981ee89d15526285b3cc87b4393d3defe644/AssetStoreTemplate/AssetStore.aftemplate fbb196e918e302c16f8de9f2e6dffaef70ab3c317b32a9f44607fd4b2ead0206 jac21-SimpleLogo.afdesign MIT https://raw.githubusercontent.com/Jac21/Branding/57ae3f45bf4637f6927c0b07de25b922c55f944f/Logos/JC/DesignerFiles/Affinity/SimpleLogo.afdesign 9d874245936483de3ec04cca48b383028d33666801dff1814246a44e695e29b8 jac21-SimpleLogoBanner.afdesign MIT https://raw.githubusercontent.com/Jac21/Branding/57ae3f45bf4637f6927c0b07de25b922c55f944f/Logos/JC/DesignerFiles/Affinity/SimpleLogoBanner.afdesign +fbb196e918e302c16f8de9f2e6dffaef70ab3c317b32a9f44607fd4b2ead0206 jac21/SimpleLogo.afdesign MIT https://raw.githubusercontent.com/Jac21/Branding/57ae3f45bf4637f6927c0b07de25b922c55f944f/Logos/JC/DesignerFiles/Affinity/SimpleLogo.afdesign +9d874245936483de3ec04cca48b383028d33666801dff1814246a44e695e29b8 jac21/SimpleLogoBanner.afdesign MIT https://raw.githubusercontent.com/Jac21/Branding/57ae3f45bf4637f6927c0b07de25b922c55f944f/Logos/JC/DesignerFiles/Affinity/SimpleLogoBanner.afdesign ec1072cb03fcd0ed7ac1dfc7e087f4920df545fe9c84a386a7dac2fd33fbbe1f leakcanary-vector_icon.afdesign Apache-2.0 https://raw.githubusercontent.com/square/leakcanary/00880a8fb66eb4046a0124bff8303a04874bc07e/docs/assets/vector_icon.afdesign bbc50c8c376e0e5980939be7df6769feed1a30289c7efc6391b204dfb15de88d patchy-LICENSE.txt MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/LICENSE 150e423be0c5eb22f252a38c2de68ab3ccec7e20bfba166f7ddd622ac243d8f4 patchy-NOTICE.txt MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/NOTICE-THIRD-PARTY.md @@ -51,3 +102,12 @@ a5ccb62ee8d7e36d1a19a037aba989a5ba0232767e9f3305f7d9f7d4603291dd patchy-text-rot 87e1dee23769932b7e414116233063a8d68726cbffdebb975b3cd0ef447c602a patchy-transform.af MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/test-fixtures/af/tiny-transform.af fa5dd80f44ae0712a9888c62953fd442a28119034d343db93b7892d5d2a61328 patchy-vector-mask.af MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/test-fixtures/af/tiny-vector-mask.af 8ab1995ecbd9306ebcb7a4d2cd81701f34401e67f08b242f14dc3e3d04c94803 patchy-vector.af MIT https://raw.githubusercontent.com/SethRobinson/Patchy/de84eab550758b30fa062e479f5778cce7693b73/test-fixtures/af/tiny-vector.af +87a7bd93de16c404f6c6dd9cdebff96ec47832a3b342d2ce0af73d9a87e209e0 vector-art/beer.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/beer.afdesign +09ba790c428e8e09dc0b9ddebb4f3db6daefaa2af1da492ee34e73fba1b8af69 vector-art/cactus.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/cactus.afdesign +c3bcd0395b407493bc9d2877d424029b70f0e4c817a56e893f7ac74973df52e5 vector-art/car.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/car.afdesign +112562bc91e7cccf003d34364430544208373c4fb4bb4ab9acdaf27cf0125047 vector-art/flowers.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/flowers.afdesign +023531b8216f2d4da24ab715e3b313506712e50d5f5bf7da927f434def1d0ea2 vector-art/lion-track.afdesign CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/lion-track.afdesign +c499cf3ca73cf7cd3e2e65bed048d1b8ecf03e7167b74d54f0ae6db31868cab8 vector-art/mexican-guy.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-guy.af +730e3fae51d9044ada58f51c7b5f9327d99e8d62376453bc251d0d26cda47743 vector-art/mexican-man.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-man.af +5f5fccc0bd8be2bb3b2a07461ec77a012a61eef1883485d7c80f72d4383231ce vector-art/mexican-woman.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/mexican-woman.af +0a52bb923c949c566a4339b996bce1a67b683f61def1a73dfefb910effaa450d vector-art/playing-cards.af CC0-1.0 https://raw.githubusercontent.com/samuel-etver/vector-art/255f8add3c8f0740196e22bd59502b811b532f0b/simple/playing-cards.af diff --git a/crates/ai/Cargo.toml b/crates/ai/Cargo.toml new file mode 100644 index 000000000..cca808c9c --- /dev/null +++ b/crates/ai/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "craft-ai" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true +description = "AI provider abstraction for the Craft suite — BYO API keys and one-click local models" + +[lints] +workspace = true + +[dependencies] +serde = { version = "1", features = ["derive"] } +serde_json = "1" + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +dirs = "7" +sysinfo = "0.39" +ureq = { version = "3", features = ["json"] } +sha2 = "0.11" +candle-core = { version = "0.9", optional = true } +candle-nn = { version = "0.9", optional = true } +candle-transformers = { version = "0.9", optional = true } +tokenizers = { version = "0.22", optional = true, default-features = false, features = ["onig"] } + +[features] +# Local GGUF inference on candle. Heavy dependency tree; apps opt in. +local = ["dep:candle-core", "dep:candle-nn", "dep:candle-transformers", "dep:tokenizers"] + +[dev-dependencies] +tempfile = "3" diff --git a/crates/ai/README.md b/crates/ai/README.md new file mode 100644 index 000000000..90170c8eb --- /dev/null +++ b/crates/ai/README.md @@ -0,0 +1,145 @@ +# craft-ai — AI providers for the Craft suite + +Shared AI abstraction for every Craft app: translation, rewriting, summaries, +vision captions — one crate, one settings store, one model cache. + +Status: **spec** — this file is the contract; implementation follows. + +## The feature this powers + +A per-app **AI tab** offering document-wide operations — the flagship being +"translate this whole document" — backed by either: + +1. **BYOT** — the user's own API token against an OpenAI-compatible endpoint + (OpenAI, Azure, Ollama, LM Studio, any compatible proxy), or +2. **Local model** — a one-click download of a curated open model, chosen for + the machine it's running on. + +The user experience is deliberately *informed*: every step reports what is +happening, why this model was picked, what it will cost in disk/RAM, and that +nothing leaves the machine in local mode. + +## Modules + +### `probe` — capability detection + +Runs once, caches the result: + +| Signal | Source | +| --- | --- | +| Total/free RAM | `sysinfo` or `std::alloc`-safe OS calls | +| CPU threads + features | `std::thread`, `is_x86_feature_detected!` (AVX2 gates Q4 speed) | +| GPU + VRAM | `wgpu` adapter info (apps already link wgpu — reuse, don't re-probe) | +| Free disk at the model cache dir | `fs2`/`std::fs` | +| Network reachability | optional HEAD to the model host, skipped offline | + +Output: `MachineCaps { ram_gb, vram_gb, threads, avx2, disk_free_gb }`. + +### `registry` — the curated model list + +A small hand-maintained table (Rust consts, not runtime-fetched — supply +chain stays auditable). Each entry: `id`, display name, HF repo + file, +`sha256`, size, min RAM, recommended RAM, license id, quality tier, +languages covered. + +Curation rule — **permissive licenses only** (the suite is MIT OR Apache-2.0 +and commercial users run it): + +- ✅ Qwen2.5-0.5B / 1.5B / 3B Instruct (Apache-2.0) +- ✅ SmolLM2-1.7B (Apache-2.0) +- ✅ Gemma-3 (Gemma Terms — allowed, flag in UI) +- ✅ Helsinki-NLP opus-mt-* per-language pairs (CC-BY/MIT) +- ❌ NLLB-200, M2M-100 — CC-BY-NC, excluded +- ❌ Llama-* — custom license acceptable? default: exclude until reviewed + +`registry::suggest(&caps)` picks the best fitting model: prefers 3B when +VRAM ≥ 6 GB or RAM ≥ 16 GB, falls to 1.5B at 8 GB, 0.5B below. Always shows +alternatives — the suggestion is a default, not a cage. + +### `provider` — inference backends + +```rust +trait Provider { + fn name(&self) -> &str; + fn complete(&self, prompt: &str, opts: &GenOpts) -> Result; + fn test(&self) -> Result; +} +``` + +- `OpenAiCompat` — POST `/v1/chat/completions`, configurable endpoint+token+ + model. Covers OpenAI, Ollama (`http://localhost:11434/v1`), LM Studio, + Groq, OpenRouter… Token stored via the OS keyring (`keyring` crate) or a + 0600-permissioned config file — never in project files, never logged. +- `LocalGguf` — GGUF inference. First implementation target: **candle** + (pure Rust, Apache/MIT — keeps the no-C++ toolchain rule). `llama-cpp-2` + stays a possible perf upgrade later, gated behind a feature flag. + +### `fetch` — one-click download + +- Resumable HTTP GET (range requests), streamed to `*.part` then renamed — + a half-downloaded GGUF can never be mistaken for a valid one. +- SHA-256 checked against the registry entry; mismatch = hard fail, delete. +- Progress events (`Download { bytes_done, bytes_total, bytes_per_sec }`) + feed the UI's live status line. +- Cache dir is **shared across the suite**: + `%LOCALAPPDATA%\artcraft\models` (Windows), `~/Library/Application + Support/artcraft/models` (macOS), `~/.cache/artcraft/models` (Linux). + +### `translate` — document segmentation + +The model is the easy part; the hard part is *what not to touch*. The +translator walks the document as segments, not raw text: + +- **Protected**: LaTeX commands/envs/math (`\cite{…}`, `$…$`), code fences, + placeholders (`{count}`), URLs, citation keys, markup tags. +- **Translated**: prose runs, captions, headings, table cells. +- Per-app adapters declare the grammar: TexCraft protects `\command{…}` and + math mode; WordCraft protects field codes; MatCraft protects code. +- Batch-friendly: segments → prompt with numbered items → parse numbered + output; verify count matches, retry split halves on mismatch. +- Deterministic options: temperature 0-ish, glossary hook (`HashMap`) applied post-hoc. + +## The AI tab (per app, `ui-egui/src/ai.rs`) + +First-open flow — the "smooth informed" sequence: + +1. Two cards: **Use an API provider** / **Run on this PC**. +2. BYOT card: endpoint, token, model fields + **Test connection** → green + check with model list echo. +3. Local card: **Detect this PC** → probe runs → + `Suggested: Qwen2.5-1.5B-Instruct (Q4_K_M, 1.1 GB) — fits your 16 GB RAM; + est. ~8 tok/s` with an "Other models ▸" disclosure showing the registry. +4. **Download** → progress bar with MB/%/speed → `Verifying checksum…` → + `Loading…` → `Ready — local, offline, nothing leaves this PC`. +5. Then the operations: "Translate document to [language ▾]", "Summarize + selection", custom prompt box. Status line narrates each step; + expandable activity log for the curious. +6. Language detected vs document language → gentle offer chip: + `This document is French — translate to English?` + +## Non-negotiables + +- **Honesty**: the UI always says which provider is active, what model, + and "cloud — your document text is sent to the endpoint" vs "local — + nothing leaves this PC". No silent cloud calls; AI is strictly opt-in. +- **No crash**: model load failures, corrupt GGUFs, OOM — all `Result`, + all surfaced as messages. +- **Rust only**: candle over llama.cpp bindings preserves the wasm story + (local inference on wasm is future-possible with `candle` + WGPU backend). +- **Licensing**: registry carries SPDX ids; the UI shows them; CC-BY-NC and + unreviewed custom licenses are excluded by policy. +- **Everything is a command**: `ai.translate_doc`, `ai.status`, + `ai.models.list`, `ai.model.download`, `ai.provider.test` — reachable + from the app's control channel and MCP, same as every Craft convention. + +## Milestones + +| # | Deliverable | Done when | +| --- | --- | --- | +| A1 | `probe` + `registry` + `fetch` | CLI-free test: probe reports real caps; `suggest` picks sanely per fixture; a tiny model downloads + sha256 verifies | +| A2 | `OpenAiCompat` provider | `translate` a string through Ollama/OpenAI with mocked HTTP tests | +| A3 | `LocalGguf` on candle | Qwen2.5-0.5B loads and answers on CPU in a test (model fixture fetched in-test, skipped without network) | +| A4 | `translate` segmentation | LaTeX/code/placeholder round-trip tests — protected spans byte-identical | +| A5 | AI tab in one pilot app (suggest TexCraft or WordCraft) | download → translate document end-to-end, live status UX | +| A6 | Commands + MCP surface | `ai.*` commands reachable via control channel | diff --git a/crates/ai/src/error.rs b/crates/ai/src/error.rs new file mode 100644 index 000000000..5955b260c --- /dev/null +++ b/crates/ai/src/error.rs @@ -0,0 +1,52 @@ +//! The crate's error type: everything fails with `Result`, never a panic. + +use std::fmt; + +/// Every failure a caller can act on: the variant says what went wrong, +/// the message says why. +#[derive(Debug)] +pub enum AiError { + /// The HTTP layer failed (DNS, TLS, status code, body). + Http(String), + /// A downloaded file's SHA-256 did not match the expected digest. + Checksum { expected: String, got: String }, + /// The local disk refused us (permissions, full, vanished mid-write). + Io(std::io::Error), + /// The provider returned a response we could not parse. + BadResponse(String), + /// The provider rejected the request (bad token, quota, model name). + Rejected(String), + /// No provider is configured — the UI should route to setup. + NotConfigured, + /// Local model inference failed (load, OOM, bad GGUF). + Inference(String), + /// The operation was cancelled by the user. + Cancelled, + /// The same cache entry is already being downloaded by another + /// caller — only one may hold the `.part` file at a time. + Busy, +} + +impl fmt::Display for AiError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Http(m) => write!(f, "network error: {m}"), + Self::Checksum { expected, got } => write!(f, "checksum mismatch: expected {expected}, got {got}"), + Self::Io(e) => write!(f, "disk error: {e}"), + Self::BadResponse(m) => write!(f, "unreadable response: {m}"), + Self::Rejected(m) => write!(f, "provider rejected the request: {m}"), + Self::NotConfigured => write!(f, "no AI provider configured"), + Self::Inference(m) => write!(f, "local model error: {m}"), + Self::Cancelled => write!(f, "cancelled"), + Self::Busy => write!(f, "a download of this model is already in progress"), + } + } +} + +impl std::error::Error for AiError {} + +impl From for AiError { + fn from(e: std::io::Error) -> Self { + Self::Io(e) + } +} diff --git a/crates/ai/src/fetch.rs b/crates/ai/src/fetch.rs new file mode 100644 index 000000000..d725127a2 --- /dev/null +++ b/crates/ai/src/fetch.rs @@ -0,0 +1,338 @@ +//! One-click model download: resumable, `.part`-suffixed while in flight +//! (a half-downloaded GGUF is never mistaken for a valid one), SHA-256 +//! verified, then atomically renamed. + +use std::fs::{self, File, OpenOptions}; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::time::{Duration, Instant}; + +use sha2::Digest; + +use crate::error::AiError; +use crate::registry::ModelEntry; + +/// Progress + lifecycle events, fed to the UI's status line. +#[derive(Clone, Debug)] +pub enum Event { + /// We know the total; download is starting or resuming. + Started { total: u64, resumed_from: u64 }, + /// Bytes on disk so far. `bytes_per_sec` is a running average. + Progress { done: u64, total: u64, bytes_per_sec: u64 }, + /// Body finished; hashing the file before we trust it. + Verifying, + /// Download is complete. `verified` is false when no reference digest + /// existed anywhere (registry table and the host's ETag) — the file is + /// kept but the UI should say "unverified" rather than pretend. + Done { verified: bool }, +} + +/// A handle to abort a download: `cancel` returns false → `AiError::Cancelled`. +pub type Cancel<'a> = &'a dyn Fn() -> bool; + +/// ureq's `timeout_recv_body` is a *total* budget, not a stall timer, so a +/// single fixed cap would kill slow-but-alive downloads of multi-GB models. +/// Instead the GET's body deadline scales with the registry's declared +/// size: a floor rate of 256 KiB/s, between 5 min and 6 h. A dead +/// connection can therefore never block the read loop indefinitely, and a +/// retry still resumes from the `.part` file. +const MIN_BODY_RATE: u64 = 256 * 1024; +const BODY_BUDGET_MIN: Duration = Duration::from_secs(5 * 60); +const BODY_BUDGET_MAX: Duration = Duration::from_secs(6 * 60 * 60); + +fn body_agent(remaining_bytes: u64) -> ureq::Agent { + let budget = Duration::from_secs(remaining_bytes / MIN_BODY_RATE).clamp(BODY_BUDGET_MIN, BODY_BUDGET_MAX); + ureq::Agent::config_builder() + .timeout_connect(Some(Duration::from_secs(15))) + .timeout_send_request(Some(Duration::from_secs(30))) + .timeout_recv_response(Some(Duration::from_secs(60))) + .timeout_recv_body(Some(budget)) + .build() + .into() +} + +/// OS-level lock guarding one cache entry against concurrent downloads; +/// released automatically on close/process exit, so no stale lock files. +fn lock_entry(part_path: &Path) -> Result { + let mut lock_name = part_path.as_os_str().to_os_string(); + lock_name.push(".lock"); + let lock = OpenOptions::new().write(true).create(true).truncate(false).open(PathBuf::from(lock_name))?; + match lock.try_lock() { + Ok(()) => Ok(lock), + Err(std::fs::TryLockError::WouldBlock) => Err(AiError::Busy), + Err(std::fs::TryLockError::Error(e)) => Err(AiError::Io(e)), + } +} + +/// Download `entry` into `cache_dir`, calling `on` with progress events. +/// Resumes an existing `*.part` via HTTP Range. Returns the final path. +pub fn download(entry: &ModelEntry, cache_dir: &Path, on: impl Fn(&Event), cancel: Option>) -> Result { + if cancel.is_some_and(|c| !c()) { + return Err(AiError::Cancelled); + } + fs::create_dir_all(cache_dir)?; + let final_path = cache_dir.join(entry.file); + if final_path.is_file() { + // Already on disk — verify once, reuse if good. + on(&Event::Verifying); + if let Some(expected) = expected_sha256(entry)? { + let got = sha256_file(&final_path)?; + if got.eq_ignore_ascii_case(&expected) { + on(&Event::Done { verified: true }); + return Ok(final_path); + } + return Err(AiError::Checksum { expected, got }); + } + on(&Event::Done { verified: false }); + return Ok(final_path); + } + + let part_path = cache_dir.join(format!("{}.part", entry.file)); + // One downloader per cache entry: two concurrent calls would read the + // same resume offset and append the same bytes twice, corrupting the + // `.part`. The lock is held through the final rename. + let _entry_lock = lock_entry(&part_path)?; + let mut resumed_from = part_path.metadata().map_or(0, |m| m.len()); + + let expected = expected_sha256(entry)?; + + let get = |range_from: u64| { + // Budget for the *full* size, not the remainder: a 200 that + // ignores Range means we re-download the whole model. + let mut req = body_agent(entry.size_mb.saturating_mul(1 << 20)).get(entry.url); + if range_from > 0 { + req = req.header("Range", &format!("bytes={range_from}-")); + } + req.call() + }; + let mut resp = match get(resumed_from) { + Ok(r) => r, + // A 416 means the server found our resume offset unsatisfiable — + // the common cause is a `.part` that already holds the complete + // file (cancelled between the last byte and the rename). If it + // verifies, finish the job; otherwise restart from scratch. + Err(ureq::Error::StatusCode(416)) if resumed_from > 0 => { + if let Some(expected) = &expected + && sha256_file(&part_path).is_ok_and(|got| got.eq_ignore_ascii_case(expected)) + { + on(&Event::Verifying); + fs::rename(&part_path, &final_path)?; + on(&Event::Done { verified: true }); + return Ok(final_path); + } + fs::remove_file(&part_path)?; + resumed_from = 0; + get(0).map_err(http_err)? + } + Err(e) => return Err(http_err(e)), + }; + + // A 200 to a Range request means the server ignored resume — truncate. + let honored_resume = resumed_from > 0 && resp.status() == 206; + let total = content_length(&resp).map(|len| if honored_resume { len + resumed_from } else { len }); + + on(&Event::Started { total: total.unwrap_or(0), resumed_from: if honored_resume { resumed_from } else { 0 } }); + + let mut file = if honored_resume { OpenOptions::new().append(true).open(&part_path)? } else { File::create(&part_path)? }; + + let mut body = resp.body_mut().as_reader(); + let mut buf = vec![0u8; 256 * 1024]; + let mut done = if honored_resume { resumed_from } else { 0 }; + let started = Instant::now(); + loop { + if cancel.is_some_and(|c| !c()) { + return Err(AiError::Cancelled); + } + let n = match body.read(&mut buf) { + Ok(0) => break, + Ok(n) => n, + Err(e) => return Err(AiError::Io(e)), + }; + file.write_all(&buf[..n])?; + done += n as u64; + let secs = started.elapsed().as_secs_f64().max(0.001); + on(&Event::Progress { done, total: total.unwrap_or(0), bytes_per_sec: (done.saturating_sub(resumed_from) as f64 / secs) as u64 }); + } + file.flush()?; + drop(file); + + // A short read means the connection dropped mid-body — keep the .part + // for resume and report it as an HTTP failure, not a success. + if let Some(t) = total + && done < t + { + return Err(AiError::Http(format!("connection dropped at {done}/{t} bytes — retry to resume"))); + } + + on(&Event::Verifying); + let verified = if let Some(expected) = expected { + let got = sha256_file(&part_path)?; + if !got.eq_ignore_ascii_case(&expected) { + // A corrupt download is deleted — it can never silently pass. + let _ = fs::remove_file(&part_path); + return Err(AiError::Checksum { expected, got }); + } + true + } else { + false + }; + + fs::rename(&part_path, &final_path)?; + on(&Event::Done { verified }); + Ok(final_path) +} + +/// The digest we hold this file to: our registry hash if pinned, else the +/// ETag of the resolved URL (Hugging Face serves the LFS sha256 there). +fn expected_sha256(entry: &ModelEntry) -> Result, AiError> { + if let Some(s) = entry.sha256 { + return Ok(Some(s.to_string())); + } + // No redirects: HF serves the LFS sha256 in `x-linked-etag` on the + // 302 itself; following it would return the CDN's own etag instead. + let agent: ureq::Agent = ureq::Agent::config_builder() + .max_redirects(0) + .timeout_connect(Some(Duration::from_secs(15))) + .timeout_recv_response(Some(Duration::from_secs(30))) + .build() + .into(); + match agent.head(entry.url).call() { + Ok(resp) => Ok(etag(&resp)), + // HEAD unsupported/unreachable → proceed without verification. + Err(_) => Ok(None), + } +} + +fn etag(resp: &ureq::http::Response) -> Option { + // Prefer the redirect target's etag (the LFS object), fall back to + // the response's own. Quotes stripped; weak etags ("W/…") ignored. + for name in ["x-linked-etag", "etag"] { + if let Some(v) = resp.headers().get(name).and_then(|v| v.to_str().ok()) { + let v = v.trim().trim_matches('"'); + if !v.starts_with("W/") && v.len() == 64 && v.bytes().all(|b| b.is_ascii_hexdigit()) { + return Some(v.to_string()); + } + } + } + None +} + +fn content_length(resp: &ureq::http::Response) -> Option { + resp.headers() + .get("content-length") + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.trim().parse().ok()) + // A redirect's own length is meaningless; only trust it after + // ureq followed redirects (it does — so the final body length). + .filter(|&n: &u64| n > 0) +} + +fn sha256_file(path: &Path) -> Result { + let mut file = File::open(path)?; + let mut hasher = sha2::Sha256::new(); + let mut buf = vec![0u8; 256 * 1024]; + loop { + let n = file.read(&mut buf)?; + if n == 0 { + break; + } + hasher.update(&buf[..n]); + } + Ok(hasher.finalize().iter().map(|b| format!("{b:02x}")).collect()) +} + +fn http_err(e: ureq::Error) -> AiError { + match e { + ureq::Error::StatusCode(code) => AiError::Rejected(format!("HTTP {code}")), + other => AiError::Http(other.to_string()), + } +} + +/// Where a model would land once downloaded. +pub fn model_path(entry: &ModelEntry, cache_dir: &Path) -> PathBuf { + cache_dir.join(entry.file) +} + +/// Is `entry` already fully downloaded? +pub fn is_downloaded(entry: &ModelEntry, cache_dir: &Path) -> bool { + cache_dir.join(entry.file).is_file() +} + +/// `tokenizer.json` beside the GGUF: `.tokenizer.json`. +pub fn tokenizer_path(entry: &ModelEntry, cache_dir: &Path) -> PathBuf { + cache_dir.join(format!("{}.tokenizer.json", entry.file)) +} + +/// The companion tokenizer — small (a few MB), so no resume loop, but the +/// same `.part`-then-rename rule applies. +pub fn download_tokenizer(entry: &ModelEntry, cache_dir: &Path) -> Result { + let dest = tokenizer_path(entry, cache_dir); + if dest.is_file() { + return Ok(dest); + } + let tmp = dest.with_extension("part"); + let mut resp = body_agent(32 * 1024 * 1024).get(entry.tokenizer_url).call().map_err(http_err)?; + let bytes = resp.body_mut().read_to_vec().map_err(|e| AiError::Http(e.to_string()))?; + fs::write(&tmp, bytes)?; + fs::rename(&tmp, &dest)?; + Ok(dest) +} + +/// Remove `entry`'s file (and any `.part` remnant). Missing is fine; +/// other failures (permissions, locked) are surfaced to the caller. +pub fn delete(entry: &ModelEntry, cache_dir: &Path) -> Result<(), AiError> { + for p in [cache_dir.join(entry.file), cache_dir.join(format!("{}.part", entry.file))] { + match fs::remove_file(&p) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(AiError::Io(e)), + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::registry::Arch; + + fn entry() -> &'static ModelEntry { + static E: ModelEntry = ModelEntry { + id: "t", + name: "t", + url: "", + file: "m.gguf", + tokenizer_url: "", + sha256: None, + size_mb: 1, + min_ram_gb: 0.0, + rec_ram_gb: 0.0, + license: "MIT", + quality: crate::registry::Quality::Tiny, + arch: Arch::Llama, + langs: "", + }; + &E + } + + #[test] + fn second_lock_on_same_entry_is_busy() { + let dir = tempfile::tempdir().expect("tempdir"); + let part = dir.path().join("m.gguf.part"); + let _lock = lock_entry(&part).expect("first lock"); + assert!(matches!(lock_entry(&part), Err(AiError::Busy))); + // Releasing lets the next caller in. + drop(_lock); + assert!(lock_entry(&part).is_ok()); + } + + #[test] + fn delete_reports_failures_beyond_not_found() { + let dir = tempfile::tempdir().expect("tempdir"); + // Absent paths are fine. + delete(entry(), dir.path()).expect("missing ok"); + // A directory where the file would be fails removal on every OS. + fs::create_dir(dir.path().join("m.gguf")).expect("mkdir"); + assert!(matches!(delete(entry(), dir.path()), Err(AiError::Io(_)))); + } +} diff --git a/crates/ai/src/lib.rs b/crates/ai/src/lib.rs new file mode 100644 index 000000000..b180040bc --- /dev/null +++ b/crates/ai/src/lib.rs @@ -0,0 +1,27 @@ +//! craft-ai — AI provider abstraction for the Craft suite. +//! +//! See `README.md` for the design: BYO API provider (OpenAI-compatible) + +//! one-click local models with capability detection. +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +mod error; +mod provider; +mod registry; +mod translate; + +#[cfg(not(target_arch = "wasm32"))] +pub mod fetch; +#[cfg(not(target_arch = "wasm32"))] +#[cfg(feature = "local")] +pub mod local; +// `MachineCaps` is portable data the registry needs on every target; only +// the OS probe itself is native-only. +pub mod probe; + +pub use error::AiError; +#[cfg(not(target_arch = "wasm32"))] +pub use provider::OpenAiCompat; +pub use provider::{GenOpts, Provider, ProviderInfo}; +pub use registry::{Arch, MODELS, ModelEntry, Quality, by_id, fits, suggest}; +pub use translate::{PdfGrammar, Segment, TranslateGrammar, is_protected_token, segment, translate_segments}; diff --git a/crates/ai/src/local.rs b/crates/ai/src/local.rs new file mode 100644 index 000000000..6c42aabae --- /dev/null +++ b/crates/ai/src/local.rs @@ -0,0 +1,144 @@ +//! Local GGUF inference on candle (pure Rust — keeps the no-C++ rule and +//! the door open for wasm later). CPU only for now; GPU comes from the +//! app's wgpu adapter in a later milestone. +//! +//! Both supported architectures speak ChatML — one prompt template covers +//! Qwen2.5 and SmolLM2. + +use std::fs::File; +use std::path::Path; + +use candle_core::quantized::gguf_file; +use candle_core::{Device, IndexOp, Tensor}; +use candle_transformers::generation::LogitsProcessor; +use candle_transformers::models::{quantized_llama, quantized_qwen2}; +use tokenizers::Tokenizer; + +use crate::error::AiError; +use crate::provider::{GenOpts, Provider, ProviderInfo}; +use crate::registry::{Arch, ModelEntry}; + +enum Weights { + Qwen2(quantized_qwen2::ModelWeights), + Llama(quantized_llama::ModelWeights), +} + +impl Weights { + fn forward(&mut self, ids: &Tensor, pos: usize) -> Result { + match self { + Self::Qwen2(w) => w.forward(ids, pos), + Self::Llama(w) => w.forward(ids, pos), + } + } +} + +/// A downloaded GGUF loaded and ready. Construction is the expensive +/// part; `complete` is then just a forward loop. +pub struct LocalGguf { + weights: Weights, + tokenizer: Tokenizer, + device: Device, + eos: u32, + entry: &'static ModelEntry, +} + +const SEED: u64 = 0xC0FFEE; + +impl LocalGguf { + /// Load the GGUF + tokenizer.json of `entry` from the model cache. + /// All failures are `Inference` errors — a corrupt file, an OOM, a + /// wrong-arch GGUF, all land in the status line, never in a panic. + pub fn load(entry: &'static ModelEntry, gguf_path: &Path, tokenizer_path: &Path) -> Result { + let device = Device::Cpu; + let mut file = File::open(gguf_path).map_err(AiError::Io)?; + // Malformed GGUF metadata (e.g. `general.alignment = 0`) makes + // candle's parser panic on a division — hostile input must come + // back as an error, not a crash. + // Malformed GGUF metadata (e.g. `general.alignment = 0`, or a + // zero `attention.head_count` reaching a division in the weight + // constructors) makes candle panic — hostile input must come + // back as an error, not a crash. The guard covers parsing *and* + // weight construction. + let weights = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> Result { + let content = gguf_file::Content::read(&mut file).map_err(|e| AiError::Inference(format!("bad GGUF: {e}")))?; + match entry.arch { + Arch::Qwen2 => Ok(Weights::Qwen2( + quantized_qwen2::ModelWeights::from_gguf(content, &mut file, &device) + .map_err(|e| AiError::Inference(format!("cannot load {}: {e}", entry.file)))?, + )), + Arch::Llama => Ok(Weights::Llama( + quantized_llama::ModelWeights::from_gguf(content, &mut file, &device) + .map_err(|e| AiError::Inference(format!("cannot load {}: {e}", entry.file)))?, + )), + } + })) + .map_err(|_| AiError::Inference("GGUF loader panicked on malformed metadata".to_owned()))??; + let tokenizer = Tokenizer::from_file(tokenizer_path).map_err(|e| AiError::Inference(format!("bad tokenizer.json: {e}")))?; + let eos = tokenizer.token_to_id("<|im_end|>").ok_or_else(|| AiError::Inference("tokenizer has no EOS token".into()))?; + Ok(Self { weights, tokenizer, device, eos, entry }) + } + + /// ChatML wrap — Qwen2.5 and SmolLM2 both ship this template. + fn chat(prompt: &str) -> String { + format!( + "<|im_start|>system\nYou are a precise translation engine. Follow the instructions exactly.<|im_end|>\n<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n", + ) + } + + fn generate(&mut self, prompt: &str, opts: &GenOpts) -> Result { + let text = Self::chat(prompt); + let encoding = self.tokenizer.encode(text, true).map_err(|e| AiError::Inference(format!("encode failed: {e}")))?; + let mut tokens: Vec = encoding.get_ids().to_vec(); + + let temp = if opts.temperature <= 0.02 { None } else { Some(f64::from(opts.temperature)) }; + let mut sampler = LogitsProcessor::new(SEED, temp, None); + + let mut pos = 0usize; + let mut generated: Vec = Vec::with_capacity(opts.max_tokens.min(4096) as usize); + let mut hit_eos = false; + for _ in 0..opts.max_tokens { + let input_len = tokens.len() - pos; + let input = + Tensor::new(&tokens[pos..], &self.device).and_then(|t| t.reshape((1, t.dims1()?))).map_err(|e| AiError::Inference(e.to_string()))?; + let logits = self.weights.forward(&input, pos).map_err(|e| AiError::Inference(e.to_string()))?; + pos += input_len; + // `forward` already returns the last sequence position's + // logits [batch, vocab] — index the batch row only. + let logits = logits.i(0).map_err(|e| AiError::Inference(e.to_string()))?; + let next = sampler.sample(&logits).map_err(|e| AiError::Inference(e.to_string()))?; + if next == self.eos { + hit_eos = true; + break; + } + tokens.push(next); + generated.push(next); + } + // No EOS inside the budget → the reply was cut mid-translation; + // partial output would silently truncate the document. + if !hit_eos { + return Err(AiError::Inference(format!("generation hit max_tokens ({}) without EOS", opts.max_tokens))); + } + // Decode the generated ids through the tokenizer: byte-level BPE + // vocab strings can't be concatenated (spaces/newlines/non-ASCII + // bytes stay encoded, and UTF-8 can split across tokens). + self.tokenizer.decode(&generated, true).map_err(|e| AiError::Inference(format!("decode failed: {e}"))) + } +} + +impl Provider for LocalGguf { + fn name(&self) -> String { + format!("local: {}", self.entry.name) + } + + fn complete(&mut self, prompt: &str, opts: &GenOpts) -> Result { + self.generate(prompt, opts).map(|s| s.trim().to_string()) + } + + fn test(&mut self) -> Result { + Ok(ProviderInfo { label: format!("loaded: {}", self.entry.name), models: vec![self.entry.id.to_string()] }) + } + + fn is_local(&self) -> bool { + true + } +} diff --git a/crates/ai/src/probe.rs b/crates/ai/src/probe.rs new file mode 100644 index 000000000..23c948b37 --- /dev/null +++ b/crates/ai/src/probe.rs @@ -0,0 +1,82 @@ +//! Machine capability detection — runs once, cache the result. +//! +//! GPU/VRAM is deliberately *not* probed here: apps already link wgpu, so +//! they read their own adapter info and fill `MachineCaps::vram_gb` after +//! calling [`probe`]. Re-probing the GPU would double the surface for a +//! bug that reports nonsense VRAM on hybrid laptops. + +/// What this machine can comfortably run. +#[derive(Clone, Debug, PartialEq)] +pub struct MachineCaps { + /// Total physical RAM, in GiB. + pub ram_gb: f32, + /// Free space on the volume holding the model cache, in GiB. + pub disk_free_gb: f32, + /// Logical CPU threads. + pub threads: usize, + /// AVX2 detected — without it quantized GGUF inference crawls. + pub avx2: bool, + /// Dedicated VRAM in GiB, when the app supplied it from its wgpu adapter. + /// `None` means "unknown or no discrete GPU" — never guessed. + pub vram_gb: Option, +} + +impl MachineCaps { + /// Rough throughput hint for a model of `size_mb`: tokens/sec on CPU. + /// Deliberately conservative — an honest underestimate beats an + /// optimistic one that makes the app look slow. + pub fn est_tokens_per_sec(&self, size_mb: u64) -> f32 { + let base = if self.avx2 { 6.0 } else { 1.2 }; + // ~0.5B params ≈ 350 MiB Q4; speed scales inversely with size. + let scale = 350.0 / (size_mb.max(1) as f32); + let thread_boost = (self.threads as f32 / 4.0).min(1.5); + (base * scale * thread_boost).max(0.3) + } +} + +/// Probe the real machine once. `cache_dir` is the future model-cache +/// location — disk space is measured on *its* volume, which may not be +/// the OS volume. +#[cfg(not(target_arch = "wasm32"))] +pub fn probe(cache_dir: &std::path::Path) -> MachineCaps { + let mut sys = sysinfo::System::new(); + sys.refresh_memory(); + let ram_gb = sys.total_memory() as f32 / (1 << 30) as f32; + + let disk_free_gb = disk_free(cache_dir); + + MachineCaps { ram_gb, disk_free_gb, threads: std::thread::available_parallelism().map_or(1, |n| n.get()), avx2: avx2(), vram_gb: None } +} + +#[cfg(all(not(target_arch = "wasm32"), any(target_arch = "x86_64", target_arch = "x86")))] +fn avx2() -> bool { + std::arch::is_x86_feature_detected!("avx2") +} + +#[cfg(all(not(target_arch = "wasm32"), not(any(target_arch = "x86_64", target_arch = "x86"))))] +fn avx2() -> bool { + // AArch64 NEON is mandatory, so the honest answer is "fast enough". + true +} + +/// Free bytes on the volume containing `dir`, walking up to the nearest +/// existing ancestor if `dir` doesn't exist yet (the common case). +#[cfg(not(target_arch = "wasm32"))] +fn disk_free(dir: &std::path::Path) -> f32 { + // Canonicalize the existing ancestor: a relative or symlinked + // `cache_dir` must still match an absolute mount point. + let probe: std::path::PathBuf = + dir.ancestors().find(|p| p.exists()).and_then(|p| p.canonicalize().ok()).unwrap_or_else(|| std::path::PathBuf::from(".")); + let disks = sysinfo::Disks::new_with_refreshed_list(); + // The longest mount point that prefixes our dir owns it. + let best = disks.iter().filter(|d| probe.starts_with(d.mount_point())).max_by_key(|d| d.mount_point().as_os_str().len()); + best.map_or(0.0, |d| d.available_space() as f32 / (1 << 30) as f32) +} + +/// The suite-shared model cache (`%LOCALAPPDATA%\artcraft\models` etc.). +/// `None` on platforms without a cache dir — the caller should refuse +/// local models rather than splashing files into the cwd. +#[cfg(not(target_arch = "wasm32"))] +pub fn models_dir() -> Option { + dirs::cache_dir().map(|d| d.join("artcraft").join("models")) +} diff --git a/crates/ai/src/provider.rs b/crates/ai/src/provider.rs new file mode 100644 index 000000000..ae7bd757d --- /dev/null +++ b/crates/ai/src/provider.rs @@ -0,0 +1,196 @@ +//! Inference backends behind one trait. Apps hold a `Box` +//! and never care whether the bytes went to OpenAI, Ollama or a local GGUF. + +use crate::error::AiError; + +/// Generation parameters — kept minimal on purpose; the UI exposes +/// "translate", not a sampler playground. +#[derive(Clone, Debug)] +pub struct GenOpts { + /// Max tokens to generate. + pub max_tokens: u32, + /// Sampling temperature; translation wants ~0. + pub temperature: f32, +} + +impl Default for GenOpts { + fn default() -> Self { + Self { max_tokens: 2048, temperature: 0.1 } + } +} + +/// What `test()` learned about a provider — shown next to the green check. +#[derive(Clone, Debug)] +pub struct ProviderInfo { + /// Human label, e.g. "Ollama at http://localhost:11434". + pub label: String, + /// Model ids the endpoint advertised (may be empty if it doesn't say). + pub models: Vec, +} + +/// An AI backend the suite can use. `Send` so apps can move a loaded +/// provider onto a worker thread for long generations. +pub trait Provider: Send { + /// Short name for the status line ("local: Qwen2.5 1.5B", "OpenAI-compatible"). + fn name(&self) -> String; + /// One completion. Prompt construction is the caller's job — this + /// trait is deliberately text-in/text-out so every consumer (translate, + /// summarize, custom prompt) shares one path. + fn complete(&mut self, prompt: &str, opts: &GenOpts) -> Result; + /// Can we reach it? Never sends document text — just a handshake. + fn test(&mut self) -> Result; + /// True when document text stays on this machine. + fn is_local(&self) -> bool; +} + +/// Any OpenAI-compatible `/v1/chat/completions` endpoint: OpenAI, Azure, +/// Ollama (`http://localhost:11434/v1`), LM Studio, Groq, OpenRouter… +/// The token is a plain string — storage (OS keyring, settings file) is +/// the app's choice, and it is never logged or echoed into errors. +#[cfg(not(target_arch = "wasm32"))] +pub struct OpenAiCompat { + /// Base URL ending at the API root, e.g. `https://api.openai.com/v1`. + pub endpoint: String, + /// Bearer token; `None` for unauthenticated local endpoints (Ollama). + pub token: Option, + /// Model id sent in every request. + pub model: String, +} + +#[cfg(not(target_arch = "wasm32"))] +impl OpenAiCompat { + pub fn new(endpoint: impl Into, token: Option, model: impl Into) -> Self { + Self { endpoint: endpoint.into(), token, model: model.into() } + } + + fn url(&self, path: &str) -> String { + format!("{}/{}", self.endpoint.trim_end_matches('/'), path) + } + + /// ureq sets no timeouts by default — an endpoint that accepts the + /// socket and never replies would block the caller forever, so every + /// request goes through an agent with finite deadlines. The response + /// budgets are generous: a non-streamed chat completion only sends + /// headers once generation is done, which can take minutes. + fn agent() -> ureq::Agent { + ureq::Agent::config_builder() + .timeout_connect(Some(std::time::Duration::from_secs(15))) + .timeout_send_request(Some(std::time::Duration::from_secs(30))) + .timeout_send_body(Some(std::time::Duration::from_secs(60))) + .timeout_recv_response(Some(std::time::Duration::from_secs(300))) + .timeout_recv_body(Some(std::time::Duration::from_secs(300))) + .build() + .into() + } + + fn auth(&self, req: ureq::RequestBuilder) -> ureq::RequestBuilder { + match &self.token { + Some(t) if !t.is_empty() => req.header("Authorization", format!("Bearer {t}")), + _ => req, + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl Provider for OpenAiCompat { + fn name(&self) -> String { + format!("{} ({})", self.model, self.endpoint) + } + + fn complete(&mut self, prompt: &str, opts: &GenOpts) -> Result { + if self.endpoint.trim().is_empty() { + return Err(AiError::NotConfigured); + } + let body = serde_json::json!({ + "model": self.model, + "messages": [{"role": "user", "content": prompt}], + "temperature": opts.temperature, + "max_tokens": opts.max_tokens, + }); + let mut resp = + self.auth(Self::agent().post(self.url("chat/completions"))).header("Content-Type", "application/json").send_json(body).map_err(|e| { + match e { + ureq::Error::StatusCode(401 | 403) => AiError::Rejected("authentication failed — check the API token".into()), + ureq::Error::StatusCode(404) => AiError::Rejected("model or endpoint not found — check both fields".into()), + ureq::Error::StatusCode(c) => AiError::Rejected(format!("HTTP {c}")), + other => AiError::Http(other.to_string()), + } + })?; + let v: serde_json::Value = resp.body_mut().read_json().map_err(|e| AiError::BadResponse(e.to_string()))?; + // A completion cut off at max_tokens ("length") or aborted is not + // a translation — returning it would silently truncate the doc. + match v.pointer("/choices/0/finish_reason").and_then(serde_json::Value::as_str) { + Some("stop") | Some("tool_calls") | None => {} + Some(reason) => return Err(AiError::BadResponse(format!("incomplete completion (finish_reason={reason})"))), + } + v.pointer("/choices/0/message/content") + .and_then(serde_json::Value::as_str) + .map(str::to_string) + .ok_or_else(|| AiError::BadResponse("no choices[0].message.content in response".into())) + } + + fn test(&mut self) -> Result { + if self.endpoint.trim().is_empty() { + return Err(AiError::NotConfigured); + } + // GET /v1/models — a handshake only, no user text leaves the app. + let mut req = Self::agent().get(self.url("models")); + if let Some(t) = &self.token + && !t.is_empty() + { + req = req.header("Authorization", format!("Bearer {t}")); + } + let mut resp = req.call().map_err(|e| match e { + ureq::Error::StatusCode(401 | 403) => AiError::Rejected("authentication failed — check the API token".into()), + ureq::Error::StatusCode(c) => AiError::Rejected(format!("HTTP {c}")), + other => AiError::Http(other.to_string()), + })?; + let v: serde_json::Value = resp.body_mut().read_json().map_err(|e| AiError::BadResponse(e.to_string()))?; + let models = v + .get("data") + .and_then(serde_json::Value::as_array) + .map(|arr| arr.iter().filter_map(|m| m.get("id").and_then(serde_json::Value::as_str).map(str::to_string)).collect()) + .unwrap_or_default(); + Ok(ProviderInfo { label: format!("reachable: {}", self.endpoint), models }) + } + + fn is_local(&self) -> bool { + // Local endpoints keep text on the machine; we can't verify a + // remote URL's forwarding, so honesty means: loopback → local. + // Parse the authority properly — a prefix match would call + // `localhost.example.com` or `127.0.0.1.example.com` "local" + // while document text leaves the machine. + let e = self.endpoint.trim(); + let after_scheme = e.split_once("://").map_or(e, |(_, rest)| rest); + let authority = after_scheme.split(['/', '?', '#']).next().unwrap_or(""); + let host_port = authority.rsplit('@').next().unwrap_or(""); // strip userinfo + let host = if let Some(rest) = host_port.strip_prefix('[') { + rest.split(']').next().unwrap_or("") + } else { + host_port.split(':').next().unwrap_or("") + }; + host.eq_ignore_ascii_case("localhost") || host.parse::().is_ok_and(|ip| ip.is_loopback()) + } +} + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod tests { + use super::*; + + #[test] + fn is_local_accepts_only_real_loopback() { + let local = |ep: &str| OpenAiCompat::new(ep, None, "m").is_local(); + assert!(local("http://localhost:11434/v1")); + assert!(local("https://localhost/v1")); + assert!(local("http://127.0.0.1:8080/v1")); + assert!(local("http://127.99.0.1/v1")); // whole 127/8 is loopback + assert!(local("http://[::1]:11434/v1")); + // Lookalike hostnames must NOT report local — document text would + // leave the machine while the UI promises it stays. + assert!(!local("https://localhost.example.com/v1")); + assert!(!local("https://127.0.0.1.example.com/v1")); + assert!(!local("https://localhost.evil.io/v1")); + assert!(!local("https://api.openai.com/v1")); + assert!(!local("https://example.com/localhost/v1")); + } +} diff --git a/crates/ai/src/registry.rs b/crates/ai/src/registry.rs new file mode 100644 index 000000000..2035ab836 --- /dev/null +++ b/crates/ai/src/registry.rs @@ -0,0 +1,138 @@ +//! The curated model registry — a hand-maintained table, compiled in. +//! Runtime-fetching this list would make our supply chain un-auditable. +//! +//! Curation rule: **permissive licenses only** (the suite is MIT OR +//! Apache-2.0 and commercial users run it). CC-BY-NC models like NLLB-200 +//! are excluded by policy; their absence is deliberate. +//! +//! `sha256` may be `None`: Hugging Face serves the LFS digest in the +//! `X-Linked-ETag` header of the resolve redirect, which [`crate::fetch`] +//! uses as the expected checksum. We only hard-code a hash when we've +//! verified it ourselves — never a guessed one. + +use crate::probe::MachineCaps; + +/// Model quality tier — higher is better output, bigger download. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub enum Quality { + Tiny, + Small, + Medium, +} + +/// Which candle architecture loads the GGUF. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Arch { + Qwen2, + Llama, +} + +/// One downloadable model. +#[derive(Clone, Copy, Debug)] +pub struct ModelEntry { + /// Stable identifier stored in settings (`"qwen2.5-1.5b-q4km"`). + pub id: &'static str, + /// Display name for the picker. + pub name: &'static str, + /// Canonical download URL (Hugging Face `resolve` endpoint). + pub url: &'static str, + /// Filename inside the model cache. + pub file: &'static str, + /// Companion tokenizer (needed by local inference; the GGUF ships + /// vocab only, not the chat-time tokenizer.json). + pub tokenizer_url: &'static str, + /// Verified SHA-256 of the GGUF, when we have one. + pub sha256: Option<&'static str>, + /// Download size, MiB — shown to the user before they commit. + pub size_mb: u64, + /// Below this RAM the model is refused outright. + pub min_ram_gb: f32, + /// Recommended RAM for comfortable use. + pub rec_ram_gb: f32, + /// SPDX license id, displayed in the UI. + pub license: &'static str, + pub quality: Quality, + pub arch: Arch, + /// Human-readable language coverage for the picker. + pub langs: &'static str, +} + +/// The registry, smallest first. +pub static MODELS: &[ModelEntry] = &[ + ModelEntry { + id: "qwen2.5-0.5b-q4km", + name: "Qwen2.5 0.5B Instruct (Q4_K_M)", + url: "https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf", + file: "qwen2.5-0.5b-instruct-q4_k_m.gguf", + tokenizer_url: "https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct/resolve/main/tokenizer.json", + sha256: None, + size_mb: 389, + min_ram_gb: 2.0, + rec_ram_gb: 4.0, + license: "Apache-2.0", + quality: Quality::Tiny, + arch: Arch::Qwen2, + langs: "29+ languages incl. EN/ZH/FR/DE/ES/JA/KO/RU", + }, + ModelEntry { + id: "smollm2-1.7b-q4km", + name: "SmolLM2 1.7B Instruct (Q4_K_M)", + url: "https://huggingface.co/bartowski/SmolLM2-1.7B-Instruct-GGUF/resolve/main/SmolLM2-1.7B-Instruct-Q4_K_M.gguf", + file: "SmolLM2-1.7B-Instruct-Q4_K_M.gguf", + tokenizer_url: "https://huggingface.co/HuggingFaceTB/SmolLM2-1.7B-Instruct/resolve/main/tokenizer.json", + sha256: None, + size_mb: 1033, + min_ram_gb: 4.0, + rec_ram_gb: 8.0, + license: "Apache-2.0", + quality: Quality::Small, + arch: Arch::Llama, + langs: "primarily EN/FR/DE/ES/IT", + }, + ModelEntry { + id: "qwen2.5-1.5b-q4km", + name: "Qwen2.5 1.5B Instruct (Q4_K_M)", + url: "https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf", + file: "qwen2.5-1.5b-instruct-q4_k_m.gguf", + tokenizer_url: "https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct/resolve/main/tokenizer.json", + sha256: None, + size_mb: 986, + min_ram_gb: 4.0, + rec_ram_gb: 8.0, + license: "Apache-2.0", + quality: Quality::Small, + arch: Arch::Qwen2, + langs: "29+ languages incl. EN/ZH/FR/DE/ES/JA/KO/RU", + }, + // NOTE: Qwen2.5-3B was removed — its `qwen-research` license is + // non-commercial and breaks the permissive-only rule above. No + // permissively-licensed Medium-tier candidate is currently curated. +]; + +/// Look up a model by stable id. +pub fn by_id(id: &str) -> Option<&'static ModelEntry> { + MODELS.iter().find(|m| m.id == id) +} + +/// Whether `entry` can run at all on this machine. +pub fn fits(caps: &MachineCaps, entry: &ModelEntry) -> bool { + let ram_ok = caps.ram_gb >= entry.min_ram_gb; + let disk_ok = caps.disk_free_gb >= entry.size_mb as f32 / 1024.0 * 1.3; // file + headroom + ram_ok && disk_ok +} + +/// The default suggestion for `caps`: best quality that fits, preferring +/// 3B on roomy machines, 1.5B mid-range, 0.5B on small ones. Returns +/// `None` when nothing fits — the UI then says so honestly instead of +/// offering a download that can't run. +pub fn suggest(caps: &MachineCaps) -> Option<&'static ModelEntry> { + let capable: Vec<&ModelEntry> = MODELS.iter().filter(|m| fits(caps, m)).collect(); + let best_allowed = if caps.vram_gb.is_some_and(|v| v >= 6.0) || caps.ram_gb >= 16.0 { + Quality::Medium + } else if caps.ram_gb >= 8.0 { + Quality::Small + } else { + Quality::Tiny + }; + capable.into_iter().filter(|m| m.quality <= best_allowed).max_by_key(|m| m.quality) +} diff --git a/crates/ai/src/translate.rs b/crates/ai/src/translate.rs new file mode 100644 index 000000000..a5de0622c --- /dev/null +++ b/crates/ai/src/translate.rs @@ -0,0 +1,525 @@ +//! Document translation as *segments*, never raw text: the model sees +//! only translatable runs, and protected spans (URLs, placeholders, +//! markup, numbers-only runs) come back byte-identical because we never +//! sent them. +//! +//! Each app declares its grammar via [`TranslateGrammar`]; the default +//! [`PdfGrammar`] suits extracted PDF text. + +use crate::error::AiError; +use crate::provider::{GenOpts, Provider}; + +/// A unit of text the translator decided on. +#[derive(Clone, Debug, PartialEq)] +pub enum Segment { + /// Sent to the model; replace with the translation. + Text(String), + /// Never sent; re-inserted verbatim. + Protected(String), +} + +impl Segment { + /// The source text, translated or not. + pub fn source(&self) -> &str { + match self { + Self::Text(s) | Self::Protected(s) => s, + } + } +} + +/// Which runs of a document's text must never be translated. +pub trait TranslateGrammar { + /// True when `token` (already delimited — a word, symbol, or + /// whitespace-free run) is structural and must survive verbatim. + fn is_protected(&self, token: &str) -> bool; +} + +/// Grammar for text extracted from a PDF: protects URLs, e-mail +/// addresses, `{placeholders}`, ``, and runs with no letters +/// (page furniture, digits, punctuation). +pub struct PdfGrammar; + +impl TranslateGrammar for PdfGrammar { + fn is_protected(&self, token: &str) -> bool { + is_protected_token(token) + } +} + +/// Shared protected-token test, useful to other apps' grammars. +pub fn is_protected_token(token: &str) -> bool { + let t = token.trim(); + if t.is_empty() { + return true; + } + if !t.chars().any(|c| c.is_alphabetic()) { + return true; // numbers, punctuation, symbols + } + let lower = t.to_ascii_lowercase(); + lower.starts_with("http://") + || lower.starts_with("https://") + || lower.starts_with("www.") + || lower.contains('@') && lower.contains('.') && !lower.contains(' ') + || (t.starts_with('{') && t.ends_with('}')) + || (t.starts_with('<') && t.ends_with('>')) +} + +/// Split `text` into segments on whitespace boundaries, grouping +/// consecutive protected or translatable tokens. `` spans are +/// carved out first — a tag with attributes contains whitespace, and a +/// whole `Hello` run must keep "Hello" translatable while the +/// tags themselves stay byte-identical. +pub fn segment(text: &str, grammar: &dyn TranslateGrammar) -> Vec { + fn flush(out: &mut Vec, buf: &mut String, protected: bool) { + if buf.is_empty() { + return; + } + let seg = if protected { Segment::Protected(std::mem::take(buf)) } else { Segment::Text(std::mem::take(buf)) }; + out.push(seg); + } + + let mut out: Vec = Vec::new(); + let mut buf = String::new(); + let mut buf_protected = false; + let mut started = false; + + for (token, is_tag) in split_tokens(text) { + let ws = token.chars().all(char::is_whitespace); + // Whitespace glues to whatever the current run is; markup tags + // are always protected. + let this_protected = if ws { buf_protected } else { is_tag || grammar.is_protected(token) }; + if started && this_protected != buf_protected { + flush(&mut out, &mut buf, buf_protected); + } + buf_protected = this_protected; + started = true; + buf.push_str(token); + } + flush(&mut out, &mut buf, buf_protected); + out +} + +/// Tokenize into `(slice, is_tag)`: markup tags are whole `<…>` spans +/// (attributes included), everything else alternates between maximal +/// whitespace and non-whitespace runs. +fn split_tokens(text: &str) -> Vec<(&str, bool)> { + let mut out = Vec::new(); + let mut pos = 0usize; + let bytes = text.as_bytes(); + while pos < bytes.len() { + if bytes[pos] == b'<' + && let Some(tag_len) = tag_len(&text[pos..]) + { + out.push((&text[pos..pos + tag_len], true)); + pos += tag_len; + continue; + } + // Advance a non-tag run up to the next '<' that starts a tag; + // bare '<' characters just stay inside the run. + let mut scan = pos; + let mut run_end = bytes.len(); + while let Some(lt) = bytes[scan..].iter().position(|&b| b == b'<') { + let at = scan + lt; + if tag_len(&text[at..]).is_some() { + run_end = at; + break; + } + scan = at + 1; + } + for piece in split_words_ws(&text[pos..run_end]) { + out.push((piece, false)); + } + pos = run_end; + } + out +} + +/// Length of a `<…>` tag starting at `s[0]`, or `None` when `<` does not +/// begin markup (bare `<`, `<3`, `< hello`). Tags start `` before any `<` or newline; +/// the scan is capped so pathological input can't stall it. +fn tag_len(s: &str) -> Option { + const MAX_TAG: usize = 512; + let second = s.as_bytes().get(1)?; + let opener_ok = second.is_ascii_alphabetic() || matches!(second, b'/' | b'!' | b'?'); + if !opener_ok { + return None; + } + s.char_indices().skip(2).take(MAX_TAG).find(|(_, c)| matches!(c, '>' | '<' | '\n')).and_then(|(i, c)| (c == '>').then_some(i + 1)) +} + +/// Split into alternating word/whitespace runs (like `split_inclusive` +/// but grouping every maximal run of whitespace or non-whitespace). +fn split_words_ws(text: &str) -> Vec<&str> { + let mut out = Vec::new(); + let mut start = 0usize; + let mut prev_ws: Option = None; + for (i, c) in text.char_indices() { + let ws = c.is_whitespace(); + if let Some(p) = prev_ws + && p != ws + { + out.push(&text[start..i]); + start = i; + } + prev_ws = Some(ws); + } + if start < text.len() { + out.push(&text[start..]); + } + out +} + +/// How many segments one prompt carries — keeps responses snappy and +/// makes a numbering slip cheap to retry. +const BATCH: usize = 20; +/// …and how many bytes one prompt carries. A document with no protected +/// tokens is one `Text` segment of arbitrary length — without a size +/// bound the request can exceed `max_tokens` and come back truncated +/// (which providers must reject, not return as success). +const BATCH_BYTES: usize = 4 * 1024; +/// A single `Text` run larger than this is chopped into pieces before +/// batching, so no one request is unbounded. +const RUN_BYTES: usize = 3 * 1024; + +/// A translatable piece of one segment: the whitespace-stripped `core` +/// is sent to the model, `lead`/`trail` are restored verbatim around the +/// translation so `Visitez` never glues to `https://a.io`. +struct Run<'a> { + seg: usize, + lead: &'a str, + core: &'a str, + trail: &'a str, + /// False for whitespace-only pieces: never sent to the model, but + /// still reassembled into the segment. + send: bool, +} + +/// `(leading ws, core, trailing ws)` of `s`, all on char boundaries. +fn ws_edges(s: &str) -> (&str, &str, &str) { + let lead = s.len() - s.trim_start().len(); + let tail = s.len() - s.trim_end().len(); + if lead + tail >= s.len() { + return (s, "", ""); // all-whitespace run: nothing to translate + } + (&s[..lead], &s[lead..s.len() - tail], &s[s.len() - tail..]) +} + +/// Chop `s` into ≤ `max`-byte pieces on whitespace boundaries (the last +/// ws before the limit; a run with no ws is hard-cut at the limit). The +/// boundary whitespace stays inside the pieces — nothing is lost. +fn split_run(s: &str, max: usize) -> Vec<&str> { + let mut out = Vec::new(); + let mut rest = s; + while rest.len() > max { + let mut ws_cut = None; + let mut boundary = 0usize; + for (i, c) in rest.char_indices() { + if i >= max { + break; + } + boundary = i + c.len_utf8(); + if c.is_whitespace() { + ws_cut = Some(i); // cut *before* the ws — it leads the next piece + } + } + let cut = match ws_cut.filter(|w| *w > max / 4) { + Some(w) => w, + None => boundary.max(1), + }; + out.push(&rest[..cut]); + rest = &rest[cut..]; + } + if !rest.is_empty() { + out.push(rest); + } + out +} + +/// Translate every `Text` segment to `target_lang`, leaving `Protected` +/// untouched. Batched with numbered items; a count mismatch retries the +/// offending half rather than misaligning the whole document. +pub fn translate_segments(provider: &mut dyn Provider, segs: &[Segment], target_lang: &str) -> Result, AiError> { + // Source whitespace never reaches the model: each Text segment is + // split edge-to-edge, and oversized runs are chopped further. + let mut runs: Vec> = Vec::new(); + for (i, s) in segs.iter().enumerate() { + let Segment::Text(src) = s else { continue }; + for piece in split_run(src, RUN_BYTES) { + let (lead, core, trail) = ws_edges(piece); + // Pure-whitespace pieces (possible after a long run is chopped) + // keep their bytes in the output but skip the provider. + runs.push(Run { seg: i, lead, core, trail, send: !core.is_empty() }); + } + } + let run_segs: std::collections::HashSet = runs.iter().map(|r| r.seg).collect(); + + let mut out: Vec = segs.iter().map(|s| s.source().to_string()).collect(); + for &i in &run_segs { + out[i].clear(); // rebuilt from translated runs below + } + let mut i = 0; + while i < runs.len() { + // Whitespace-only pieces restore verbatim, in order, and never + // reach the model. + while i < runs.len() && !runs[i].send { + let r = &runs[i]; + out[r.seg].push_str(r.lead); + out[r.seg].push_str(r.trail); + i += 1; + } + // A batch is bounded by item count *and* total prompt bytes. + let mut bytes = 0usize; + let start = i; + while i < runs.len() && runs[i].send && i - start < BATCH && bytes + runs[i].core.len() <= BATCH_BYTES { + bytes += runs[i].core.len(); + i += 1; + } + if i == start { + if i < runs.len() { + i += 1; // never spin on an oversized single item + } + continue; + } + let chunk = &runs[start..i]; + let translated = translate_batch(provider, chunk, target_lang)?; + for (run, t) in chunk.iter().zip(translated) { + out[run.seg].push_str(run.lead); + out[run.seg].push_str(&t); + out[run.seg].push_str(run.trail); + } + } + Ok(out) +} + +/// One numbered batch; splits recursively on count mismatch so a single +/// malformed reply can't corrupt neighbors. +fn translate_batch(provider: &mut dyn Provider, runs: &[Run<'_>], lang: &str) -> Result, AiError> { + if runs.len() == 1 { + return Ok(vec![translate_one(provider, runs[0].core, lang)?]); + } + let prompt = numbered_prompt(&runs.iter().map(|r| r.core.to_string()).collect::>(), lang); + let reply = provider.complete(&prompt, &GenOpts::default())?; + match parse_numbered(&reply, runs.len()) { + Some(v) => Ok(v), + None => { + let mid = runs.len() / 2; + let mut a = translate_batch(provider, &runs[..mid], lang)?; + let b = translate_batch(provider, &runs[mid..], lang)?; + a.extend(b); + Ok(a) + } + } +} + +fn translate_one(provider: &mut dyn Provider, source: &str, lang: &str) -> Result { + let prompt = format!("Translate the following text to {lang}. Reply with ONLY the translation, no quotes, no notes.\n\n{source}"); + Ok(provider.complete(&prompt, &GenOpts::default())?.trim().to_string()) +} + +/// `"1. …"`-style numbered prompt — models follow it far more reliably +/// than delimiter schemes. +fn numbered_prompt(sources: &[String], lang: &str) -> String { + let mut p = format!( + "Translate each numbered item to {lang}. Keep the numbering. \ + Preserve all formatting, placeholders like {{name}}, URLs and markup verbatim. \ + Reply with ONLY the numbered translations.\n\n" + ); + for (i, s) in sources.iter().enumerate() { + p.push_str(&format!("{}. {}\n", i + 1, s.replace('\n', " "))); + } + p +} + +/// Parse `"1. …\n2. …"` back into items; `None` when the reply is +/// malformed — duplicates, out-of-range numbers, or a missing item all +/// mean the numbering slipped, and the caller retries/splits rather +/// than accepting corrupted text. Non-numbered lines after an item are +/// continuations of it; non-numbered chatter before item 1 is rejected +/// the same way, so nothing is silently dropped. +fn parse_numbered(reply: &str, want: usize) -> Option> { + let mut out = vec![String::new(); want]; + let mut seen = vec![false; want]; + let mut current: Option = None; + for line in reply.lines() { + let line = line.trim(); + if line.is_empty() { + continue; + } + match numbered_line(line) { + Some(idx) if idx >= 1 && idx <= want => { + if seen[idx - 1] { + return None; // "1. un\n2. deux\n1. trois" overwrites — reject + } + seen[idx - 1] = true; + let rest = line.split_once(['.', ')']).map_or("", |(_, r)| r).trim().to_string(); + out[idx - 1] = rest; + current = Some(idx - 1); + } + Some(_) => return None, // numbered but out of range — malformed reply + // A wrapped/multi-line item: keep the continuation, not + // "1. Première phrase.\nSuite" dropping "Suite". Unnumbered + // preamble is rejected — not our format. + None => { + let i = current?; + out[i].push('\n'); + out[i].push_str(line); + } + } + } + (seen.iter().all(|s| *s) && out.iter().all(|s| !s.is_empty())).then_some(out) +} + +/// `Some(idx)` when `line` opens as `"12."` / `"12)"` — a 1-3 digit +/// number followed by `'.'` or `')'`. Anything else is `None`, so +/// "Section 2. notes" is a continuation, not item 2. +fn numbered_line(line: &str) -> Option { + let line = line.trim_start(); + let digits = line.bytes().take_while(|b| b.is_ascii_digit()).count(); + if digits == 0 || digits > 3 || !matches!(line.as_bytes().get(digits), Some(b'.' | b')')) { + return None; + } + line.get(..digits)?.parse().ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn split_words_ws_groups_runs() { + assert_eq!(split_words_ws("a b\tc"), vec!["a", " ", "b", "\t", "c"]); + } + + #[test] + fn pdf_grammar_protects() { + let g = PdfGrammar; + assert!(g.is_protected("https://x.io/a")); + assert!(g.is_protected("{count}")); + assert!(g.is_protected("42.5")); + assert!(!g.is_protected("Bonjour")); + } + + #[test] + fn segmentation_glues_whitespace_and_protects_spans() { + let segs = segment("Visit https://a.io now {n} times", &PdfGrammar); + // "Visit " text, protected url, " now " text, protected {n}, " times" text + let texts: Vec<&Segment> = segs.iter().filter(|s| matches!(s, Segment::Text(_))).collect(); + assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p.trim() == "https://a.io"))); + assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p.trim() == "{n}"))); + assert!(texts.iter().any(|s| s.source().contains("Visit"))); + } + + #[test] + fn numbered_parse_roundtrip() { + let r = "1. un\n2. deux\n3. trois"; + assert_eq!(parse_numbered(r, 3), Some(vec!["un".into(), "deux".into(), "trois".into()])); + assert_eq!(parse_numbered("1. un\n3. trois", 3), None); + } + + #[test] + fn numbered_parse_rejects_duplicates_and_overflow() { + assert_eq!(parse_numbered("1. un\n2. deux\n1. trois", 2), None); + assert_eq!(parse_numbered("1. un\n2. deux\n3. trois", 2), None); + assert_eq!(parse_numbered("0. zero\n1. un\n2. deux", 2), None); + } + + #[test] + fn numbered_parse_keeps_continuation_lines() { + let r = "1. Première phrase.\nSuite du paragraphe\n2. Deuxième élément."; + assert_eq!(parse_numbered(r, 2), Some(vec!["Première phrase.\nSuite du paragraphe".into(), "Deuxième élément.".into()])); + // Preamble chatter that isn't a numbered item is rejected, not + // silently dropped. + assert_eq!(parse_numbered("Sure, here you go:\n1. un\n2. deux", 2), None); + // "Section 2. notes" looks numbered but isn't → continuation. + let r2 = "1. un\nSection 2. notes\n2. deux"; + assert_eq!(parse_numbered(r2, 2), Some(vec!["un\nSection 2. notes".into(), "deux".into()])); + } + + #[test] + fn markup_tags_split_from_their_text() { + let segs = segment("Hello world", &PdfGrammar); + assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p == ""))); + assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p.trim() == ""))); + assert!(segs.iter().any(|s| matches!(s, Segment::Text(t) if t.contains("Hello")))); + assert!(segs.iter().any(|s| matches!(s, Segment::Text(t) if t.contains("world")))); + // Tags containing spaces stay whole and protected. + let segs = segment("link", &PdfGrammar); + assert!(segs.iter().any(|s| matches!(s, Segment::Protected(p) if p == ""))); + // A bare '<' is text, not a tag opener — words around it stay + // translatable (the lone '<' itself is protected as punctuation). + let segs = segment("a < b and <3 hearts", &PdfGrammar); + assert!(segs.iter().any(|s| matches!(s, Segment::Text(t) if t.contains("hearts")))); + assert!(!segs.iter().any(|s| matches!(s, Segment::Protected(p) if p.contains('<') && p.contains('>')))); + } + + #[test] + fn ws_edges_and_split_run_preserve_every_byte() { + assert_eq!(ws_edges(" héllo \n"), (" ", "héllo", " \n")); + assert_eq!(ws_edges(" "), (" ", "", "")); + let s = "alpha beta gamma delta epsilon"; + let pieces = split_run(s, 12); + assert!(pieces.iter().all(|p| p.len() <= 12)); + assert_eq!(pieces.concat(), s); // boundary whitespace survives + // A run with no whitespace is hard-cut at a char boundary. + let long = "x".repeat(50); + let pieces = split_run(&long, 20); + assert!(pieces.iter().all(|p| p.len() <= 20)); + assert_eq!(pieces.concat(), long); + } + + /// A provider that echoes numbered replies — exercises the batching, + /// whitespace restore and reassembly end to end without a network. + struct Echo; + + impl Provider for Echo { + fn name(&self) -> String { + "echo".into() + } + + fn complete(&mut self, prompt: &str, _opts: &GenOpts) -> Result { + if prompt.starts_with("Translate each numbered item") { + // Parse our own prompt's numbering and echo each item. + let body = prompt.split("\n\n").nth(1).unwrap_or(""); + let mut out = String::new(); + for line in body.lines() { + out.push_str(line); + out.push('\n'); + } + return Ok(out); + } + Ok(prompt.rsplit("\n\n").next().unwrap_or(prompt).to_string()) + } + + fn test(&mut self) -> Result { + Ok(crate::provider::ProviderInfo { label: "echo".into(), models: vec![] }) + } + + fn is_local(&self) -> bool { + true + } + } + + #[test] + fn numbered_parse_accepts_paren_markers() { + // "1) …" replies contain sentence-ending periods inside items — + // the leading delimiter must be picked before inspecting text. + let r = "1) Première phrase.\nSuite.\n2) Deuxième élément."; + assert_eq!(parse_numbered(r, 2), Some(vec!["Première phrase.\nSuite.".into(), "Deuxième élément.".into()])); + } + + #[test] + fn whitespace_only_pieces_survive_reconstruction() { + let segs = vec![Segment::Text(format!("{}Hello", " ".repeat(RUN_BYTES)))]; + let mut echo = Echo; + let out = translate_segments(&mut echo, &segs, "fr").unwrap_or_default(); + assert_eq!(out.concat(), format!("{}Hello", " ".repeat(RUN_BYTES))); + } + + #[test] + fn translate_restores_whitespace_around_protected_spans() { + let segs = segment("Visit https://a.io now", &PdfGrammar); + let mut echo = Echo; + let out = translate_segments(&mut echo, &segs, "fr").unwrap_or_default(); + assert_eq!(out.concat(), "Visit https://a.io now"); + } +} diff --git a/crates/atomic/src/lib.rs b/crates/atomic/src/lib.rs index b67be9885..a87a8734d 100644 --- a/crates/atomic/src/lib.rs +++ b/crates/atomic/src/lib.rs @@ -71,6 +71,9 @@ pub trait AtomicFs { fn sync_dir(&self, dir: &Path) -> io::Result<()>; /// Give the open temporary file its final permissions. fn set_permissions(&self, file: &File, perms: std::fs::Permissions) -> io::Result<()>; + /// Put the open temporary file in group `gid` (Unix only). + #[cfg(unix)] + fn set_group(&self, file: &File, gid: u32) -> io::Result<()>; } /// The production file system implementation. @@ -127,6 +130,11 @@ impl AtomicFs for RealFs { fn set_permissions(&self, file: &File, perms: std::fs::Permissions) -> io::Result<()> { file.set_permissions(perms) } + + #[cfg(unix)] + fn set_group(&self, file: &File, gid: u32) -> io::Result<()> { + std::os::unix::fs::fchown(file, None, Some(gid)) + } } /// Flush `file` to stable storage (`sync_all`). @@ -343,10 +351,29 @@ fn split(target: &Path, path: &Path) -> io::Result<(PathBuf, String)> { Ok((dir, name.to_string_lossy().into_owned())) } -/// Create a temporary file in `dir`, fill it with `fill`, give it the permissions of the file it -/// replaces (`replaced`, Unix) and flush it. Permission restoration is skipped when the mode -/// already matches after `fill`. A leftover file with the same name is skipped. When any step -/// fails, the temporary file is removed. +/// Give `perms` the replaced file's group, or drop its group bits when the stage cannot join +/// that group. A stage already in the group needs no fchown, which some filesystems reject even +/// as a no-op, and dropping the bits there would narrow a mode the replaced file granted. +#[cfg(unix)] +fn adopt_group(fs: &dyn AtomicFs, file: &File, stage_gid: u32, replaced_gid: u32, perms: std::fs::Permissions) -> io::Result { + use std::os::unix::fs::PermissionsExt; + if perms.mode() & 0o070 == 0 || stage_gid == replaced_gid { + return Ok(perms); + } + if fs.set_group(file, replaced_gid).is_ok() { + return Ok(perms); + } + let mut narrowed = perms; + narrowed.set_mode(narrowed.mode() & !0o070); + Ok(narrowed) +} + +/// Create a temporary file in `dir`, fill it with `fill`, give it the permissions and group of +/// the file it replaces (`replaced`, Unix) and flush it. Permission restoration is skipped when +/// the mode already matches after `fill`; the group bits are dropped when the file cannot be put +/// in the replaced file's group, so they never open the file to a group that was not granted +/// them. A leftover file with the same name is skipped. When any step fails, the temporary file +/// is removed. fn stage( fs: &dyn AtomicFs, dir: &Path, @@ -384,6 +411,14 @@ fn stage( if (file.metadata()?.permissions().mode() & 0o7777) == (p.mode() & 0o7777) { return Ok(()); } + // The group bits belong to the replaced file's group, which a new + // inode need not be in (setgid directory, another primary group). + if let Some(replaced) = replaced { + let stage_gid = std::os::unix::fs::MetadataExt::gid(&file.metadata()?); + let replaced_gid = std::os::unix::fs::MetadataExt::gid(replaced); + let perms = adopt_group(fs, &file, stage_gid, replaced_gid, p)?; + return fs.set_permissions(&file, perms); + } } fs.set_permissions(&file, p) }) diff --git a/crates/atomic/src/tests.rs b/crates/atomic/src/tests.rs index 56863f1f9..6edcc5430 100644 --- a/crates/atomic/src/tests.rs +++ b/crates/atomic/src/tests.rs @@ -48,6 +48,9 @@ struct Faulty { /// Simulate a network filesystem exposing the same fixed mode on every new file. #[cfg(unix)] fixed_mode: Option, + /// Simulate a filesystem that refuses the stage's group change. + #[cfg(unix)] + fail_set_group: bool, } impl AtomicFs for Faulty { @@ -92,6 +95,14 @@ impl AtomicFs for Faulty { self.events.borrow_mut().push("sync_dir"); RealFs.sync_dir(dir) } + #[cfg(unix)] + fn set_group(&self, file: &File, gid: u32) -> io::Result<()> { + self.events.borrow_mut().push("set_group"); + if self.fail_set_group { + return Err(io::Error::new(io::ErrorKind::PermissionDenied, "injected: set_group failed")); + } + RealFs.set_group(file, gid) + } /// Record permission restoration, then inject a configured error or delegate the /// mode change to the real filesystem. fn set_permissions(&self, file: &File, perms: std::fs::Permissions) -> io::Result<()> { @@ -163,6 +174,36 @@ fn unchanged_replacement_modes_need_no_chmod() -> io::Result<()> { Ok(()) } +#[cfg(unix)] +#[test] +fn group_bits_are_dropped_when_the_stage_cannot_join_the_replaced_group() -> io::Result<()> { + use std::os::unix::fs::{MetadataExt, PermissionsExt}; + let d = TempDir::new("group-fallback"); + let file = File::create(d.0.join("stage"))?; + let gid = file.metadata()?.gid(); + let elsewhere = gid.wrapping_add(1); + + let fs = Faulty { fail_set_group: true, ..Default::default() }; + let mode = adopt_group(&fs, &file, gid, elsewhere, std::fs::Permissions::from_mode(0o640))?.mode(); + assert_eq!(mode & 0o777, 0o600, "group bits are dropped when the group cannot be joined"); + assert!(fs.events.borrow().contains(&"set_group")); + + // No group bits: the group change is never attempted. + let fs = Faulty { fail_set_group: true, ..Default::default() }; + let mode = adopt_group(&fs, &file, gid, elsewhere, std::fs::Permissions::from_mode(0o600))?.mode(); + assert_eq!(mode & 0o777, 0o600); + assert!(!fs.events.borrow().contains(&"set_group")); + + // A stage already in the replaced file's group keeps its bits untouched. + let fs = Faulty { fail_set_group: true, ..Default::default() }; + let mode = adopt_group(&fs, &file, gid, gid, std::fs::Permissions::from_mode(0o640))?.mode(); + assert_eq!(mode & 0o777, 0o640); + assert!(!fs.events.borrow().contains(&"set_group")); + + assert_no_temp(&d.0); + Ok(()) +} + #[test] fn distinguishes_atomicity_and_durability_order() { let d = TempDir::new("durability_order"); diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index 849163814..734cb7f45 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -47,6 +47,10 @@ qoa = ["qoa-decode", "qoa-encode"] qoa-decode = [] qoa-encode = [] opus-encode = [] +# GSM 06.10 full-rate speech: decoder and encoder in the `gsm` module, each on its own. +gsm = ["gsm-decode", "gsm-encode"] +gsm-decode = [] +gsm-encode = [] ac3-decode = ["craft-media/bitstream"] mpa-decode = ["craft-media/bitstream"] # MPEG-1/2 Layer III (MP3) decoder (`mp3::Decoder`). @@ -59,6 +63,8 @@ ac3-encode = ["craft-media/bitstream"] eac3-encode = ["craft-media/bitstream"] # MPEG-1/2 Layer III (MP3) encoder (`mp3`). mp3-encode = ["craft-media/bitstream"] +# Bluetooth Low Complexity Subband Coding (SBC) audio codec (`sbc`). +sbc = [] # Multi-resolution waveform overviews (`peaks`). peaks = [] # Encode AAC channels in parallel (with `aac-encode`). diff --git a/crates/audio/README.md b/crates/audio/README.md index 7469a9401..10080ebbe 100644 --- a/crates/audio/README.md +++ b/crates/audio/README.md @@ -44,6 +44,7 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou | `mpa-decode` | `mpa::Decoder` | craft-media `bitstream` | | `mp3-decode` | `mp3::Decoder`, `mp3::InfoTag` | `mpa-decode` | | `mp3-encode` | `mp3::Encoder`; `encode`, `FileEncoder` for MP3 | craft-media `bitstream` | +| `gsm-decode`, `gsm-encode` (`gsm`) | `gsm::Decoder`, `gsm::Encoder`, `gsm::decode`, `gsm::encode`: GSM 06.10 full-rate speech, bit-exact, raw `.gsm` and WAV (GSM 6.10) packings | none | | `peaks` | `peaks::Peaks` | none | | `rayon` | parallel AAC encoding (with `aac-encode`) | `rayon` | | `serde` | `Serialize`/`Deserialize` on the shared types | `serde` | diff --git a/crates/audio/src/gsm.rs b/crates/audio/src/gsm.rs new file mode 100644 index 000000000..450922c29 --- /dev/null +++ b/crates/audio/src/gsm.rs @@ -0,0 +1,1038 @@ +//! GSM 06.10 full-rate speech (RPE-LTP): a decoder (feature `gsm-decode`) and an encoder (feature +//! `gsm-encode`), 8 kHz mono, 13 kbit/s. +//! +//! Written from the public ETSI specification (GSM 06.10 / 3GPP TS 46.010, clause 5: the bit-exact +//! fixed-point description of the coder and decoder, and its tables). A frame is 160 samples +//! (20 ms) coded as 76 parameters in 260 bits: eight log-area ratios for the short-term filter, +//! then four sub-frames of 40 samples, each with a long-term lag and gain, an RPE grid position, +//! a block amplitude and thirteen 3-bit pulses. +//! +//! The arithmetic is the specification's own 16-bit saturating arithmetic, so the output is bit +//! exact. Samples are `i16`; the coder uses the upper 13 bits of its input and the decoder's +//! output has its three low bits clear, as the specification says. +//! +//! Two packings of the 260 bits are supported ([`Packing`]): +//! +//! - [`Packing::Raw`]: 33 bytes per frame, the signature nibble `0xD` and then the parameters +//! most significant bit first (the packing of RFC 3551 and of `.gsm` files). +//! - [`Packing::Wav49`]: 65 bytes per pair of frames, the parameters least significant bit first +//! (the packing of the WAV format tag `0x31`, "GSM 6.10"). +//! +//! Any 260 bits are a decodable frame, so decoding never fails on content, only on length. +//! Each stream starts from the specification's initial state. + +// Index loops follow the specification's pseudo-code, which numbers the arrays the same way. +#![allow(clippy::needless_range_loop)] +// The two halves share the arithmetic, tables and synthesis helpers; with only one feature on, some +// of them are unused. +#![cfg_attr(not(all(feature = "gsm-decode", feature = "gsm-encode")), allow(dead_code))] + +use std::fmt; + +/// Samples in a frame. +pub const FRAME_SAMPLES: usize = 160; +/// Samples in a sub-frame. +pub const SUBFRAME_SAMPLES: usize = 40; +/// Bytes of a frame in the [`Packing::Raw`] packing. +pub const RAW_FRAME_BYTES: usize = 33; +/// Bytes of a pair of frames in the [`Packing::Wav49`] packing. +pub const WAV49_BLOCK_BYTES: usize = 65; +/// The sampling rate, in Hz. +pub const SAMPLE_RATE: u32 = 8000; + +/// How the 260 bits of a frame are laid out in bytes. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Packing { + /// 33 bytes per frame: `0xD` then the parameters, most significant bit first. + Raw, + /// 65 bytes per two frames: the parameters least significant bit first, with no signature. + Wav49, +} + +impl Packing { + /// Bytes of one block: a frame for [`Packing::Raw`], two frames for [`Packing::Wav49`]. + #[must_use] + pub const fn block_bytes(self) -> usize { + match self { + Self::Raw => RAW_FRAME_BYTES, + Self::Wav49 => WAV49_BLOCK_BYTES, + } + } + + /// Samples of one block. + #[must_use] + pub const fn block_samples(self) -> usize { + match self { + Self::Raw => FRAME_SAMPLES, + Self::Wav49 => 2 * FRAME_SAMPLES, + } + } +} + +/// What went wrong reading GSM data. +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Error { + /// The data is not a whole number of blocks. + Truncated { + /// The bytes in each block. + block: usize, + /// The bytes given. + len: usize, + }, + /// A raw frame does not start with the signature nibble `0xD`. + BadSignature { + /// The byte offset of the frame. + offset: usize, + }, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Truncated { block, len } => write!(f, "GSM data of {len} bytes is not a whole number of {block}-byte blocks"), + Self::BadSignature { offset } => write!(f, "GSM frame at byte {offset} does not start with the signature 0xD"), + } + } +} + +impl std::error::Error for Error {} + +// ------------------------------------------------------------------ the specification's arithmetic + +const MIN16: i32 = -32768; +const MAX16: i32 = 32767; + +fn sat16(x: i32) -> i32 { + x.clamp(MIN16, MAX16) +} +fn add(a: i32, b: i32) -> i32 { + sat16(a + b) +} +fn sub(a: i32, b: i32) -> i32 { + sat16(a - b) +} +fn mult(a: i32, b: i32) -> i32 { + if a == MIN16 && b == MIN16 { MAX16 } else { (a * b) >> 15 } +} +fn mult_r(a: i32, b: i32) -> i32 { + if a == MIN16 && b == MIN16 { MAX16 } else { (a * b + 16384) >> 15 } +} +fn abs16(a: i32) -> i32 { + if a == MIN16 { MAX16 } else { a.abs() } +} +fn l_add(a: i32, b: i32) -> i32 { + a.saturating_add(b) +} +fn l_sub(a: i32, b: i32) -> i32 { + a.saturating_sub(b) +} +fn l_mult(a: i32, b: i32) -> i32 { + let p = (i64::from(a) * i64::from(b)) << 1; + p.clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32 +} +/// A 16-bit variable assigned from a wider value: the low 16 bits, signed. +fn w16(x: i32) -> i32 { + i32::from(x as i16) +} +/// The specification's `<<`: a negative count shifts right. +fn asl(x: i32, n: i32) -> i32 { + if n >= 0 { x.wrapping_shl(n.min(31) as u32) } else { asr(x, -n) } +} +/// The specification's `>>`: a negative count shifts left. +fn asr(x: i32, n: i32) -> i32 { + if n >= 0 { x >> n.min(31) } else { asl(x, -n) } +} +/// Left shifts that normalize a 32-bit value into `[2^30, 2^31)` (or its negative counterpart). +fn norm(x: i32) -> i32 { + if x == 0 { + return 0; + } + let y = if x < 0 { !x } else { x }; + if y == 0 { 31 } else { y.leading_zeros() as i32 - 1 } +} +/// Fractional division of `num` by `denum`, `0 < num <= denum`, as the specification gives it. +fn div(num: i32, denum: i32) -> i32 { + let (mut l_num, mut q) = (num, 0); + for _ in 0..15 { + q <<= 1; + l_num = l_num.wrapping_shl(1); + if l_num >= denum { + l_num = l_num.wrapping_sub(denum); + q += 1; + } + } + q +} + +// ------------------------------------------------------------------------------ the tables + +/// Table 5.1: `A`, `B`, `MIC`, `MAC` of the log-area ratio quantizers. +const A: [i32; 8] = [20480, 20480, 20480, 20480, 13964, 15360, 8534, 9036]; +const B: [i32; 8] = [0, 0, 2048, -2560, 94, -1792, -341, -1144]; +const MIC: [i32; 8] = [-32, -32, -16, -16, -8, -8, -4, -4]; +const MAC: [i32; 8] = [31, 31, 15, 15, 7, 7, 3, 3]; +/// Table 5.2: `1 / A`. +const INVA: [i32; 8] = [13107, 13107, 13107, 13107, 19223, 17476, 31454, 29708]; +/// Table 5.3a: decision levels of the long-term gain quantizer. +const DLB: [i32; 4] = [6554, 16384, 26214, 32767]; +/// Table 5.3b: its quantization levels. +const QLB: [i32; 4] = [3277, 11469, 21299, 32767]; +/// Table 5.4: the weighting filter. +const H: [i32; 11] = [-134, -374, 0, 2054, 5741, 8192, 5741, 2054, 0, -374, -134]; +/// Table 5.5: normalized inverse mantissa. +const NRFAC: [i32; 8] = [29128, 26215, 23832, 21846, 20165, 18725, 17476, 16384]; +/// Table 5.6: normalized direct mantissa. +const FAC: [i32; 8] = [18431, 20479, 22527, 24575, 26623, 28671, 30719, 32767]; + +// -------------------------------------------------------------------------- the parameters + +/// The parameters of one 40-sample sub-frame. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct SubFrame { + /// Long-term lag, 7 bits (40 to 120 from a coder). + pub lag: u8, + /// Long-term gain index, 2 bits. + pub gain: u8, + /// RPE grid position, 2 bits. + pub grid: u8, + /// Block amplitude, 6 bits. + pub xmax: u8, + /// The 13 RPE pulses, 3 bits each. + pub pulses: [u8; 13], +} + +/// The 76 parameters of one frame. A decoder uses only the low bits each field is allowed. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct Frame { + /// The eight log-area ratios, 6, 6, 5, 5, 4, 4, 3 and 3 bits, offset to be non-negative. + pub lar: [u8; 8], + /// The four sub-frames. + pub sub: [SubFrame; 4], +} + +const LAR_BITS: [u32; 8] = [6, 6, 5, 5, 4, 4, 3, 3]; +const SUB_FIELDS: usize = 4 + 13; +const PARAMS: usize = 8 + 4 * SUB_FIELDS; + +fn field_bits(i: usize) -> u32 { + if i < 8 { + return LAR_BITS.get(i).copied().unwrap_or(0); + } + match (i - 8) % SUB_FIELDS { + 0 => 7, + 1 | 2 => 2, + 3 => 6, + _ => 3, + } +} + +impl Frame { + fn values(&self) -> [u8; PARAMS] { + let mut v = [0u8; PARAMS]; + v[..8].copy_from_slice(&self.lar); + for (j, s) in self.sub.iter().enumerate() { + let at = 8 + j * SUB_FIELDS; + v[at] = s.lag; + v[at + 1] = s.gain; + v[at + 2] = s.grid; + v[at + 3] = s.xmax; + v[at + 4..at + SUB_FIELDS].copy_from_slice(&s.pulses); + } + v + } + + fn from_values(v: &[u8; PARAMS]) -> Self { + let mut f = Self::default(); + f.lar.copy_from_slice(&v[..8]); + for (j, s) in f.sub.iter_mut().enumerate() { + let at = 8 + j * SUB_FIELDS; + s.lag = v[at]; + s.gain = v[at + 1]; + s.grid = v[at + 2]; + s.xmax = v[at + 3]; + s.pulses.copy_from_slice(&v[at + 4..at + SUB_FIELDS]); + } + f + } + + /// The frame in the [`Packing::Raw`] layout. + #[must_use] + pub fn to_raw(&self) -> [u8; RAW_FRAME_BYTES] { + let mut out = [0u8; RAW_FRAME_BYTES]; + out[0] = 0xD0; + let mut pos = 4usize; + for (i, &v) in self.values().iter().enumerate() { + let w = field_bits(i); + for b in (0..w).rev() { + if (u32::from(v) >> b) & 1 == 1 { + out[pos / 8] |= 0x80 >> (pos % 8); + } + pos += 1; + } + } + out + } + + /// A frame from the [`Packing::Raw`] layout; fails when the signature nibble is not `0xD`. + pub fn from_raw(bytes: &[u8; RAW_FRAME_BYTES]) -> Result { + if bytes[0] >> 4 != 0xD { + return Err(Error::BadSignature { offset: 0 }); + } + let mut v = [0u8; PARAMS]; + let mut pos = 4usize; + for (i, slot) in v.iter_mut().enumerate() { + let mut x = 0u8; + for _ in 0..field_bits(i) { + x = (x << 1) | ((bytes[pos / 8] >> (7 - pos % 8)) & 1); + pos += 1; + } + *slot = x; + } + Ok(Self::from_values(&v)) + } + + /// Two frames in the [`Packing::Wav49`] layout. + #[must_use] + pub fn pair_to_wav49(a: &Self, b: &Self) -> [u8; WAV49_BLOCK_BYTES] { + let mut out = [0u8; WAV49_BLOCK_BYTES]; + let mut pos = 0usize; + for f in [a, b] { + for (i, &v) in f.values().iter().enumerate() { + for bit in 0..field_bits(i) { + if (u32::from(v) >> bit) & 1 == 1 { + out[pos / 8] |= 1 << (pos % 8); + } + pos += 1; + } + } + } + out + } + + /// Two frames from the [`Packing::Wav49`] layout. + #[must_use] + pub fn pair_from_wav49(bytes: &[u8; WAV49_BLOCK_BYTES]) -> [Self; 2] { + let mut pos = 0usize; + let mut frames = [Self::default(); 2]; + for f in &mut frames { + let mut v = [0u8; PARAMS]; + for (i, slot) in v.iter_mut().enumerate() { + let mut x = 0u8; + for bit in 0..field_bits(i) { + x |= ((bytes[pos / 8] >> (pos % 8)) & 1) << bit; + pos += 1; + } + *slot = x; + } + *f = Self::from_values(&v); + } + frames + } +} + +// ------------------------------------------------------------------- shared synthesis pieces + +/// Clause 5.2.8: the decoded log-area ratios `LARpp` of a frame's coded ones. +fn decode_lar(lar: &[u8; 8]) -> [i32; 8] { + let mut out = [0i32; 8]; + for i in 0..8 { + let c = i32::from(lar[i] & ((1u8 << LAR_BITS[i]) - 1)); + let t1 = w16(add(c, MIC[i]) << 10); + let t2 = w16(B[i] << 1); + let t1 = sub(t1, t2); + let t1 = mult_r(INVA[i], t1); + out[i] = add(t1, t1); + } + out +} + +/// Clause 5.2.9.1: the interpolated log-area ratios of segment `seg` (0 to 3). +fn interpolate(prev: &[i32; 8], cur: &[i32; 8], seg: usize) -> [i32; 8] { + let mut out = [0i32; 8]; + for i in 0..8 { + out[i] = match seg { + 0 => add(add(prev[i] >> 2, cur[i] >> 2), prev[i] >> 1), + 1 => add(prev[i] >> 1, cur[i] >> 1), + 2 => add(add(prev[i] >> 2, cur[i] >> 2), cur[i] >> 1), + _ => cur[i], + }; + } + out +} + +/// Clause 5.2.9.2: reflection coefficients from log-area ratios. +fn reflection(larp: &[i32; 8]) -> [i32; 8] { + let mut rp = [0i32; 8]; + for i in 0..8 { + let a = abs16(larp[i]); + let t = if a < 11059 { + w16(a << 1) + } else if a < 20070 { + add(a, 11059) + } else { + add(a >> 2, 26112) + }; + rp[i] = if larp[i] < 0 { sub(0, t) } else { t }; + } + rp +} + +/// The sample ranges the four sets of interpolated coefficients cover. +const SEGMENTS: [(usize, usize); 4] = [(0, 12), (13, 26), (27, 39), (40, 159)]; + +/// The exponent and mantissa of a block amplitude (clause 5.2.15). +fn exp_mant(xmaxc: i32) -> (i32, i32) { + let mut exp = 0; + if xmaxc > 15 { + exp = (xmaxc >> 3) - 1; + } + let mut mant = xmaxc - (exp << 3); + if mant == 0 { + exp = -4; + mant = 15; + } else { + let mut itest = false; + for _ in 0..3 { + if mant > 7 { + itest = true; + } + if !itest { + mant = (mant << 1) + 1; + exp -= 1; + } + } + } + (exp, mant - 8) +} + +/// Clauses 5.2.16 and 5.2.17: the 40 samples of the long-term residual from a sub-frame's RPE data. +fn rpe_decode(s: &SubFrame) -> [i32; SUBFRAME_SAMPLES] { + let (exp, mant) = exp_mant(i32::from(s.xmax & 0x3F)); + let temp1 = FAC[mant as usize & 7]; + let temp2 = 6 - exp; + let temp3 = asl(1, temp2 - 1); + let mut ep = [0i32; SUBFRAME_SAMPLES]; + let grid = usize::from(s.grid & 3); + for (i, &p) in s.pulses.iter().enumerate() { + let t = sub(i32::from(p & 7) << 1, 7); + let t = w16(t << 12); + let t = mult_r(temp1, t); + let t = add(t, temp3); + let at = grid + 3 * i; + if at < SUBFRAME_SAMPLES { + ep[at] = asr(t, temp2); + } + } + ep +} + +/// The long-term lag, gain and reconstructed-residual history shared by coder and decoder. +#[derive(Clone)] +struct Synth { + /// The reconstructed short-term residual: `dp[-120..-1]`, oldest first. + dp: [i32; 120], + nrp: i32, + /// The short-term synthesis (or analysis) lattice state. + v: [i32; 9], + prev_lar: [i32; 8], +} + +impl Synth { + fn new() -> Self { + Self { dp: [0; 120], nrp: 40, v: [0; 9], prev_lar: [0; 8] } + } + + /// Clauses 5.3.1 and 5.3.2 for one sub-frame: the reconstructed residual, which is also pushed + /// into the history. + fn long_term(&mut self, s: &SubFrame) -> [i32; SUBFRAME_SAMPLES] { + let erp = rpe_decode(s); + let mut nr = i32::from(s.lag & 0x7F); + if !(40..=120).contains(&nr) { + nr = self.nrp; + } + self.nrp = nr; + let brp = QLB[usize::from(s.gain & 3)]; + let mut drp = [0i32; SUBFRAME_SAMPLES]; + for k in 0..SUBFRAME_SAMPLES { + // dp[k - nr] with k - nr in -120..=-1 is the history; later samples are this sub-frame's own. + let idx = 120 + k as i32 - nr; + let past = if idx < 120 { self.dp[idx as usize] } else { drp[(idx - 120) as usize] }; + drp[k] = add(erp[k], mult_r(brp, past)); + } + self.dp.copy_within(40.., 0); + self.dp[80..].copy_from_slice(&drp); + drp + } +} + +// ------------------------------------------------------------------------------ the decoder + +#[cfg(feature = "gsm-decode")] +/// A GSM 06.10 decoder. It keeps the filter and long-term state between frames. +#[derive(Clone)] +pub struct Decoder { + synth: Synth, + msr: i32, +} + +#[cfg(feature = "gsm-decode")] +impl Default for Decoder { + fn default() -> Self { + Self::new() + } +} + +#[cfg(feature = "gsm-decode")] +impl Decoder { + /// A decoder in the specification's initial state. + #[must_use] + pub fn new() -> Self { + Self { synth: Synth::new(), msr: 0 } + } + + /// Decode one frame into 160 samples (clause 5.3). + pub fn decode_frame(&mut self, frame: &Frame) -> [i16; FRAME_SAMPLES] { + let cur = decode_lar(&frame.lar); + let mut wt = [0i32; FRAME_SAMPLES]; + for (j, s) in frame.sub.iter().enumerate() { + let drp = self.synth.long_term(s); + wt[j * SUBFRAME_SAMPLES..(j + 1) * SUBFRAME_SAMPLES].copy_from_slice(&drp); + } + let mut sr = [0i32; FRAME_SAMPLES]; + for (seg, &(k0, k1)) in SEGMENTS.iter().enumerate() { + let rrp = reflection(&interpolate(&self.synth.prev_lar, &cur, seg)); + for k in k0..=k1 { + let mut sri = wt[k]; + for i in 1..=8 { + sri = sub(sri, mult_r(rrp[8 - i], self.synth.v[8 - i])); + self.synth.v[9 - i] = add(self.synth.v[8 - i], mult_r(rrp[8 - i], sri)); + } + sr[k] = sri; + self.synth.v[0] = sri; + } + } + self.synth.prev_lar = cur; + let mut out = [0i16; FRAME_SAMPLES]; + for k in 0..FRAME_SAMPLES { + self.msr = add(sr[k], mult_r(self.msr, 28180)); + let up = add(self.msr, self.msr); + out[k] = ((up >> 3) << 3) as i16; + } + out + } +} + +// ------------------------------------------------------------------------------ the encoder + +#[cfg(feature = "gsm-encode")] +/// A GSM 06.10 encoder. It keeps the filter and long-term state between frames. +#[derive(Clone)] +pub struct Encoder { + synth: Synth, + z1: i32, + l_z2: i32, + mp: i32, + /// The short-term analysis filter state. + u: [i32; 8], +} + +#[cfg(feature = "gsm-encode")] +impl Default for Encoder { + fn default() -> Self { + Self::new() + } +} + +#[cfg(feature = "gsm-encode")] +impl Encoder { + /// An encoder in the specification's initial state. + #[must_use] + pub fn new() -> Self { + Self { synth: Synth::new(), z1: 0, l_z2: 0, mp: 0, u: [0; 8] } + } + + /// Clauses 5.2.0 to 5.2.3: scaling, offset compensation and pre-emphasis. + fn preprocess(&mut self, input: &[i16; FRAME_SAMPLES]) -> [i32; FRAME_SAMPLES] { + let mut s = [0i32; FRAME_SAMPLES]; + for k in 0..FRAME_SAMPLES { + let so = (i32::from(input[k]) >> 3) << 2; + let s1 = sub(so, self.z1); + self.z1 = so; + let mut l_s2 = s1 << 15; + let msp = self.l_z2 >> 15; + let lsp = l_sub(self.l_z2, msp << 15); + let temp = mult_r(lsp, 32735); + l_s2 = l_add(l_s2, temp); + self.l_z2 = l_add(l_mult(msp, 32735) >> 1, l_s2); + let sof = w16(l_add(self.l_z2, 16384) >> 15); + s[k] = add(sof, mult_r(self.mp, -28180)); + self.mp = sof; + } + s + } + + /// Clauses 5.2.4 to 5.2.7: the coded log-area ratios of the frame. + fn analyse(s: &[i32; FRAME_SAMPLES]) -> [u8; 8] { + // Autocorrelation, with the signal scaled so the sums cannot overflow. + let smax = s.iter().map(|&x| abs16(x)).max().unwrap_or(0); + let scalauto = if smax == 0 { 0 } else { 4 - norm(smax << 16) }; + let mut sc = *s; + if scalauto > 0 { + let temp = 16384 >> (scalauto - 1); + for x in &mut sc { + *x = mult_r(*x, temp); + } + } + let mut l_acf = [0i32; 9]; + for (k, acf) in l_acf.iter_mut().enumerate() { + for i in k..FRAME_SAMPLES { + *acf = l_add(*acf, l_mult(sc[i], sc[i - k])); + } + } + // Schur recursion for the reflection coefficients. + let mut r = [0i32; 9]; + if l_acf[0] != 0 { + let t = norm(l_acf[0]); + let mut acf = [0i32; 9]; + for k in 0..9 { + acf[k] = w16(l_acf[k].wrapping_shl(t as u32) >> 16); + } + let mut kk = [0i32; 9]; + for i in 1..=7 { + kk[9 - i] = acf[i]; + } + let mut p = acf; + for n in 1..=8 { + if p[0] < abs16(p[1]) { + break; + } + r[n] = div(abs16(p[1]), p[0]); + if p[1] > 0 { + r[n] = sub(0, r[n]); + } + if n == 8 { + break; + } + p[0] = add(p[0], mult_r(p[1], r[n])); + for m in 1..=8 - n { + p[m] = add(p[m + 1], mult_r(kk[9 - m], r[n])); + kk[9 - m] = add(kk[9 - m], mult_r(p[m + 1], r[n])); + } + } + } + // Log-area ratios, quantized and offset to be non-negative. + let mut lar = [0u8; 8]; + for i in 0..8 { + let mut t = abs16(r[i + 1]); + t = if t < 22118 { + t >> 1 + } else if t < 31130 { + sub(t, 11059) + } else { + w16(sub(t, 26112) << 2) + }; + let l = if r[i + 1] < 0 { sub(0, t) } else { t }; + let mut c = add(add(mult(A[i], l), B[i]), 256) >> 9; + c = c.clamp(MIC[i], MAC[i]); + lar[i] = (c - MIC[i]) as u8; + } + lar + } + + /// Encode 160 samples into one frame (clause 5.2). + pub fn encode_frame(&mut self, input: &[i16; FRAME_SAMPLES]) -> Frame { + let s = self.preprocess(input); + let mut frame = Frame { lar: Self::analyse(&s), ..Frame::default() }; + + // Short-term analysis filtering with the quantized coefficients. + let cur = decode_lar(&frame.lar); + let mut d = [0i32; FRAME_SAMPLES]; + for (seg, &(k0, k1)) in SEGMENTS.iter().enumerate() { + let rp = reflection(&interpolate(&self.synth.prev_lar, &cur, seg)); + for k in k0..=k1 { + let mut di = s[k]; + let mut sav = di; + for i in 0..8 { + let temp = add(self.u[i], mult_r(rp[i], di)); + di = add(di, mult_r(rp[i], self.u[i])); + self.u[i] = sav; + sav = temp; + } + d[k] = di; + } + } + self.synth.prev_lar = cur; + + for (j, sub_frame) in frame.sub.iter_mut().enumerate() { + let mut dj = [0i32; SUBFRAME_SAMPLES]; + dj.copy_from_slice(&d[j * SUBFRAME_SAMPLES..(j + 1) * SUBFRAME_SAMPLES]); + *sub_frame = self.code_subframe(&dj); + } + frame + } + + /// Clauses 5.2.11 to 5.2.18 for one sub-frame. + fn code_subframe(&mut self, d: &[i32; SUBFRAME_SAMPLES]) -> SubFrame { + let dp = self.synth.dp; + let hist = |k: usize, lambda: i32| dp[(120 + k as i32 - lambda) as usize]; + + // Long-term lag: the maximum cross-correlation with the reconstructed residual. + let dmax = d.iter().map(|&x| abs16(x)).max().unwrap_or(0); + let mut temp = 0; + let mut scal = 0; + if dmax != 0 { + temp = norm(dmax << 16); + } + if temp <= 6 { + scal = 6 - temp; + } + let mut wt = [0i32; SUBFRAME_SAMPLES]; + for k in 0..SUBFRAME_SAMPLES { + wt[k] = d[k] >> scal; + } + let mut l_max = 0i32; + let mut nc = 40i32; + for lambda in 40..=120 { + let mut l_result = 0i32; + for k in 0..SUBFRAME_SAMPLES { + l_result = l_add(l_result, l_mult(wt[k], hist(k, lambda))); + } + if l_result > l_max { + nc = lambda; + l_max = l_result; + } + } + l_max >>= 6 - scal; + + // Long-term gain. + for k in 0..SUBFRAME_SAMPLES { + wt[k] = hist(k, nc) >> 3; + } + let mut l_power = 0i32; + for k in 0..SUBFRAME_SAMPLES { + l_power = l_add(l_power, l_mult(wt[k], wt[k])); + } + let bc = if l_max <= 0 { + 0 + } else if l_max >= l_power { + 3 + } else { + let t = norm(l_power); + let r = w16(l_max.wrapping_shl(t as u32) >> 16); + let s = w16(l_power.wrapping_shl(t as u32) >> 16); + (0..3).find(|&b| r <= mult(s, DLB[b])).unwrap_or(3) + }; + + // Long-term analysis filtering. + let bp = QLB[bc]; + let mut dpp = [0i32; SUBFRAME_SAMPLES]; + let mut e = [0i32; SUBFRAME_SAMPLES]; + for k in 0..SUBFRAME_SAMPLES { + dpp[k] = mult_r(bp, hist(k, nc)); + e[k] = sub(d[k], dpp[k]); + } + + // Weighting filter, RPE grid selection. + let mut w = [0i32; 50]; + w[5..45].copy_from_slice(&e); + let mut x = [0i32; SUBFRAME_SAMPLES]; + for k in 0..SUBFRAME_SAMPLES { + let mut l_result = 8192i32; + for i in 0..11 { + l_result = l_add(l_result, l_mult(w[k + i], H[i])); + } + l_result = l_add(l_result, l_result); + l_result = l_add(l_result, l_result); + x[k] = l_result >> 16; + } + let mut em = 0i32; + let mut mc = 0usize; + for m in 0..4 { + let mut l_result = 0i32; + for i in 0..13 { + let t = x[m + 3 * i] >> 2; + l_result = l_add(l_result, l_mult(t, t)); + } + if l_result > em { + mc = m; + em = l_result; + } + } + let mut xm = [0i32; 13]; + for i in 0..13 { + xm[i] = x[mc + 3 * i]; + } + + // APCM quantization. + let xmax = xm.iter().map(|&v| abs16(v)).max().unwrap_or(0); + let mut exp = 0; + let mut t = xmax >> 9; + let mut itest = false; + for _ in 0..=5 { + if t <= 0 { + itest = true; + } + t >>= 1; + if !itest { + exp += 1; + } + } + let xmaxc = add(xmax >> (exp + 5), exp << 3); + let (exp, mant) = exp_mant(xmaxc); + let temp1 = 6 - exp; + let temp2 = NRFAC[mant as usize & 7]; + let mut pulses = [0u8; 13]; + for i in 0..13 { + let t = w16(asl(xm[i], temp1)); + let t = mult(t, temp2); + pulses[i] = (add(t >> 12, 4)).clamp(0, 7) as u8; + } + let coded = SubFrame { lag: nc as u8, gain: bc as u8, grid: mc as u8, xmax: xmaxc.clamp(0, 63) as u8, pulses }; + + // Reconstruct, exactly as the decoder will, to keep the history in step. + let ep = rpe_decode(&coded); + self.synth.dp.copy_within(40.., 0); + for k in 0..SUBFRAME_SAMPLES { + self.synth.dp[80 + k] = add(ep[k], dpp[k]); + } + coded + } +} + +// --------------------------------------------------------------------------------- streams + +#[cfg(feature = "gsm-decode")] +/// Decode a stream of whole blocks into 8 kHz samples. A [`Packing::Raw`] stream is checked for the +/// signature nibble of every frame. +pub fn decode(data: &[u8], packing: Packing) -> Result, Error> { + let block = packing.block_bytes(); + if !data.len().is_multiple_of(block) { + return Err(Error::Truncated { block, len: data.len() }); + } + let mut dec = Decoder::new(); + let mut out = Vec::with_capacity(data.len() / block * packing.block_samples()); + for (n, chunk) in data.chunks_exact(block).enumerate() { + match packing { + Packing::Raw => { + let mut b = [0u8; RAW_FRAME_BYTES]; + b.copy_from_slice(chunk); + let frame = Frame::from_raw(&b).map_err(|_| Error::BadSignature { offset: n * block })?; + out.extend_from_slice(&dec.decode_frame(&frame)); + } + Packing::Wav49 => { + let mut b = [0u8; WAV49_BLOCK_BYTES]; + b.copy_from_slice(chunk); + for frame in Frame::pair_from_wav49(&b) { + out.extend_from_slice(&dec.decode_frame(&frame)); + } + } + } + } + Ok(out) +} + +#[cfg(feature = "gsm-encode")] +/// Encode 8 kHz samples. The last block is padded with silence to a whole block, so the output +/// decodes to `samples.len()` rounded up to a block, with the first `samples.len()` as the signal. +#[must_use] +pub fn encode(samples: &[i16], packing: Packing) -> Vec { + let per_block = packing.block_samples(); + let mut enc = Encoder::new(); + let mut out = Vec::with_capacity(samples.len().div_ceil(per_block) * packing.block_bytes()); + for chunk in samples.chunks(per_block) { + let mut padded = [0i16; 2 * FRAME_SAMPLES]; + padded[..chunk.len()].copy_from_slice(chunk); + let mut frame = |at: usize| { + let mut input = [0i16; FRAME_SAMPLES]; + input.copy_from_slice(&padded[at..at + FRAME_SAMPLES]); + enc.encode_frame(&input) + }; + match packing { + Packing::Raw => out.extend_from_slice(&frame(0).to_raw()), + Packing::Wav49 => { + let a = frame(0); + let b = frame(FRAME_SAMPLES); + out.extend_from_slice(&Frame::pair_to_wav49(&a, &b)); + } + } + } + out +} + +#[cfg(all(test, feature = "gsm-decode", feature = "gsm-encode"))] +mod tests { + use super::*; + + fn xorshift(seed: &mut u32) -> u32 { + *seed ^= *seed << 13; + *seed ^= *seed >> 17; + *seed ^= *seed << 5; + *seed + } + + fn speechlike(n: usize) -> Vec { + let mut seed = 0x1234_5678u32; + (0..n) + .map(|i| { + let t = i as f64 / 8000.0; + let f0 = 120.0 + 30.0 * (t * 3.0).sin(); + let v = (t * f0 * std::f64::consts::TAU).sin() * 6000.0 + + (t * 3.0 * f0 * std::f64::consts::TAU).sin() * 2500.0 + + f64::from(xorshift(&mut seed) % 400) + - 200.0; + (v * (0.6 + 0.4 * (t * 5.0).sin())) as i16 + }) + .collect() + } + + #[test] + fn arithmetic_matches_the_specification() { + assert_eq!(add(32767, 1), 32767); + assert_eq!(sub(-32768, 1), -32768); + assert_eq!(mult(-32768, -32768), 32767); + assert_eq!(mult_r(-32768, -32768), 32767); + assert_eq!(mult_r(16384, 16384), 8192); + assert_eq!(abs16(-32768), 32767); + assert_eq!(div(5, 5), 32767); + assert_eq!(div(1, 2), 16384); + assert_eq!(norm(1), 30); + assert_eq!(norm(0x4000_0000), 0); + assert_eq!(norm(-1), 31); + assert_eq!(norm(i32::MIN), 0); + assert_eq!(asl(1, -1), 0); + assert_eq!(asr(-8, 1), -4); + assert_eq!(l_mult(-32768, -32768), i32::MAX); + } + + #[test] + fn raw_packing_round_trips_every_field() { + let mut seed = 99u32; + for _ in 0..200 { + let mut v = [0u8; PARAMS]; + for (i, x) in v.iter_mut().enumerate() { + *x = (xorshift(&mut seed) & ((1 << field_bits(i)) - 1)) as u8; + } + let frame = Frame::from_values(&v); + let raw = frame.to_raw(); + assert_eq!(raw[0] >> 4, 0xD); + assert_eq!(Frame::from_raw(&raw), Ok(frame)); + let pair = Frame::pair_to_wav49(&frame, &Frame::default()); + assert_eq!(Frame::pair_from_wav49(&pair), [frame, Frame::default()]); + let pair = Frame::pair_to_wav49(&Frame::default(), &frame); + assert_eq!(Frame::pair_from_wav49(&pair)[1], frame); + } + assert_eq!((0..PARAMS).map(field_bits).sum::(), 260); + } + + #[test] + fn a_raw_frame_needs_its_signature() { + let mut raw = Frame::default().to_raw(); + raw[0] = 0x30; + assert_eq!(Frame::from_raw(&raw), Err(Error::BadSignature { offset: 0 })); + assert_eq!(decode(&raw, Packing::Raw), Err(Error::BadSignature { offset: 0 })); + } + + #[test] + fn partial_blocks_are_rejected() { + assert_eq!(decode(&[0xD0; 32], Packing::Raw), Err(Error::Truncated { block: 33, len: 32 })); + assert_eq!(decode(&[0; 66], Packing::Wav49), Err(Error::Truncated { block: 65, len: 66 })); + assert_eq!(decode(&[], Packing::Raw), Ok(vec![])); + } + + #[test] + fn silence_stays_near_silence() { + let pcm = vec![0i16; 160 * 4]; + for packing in [Packing::Raw, Packing::Wav49] { + let bytes = encode(&pcm, packing); + let out = decode(&bytes, packing).expect("decode"); + assert_eq!(out.len(), pcm.len()); + // The quantizers have no exact zero, so silence comes back as a faint residue. + assert!(out.iter().all(|&s| s.abs() <= 32), "{packing:?}: {:?}", out.iter().map(|s| s.abs()).max()); + } + } + + #[test] + fn output_has_three_clear_low_bits() { + let out = decode(&encode(&speechlike(1600), Packing::Raw), Packing::Raw).expect("decode"); + assert!(out.iter().all(|&s| s & 7 == 0)); + } + + #[test] + fn speech_survives_the_round_trip() { + let pcm = speechlike(8000); + for packing in [Packing::Raw, Packing::Wav49] { + let out = decode(&encode(&pcm, packing), packing).expect("decode"); + assert_eq!(out.len(), pcm.len()); + let (mut sig, mut err) = (0f64, 0f64); + for i in 400..pcm.len() { + let (a, b) = (f64::from(pcm[i]), f64::from(out[i])); + sig += a * a; + err += (a - b) * (a - b); + } + let snr = 10.0 * (sig / err).log10(); + assert!(snr > 8.0, "{packing:?}: SNR {snr:.1} dB"); + } + } + + #[test] + fn encoding_is_deterministic_and_packings_agree() { + let pcm = speechlike(1600); + assert_eq!(encode(&pcm, Packing::Raw), encode(&pcm, Packing::Raw)); + let raw = decode(&encode(&pcm, Packing::Raw), Packing::Raw).expect("raw"); + let wav = decode(&encode(&pcm, Packing::Wav49), Packing::Wav49).expect("wav49"); + assert_eq!(raw, wav); + } + + #[test] + fn short_input_is_padded_to_a_block() { + assert_eq!(encode(&[], Packing::Raw), Vec::::new()); + assert_eq!(encode(&[100; 1], Packing::Raw).len(), 33); + assert_eq!(encode(&[100; 161], Packing::Raw).len(), 66); + assert_eq!(encode(&[100; 1], Packing::Wav49).len(), 65); + assert_eq!(encode(&[100; 321], Packing::Wav49).len(), 130); + } + + #[test] + fn any_bits_decode_without_panicking() { + let mut seed = 7u32; + for _ in 0..300 { + let mut v = [0u8; PARAMS]; + for x in &mut v { + // Fields wider than their width and lags outside 40..=120 must be tolerated. + *x = xorshift(&mut seed) as u8; + } + let frame = Frame::from_values(&v); + let mut dec = Decoder::new(); + for _ in 0..3 { + let _ = dec.decode_frame(&frame); + } + } + } + + #[test] + fn extreme_input_encodes_without_panicking() { + for pattern in [i16::MAX, i16::MIN] { + let pcm = vec![pattern; 1600]; + let out = decode(&encode(&pcm, Packing::Raw), Packing::Raw).expect("decode"); + assert_eq!(out.len(), 1600); + } + let alternating: Vec = (0..3200).map(|i| if i % 2 == 0 { i16::MAX } else { i16::MIN }).collect(); + let _ = encode(&alternating, Packing::Wav49); + let mut seed = 5u32; + let noise: Vec = (0..3200).map(|_| xorshift(&mut seed) as i16).collect(); + let _ = encode(&noise, Packing::Raw); + } + + #[test] + fn a_decoder_keeps_state_between_frames() { + let bytes = encode(&speechlike(1600), Packing::Raw); + let whole = decode(&bytes, Packing::Raw).expect("whole"); + let mut dec = Decoder::new(); + let mut piecewise = Vec::new(); + for b in bytes.as_chunks::<33>().0 { + piecewise.extend_from_slice(&dec.decode_frame(&Frame::from_raw(b).expect("frame"))); + } + assert_eq!(whole, piecewise); + } +} diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 2766be64d..e5e859d3c 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -30,7 +30,8 @@ //! `aac-encode`), [`alac`] (`alac-decode`, `alac-encode`), [`opus`] (`opus-decode`, and //! [`opus::encoder`] with `opus-encode`), [`ac3`] (`ac3-decode`), [`mpa`] (`mpa-decode`), [`mp3`] //! (`mp3-decode`, `mp3-encode`), [`flac`] (`flac-decode`: the frame and file decoder; -//! `flac-encode`: the packet encoder), [`qoa`] (`qoa-decode`, `qoa-encode`: whole QOA files). +//! `flac-encode`: the packet encoder), [`qoa`] (`qoa-decode`, `qoa-encode`: whole QOA files), +//! [`gsm`] (`gsm-decode`, `gsm-encode`: GSM 06.10 full-rate speech). //! //! The shared types below build with no features. @@ -41,6 +42,8 @@ pub mod ac3; #[cfg(any(feature = "alac-decode", feature = "alac-encode"))] pub mod alac; mod buffer; +#[cfg(any(feature = "gsm-decode", feature = "gsm-encode"))] +pub mod gsm; mod layout; #[cfg(any(feature = "mp3-encode", feature = "mp3-decode"))] pub mod mp3; @@ -56,6 +59,8 @@ pub mod peaks; pub mod qoa; #[cfg(feature = "decode-native")] mod read; +#[cfg(feature = "sbc")] +pub mod sbc; #[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))] mod track; #[cfg(feature = "vorbis-encode")] @@ -782,3 +787,7 @@ mod tests { /// DVD and Blu-ray LPCM packet decoding. pub mod lpcm; + +/// Non-seeking canonical WAV streaming primitives. +#[cfg(feature = "pcm-encode")] +pub mod wav_stream; diff --git a/crates/audio/src/sbc.rs b/crates/audio/src/sbc.rs new file mode 100644 index 000000000..35165e966 --- /dev/null +++ b/crates/audio/src/sbc.rs @@ -0,0 +1,1999 @@ +//! Bluetooth Low Complexity Subband Coding (SBC) audio codec. +//! +//! Clean-room implementation based on Bluetooth SIG Advanced Audio Distribution Profile +//! (A2DP) Specification v1.3, Appendix B (Section 12: "Technical Specification of SBC") +//! and Hands-Free Profile (HFP 1.6) mSBC specification. +//! +//! SBC is a subband codec designed to deliver audio quality with low computational +//! complexity, operating at 16 kHz, 32 kHz, 44.1 kHz, or 48 kHz in Mono, Dual Channel, +//! Stereo, or Joint Stereo configurations with 4 or 8 subbands. + +#![allow(clippy::excessive_precision, clippy::needless_range_loop, clippy::cognitive_complexity, clippy::too_many_arguments)] + +const MAX_INPUT_BYTES: usize = 64 * 1024 * 1024; +const MAX_SAMPLES: usize = 64 * 1024 * 1024; + +/// Standard SBC syncword byte (`0x9C`). +pub const SYNCWORD_SBC: u8 = 0x9C; +/// mSBC wideband speech syncword byte (`0xAD`). +pub const SYNCWORD_MSBC: u8 = 0xAD; + +/// Offset table for 4 subbands loudness bit allocation (Table 12.21). +pub static OFFSET4: [[i8; 4]; 4] = [[-1, 0, 0, 0], [-2, 0, 0, 1], [-2, 0, 0, 1], [-2, 0, 0, 1]]; + +/// Offset table for 8 subbands loudness bit allocation (Table 12.22). +pub static OFFSET8: [[i8; 8]; 4] = [[-2, 0, 0, 0, 0, 0, 0, 1], [-3, 0, 0, 0, 0, 0, 1, 2], [-4, 0, 0, 0, 0, 0, 1, 2], [-4, 0, 0, 0, 0, 0, 1, 2]]; + +/// Analysis and synthesis filter coefficients for 4 subbands (Table 12.23). +pub(crate) static PROTO_4_40: [f32; 40] = [ + 0.0000000000e+00_f32, + 5.3654897600e-04_f32, + 1.4918835700e-03_f32, + 2.7337090400e-03_f32, + 3.8372019300e-03_f32, + 3.8920514900e-03_f32, + 1.8658169100e-03_f32, + -3.0601228600e-03_f32, + 1.0913762000e-02_f32, + 2.0438508700e-02_f32, + 2.8875739200e-02_f32, + 3.2193929000e-02_f32, + 2.5876781100e-02_f32, + 6.1324518600e-03_f32, + -2.8821727400e-02_f32, + -7.7646349400e-02_f32, + 1.3559327400e-01_f32, + 1.9498784100e-01_f32, + 2.4663666200e-01_f32, + 2.8182820300e-01_f32, + 2.9431533200e-01_f32, + 2.8182820300e-01_f32, + 2.4663666200e-01_f32, + 1.9498784100e-01_f32, + -1.3559327400e-01_f32, + -7.7646349400e-02_f32, + -2.8821727400e-02_f32, + 6.1324518600e-03_f32, + 2.5876781100e-02_f32, + 3.2193929000e-02_f32, + 2.8875739200e-02_f32, + 2.0438508700e-02_f32, + -1.0913762000e-02_f32, + -3.0601228600e-03_f32, + 1.8658169100e-03_f32, + 3.8920514900e-03_f32, + 3.8372019300e-03_f32, + 2.7337090400e-03_f32, + 1.4918835700e-03_f32, + 5.3654897600e-04_f32, +]; + +/// Analysis and synthesis filter coefficients for 8 subbands (Table 12.24). +pub(crate) static PROTO_8_80: [f32; 80] = [ + 0.0000000000e+00_f32, + 1.5657539800e-04_f32, + 3.4325642500e-04_f32, + 5.5462020200e-04_f32, + 8.2391950600e-04_f32, + 1.1399250700e-03_f32, + 1.4764016900e-03_f32, + 1.7837172500e-03_f32, + 2.0118254200e-03_f32, + 2.1037198900e-03_f32, + 1.9945455400e-03_f32, + 1.6165628300e-03_f32, + 9.0215450200e-04_f32, + -1.7880536100e-04_f32, + -1.6497309800e-03_f32, + -3.4971745400e-03_f32, + 5.6594947300e-03_f32, + 8.0294116300e-03_f32, + 1.0458444300e-02_f32, + 1.2747233500e-02_f32, + 1.4652526300e-02_f32, + 1.5904560300e-02_f32, + 1.6220847100e-02_f32, + 1.5318410600e-02_f32, + 1.2937180600e-02_f32, + 8.8575754000e-03_f32, + 2.9240844200e-03_f32, + -4.9157802400e-03_f32, + -1.4640407600e-02_f32, + -2.6109875200e-02_f32, + -3.9075138100e-02_f32, + -5.3187303200e-02_f32, + 6.7998943100e-02_f32, + 8.2984757800e-02_f32, + 9.7575391800e-02_f32, + 1.1119668900e-01_f32, + 1.2326454800e-01_f32, + 1.3326441500e-01_f32, + 1.4075350500e-01_f32, + 1.4538984700e-01_f32, + 1.4695506800e-01_f32, + 1.4538984700e-01_f32, + 1.4075350500e-01_f32, + 1.3326441500e-01_f32, + 1.2326454800e-01_f32, + 1.1119668900e-01_f32, + 9.7575391800e-02_f32, + 8.2984757800e-02_f32, + -6.7998943100e-02_f32, + -5.3187303200e-02_f32, + -3.9075138100e-02_f32, + -2.6109875200e-02_f32, + -1.4640407600e-02_f32, + -4.9157802400e-03_f32, + 2.9240844200e-03_f32, + 8.8575754000e-03_f32, + 1.2937180600e-02_f32, + 1.5318410600e-02_f32, + 1.6220847100e-02_f32, + 1.5904560300e-02_f32, + 1.4652526300e-02_f32, + 1.2747233500e-02_f32, + 1.0458444300e-02_f32, + 8.0294116300e-03_f32, + -5.6594947300e-03_f32, + -3.4971745400e-03_f32, + -1.6497309800e-03_f32, + -1.7880536100e-04_f32, + 9.0215450200e-04_f32, + 1.6165628300e-03_f32, + 1.9945455400e-03_f32, + 2.1037198900e-03_f32, + 2.0118254200e-03_f32, + 1.7837172500e-03_f32, + 1.4764016900e-03_f32, + 1.1399250700e-03_f32, + 8.2391950600e-04_f32, + 5.5462020200e-04_f32, + 3.4325642500e-04_f32, + 1.5657539800e-04_f32, +]; + +/// Scaled synthesis filter prototype for 4 subbands. +pub(crate) static SYNTH_D_4: [f32; 40] = [ + -0.0000000000e+00_f32, + -2.1461959040e-03_f32, + -5.9675342800e-03_f32, + -1.0934836160e-02_f32, + -1.5348807720e-02_f32, + -1.5568205960e-02_f32, + -7.4632676400e-03_f32, + 1.2240491440e-02_f32, + -4.3655048000e-02_f32, + -8.1754034800e-02_f32, + -1.1550295680e-01_f32, + -1.2877571600e-01_f32, + -1.0350712440e-01_f32, + -2.4529807440e-02_f32, + 1.1528690960e-01_f32, + 3.1058539760e-01_f32, + -5.4237309600e-01_f32, + -7.7995136400e-01_f32, + -9.8654664800e-01_f32, + -1.1273128120e+00_f32, + -1.1772613280e+00_f32, + -1.1273128120e+00_f32, + -9.8654664800e-01_f32, + -7.7995136400e-01_f32, + 5.4237309600e-01_f32, + 3.1058539760e-01_f32, + 1.1528690960e-01_f32, + -2.4529807440e-02_f32, + -1.0350712440e-01_f32, + -1.2877571600e-01_f32, + -1.1550295680e-01_f32, + -8.1754034800e-02_f32, + 4.3655048000e-02_f32, + 1.2240491440e-02_f32, + -7.4632676400e-03_f32, + -1.5568205960e-02_f32, + -1.5348807720e-02_f32, + -1.0934836160e-02_f32, + -5.9675342800e-03_f32, + -2.1461959040e-03_f32, +]; + +/// Scaled synthesis filter prototype for 8 subbands. +pub(crate) static SYNTH_D_8: [f32; 80] = [ + -0.0000000000e+00_f32, + -1.2526031840e-03_f32, + -2.7460514000e-03_f32, + -4.4369616160e-03_f32, + -6.5913560480e-03_f32, + -9.1194005600e-03_f32, + -1.1811213520e-02_f32, + -1.4269738000e-02_f32, + -1.6094603360e-02_f32, + -1.6829759120e-02_f32, + -1.5956364320e-02_f32, + -1.2932502640e-02_f32, + -7.2172360160e-03_f32, + 1.4304428880e-03_f32, + 1.3197847840e-02_f32, + 2.7977396320e-02_f32, + -4.5275957840e-02_f32, + -6.4235293040e-02_f32, + -8.3667554400e-02_f32, + -1.0197786800e-01_f32, + -1.1722021040e-01_f32, + -1.2723648240e-01_f32, + -1.2976677680e-01_f32, + -1.2254728480e-01_f32, + -1.0349744480e-01_f32, + -7.0860603200e-02_f32, + -2.3392675360e-02_f32, + 3.9326241920e-02_f32, + 1.1712326080e-01_f32, + 2.0887900160e-01_f32, + 3.1260110480e-01_f32, + 4.2549842560e-01_f32, + -5.4399154480e-01_f32, + -6.6387806240e-01_f32, + -7.8060313440e-01_f32, + -8.8957351200e-01_f32, + -9.8611638400e-01_f32, + -1.0661153200e+00_f32, + -1.1260280400e+00_f32, + -1.1631187760e+00_f32, + -1.1756405440e+00_f32, + -1.1631187760e+00_f32, + -1.1260280400e+00_f32, + -1.0661153200e+00_f32, + -9.8611638400e-01_f32, + -8.8957351200e-01_f32, + -7.8060313440e-01_f32, + -6.6387806240e-01_f32, + 5.4399154480e-01_f32, + 4.2549842560e-01_f32, + 3.1260110480e-01_f32, + 2.0887900160e-01_f32, + 1.1712326080e-01_f32, + 3.9326241920e-02_f32, + -2.3392675360e-02_f32, + -7.0860603200e-02_f32, + -1.0349744480e-01_f32, + -1.2254728480e-01_f32, + -1.2976677680e-01_f32, + -1.2723648240e-01_f32, + -1.1722021040e-01_f32, + -1.0197786800e-01_f32, + -8.3667554400e-02_f32, + -6.4235293040e-02_f32, + 4.5275957840e-02_f32, + 2.7977396320e-02_f32, + 1.3197847840e-02_f32, + 1.4304428880e-03_f32, + -7.2172360160e-03_f32, + -1.2932502640e-02_f32, + -1.5956364320e-02_f32, + -1.6829759120e-02_f32, + -1.6094603360e-02_f32, + -1.4269738000e-02_f32, + -1.1811213520e-02_f32, + -9.1194005600e-03_f32, + -6.5913560480e-03_f32, + -4.4369616160e-03_f32, + -2.7460514000e-03_f32, + -1.2526031840e-03_f32, +]; + +pub(crate) static SYNTH_COS_4: [[f32; 4]; 8] = [ + [7.0710678119e-01_f32, -7.0710678119e-01_f32, -7.0710678119e-01_f32, 7.0710678119e-01_f32], + [3.8268343237e-01_f32, -9.2387953251e-01_f32, 9.2387953251e-01_f32, -3.8268343237e-01_f32], + [6.1232339957e-17_f32, -1.8369701987e-16_f32, 3.0616169979e-16_f32, -4.2862637970e-16_f32], + [-3.8268343237e-01_f32, 9.2387953251e-01_f32, -9.2387953251e-01_f32, 3.8268343237e-01_f32], + [-7.0710678119e-01_f32, 7.0710678119e-01_f32, 7.0710678119e-01_f32, -7.0710678119e-01_f32], + [-9.2387953251e-01_f32, -3.8268343237e-01_f32, 3.8268343237e-01_f32, 9.2387953251e-01_f32], + [-1.0000000000e+00_f32, -1.0000000000e+00_f32, -1.0000000000e+00_f32, -1.0000000000e+00_f32], + [-9.2387953251e-01_f32, -3.8268343237e-01_f32, 3.8268343237e-01_f32, 9.2387953251e-01_f32], +]; + +pub(crate) static SYNTH_COS_8: [[f32; 8]; 16] = [ + [ + 7.0710678119e-01_f32, + -7.0710678119e-01_f32, + -7.0710678119e-01_f32, + 7.0710678119e-01_f32, + 7.0710678119e-01_f32, + -7.0710678119e-01_f32, + -7.0710678119e-01_f32, + 7.0710678119e-01_f32, + ], + [ + 5.5557023302e-01_f32, + -9.8078528040e-01_f32, + 1.9509032202e-01_f32, + 8.3146961230e-01_f32, + -8.3146961230e-01_f32, + -1.9509032202e-01_f32, + 9.8078528040e-01_f32, + -5.5557023302e-01_f32, + ], + [ + 3.8268343237e-01_f32, + -9.2387953251e-01_f32, + 9.2387953251e-01_f32, + -3.8268343237e-01_f32, + -3.8268343237e-01_f32, + 9.2387953251e-01_f32, + -9.2387953251e-01_f32, + 3.8268343237e-01_f32, + ], + [ + 1.9509032202e-01_f32, + -5.5557023302e-01_f32, + 8.3146961230e-01_f32, + -9.8078528040e-01_f32, + 9.8078528040e-01_f32, + -8.3146961230e-01_f32, + 5.5557023302e-01_f32, + -1.9509032202e-01_f32, + ], + [ + 6.1232339957e-17_f32, + -1.8369701987e-16_f32, + 3.0616169979e-16_f32, + -4.2862637970e-16_f32, + 5.5109105962e-16_f32, + -2.4499125789e-15_f32, + -9.8033641995e-16_f32, + -2.6948419388e-15_f32, + ], + [ + -1.9509032202e-01_f32, + 5.5557023302e-01_f32, + -8.3146961230e-01_f32, + 9.8078528040e-01_f32, + -9.8078528040e-01_f32, + 8.3146961230e-01_f32, + -5.5557023302e-01_f32, + 1.9509032202e-01_f32, + ], + [ + -3.8268343237e-01_f32, + 9.2387953251e-01_f32, + -9.2387953251e-01_f32, + 3.8268343237e-01_f32, + 3.8268343237e-01_f32, + -9.2387953251e-01_f32, + 9.2387953251e-01_f32, + -3.8268343237e-01_f32, + ], + [ + -5.5557023302e-01_f32, + 9.8078528040e-01_f32, + -1.9509032202e-01_f32, + -8.3146961230e-01_f32, + 8.3146961230e-01_f32, + 1.9509032202e-01_f32, + -9.8078528040e-01_f32, + 5.5557023302e-01_f32, + ], + [ + -7.0710678119e-01_f32, + 7.0710678119e-01_f32, + 7.0710678119e-01_f32, + -7.0710678119e-01_f32, + -7.0710678119e-01_f32, + 7.0710678119e-01_f32, + 7.0710678119e-01_f32, + -7.0710678119e-01_f32, + ], + [ + -8.3146961230e-01_f32, + 1.9509032202e-01_f32, + 9.8078528040e-01_f32, + 5.5557023302e-01_f32, + -5.5557023302e-01_f32, + -9.8078528040e-01_f32, + -1.9509032202e-01_f32, + 8.3146961230e-01_f32, + ], + [ + -9.2387953251e-01_f32, + -3.8268343237e-01_f32, + 3.8268343237e-01_f32, + 9.2387953251e-01_f32, + 9.2387953251e-01_f32, + 3.8268343237e-01_f32, + -3.8268343237e-01_f32, + -9.2387953251e-01_f32, + ], + [ + -9.8078528040e-01_f32, + -8.3146961230e-01_f32, + -5.5557023302e-01_f32, + -1.9509032202e-01_f32, + 1.9509032202e-01_f32, + 5.5557023302e-01_f32, + 8.3146961230e-01_f32, + 9.8078528040e-01_f32, + ], + [ + -1.0000000000e+00_f32, + -1.0000000000e+00_f32, + -1.0000000000e+00_f32, + -1.0000000000e+00_f32, + -1.0000000000e+00_f32, + -1.0000000000e+00_f32, + -1.0000000000e+00_f32, + -1.0000000000e+00_f32, + ], + [ + -9.8078528040e-01_f32, + -8.3146961230e-01_f32, + -5.5557023302e-01_f32, + -1.9509032202e-01_f32, + 1.9509032202e-01_f32, + 5.5557023302e-01_f32, + 8.3146961230e-01_f32, + 9.8078528040e-01_f32, + ], + [ + -9.2387953251e-01_f32, + -3.8268343237e-01_f32, + 3.8268343237e-01_f32, + 9.2387953251e-01_f32, + 9.2387953251e-01_f32, + 3.8268343237e-01_f32, + -3.8268343237e-01_f32, + -9.2387953251e-01_f32, + ], + [ + -8.3146961230e-01_f32, + 1.9509032202e-01_f32, + 9.8078528040e-01_f32, + 5.5557023302e-01_f32, + -5.5557023302e-01_f32, + -9.8078528040e-01_f32, + -1.9509032202e-01_f32, + 8.3146961230e-01_f32, + ], +]; + +pub(crate) static ANALYSIS_COS_4: [[f32; 8]; 4] = [ + [ + 7.0710678119e-01_f32, + 9.2387953251e-01_f32, + 1.0000000000e+00_f32, + 9.2387953251e-01_f32, + 7.0710678119e-01_f32, + 3.8268343237e-01_f32, + 6.1232339957e-17_f32, + -3.8268343237e-01_f32, + ], + [ + -7.0710678119e-01_f32, + 3.8268343237e-01_f32, + 1.0000000000e+00_f32, + 3.8268343237e-01_f32, + -7.0710678119e-01_f32, + -9.2387953251e-01_f32, + -1.8369701987e-16_f32, + 9.2387953251e-01_f32, + ], + [ + -7.0710678119e-01_f32, + -3.8268343237e-01_f32, + 1.0000000000e+00_f32, + -3.8268343237e-01_f32, + -7.0710678119e-01_f32, + 9.2387953251e-01_f32, + 3.0616169979e-16_f32, + -9.2387953251e-01_f32, + ], + [ + 7.0710678119e-01_f32, + -9.2387953251e-01_f32, + 1.0000000000e+00_f32, + -9.2387953251e-01_f32, + 7.0710678119e-01_f32, + -3.8268343237e-01_f32, + -4.2862637970e-16_f32, + 3.8268343237e-01_f32, + ], +]; + +pub(crate) static ANALYSIS_COS_8: [[f32; 16]; 8] = [ + [ + 7.0710678119e-01_f32, + 8.3146961230e-01_f32, + 9.2387953251e-01_f32, + 9.8078528040e-01_f32, + 1.0000000000e+00_f32, + 9.8078528040e-01_f32, + 9.2387953251e-01_f32, + 8.3146961230e-01_f32, + 7.0710678119e-01_f32, + 5.5557023302e-01_f32, + 3.8268343237e-01_f32, + 1.9509032202e-01_f32, + 6.1232339957e-17_f32, + -1.9509032202e-01_f32, + -3.8268343237e-01_f32, + -5.5557023302e-01_f32, + ], + [ + -7.0710678119e-01_f32, + -1.9509032202e-01_f32, + 3.8268343237e-01_f32, + 8.3146961230e-01_f32, + 1.0000000000e+00_f32, + 8.3146961230e-01_f32, + 3.8268343237e-01_f32, + -1.9509032202e-01_f32, + -7.0710678119e-01_f32, + -9.8078528040e-01_f32, + -9.2387953251e-01_f32, + -5.5557023302e-01_f32, + -1.8369701987e-16_f32, + 5.5557023302e-01_f32, + 9.2387953251e-01_f32, + 9.8078528040e-01_f32, + ], + [ + -7.0710678119e-01_f32, + -9.8078528040e-01_f32, + -3.8268343237e-01_f32, + 5.5557023302e-01_f32, + 1.0000000000e+00_f32, + 5.5557023302e-01_f32, + -3.8268343237e-01_f32, + -9.8078528040e-01_f32, + -7.0710678119e-01_f32, + 1.9509032202e-01_f32, + 9.2387953251e-01_f32, + 8.3146961230e-01_f32, + 3.0616169979e-16_f32, + -8.3146961230e-01_f32, + -9.2387953251e-01_f32, + -1.9509032202e-01_f32, + ], + [ + 7.0710678119e-01_f32, + -5.5557023302e-01_f32, + -9.2387953251e-01_f32, + 1.9509032202e-01_f32, + 1.0000000000e+00_f32, + 1.9509032202e-01_f32, + -9.2387953251e-01_f32, + -5.5557023302e-01_f32, + 7.0710678119e-01_f32, + 8.3146961230e-01_f32, + -3.8268343237e-01_f32, + -9.8078528040e-01_f32, + -4.2862637970e-16_f32, + 9.8078528040e-01_f32, + 3.8268343237e-01_f32, + -8.3146961230e-01_f32, + ], + [ + 7.0710678119e-01_f32, + 5.5557023302e-01_f32, + -9.2387953251e-01_f32, + -1.9509032202e-01_f32, + 1.0000000000e+00_f32, + -1.9509032202e-01_f32, + -9.2387953251e-01_f32, + 5.5557023302e-01_f32, + 7.0710678119e-01_f32, + -8.3146961230e-01_f32, + -3.8268343237e-01_f32, + 9.8078528040e-01_f32, + 5.5109105962e-16_f32, + -9.8078528040e-01_f32, + 3.8268343237e-01_f32, + 8.3146961230e-01_f32, + ], + [ + -7.0710678119e-01_f32, + 9.8078528040e-01_f32, + -3.8268343237e-01_f32, + -5.5557023302e-01_f32, + 1.0000000000e+00_f32, + -5.5557023302e-01_f32, + -3.8268343237e-01_f32, + 9.8078528040e-01_f32, + -7.0710678119e-01_f32, + -1.9509032202e-01_f32, + 9.2387953251e-01_f32, + -8.3146961230e-01_f32, + -2.4499125789e-15_f32, + 8.3146961230e-01_f32, + -9.2387953251e-01_f32, + 1.9509032202e-01_f32, + ], + [ + -7.0710678119e-01_f32, + 1.9509032202e-01_f32, + 3.8268343237e-01_f32, + -8.3146961230e-01_f32, + 1.0000000000e+00_f32, + -8.3146961230e-01_f32, + 3.8268343237e-01_f32, + 1.9509032202e-01_f32, + -7.0710678119e-01_f32, + 9.8078528040e-01_f32, + -9.2387953251e-01_f32, + 5.5557023302e-01_f32, + -9.8033641995e-16_f32, + -5.5557023302e-01_f32, + 9.2387953251e-01_f32, + -9.8078528040e-01_f32, + ], + [ + 7.0710678119e-01_f32, + -8.3146961230e-01_f32, + 9.2387953251e-01_f32, + -9.8078528040e-01_f32, + 1.0000000000e+00_f32, + -9.8078528040e-01_f32, + 9.2387953251e-01_f32, + -8.3146961230e-01_f32, + 7.0710678119e-01_f32, + -5.5557023302e-01_f32, + 3.8268343237e-01_f32, + -1.9509032202e-01_f32, + -2.6948419388e-15_f32, + 1.9509032202e-01_f32, + -3.8268343237e-01_f32, + 5.5557023302e-01_f32, + ], +]; + +/// Sampling frequency of the SBC bitstream. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SamplingFrequency { + /// 16 000 Hz + Freq16k, + /// 32 000 Hz + Freq32k, + /// 44 100 Hz + Freq44k1, + /// 48 000 Hz + Freq48k, +} + +impl SamplingFrequency { + /// Returns sampling frequency in Hz. + #[inline] + #[must_use] + pub const fn as_hz(self) -> u32 { + match self { + Self::Freq16k => 16_000, + Self::Freq32k => 32_000, + Self::Freq44k1 => 44_100, + Self::Freq48k => 48_000, + } + } + + /// Derives sampling frequency from Hz. + pub fn from_hz(hz: u32) -> Result { + match hz { + 16_000 => Ok(Self::Freq16k), + 32_000 => Ok(Self::Freq32k), + 44_100 => Ok(Self::Freq44k1), + 48_000 => Ok(Self::Freq48k), + _ => Err(Error::InvalidSampleRate(hz)), + } + } + + /// Returns the 2-bit header code. + #[inline] + #[must_use] + pub const fn code(self) -> u8 { + match self { + Self::Freq16k => 0b00, + Self::Freq32k => 0b01, + Self::Freq44k1 => 0b10, + Self::Freq48k => 0b11, + } + } + + /// Index into offset tables (0 to 3). + #[inline] + #[must_use] + pub const fn table_index(self) -> usize { + match self { + Self::Freq16k => 0, + Self::Freq32k => 1, + Self::Freq44k1 => 2, + Self::Freq48k => 3, + } + } +} + +/// Number of blocks per SBC frame. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BlockCount { + /// 4 blocks + Blocks4, + /// 8 blocks + Blocks8, + /// 12 blocks + Blocks12, + /// 16 blocks + Blocks16, + /// 15 blocks (mSBC wideband speech) + Blocks15, +} + +impl BlockCount { + /// Returns integer block count. + #[inline] + #[must_use] + pub const fn count(self) -> usize { + match self { + Self::Blocks4 => 4, + Self::Blocks8 => 8, + Self::Blocks12 => 12, + Self::Blocks16 => 16, + Self::Blocks15 => 15, + } + } + + /// Derives block count from integer. + pub fn from_count(count: usize) -> Result { + match count { + 4 => Ok(Self::Blocks4), + 8 => Ok(Self::Blocks8), + 12 => Ok(Self::Blocks12), + 16 => Ok(Self::Blocks16), + 15 => Ok(Self::Blocks15), + _ => Err(Error::InvalidBlockCount(count)), + } + } + + /// Returns 2-bit header code. + #[inline] + #[must_use] + pub const fn code(self) -> u8 { + match self { + Self::Blocks4 => 0b00, + Self::Blocks8 => 0b01, + Self::Blocks12 => 0b10, + Self::Blocks16 | Self::Blocks15 => 0b11, + } + } +} + +/// Channel mode of the SBC bitstream. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ChannelMode { + /// 1 channel (Mono) + Mono, + /// 2 channels with independent bit allocation + DualChannel, + /// 2 channels with shared bit allocation + Stereo, + /// 2 channels with shared bit allocation and joint mid/side subbands + JointStereo, +} + +impl ChannelMode { + /// Returns number of audio channels. + #[inline] + #[must_use] + pub const fn channels(self) -> usize { + match self { + Self::Mono => 1, + Self::DualChannel | Self::Stereo | Self::JointStereo => 2, + } + } + + /// Returns 2-bit header code. + #[inline] + #[must_use] + pub const fn code(self) -> u8 { + match self { + Self::Mono => 0b00, + Self::DualChannel => 0b01, + Self::Stereo => 0b10, + Self::JointStereo => 0b11, + } + } +} + +/// Bit allocation method. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum AllocationMethod { + /// Psychoacoustic loudness allocation + #[default] + Loudness, + /// Signal-to-noise ratio allocation + Snr, +} + +impl AllocationMethod { + /// Returns 1-bit header code. + #[inline] + #[must_use] + pub const fn code(self) -> u8 { + match self { + Self::Loudness => 0, + Self::Snr => 1, + } + } +} + +/// Number of subbands. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Subbands { + /// 4 subbands + Subbands4, + /// 8 subbands + Subbands8, +} + +impl Subbands { + /// Returns number of subbands as integer. + #[inline] + #[must_use] + pub const fn count(self) -> usize { + match self { + Self::Subbands4 => 4, + Self::Subbands8 => 8, + } + } + + /// Derives Subbands from integer. + pub fn from_count(count: usize) -> Result { + match count { + 4 => Ok(Self::Subbands4), + 8 => Ok(Self::Subbands8), + _ => Err(Error::InvalidSubbands(count)), + } + } + + /// Returns 1-bit header code. + #[inline] + #[must_use] + pub const fn code(self) -> u8 { + match self { + Self::Subbands4 => 0, + Self::Subbands8 => 1, + } + } +} + +/// Parsed header of an SBC audio frame. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Header { + /// Syncword byte (`0x9C` or `0xAD`). + pub syncword: u8, + /// Sampling frequency. + pub sampling_frequency: SamplingFrequency, + /// Number of blocks. + pub blocks: BlockCount, + /// Channel mode. + pub channel_mode: ChannelMode, + /// Allocation method. + pub allocation_method: AllocationMethod, + /// Number of subbands. + pub subbands: Subbands, + /// Bitpool size (1 to 255). + pub bitpool: u8, + /// Stored CRC-8 parity check word. + pub crc_check: u8, + /// Joint stereo flags for subbands 0..subbands-1. + pub join: [bool; 8], +} + +impl Header { + /// Parses an SBC frame header from a byte slice. + pub fn parse(src: &[u8]) -> Result { + if src.len() < 4 { + return Err(Error::TruncatedFrame { expected: 4, actual: src.len() }); + } + let syncword = src[0]; + if syncword == SYNCWORD_MSBC { + let crc_check = src[3]; + return Ok(Self { + syncword, + sampling_frequency: SamplingFrequency::Freq16k, + blocks: BlockCount::Blocks15, + channel_mode: ChannelMode::Mono, + allocation_method: AllocationMethod::Loudness, + subbands: Subbands::Subbands8, + bitpool: 26, + crc_check, + join: [false; 8], + }); + } + if syncword != SYNCWORD_SBC { + return Err(Error::InvalidSyncword(syncword)); + } + let b1 = src[1]; + let freq_code = (b1 >> 6) & 3; + let blocks_code = (b1 >> 4) & 3; + let mode_code = (b1 >> 2) & 3; + let alloc_code = (b1 >> 1) & 1; + let subbands_code = b1 & 1; + + let sampling_frequency = match freq_code { + 0 => SamplingFrequency::Freq16k, + 1 => SamplingFrequency::Freq32k, + 2 => SamplingFrequency::Freq44k1, + _ => SamplingFrequency::Freq48k, + }; + let blocks = match blocks_code { + 0 => BlockCount::Blocks4, + 1 => BlockCount::Blocks8, + 2 => BlockCount::Blocks12, + _ => BlockCount::Blocks16, + }; + let channel_mode = match mode_code { + 0 => ChannelMode::Mono, + 1 => ChannelMode::DualChannel, + 2 => ChannelMode::Stereo, + _ => ChannelMode::JointStereo, + }; + let allocation_method = if alloc_code == 0 { AllocationMethod::Loudness } else { AllocationMethod::Snr }; + let subbands = if subbands_code == 0 { Subbands::Subbands4 } else { Subbands::Subbands8 }; + let bitpool = src[2]; + let crc_check = src[3]; + + let max_bitpool = match channel_mode { + ChannelMode::Mono | ChannelMode::DualChannel => 16 * subbands.count(), + ChannelMode::Stereo | ChannelMode::JointStereo => 32 * subbands.count(), + }; + if bitpool == 0 || (bitpool as usize) > max_bitpool { + return Err(Error::InvalidBitpool(bitpool)); + } + + let mut join = [false; 8]; + if channel_mode == ChannelMode::JointStereo && src.len() >= 5 { + let join_byte = src[4]; + let sb_count = subbands.count(); + for sb in 0..(sb_count.saturating_sub(1)) { + let bit_shift = 7 - (sb as u8); + join[sb] = ((join_byte >> bit_shift) & 1) != 0; + } + } + + Ok(Self { syncword, sampling_frequency, blocks, channel_mode, allocation_method, subbands, bitpool, crc_check, join }) + } + + /// Total length of the frame in bytes (Section 12.9). + #[must_use] + pub fn frame_length(&self) -> usize { + if self.syncword == SYNCWORD_MSBC { + return 57; + } + let nrof_subbands = self.subbands.count(); + let nrof_channels = self.channel_mode.channels(); + let nrof_blocks = self.blocks.count(); + let bitpool = self.bitpool as usize; + + let base = 4 + (4 * nrof_subbands * nrof_channels) / 8; + match self.channel_mode { + ChannelMode::Mono | ChannelMode::DualChannel => base + (nrof_blocks * nrof_channels * bitpool).div_ceil(8), + ChannelMode::Stereo => base + (nrof_blocks * bitpool).div_ceil(8), + ChannelMode::JointStereo => base + (nrof_subbands + nrof_blocks * bitpool).div_ceil(8), + } + } + + /// Number of audio samples per channel produced by this frame. + #[inline] + #[must_use] + pub const fn samples_per_frame(&self) -> usize { + self.blocks.count() * self.subbands.count() + } + + /// Number of audio channels. + #[inline] + #[must_use] + pub const fn channels(&self) -> usize { + self.channel_mode.channels() + } + + /// Sampling rate in Hz. + #[inline] + #[must_use] + pub const fn sample_rate(&self) -> u32 { + self.sampling_frequency.as_hz() + } + + /// Bit rate of the stream in bits per second (Section 12.9). + #[must_use] + pub fn bit_rate_bps(&self) -> u32 { + let flen = self.frame_length() as u64; + let fs = self.sample_rate() as u64; + let sub = self.subbands.count() as u64; + let blk = self.blocks.count() as u64; + let bps = (8 * flen * fs) / (sub * blk); + bps as u32 + } +} + +/// Information about a decoded SBC stream. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct StreamInfo { + /// Sample rate in Hz. + pub sample_rate: u32, + /// Channel count (1 or 2). + pub channels: usize, + /// Channel mode. + pub channel_mode: ChannelMode, + /// Block count. + pub blocks: usize, + /// Subband count. + pub subbands: usize, + /// Bitpool setting. + pub bitpool: u8, + /// Allocation method. + pub allocation_method: AllocationMethod, +} + +/// Errors encountered in SBC operations. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// Invalid SBC syncword. + #[error("invalid SBC syncword: {0:#04x}")] + InvalidSyncword(u8), + /// Frame truncated before complete read. + #[error("truncated frame: expected {expected} bytes, got {actual}")] + TruncatedFrame { + /// Expected byte length. + expected: usize, + /// Actual available byte length. + actual: usize, + }, + /// CRC-8 verification mismatch. + #[error("CRC-8 check failed: computed {computed:#04x}, frame header had {stored:#04x}")] + CrcMismatch { + /// Computed CRC-8 value. + computed: u8, + /// Stored CRC-8 in frame header. + stored: u8, + }, + /// Unsupported channel count. + #[error("unsupported channel count: {0} (must be 1 or 2)")] + InvalidChannels(usize), + /// Unsupported sample rate. + #[error("unsupported sample rate: {0} Hz (must be 16000, 32000, 44100, or 48000 Hz)")] + InvalidSampleRate(u32), + /// Unsupported block count. + #[error("unsupported block count: {0} (must be 4, 8, 12, or 16)")] + InvalidBlockCount(usize), + /// Unsupported subbands count. + #[error("unsupported subband count: {0} (must be 4 or 8)")] + InvalidSubbands(usize), + /// Invalid bitpool value. + #[error("invalid bitpool value: {0}")] + InvalidBitpool(u8), + /// Input exceeds safe maximum processing limit. + #[error("input exceeds safe maximum processing limit")] + InputTooLarge, + /// Memory allocation failed. + #[error("memory allocation failed")] + AllocationFailed, +} + +/// Computes CRC-8 with generator polynomial `0x1D` and initial state `0x0F` (Section 12.6.1.1). +pub fn crc8(bits: impl IntoIterator) -> u8 { + let mut crc: u8 = 0x0F; + for bit in bits { + let feedback = (crc >> 7) ^ (if bit { 1 } else { 0 }); + crc = crc.wrapping_shl(1) ^ (if feedback != 0 { 0x1D } else { 0 }); + } + crc +} + +struct BitReader<'a> { + data: &'a [u8], + byte_pos: usize, + bit_pos: u8, +} + +impl<'a> BitReader<'a> { + fn new(data: &'a [u8]) -> Self { + Self { data, byte_pos: 0, bit_pos: 7 } + } + + fn read_bits(&mut self, n: usize) -> Option { + if n == 0 { + return Some(0); + } + if n > 32 { + return None; + } + let mut val = 0u32; + for _ in 0..n { + if self.byte_pos >= self.data.len() { + return None; + } + let bit = (self.data[self.byte_pos] >> self.bit_pos) & 1; + val = (val << 1) | u32::from(bit); + if self.bit_pos == 0 { + self.bit_pos = 7; + self.byte_pos = self.byte_pos.saturating_add(1); + } else { + self.bit_pos -= 1; + } + } + Some(val) + } +} + +struct BitWriter { + bytes: Vec, + cur_byte: u8, + bit_pos: u8, +} + +impl BitWriter { + fn with_capacity(cap: usize) -> Result { + let mut bytes = Vec::new(); + bytes.try_reserve(cap).map_err(|_| Error::AllocationFailed)?; + Ok(Self { bytes, cur_byte: 0, bit_pos: 7 }) + } + + fn write_bits(&mut self, val: u32, n: usize) -> Result<(), Error> { + if n == 0 { + return Ok(()); + } + for i in (0..n).rev() { + let bit = ((val >> i) & 1) as u8; + self.cur_byte |= bit << self.bit_pos; + if self.bit_pos == 0 { + self.bytes.try_reserve(1).map_err(|_| Error::AllocationFailed)?; + self.bytes.push(self.cur_byte); + self.cur_byte = 0; + self.bit_pos = 7; + } else { + self.bit_pos -= 1; + } + } + Ok(()) + } + + fn flush(&mut self) -> Result<(), Error> { + if self.bit_pos < 7 { + self.bytes.try_reserve(1).map_err(|_| Error::AllocationFailed)?; + self.bytes.push(self.cur_byte); + self.cur_byte = 0; + self.bit_pos = 7; + } + Ok(()) + } + + fn finish(mut self) -> Result, Error> { + self.flush()?; + Ok(self.bytes) + } +} + +fn compute_header_crc( + b1: u8, + b2: u8, + channel_mode: ChannelMode, + subbands: usize, + channels: usize, + join: &[bool; 8], + scale_factors: &[[u8; 8]; 2], +) -> u8 { + let mut crc: u8 = 0x0F; + let mut feed_bit = |bit: bool| { + let feedback = (crc >> 7) ^ (if bit { 1 } else { 0 }); + crc = crc.wrapping_shl(1) ^ (if feedback != 0 { 0x1D } else { 0 }); + }; + for i in (0..8).rev() { + feed_bit(((b1 >> i) & 1) != 0); + } + for i in (0..8).rev() { + feed_bit(((b2 >> i) & 1) != 0); + } + if channel_mode == ChannelMode::JointStereo { + for sb in 0..(subbands.saturating_sub(1)) { + feed_bit(join[sb]); + } + feed_bit(false); // RFA + } + for ch in 0..channels { + for sb in 0..subbands { + let sf = scale_factors[ch][sb]; + for i in (0..4).rev() { + feed_bit(((sf >> i) & 1) != 0); + } + } + } + crc +} + +fn compute_bit_allocation( + subbands: usize, + channels: usize, + channel_mode: ChannelMode, + freq_idx: usize, + alloc_method: AllocationMethod, + bitpool: u16, + scale_factors: &[[u8; 8]; 2], + bits: &mut [[u8; 8]; 2], +) { + if channel_mode == ChannelMode::Mono || channel_mode == ChannelMode::DualChannel { + for ch in 0..channels { + let mut bitneed = [0i16; 8]; + if alloc_method == AllocationMethod::Snr { + for sb in 0..subbands { + bitneed[sb] = i16::from(scale_factors[ch][sb]); + } + } else { + for sb in 0..subbands { + let sf = scale_factors[ch][sb]; + if sf == 0 { + bitneed[sb] = -5; + } else { + let off = if subbands == 4 { OFFSET4[freq_idx][sb] as i16 } else { OFFSET8[freq_idx][sb] as i16 }; + let loudness = (sf as i16) - off; + bitneed[sb] = if loudness > 0 { loudness / 2 } else { loudness }; + } + } + } + + let mut max_bitneed = 0i16; + for sb in 0..subbands { + if bitneed[sb] > max_bitneed { + max_bitneed = bitneed[sb]; + } + } + + let mut bitcount = 0u16; + let mut slicecount = 0u16; + let mut bitslice = max_bitneed + 1; + + loop { + bitslice -= 1; + bitcount = bitcount.saturating_add(slicecount); + slicecount = 0; + for sb in 0..subbands { + let bn = bitneed[sb]; + if bn > bitslice + 1 && bn < bitslice + 16 { + slicecount = slicecount.saturating_add(1); + } else if bn == bitslice + 1 { + slicecount = slicecount.saturating_add(2); + } + } + if bitcount.saturating_add(slicecount) >= bitpool || bitslice <= -32 { + break; + } + } + + if bitcount.saturating_add(slicecount) == bitpool { + bitcount = bitcount.saturating_add(slicecount); + bitslice -= 1; + } + + for sb in 0..subbands { + let bn = bitneed[sb]; + if bn < bitslice + 2 { + bits[ch][sb] = 0; + } else { + let b = (bn - bitslice).clamp(0, 16); + bits[ch][sb] = b as u8; + } + } + + let mut sb = 0; + while bitcount < bitpool && sb < subbands { + if bits[ch][sb] >= 2 && bits[ch][sb] < 16 { + bits[ch][sb] = bits[ch][sb].saturating_add(1); + bitcount = bitcount.saturating_add(1); + } else if bitneed[sb] == bitslice + 1 && bitpool > bitcount.saturating_add(1) { + bits[ch][sb] = 2; + bitcount = bitcount.saturating_add(2); + } + sb += 1; + } + + let mut sb = 0; + while bitcount < bitpool && sb < subbands { + if bits[ch][sb] < 16 { + bits[ch][sb] = bits[ch][sb].saturating_add(1); + bitcount = bitcount.saturating_add(1); + } + sb += 1; + } + } + } else { + let mut bitneed = [[0i16; 8]; 2]; + if alloc_method == AllocationMethod::Snr { + for ch in 0..2 { + for sb in 0..subbands { + bitneed[ch][sb] = i16::from(scale_factors[ch][sb]); + } + } + } else { + for ch in 0..2 { + for sb in 0..subbands { + let sf = scale_factors[ch][sb]; + if sf == 0 { + bitneed[ch][sb] = -5; + } else { + let off = if subbands == 4 { OFFSET4[freq_idx][sb] as i16 } else { OFFSET8[freq_idx][sb] as i16 }; + let loudness = (sf as i16) - off; + bitneed[ch][sb] = if loudness > 0 { loudness / 2 } else { loudness }; + } + } + } + } + + let mut max_bitneed = 0i16; + for ch in 0..2 { + for sb in 0..subbands { + if bitneed[ch][sb] > max_bitneed { + max_bitneed = bitneed[ch][sb]; + } + } + } + + let mut bitcount = 0u16; + let mut slicecount = 0u16; + let mut bitslice = max_bitneed + 1; + + loop { + bitslice -= 1; + bitcount = bitcount.saturating_add(slicecount); + slicecount = 0; + for ch in 0..2 { + for sb in 0..subbands { + let bn = bitneed[ch][sb]; + if bn > bitslice + 1 && bn < bitslice + 16 { + slicecount = slicecount.saturating_add(1); + } else if bn == bitslice + 1 { + slicecount = slicecount.saturating_add(2); + } + } + } + if bitcount.saturating_add(slicecount) >= bitpool || bitslice <= -32 { + break; + } + } + + if bitcount.saturating_add(slicecount) == bitpool { + bitcount = bitcount.saturating_add(slicecount); + bitslice -= 1; + } + + for ch in 0..2 { + for sb in 0..subbands { + let bn = bitneed[ch][sb]; + if bn < bitslice + 2 { + bits[ch][sb] = 0; + } else { + let b = (bn - bitslice).clamp(0, 16); + bits[ch][sb] = b as u8; + } + } + } + + let mut ch = 0; + let mut sb = 0; + while bitcount < bitpool && sb < subbands { + if bits[ch][sb] >= 2 && bits[ch][sb] < 16 { + bits[ch][sb] = bits[ch][sb].saturating_add(1); + bitcount = bitcount.saturating_add(1); + } else if bitneed[ch][sb] == bitslice + 1 && bitpool > bitcount.saturating_add(1) { + bits[ch][sb] = 2; + bitcount = bitcount.saturating_add(2); + } + if ch == 1 { + ch = 0; + sb += 1; + } else { + ch = 1; + } + } + + let mut ch = 0; + let mut sb = 0; + while bitcount < bitpool && sb < subbands { + if bits[ch][sb] < 16 { + bits[ch][sb] = bits[ch][sb].saturating_add(1); + bitcount = bitcount.saturating_add(1); + } + if ch == 1 { + ch = 0; + sb += 1; + } else { + ch = 1; + } + } + } +} + +fn synth_subbands_4(v: &mut [f32; 160], s: &[f32; 8], out: &mut [i16; 8]) { + v.copy_within(0..72, 8); + for k in 0..8 { + let mut sum = 0.0f32; + for i in 0..4 { + sum += SYNTH_COS_4[k][i] * s[i]; + } + v[k] = sum; + } + let mut u = [0.0f32; 40]; + for i in 0..5 { + for j in 0..4 { + u[i * 8 + j] = v[i * 16 + j]; + u[i * 8 + 4 + j] = v[i * 16 + 12 + j]; + } + } + for j in 0..4 { + let mut sum = 0.0f32; + for i in 0..10 { + sum += u[j + 4 * i] * SYNTH_D_4[j + 4 * i]; + } + let clamped = sum.clamp(-32768.0, 32767.0).round() as i32; + out[j] = clamped as i16; + } +} + +fn synth_subbands_8(v: &mut [f32; 160], s: &[f32; 8], out: &mut [i16; 8]) { + v.copy_within(0..144, 16); + for k in 0..16 { + let mut sum = 0.0f32; + for i in 0..8 { + sum += SYNTH_COS_8[k][i] * s[i]; + } + v[k] = sum; + } + let mut u = [0.0f32; 80]; + for i in 0..5 { + for j in 0..8 { + u[i * 16 + j] = v[i * 32 + j]; + u[i * 16 + 8 + j] = v[i * 32 + 24 + j]; + } + } + for j in 0..8 { + let mut sum = 0.0f32; + for i in 0..10 { + sum += u[j + 8 * i] * SYNTH_D_8[j + 8 * i]; + } + let clamped = sum.clamp(-32768.0, 32767.0).round() as i32; + out[j] = clamped as i16; + } +} + +fn analysis_subbands_4(x: &mut [f32; 80], pcm_in: &[i16], s_out: &mut [f32; 8]) { + x.copy_within(0..36, 4); + for i in 0..4 { + x[3 - i] = pcm_in[i] as f32; + } + let mut y = [0.0f32; 8]; + for i in 0..8 { + let mut sum = 0.0f32; + for k in 0..5 { + let idx = i + k * 8; + sum += PROTO_4_40[idx] * x[idx]; + } + y[i] = sum; + } + for i in 0..4 { + let mut sum = 0.0f32; + for k in 0..8 { + sum += ANALYSIS_COS_4[i][k] * y[k]; + } + s_out[i] = sum; + } +} + +fn analysis_subbands_8(x: &mut [f32; 80], pcm_in: &[i16], s_out: &mut [f32; 8]) { + x.copy_within(0..72, 8); + for i in 0..8 { + x[7 - i] = pcm_in[i] as f32; + } + let mut y = [0.0f32; 16]; + for i in 0..16 { + let mut sum = 0.0f32; + for k in 0..5 { + let idx = i + k * 16; + sum += PROTO_8_80[idx] * x[idx]; + } + y[i] = sum; + } + for i in 0..8 { + let mut sum = 0.0f32; + for k in 0..16 { + sum += ANALYSIS_COS_8[i][k] * y[k]; + } + s_out[i] = sum; + } +} + +fn calculate_scalefactor(max_abs: f32) -> u8 { + let v = max_abs.abs() as u32; + if v == 0 { + return 0; + } + let mut sf = 0u8; + while sf < 15 && (1u32 << (sf + 1)) <= v { + sf += 1; + } + sf +} + +/// SBC audio decoder. +#[derive(Debug, Clone)] +pub struct Decoder { + v: [[f32; 160]; 2], +} + +impl Default for Decoder { + fn default() -> Self { + Self { v: [[0.0f32; 160]; 2] } + } +} + +impl Decoder { + /// Creates a new SBC decoder. + #[must_use] + pub fn new() -> Self { + Self::default() + } + + /// Resets synthesis filterbank state. + pub fn reset(&mut self) { + self.v = [[0.0f32; 160]; 2]; + } + + /// Decodes a single SBC frame from `packet`, appending linear PCM samples to `dst`. + /// Returns the number of bytes consumed from `packet`. + pub fn decode_frame(&mut self, packet: &[u8], dst: &mut Vec) -> Result { + let header = Header::parse(packet)?; + let frame_len = header.frame_length(); + if packet.len() < frame_len { + return Err(Error::TruncatedFrame { expected: frame_len, actual: packet.len() }); + } + + let channels = header.channels(); + let subbands = header.subbands.count(); + let blocks = header.blocks.count(); + + let mut reader = BitReader::new(&packet[4..frame_len]); + + let mut join = [false; 8]; + if header.channel_mode == ChannelMode::JointStereo { + for sb in 0..(subbands.saturating_sub(1)) { + join[sb] = reader.read_bits(1).unwrap_or(0) != 0; + } + let _ = reader.read_bits(1); // RFA + } + + let mut scale_factors = [[0u8; 8]; 2]; + for ch in 0..channels { + for sb in 0..subbands { + scale_factors[ch][sb] = (reader.read_bits(4).unwrap_or(0) & 0x0F) as u8; + } + } + + // Verify CRC-8 + let expected_crc = if header.syncword == SYNCWORD_MSBC { + compute_header_crc(0x00, 0x00, ChannelMode::Mono, 8, 1, &[false; 8], &scale_factors) + } else { + compute_header_crc(packet[1], packet[2], header.channel_mode, subbands, channels, &join, &scale_factors) + }; + if expected_crc != header.crc_check { + return Err(Error::CrcMismatch { computed: expected_crc, stored: header.crc_check }); + } + + let mut bits = [[0u8; 8]; 2]; + compute_bit_allocation( + subbands, + channels, + header.channel_mode, + header.sampling_frequency.table_index(), + header.allocation_method, + u16::from(header.bitpool), + &scale_factors, + &mut bits, + ); + + let mut levels = [[0u32; 8]; 2]; + let mut scalefactors = [[0.0f32; 8]; 2]; + for ch in 0..channels { + for sb in 0..subbands { + levels[ch][sb] = (1u32 << bits[ch][sb]).saturating_sub(1); + scalefactors[ch][sb] = (1u32 << (u32::from(scale_factors[ch][sb]) + 1)) as f32; + } + } + + // Subband sample reconstruction + let mut sb_sample = [[[0.0f32; 8]; 2]; 16]; + for blk in 0..blocks { + for ch in 0..channels { + for sb in 0..subbands { + let lev = levels[ch][sb]; + if lev > 0 { + let raw_s = reader.read_bits(bits[ch][sb] as usize).unwrap_or(0); + let norm = ((raw_s as f32 * 2.0 + 1.0) / (lev as f32)) - 1.0; + sb_sample[blk][ch][sb] = scalefactors[ch][sb] * norm; + } else { + sb_sample[blk][ch][sb] = 0.0; + } + } + } + } + + // Joint stereo mid/side reconstruction + if header.channel_mode == ChannelMode::JointStereo { + for blk in 0..blocks { + for sb in 0..subbands { + if join[sb] { + let mid = sb_sample[blk][0][sb] + sb_sample[blk][1][sb]; + let side = sb_sample[blk][0][sb] - sb_sample[blk][1][sb]; + sb_sample[blk][0][sb] = mid; + sb_sample[blk][1][sb] = side; + } + } + } + } + + // Synthesis filterbank + let total_samples = blocks * subbands * channels; + dst.try_reserve(total_samples).map_err(|_| Error::AllocationFailed)?; + + let mut block_samples = [[[0i16; 8]; 2]; 16]; + for blk in 0..blocks { + for ch in 0..channels { + if subbands == 4 { + synth_subbands_4(&mut self.v[ch], &sb_sample[blk][ch], &mut block_samples[blk][ch]); + } else { + synth_subbands_8(&mut self.v[ch], &sb_sample[blk][ch], &mut block_samples[blk][ch]); + } + } + for sb in 0..subbands { + for ch in 0..channels { + dst.push(block_samples[blk][ch][sb]); + } + } + } + + Ok(frame_len) + } + + /// Decodes an entire buffer of concatenated SBC frames into interleaved 16-bit PCM. + pub fn decode(&mut self, src: &[u8], dst: &mut Vec) -> Result<(), Error> { + if src.len() > MAX_INPUT_BYTES { + return Err(Error::InputTooLarge); + } + let mut offset = 0; + while offset < src.len() { + let consumed = self.decode_frame(&src[offset..], dst)?; + if consumed == 0 { + break; + } + offset = offset.saturating_add(consumed); + } + Ok(()) + } +} + +/// SBC audio encoder. +#[derive(Debug, Clone)] +pub struct Encoder { + sample_rate: u32, + channels: usize, + channel_mode: ChannelMode, + blocks: BlockCount, + subbands: Subbands, + bitpool: u8, + allocation_method: AllocationMethod, + x: [[f32; 80]; 2], +} + +impl Encoder { + /// Creates a new SBC encoder with custom configuration. + pub fn new( + sample_rate: u32, + channels: usize, + blocks: usize, + subbands: usize, + bitpool: u8, + allocation_method: AllocationMethod, + ) -> Result { + if channels != 1 && channels != 2 { + return Err(Error::InvalidChannels(channels)); + } + let freq = SamplingFrequency::from_hz(sample_rate)?; + let block_cnt = BlockCount::from_count(blocks)?; + let subband_cnt = Subbands::from_count(subbands)?; + let channel_mode = if channels == 1 { ChannelMode::Mono } else { ChannelMode::JointStereo }; + + let max_bitpool = match channel_mode { + ChannelMode::Mono | ChannelMode::DualChannel => 16 * subband_cnt.count(), + ChannelMode::Stereo | ChannelMode::JointStereo => 32 * subband_cnt.count(), + }; + if bitpool == 0 || (bitpool as usize) > max_bitpool { + return Err(Error::InvalidBitpool(bitpool)); + } + + if block_cnt == BlockCount::Blocks15 + && (sample_rate != 16_000 || channels != 1 || subbands != 8 || bitpool != 26 || allocation_method != AllocationMethod::Loudness) + { + return Err(Error::InvalidBlockCount(15)); + } + + Ok(Self { + sample_rate: freq.as_hz(), + channels, + channel_mode, + blocks: block_cnt, + subbands: subband_cnt, + bitpool, + allocation_method, + x: [[0.0f32; 80]; 2], + }) + } + + /// Resets analysis filterbank state. + pub fn reset(&mut self) { + self.x = [[0.0f32; 80]; 2]; + } + + /// Encodes a single frame of linear PCM samples into `dst`. + /// `pcm` must contain `blocks * subbands * channels` interleaved 16-bit samples. + pub fn encode_frame(&mut self, pcm: &[i16], dst: &mut Vec) -> Result<(), Error> { + let subbands = self.subbands.count(); + let blocks = self.blocks.count(); + let expected_samples = blocks * subbands * self.channels; + if pcm.len() < expected_samples { + return Err(Error::TruncatedFrame { expected: expected_samples, actual: pcm.len() }); + } + + // Polyphase analysis filterbank + let mut sb_samples = [[[0.0f32; 8]; 2]; 16]; + for blk in 0..blocks { + for ch in 0..self.channels { + let mut channel_buf = [0i16; 8]; + for i in 0..subbands { + let idx = (blk * subbands + i) * self.channels + ch; + channel_buf[i] = pcm[idx]; + } + if subbands == 4 { + analysis_subbands_4(&mut self.x[ch], &channel_buf, &mut sb_samples[blk][ch]); + } else { + analysis_subbands_8(&mut self.x[ch], &channel_buf, &mut sb_samples[blk][ch]); + } + } + } + + // Scale factors and joint stereo decisions + let mut scale_factors = [[0u8; 8]; 2]; + let mut join = [false; 8]; + + if self.channels == 2 && self.channel_mode == ChannelMode::JointStereo { + for sb in 0..subbands { + let mut max_l = 0.0f32; + let mut max_r = 0.0f32; + for blk in 0..blocks { + let al = sb_samples[blk][0][sb].abs(); + let ar = sb_samples[blk][1][sb].abs(); + if al > max_l { + max_l = al; + } + if ar > max_r { + max_r = ar; + } + } + let sf_l = calculate_scalefactor(max_l); + let sf_r = calculate_scalefactor(max_r); + + if sb < subbands.saturating_sub(1) { + let mut max_mid = 0.0f32; + let mut max_side = 0.0f32; + for blk in 0..blocks { + let mid = (sb_samples[blk][0][sb] + sb_samples[blk][1][sb]) * 0.5; + let side = (sb_samples[blk][0][sb] - sb_samples[blk][1][sb]) * 0.5; + let am = mid.abs(); + let asd = side.abs(); + if am > max_mid { + max_mid = am; + } + if asd > max_side { + max_side = asd; + } + } + let sf_mid = calculate_scalefactor(max_mid); + let sf_side = calculate_scalefactor(max_side); + + if u16::from(sf_l) + u16::from(sf_r) > u16::from(sf_mid) + u16::from(sf_side) { + join[sb] = true; + scale_factors[0][sb] = sf_mid; + scale_factors[1][sb] = sf_side; + for blk in 0..blocks { + let mid = (sb_samples[blk][0][sb] + sb_samples[blk][1][sb]) * 0.5; + let side = (sb_samples[blk][0][sb] - sb_samples[blk][1][sb]) * 0.5; + sb_samples[blk][0][sb] = mid; + sb_samples[blk][1][sb] = side; + } + continue; + } + } + + scale_factors[0][sb] = sf_l; + scale_factors[1][sb] = sf_r; + } + } else { + for ch in 0..self.channels { + for sb in 0..subbands { + let mut max_val = 0.0f32; + for blk in 0..blocks { + let a = sb_samples[blk][ch][sb].abs(); + if a > max_val { + max_val = a; + } + } + scale_factors[ch][sb] = calculate_scalefactor(max_val); + } + } + } + + // Bit allocation + let freq = SamplingFrequency::from_hz(self.sample_rate)?; + let mut bits = [[0u8; 8]; 2]; + compute_bit_allocation( + subbands, + self.channels, + self.channel_mode, + freq.table_index(), + self.allocation_method, + u16::from(self.bitpool), + &scale_factors, + &mut bits, + ); + + // Quantization + let mut levels = [[0u32; 8]; 2]; + let mut scalefactors = [[0.0f32; 8]; 2]; + for ch in 0..self.channels { + for sb in 0..subbands { + levels[ch][sb] = (1u32 << bits[ch][sb]).saturating_sub(1); + scalefactors[ch][sb] = (1u32 << (u32::from(scale_factors[ch][sb]) + 1)) as f32; + } + } + + let mut quantized = [[[0u32; 8]; 2]; 16]; + for blk in 0..blocks { + for ch in 0..self.channels { + for sb in 0..subbands { + let lev = levels[ch][sb]; + if lev > 0 { + let norm = sb_samples[blk][ch][sb] / scalefactors[ch][sb]; + let val = ((norm + 1.0) * (lev as f32)) * 0.5; + let q = val.floor() as i32; + quantized[blk][ch][sb] = (q.clamp(0, lev as i32)) as u32; + } + } + } + } + + // Frame header bytes + let is_msbc = self.blocks == BlockCount::Blocks15; + let (syncword, b1, b2, crc, flen) = if is_msbc { + let crc = compute_header_crc(0x00, 0x00, ChannelMode::Mono, 8, 1, &[false; 8], &scale_factors); + (SYNCWORD_MSBC, 0x00, 0x00, crc, 57) + } else { + let b1 = (freq.code() << 6) + | (self.blocks.code() << 4) + | (self.channel_mode.code() << 2) + | (self.allocation_method.code() << 1) + | self.subbands.code(); + let b2 = self.bitpool; + let crc = compute_header_crc(b1, b2, self.channel_mode, subbands, self.channels, &join, &scale_factors); + let flen = Header { + syncword: SYNCWORD_SBC, + sampling_frequency: freq, + blocks: self.blocks, + channel_mode: self.channel_mode, + allocation_method: self.allocation_method, + subbands: self.subbands, + bitpool: self.bitpool, + crc_check: crc, + join, + } + .frame_length(); + (SYNCWORD_SBC, b1, b2, crc, flen) + }; + + let mut writer = BitWriter::with_capacity(flen)?; + writer.write_bits(u32::from(syncword), 8)?; + writer.write_bits(u32::from(b1), 8)?; + writer.write_bits(u32::from(b2), 8)?; + writer.write_bits(u32::from(crc), 8)?; + + if self.channel_mode == ChannelMode::JointStereo { + for sb in 0..(subbands.saturating_sub(1)) { + writer.write_bits(if join[sb] { 1 } else { 0 }, 1)?; + } + writer.write_bits(0, 1)?; // RFA + } + + for ch in 0..self.channels { + for sb in 0..subbands { + writer.write_bits(u32::from(scale_factors[ch][sb]), 4)?; + } + } + + for blk in 0..blocks { + for ch in 0..self.channels { + for sb in 0..subbands { + if bits[ch][sb] > 0 { + writer.write_bits(quantized[blk][ch][sb], bits[ch][sb] as usize)?; + } + } + } + } + + let frame_bytes = writer.finish()?; + dst.try_reserve(frame_bytes.len()).map_err(|_| Error::AllocationFailed)?; + dst.extend_from_slice(&frame_bytes); + Ok(()) + } + + /// Encodes a stream of linear PCM samples into concatenated SBC frames. + pub fn encode(&mut self, pcm: &[i16], dst: &mut Vec) -> Result<(), Error> { + if pcm.len() > MAX_SAMPLES { + return Err(Error::InputTooLarge); + } + let frame_samples = self.blocks.count() * self.subbands.count() * self.channels; + if !pcm.len().is_multiple_of(frame_samples) { + return Err(Error::TruncatedFrame { expected: frame_samples, actual: pcm.len() % frame_samples }); + } + let mut offset: usize = 0; + while offset.saturating_add(frame_samples) <= pcm.len() { + self.encode_frame(&pcm[offset..offset + frame_samples], dst)?; + offset = offset.saturating_add(frame_samples); + } + Ok(()) + } +} + +/// Decodes an entire SBC bitstream to 16-bit linear PCM and returns stream info. +pub fn decode(src: &[u8]) -> Result<(Vec, StreamInfo), Error> { + if src.is_empty() { + return Err(Error::TruncatedFrame { expected: 4, actual: 0 }); + } + let header = Header::parse(src)?; + let info = StreamInfo { + sample_rate: header.sample_rate(), + channels: header.channels(), + channel_mode: header.channel_mode, + blocks: header.blocks.count(), + subbands: header.subbands.count(), + bitpool: header.bitpool, + allocation_method: header.allocation_method, + }; + let mut decoder = Decoder::new(); + let mut dst = Vec::new(); + let mut offset = 0; + while offset < src.len() { + let frame_hdr = Header::parse(&src[offset..])?; + if frame_hdr.sample_rate() != info.sample_rate || frame_hdr.channels() != info.channels || frame_hdr.channel_mode != info.channel_mode { + return Err(Error::InvalidSampleRate(frame_hdr.sample_rate())); + } + let consumed = decoder.decode_frame(&src[offset..], &mut dst)?; + if consumed == 0 { + break; + } + offset = offset.saturating_add(consumed); + } + Ok((dst, info)) +} + +/// Encodes 16-bit linear PCM audio into an SBC bitstream using standard high-quality defaults. +pub fn encode(pcm: &[i16], sample_rate: u32, channels: usize, bitpool: u8) -> Result, Error> { + let mut encoder = Encoder::new( + sample_rate, + channels, + 16, // standard 16 blocks + 8, // standard 8 subbands + bitpool, + AllocationMethod::Loudness, + )?; + let mut dst = Vec::new(); + encoder.encode(pcm, &mut dst)?; + Ok(dst) +} diff --git a/crates/audio/src/wav_stream.rs b/crates/audio/src/wav_stream.rs new file mode 100644 index 000000000..9da2bdcc9 --- /dev/null +++ b/crates/audio/src/wav_stream.rs @@ -0,0 +1,119 @@ +//! Canonical RIFF/WAVE header and interleaved sample blocks for non-seekable sinks. +//! +//! Extracted from AniCraft's WAV writer, preserving its byte-level compatibility. +//! Unlike `FileEncoder`, these primitives do not require `Seek` and leave final lengths, +//! chunk padding and sink error handling to the caller. The caller must supply a complete +//! frame count and append exactly that many frames. For odd-length data, append a pad byte +//! (the header includes that padding in the RIFF size); current AniCraft output is always stereo (even). +//! Non-finite samples become silence, including float output. This deliberately differs +//! from the general encoder's infinity handling. Prefer `FileEncoder` for new seekable sinks. + +/// Sample formats written. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Bits { + /// Signed 16-bit PCM. + Int16, + /// Signed 24-bit PCM. + Int24, + /// IEEE 754 32-bit float. + Float32, +} + +impl Bits { + fn bytes(self) -> u16 { + match self { + Bits::Int16 => 2, + Bits::Int24 => 3, + Bits::Float32 => 4, + } + } +} + +const PCM: u16 = 1; +const FLOAT: u16 = 3; + +/// A WAV header for `frames` frames of `channels` channels. +/// Returns `None` for zero rate/channels or any size/byte-rate field overflow. +pub fn header(rate: u32, channels: u16, bits: Bits, frames: u64) -> Option> { + if rate == 0 || channels == 0 { + return None; + } + let b = bits.bytes(); + let data = u32::try_from(frames.checked_mul(u64::from(b) * u64::from(channels))?).ok()?; + let riff = data.checked_add(36)?.checked_add(data & 1)?; + let block = channels.checked_mul(b)?; + let byte_rate = rate.checked_mul(u32::from(block))?; + let tag = if bits == Bits::Float32 { FLOAT } else { PCM }; + let mut h = Vec::with_capacity(44); + h.extend_from_slice(b"RIFF"); + h.extend_from_slice(&riff.to_le_bytes()); + h.extend_from_slice(b"WAVEfmt "); + h.extend_from_slice(&16u32.to_le_bytes()); + h.extend_from_slice(&tag.to_le_bytes()); + h.extend_from_slice(&channels.to_le_bytes()); + h.extend_from_slice(&rate.to_le_bytes()); + h.extend_from_slice(&byte_rate.to_le_bytes()); + h.extend_from_slice(&block.to_le_bytes()); + h.extend_from_slice(&(b * 8).to_le_bytes()); + h.extend_from_slice(b"data"); + h.extend_from_slice(&data.to_le_bytes()); + Some(h) +} + +/// Appends samples in `bits` format. Integers are rounded and clipped to range; `x · 32768` +/// for 16-bit, so 16-bit sources read by [`crate::decode`] come back bit-identical. +pub fn append(out: &mut Vec, samples: &[f32], bits: Bits) { + out.reserve(samples.len() * usize::from(bits.bytes())); + for &s in samples { + let s = if s.is_finite() { s } else { 0.0 }; + match bits { + Bits::Int16 => out.extend_from_slice(&((s * 32_768.0).round().clamp(-32_768.0, 32_767.0) as i16).to_le_bytes()), + Bits::Int24 => { + let v = (f64::from(s) * 8_388_608.0).round().clamp(-8_388_608.0, 8_388_607.0) as i32; + out.extend_from_slice(v.to_le_bytes().get(..3).unwrap_or(&[0, 0, 0])); + } + Bits::Float32 => out.extend_from_slice(&s.to_le_bytes()), + } + } +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::indexing_slicing)] +mod tests { + use super::*; + + #[test] + fn canonical_header_and_odd_padding() { + let h = header(48_000, 2, Bits::Int16, 1).unwrap(); + assert_eq!(h.len(), 44); + assert_eq!(&h[..12], b"RIFF\x28\0\0\0WAVE"); + assert_eq!(&h[28..36], &[0, 238, 2, 0, 4, 0, 16, 0]); + let h = header(8_000, 1, Bits::Int24, 1).unwrap(); + assert_eq!(&h[4..8], &40u32.to_le_bytes()); + assert_eq!(&h[40..44], &3u32.to_le_bytes()); + assert!(header(0, 2, Bits::Int16, 1).is_none()); + assert!(header(48_000, 0, Bits::Int16, 1).is_none()); + assert!(header(u32::MAX, 2, Bits::Int16, 1).is_none()); + assert!(header(48_000, 2, Bits::Float32, u64::MAX).is_none()); + assert!(header(48_000, u16::MAX, Bits::Float32, 0).is_none()); + } + + #[test] + fn samples_preserve_rounding_clipping_silence_and_chunk_boundaries() { + let samples = [-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0, f32::NAN, f32::INFINITY, f32::NEG_INFINITY]; + let mut pcm = Vec::new(); + append(&mut pcm, &samples, Bits::Int16); + let expected = [-32768i16, -32768, -16384, 0, 16384, 32767, 32767, 0, 0, 0]; + assert_eq!(pcm, expected.iter().flat_map(|s| s.to_le_bytes()).collect::>()); + for bits in [Bits::Int16, Bits::Int24, Bits::Float32] { + let mut whole = Vec::new(); + append(&mut whole, &samples, bits); + let mut blocks = Vec::new(); + for block in samples.chunks(3) { + append(&mut blocks, block, bits); + } + assert_eq!(whole, blocks); + assert_eq!(&whole[whole.len() - 3 * usize::from(bits.bytes())..], vec![0; 3 * usize::from(bits.bytes())]); + } + } +} diff --git a/crates/audio/tests/gsm_oracle.rs b/crates/audio/tests/gsm_oracle.rs new file mode 100644 index 000000000..ec9c2f799 --- /dev/null +++ b/crates/audio/tests/gsm_oracle.rs @@ -0,0 +1,138 @@ +//! GSM 06.10 against ffmpeg's decoder as the reference (oracle; skipped when ffmpeg is absent, a +//! failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`): ffmpeg decodes what our encoder writes, in both +//! packings, to exactly the samples our decoder returns, and so does arbitrary frame data. + +use craft_audio::gsm::{Packing, decode, encode}; +use std::path::Path; +use std::process::Command; + +fn xorshift(seed: &mut u32) -> u32 { + *seed ^= *seed << 13; + *seed ^= *seed >> 17; + *seed ^= *seed << 5; + *seed +} + +fn signal(frames: usize) -> Vec { + let mut seed = 0x9E37_79B9u32; + (0..frames * 160) + .map(|i| { + let t = i as f64 / 8000.0; + let f0 = 110.0 + 40.0 * (t * 2.0).sin(); + let voiced = (t * f0 * std::f64::consts::TAU).sin() * 7000.0 + (t * 4.0 * f0 * std::f64::consts::TAU).sin() * 2000.0; + let noise = f64::from(xorshift(&mut seed) % 3000) - 1500.0; + // Voiced stretches, noise bursts, silence and full-scale clipping. + match (i / 1600) % 5 { + 0 | 1 => voiced as i16, + 2 => noise as i16, + 3 => 0, + _ => (voiced * 6.0).clamp(-32768.0, 32767.0) as i16, + } + }) + .collect() +} + +fn wav49_file(blocks: &[u8], samples: usize) -> Vec { + let mut fmt = Vec::new(); + fmt.extend_from_slice(&0x31u16.to_le_bytes()); // GSM 6.10 + fmt.extend_from_slice(&1u16.to_le_bytes()); + fmt.extend_from_slice(&8000u32.to_le_bytes()); + fmt.extend_from_slice(&1625u32.to_le_bytes()); + fmt.extend_from_slice(&65u16.to_le_bytes()); + fmt.extend_from_slice(&0u16.to_le_bytes()); + fmt.extend_from_slice(&2u16.to_le_bytes()); + fmt.extend_from_slice(&320u16.to_le_bytes()); + let mut body = b"WAVE".to_vec(); + body.extend_from_slice(b"fmt "); + body.extend_from_slice(&(fmt.len() as u32).to_le_bytes()); + body.extend_from_slice(&fmt); + body.extend_from_slice(b"fact"); + body.extend_from_slice(&4u32.to_le_bytes()); + body.extend_from_slice(&(samples as u32).to_le_bytes()); + body.extend_from_slice(b"data"); + body.extend_from_slice(&(blocks.len() as u32).to_le_bytes()); + body.extend_from_slice(blocks); + let mut out = b"RIFF".to_vec(); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend_from_slice(&body); + out +} + +fn ffmpeg_decode(ffmpeg: &Path, file: &Path, forced_format: Option<&str>) -> Vec { + let mut cmd = Command::new(ffmpeg); + cmd.args(["-v", "error", "-y"]); + if let Some(f) = forced_format { + cmd.args(["-f", f]); + } + let out = cmd.arg("-i").arg(file).args(["-f", "s16le", "-acodec", "pcm_s16le", "-"]).output().expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg failed on {}: {}", file.display(), String::from_utf8_lossy(&out.stderr)); + out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect() +} + +/// Frames whose long-term and RPE fields are arbitrary, over the log-area ratios of real speech. +/// Random log-area ratios give filters that overflow at every sample, where the specification's +/// saturating arithmetic and other decoders' differ; here every field of every sub-frame still +/// takes all of its values, with the amplitude and gain kept where the signal stays in range. +fn random_frames(n: usize, seed: &mut u32) -> Vec { + let speech = encode(&signal(n), Packing::Raw); + let mut out = Vec::new(); + for b in speech.as_chunks::<33>().0 { + let mut frame = craft_audio::gsm::Frame::from_raw(b).expect("frame"); + for s in &mut frame.sub { + s.lag = 40 + (xorshift(seed) % 81) as u8; + s.gain = (xorshift(seed) % 3) as u8; + s.grid = (xorshift(seed) & 3) as u8; + s.xmax = (xorshift(seed) % 26) as u8; + for p in &mut s.pulses { + *p = (xorshift(seed) & 7) as u8; + } + } + out.extend_from_slice(&frame.to_raw()); + } + out +} + +#[test] +fn ffmpeg_decodes_our_raw_streams_to_the_same_samples() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("gsm"); + for frames in [1usize, 2, 7, 50, 120] { + let bytes = encode(&signal(frames), Packing::Raw); + let file = dir.join(format!("ours_{frames}.gsm")); + std::fs::write(&file, &bytes).expect("write fixture"); + let theirs = ffmpeg_decode(&ffmpeg, &file, Some("gsm")); + let ours = decode(&bytes, Packing::Raw).expect("decode"); + assert_eq!(theirs.len(), ours.len(), "{frames} frames: sample count"); + assert!(theirs == ours, "{frames} frames: samples differ"); + } +} + +#[test] +fn ffmpeg_decodes_our_wav49_streams_to_the_same_samples() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("gsm"); + for frames in [2usize, 4, 20, 100] { + let pcm = signal(frames); + let bytes = encode(&pcm, Packing::Wav49); + let file = dir.join(format!("ours_{frames}.wav")); + std::fs::write(&file, wav49_file(&bytes, pcm.len())).expect("write fixture"); + let theirs = ffmpeg_decode(&ffmpeg, &file, None); + let ours = decode(&bytes, Packing::Wav49).expect("decode"); + assert_eq!(theirs.len(), ours.len(), "{frames} frames: sample count"); + assert!(theirs == ours, "{frames} frames: samples differ"); + } +} + +#[test] +fn ffmpeg_and_we_decode_arbitrary_frames_identically() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("gsm"); + let mut seed = 0xC0FF_EE11u32; + let bytes = random_frames(300, &mut seed); + let file = dir.join("random.gsm"); + std::fs::write(&file, &bytes).expect("write fixture"); + let theirs = ffmpeg_decode(&ffmpeg, &file, Some("gsm")); + let ours = decode(&bytes, Packing::Raw).expect("decode"); + assert_eq!(theirs.len(), ours.len()); + assert!(theirs == ours, "arbitrary frames decode differently"); +} diff --git a/crates/audio/tests/main.rs b/crates/audio/tests/main.rs index 225159cbd..dcba6e1d7 100644 --- a/crates/audio/tests/main.rs +++ b/crates/audio/tests/main.rs @@ -9,6 +9,12 @@ mod codecs; #[cfg(all(feature = "flac-decode", feature = "flac-encode"))] #[path = "flac_decode.rs"] mod flac_decode; +#[cfg(feature = "gsm")] +#[path = "gsm_oracle.rs"] +mod gsm_oracle; #[cfg(feature = "qoa")] #[path = "qoa_oracle.rs"] mod qoa_oracle; +#[cfg(feature = "sbc")] +#[path = "sbc_oracle.rs"] +mod sbc_oracle; diff --git a/crates/audio/tests/sbc_oracle.rs b/crates/audio/tests/sbc_oracle.rs new file mode 100644 index 000000000..62495ae16 --- /dev/null +++ b/crates/audio/tests/sbc_oracle.rs @@ -0,0 +1,298 @@ +#![cfg(feature = "sbc")] + +use craft_audio::sbc::{ + AllocationMethod, BlockCount, ChannelMode, Decoder, Encoder, Header, SYNCWORD_MSBC, SYNCWORD_SBC, SamplingFrequency, Subbands, decode, encode, +}; +use std::process::Command; + +fn pearson_correlation(x: &[i16], y: &[i16], lag: usize) -> f64 { + let n = x.len().min(y.len().saturating_sub(lag)); + if n == 0 { + return 0.0; + } + let x = &x[..n]; + let y = &y[lag..lag + n]; + let mean_x = x.iter().map(|&v| v as f64).sum::() / (n as f64); + let mean_y = y.iter().map(|&v| v as f64).sum::() / (n as f64); + let mut num = 0.0; + let mut denom_x = 0.0; + let mut denom_y = 0.0; + for (&a, &b) in x.iter().zip(y) { + let da = (a as f64) - mean_x; + let db = (b as f64) - mean_y; + num += da * db; + denom_x += da * da; + denom_y += db * db; + } + let denom = (denom_x * denom_y).sqrt(); + if denom > 0.0 { num / denom } else { 0.0 } +} + +fn generate_sine(sample_rate: u32, channels: usize, num_frames: usize) -> Vec { + let mut pcm = Vec::with_capacity(num_frames * channels); + for i in 0..num_frames { + let t = (i as f64) / (sample_rate as f64); + let s_l = (16000.0 * (2.0 * std::f64::consts::PI * 440.0 * t).sin()) as i16; + pcm.push(s_l); + if channels == 2 { + let s_r = (16000.0 * (2.0 * std::f64::consts::PI * 880.0 * t).sin()) as i16; + pcm.push(s_r); + } + } + pcm +} + +#[test] +fn test_header_parse_and_frame_length() { + // Standard SBC header: 0x9C, 44.1kHz, 16 blocks, Joint Stereo, Loudness, 8 subbands, bitpool 32 + let mut packet = vec![SYNCWORD_SBC, 0xBD, 32, 0x00]; + let header = Header::parse(&packet).expect("parse header"); + assert_eq!(header.sampling_frequency, SamplingFrequency::Freq44k1); + assert_eq!(header.blocks, BlockCount::Blocks16); + assert_eq!(header.channel_mode, ChannelMode::JointStereo); + assert_eq!(header.allocation_method, AllocationMethod::Loudness); + assert_eq!(header.subbands, Subbands::Subbands8); + assert_eq!(header.bitpool, 32); + assert_eq!(header.channels(), 2); + assert_eq!(header.samples_per_frame(), 128); + + // Frame length formula check + let expected_len = 4 + (4 * 8 * 2) / 8 + (8usize + 16 * 32).div_ceil(8); + assert_eq!(header.frame_length(), expected_len); + assert_eq!(header.frame_length(), 77); + + // mSBC header: 0xAD + packet[0] = SYNCWORD_MSBC; + let msbc_header = Header::parse(&packet).expect("parse msbc header"); + assert_eq!(msbc_header.frame_length(), 57); + assert_eq!(msbc_header.sample_rate(), 16000); + assert_eq!(msbc_header.channels(), 1); +} + +#[test] +fn test_silence_roundtrip() { + for &sample_rate in &[16_000u32, 32_000, 44_100, 48_000] { + for &channels in &[1usize, 2] { + for &subbands in &[4usize, 8] { + for &blocks in &[4usize, 8, 12, 16] { + let frame_samples = blocks * subbands * channels; + let pcm = vec![0i16; frame_samples * 4]; + let mut encoder = Encoder::new(sample_rate, channels, blocks, subbands, 32, AllocationMethod::Loudness).expect("create encoder"); + + let mut sbc_bytes = Vec::new(); + encoder.encode(&pcm, &mut sbc_bytes).expect("encode silence"); + assert!(!sbc_bytes.is_empty()); + + let mut decoder = Decoder::new(); + let mut decoded = Vec::new(); + decoder.decode(&sbc_bytes, &mut decoded).expect("decode silence"); + + assert_eq!(decoded.len(), pcm.len()); + for &s in &decoded { + assert!(s.abs() <= 16, "silence sample too large: {s}"); + } + } + } + } + } +} + +#[test] +fn test_sine_wave_roundtrip_fidelity() { + let sample_rate = 44_100u32; + let channels = 2usize; + let num_frames = 128 * 20; + let pcm = generate_sine(sample_rate, channels, num_frames); + + let sbc_bytes = encode(&pcm, sample_rate, channels, 32).expect("encode sine"); + let (decoded, info) = decode(&sbc_bytes).expect("decode sine"); + + assert_eq!(info.sample_rate, sample_rate); + assert_eq!(info.channels, channels); + assert_eq!(decoded.len(), pcm.len()); + + // 8-subband filterbank total delay is 73 samples per channel + let corr_l = pearson_correlation(&pcm[0..], &decoded[0..], 73 * 2); + assert!(corr_l > 0.98, "left channel correlation too low: {corr_l}"); +} + +#[test] +fn test_invalid_and_corrupt_inputs() { + let mut decoder = Decoder::new(); + let mut dst = Vec::new(); + + // Empty packet + assert!(decoder.decode_frame(&[], &mut dst).is_err()); + + // Too short header + assert!(decoder.decode_frame(&[SYNCWORD_SBC, 0x00], &mut dst).is_err()); + + // Invalid syncword + assert!(decoder.decode_frame(&[0x00, 0xBD, 32, 0x00], &mut dst).is_err()); + + // CRC mismatch + let bad_crc_frame = vec![SYNCWORD_SBC, 0xBD, 32, 0x55, 0x00, 0x00, 0x00, 0x00]; + assert!(decoder.decode_frame(&bad_crc_frame, &mut dst).is_err()); + + // Bitpool capacity exceeded (Mono 4 subbands capacity is 64; 65 must fail) + let bad_bitpool_frame = vec![SYNCWORD_SBC, 0x00, 65, 0x00]; + assert!(decoder.decode_frame(&bad_bitpool_frame, &mut dst).is_err()); + assert!(Encoder::new(16000, 1, 4, 4, 65, AllocationMethod::Loudness).is_err()); + + // Unsupported 15-block configuration (only 16kHz mono 8-subband 26-bitpool is valid mSBC) + assert!(Encoder::new(44100, 2, 15, 8, 32, AllocationMethod::Loudness).is_err()); + + // Incomplete PCM frames rejected by encoder + let mut enc = Encoder::new(44100, 2, 16, 8, 32, AllocationMethod::Loudness).expect("enc"); + let mut out = Vec::new(); + assert!(enc.encode(&[0i16; 100], &mut out).is_err()); + + // Valid mSBC encoding produces exact 57-byte mSBC frame + let mut msbc_enc = Encoder::new(16000, 1, 15, 8, 26, AllocationMethod::Loudness).expect("msbc enc"); + let mut msbc_out = Vec::new(); + msbc_enc.encode(&vec![0i16; 120], &mut msbc_out).expect("encode msbc frame"); + assert_eq!(msbc_out.len(), 57); + assert_eq!(msbc_out[0], SYNCWORD_MSBC); + + // Hostile fuzzed input should never panic + let mut seed = 0x1234_5678u32; + for len in [10, 50, 100, 500] { + let mut random_bytes = vec![0u8; len]; + for b in &mut random_bytes { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + *b = seed as u8; + } + let _ = decoder.decode(&random_bytes, &mut dst); + } +} + +#[test] +fn test_oracle_ffmpeg_cross_verification() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { + return; + }; + let dir = craft_media_testkit::fixture_subdir("sbc"); + + // 1. FFmpeg decodes our encoded SBC files + for &(sample_rate, channels) in &[(16_000u32, 1usize), (44_100, 2), (48_000, 2)] { + let pcm = generate_sine(sample_rate, channels, 128 * 20); + let sbc_bytes = encode(&pcm, sample_rate, channels, 32).expect("encode sbc"); + + let sbc_file = dir.join(format!("ours_{sample_rate}_{channels}.sbc")); + std::fs::write(&sbc_file, &sbc_bytes).expect("write sbc fixture"); + + let out = Command::new(&ffmpeg) + .args(["-v", "error", "-y", "-i"]) + .arg(&sbc_file) + .args(["-f", "s16le", "-acodec", "pcm_s16le", "-"]) + .output() + .expect("run ffmpeg"); + + assert!(out.status.success(), "ffmpeg failed to decode our SBC: {}", String::from_utf8_lossy(&out.stderr)); + + let theirs: Vec = out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect(); + + assert!(!theirs.is_empty()); + let corr = pearson_correlation(&pcm, &theirs, 73 * channels); + assert!(corr > 0.95, "FFmpeg decoded output correlation against input too low ({sample_rate} Hz, {channels} ch): {corr}"); + } + + // 2. Our decoder decodes FFmpeg-encoded SBC files + for &(sample_rate, channels) in &[(16_000u32, 1usize), (44_100, 2), (48_000, 2)] { + let ff_sbc_file = dir.join(format!("ff_{sample_rate}_{channels}.sbc")); + let ff_enc = Command::new(&ffmpeg) + .args([ + "-v", + "error", + "-y", + "-f", + "lavfi", + "-i", + &format!("sine=frequency=440:duration=0.2:sample_rate={sample_rate}"), + "-c:a", + "sbc", + "-ac", + &channels.to_string(), + ]) + .arg(&ff_sbc_file) + .output() + .expect("ffmpeg encode sbc"); + + assert!(ff_enc.status.success(), "ffmpeg failed to encode SBC: {}", String::from_utf8_lossy(&ff_enc.stderr)); + + let ff_sbc_bytes = std::fs::read(&ff_sbc_file).expect("read ff sbc fixture"); + let (decoded, info) = decode(&ff_sbc_bytes).expect("decode ff sbc"); + assert_eq!(info.sample_rate, sample_rate); + assert_eq!(info.channels, channels); + assert!(!decoded.is_empty()); + + // Decode ff_sbc_file with ffmpeg as well to compare + let ff_raw = Command::new(&ffmpeg) + .args(["-v", "error", "-y", "-i"]) + .arg(&ff_sbc_file) + .args(["-f", "s16le", "-acodec", "pcm_s16le", "-"]) + .output() + .expect("ffmpeg decode raw"); + + let ff_pcm: Vec = ff_raw.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect(); + + let corr = pearson_correlation(&decoded, &ff_pcm, 0); + assert!(corr > 0.99, "Our decoder output correlation against FFmpeg decoder output too low: {corr}"); + } + + // 3. Our decoder decodes FFmpeg-encoded mSBC wideband speech + let msbc_file = dir.join("ff_msbc.sbc"); + let ff_msbc_enc = Command::new(&ffmpeg) + .args(["-v", "error", "-y", "-f", "lavfi", "-i", "sine=frequency=440:duration=0.2:sample_rate=16000", "-c:a", "sbc", "-msbc", "1"]) + .arg(&msbc_file) + .output() + .expect("ffmpeg encode msbc"); + + assert!(ff_msbc_enc.status.success(), "ffmpeg failed to encode mSBC: {}", String::from_utf8_lossy(&ff_msbc_enc.stderr)); + + let msbc_bytes = std::fs::read(&msbc_file).expect("read msbc fixture"); + let (decoded, info) = decode(&msbc_bytes).expect("decode msbc"); + assert_eq!(info.sample_rate, 16000); + assert_eq!(info.channels, 1); + assert_eq!(info.blocks, 15); + assert_eq!(info.subbands, 8); + assert!(!decoded.is_empty()); + + let ff_msbc_raw = Command::new(&ffmpeg) + .args(["-v", "error", "-y", "-i"]) + .arg(&msbc_file) + .args(["-f", "s16le", "-acodec", "pcm_s16le", "-"]) + .output() + .expect("ffmpeg decode msbc raw"); + + let ff_msbc_pcm: Vec = ff_msbc_raw.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect(); + + let corr = pearson_correlation(&decoded, &ff_msbc_pcm, 0); + assert!(corr > 0.99, "mSBC decoded output correlation against FFmpeg decoder too low: {corr}"); + + // 4. FFmpeg decodes our encoded mSBC file + let msbc_sine = generate_sine(16000, 1, 120 * 20); + let mut our_msbc_enc = Encoder::new(16000, 1, 15, 8, 26, AllocationMethod::Loudness).expect("msbc enc"); + let mut our_msbc_bytes = Vec::new(); + our_msbc_enc.encode(&msbc_sine, &mut our_msbc_bytes).expect("encode msbc"); + + let our_msbc_file = dir.join("ours_msbc.sbc"); + std::fs::write(&our_msbc_file, &our_msbc_bytes).expect("write our msbc fixture"); + + let our_msbc_out = Command::new(&ffmpeg) + .args(["-v", "error", "-y", "-i"]) + .arg(&our_msbc_file) + .args(["-f", "s16le", "-acodec", "pcm_s16le", "-"]) + .output() + .expect("ffmpeg decode our msbc"); + + assert!(our_msbc_out.status.success(), "ffmpeg failed to decode our mSBC: {}", String::from_utf8_lossy(&our_msbc_out.stderr)); + + let our_msbc_theirs: Vec = our_msbc_out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect(); + + assert!(!our_msbc_theirs.is_empty()); + let corr_msbc = pearson_correlation(&msbc_sine, &our_msbc_theirs, 73); + assert!(corr_msbc > 0.95, "FFmpeg decoded output correlation against our mSBC input too low: {corr_msbc}"); +} diff --git a/crates/captions/src/jacosub.rs b/crates/captions/src/jacosub.rs new file mode 100644 index 000000000..30233b6f1 --- /dev/null +++ b/crates/captions/src/jacosub.rs @@ -0,0 +1,242 @@ +//! JACOsub (`.jss`, `.js`): multi-feature scriptable subtitle format. +//! +//! ```text +//! # JACOsub file +//! #TimeRes 100 +//! 0:00:01.00 0:00:03.00 D0 First line\nsecond line +//! ``` +//! +//! Lines beginning with `#` are comments or header directives. Timed caption lines begin with +//! `START STOP [DIRECTIVE] Text`, where timestamps are `H:MM:SS.FF` (or `@units`) relative to +//! the declared `#TimeRes` (default 30). Inside the text, `\n` represents a newline. + +use crate::text::{escape_markup, ms_to_tick, plain_text, tick_to_ms}; +use crate::{Cue, Document, Error, Result}; + +/// Parse a JACOsub timestamp `H:MM:SS.FF` or `@units` into milliseconds. +pub(crate) fn parse_time(s: &str, time_res: i64) -> Option { + let s = s.trim(); + if s.is_empty() { + return None; + } + + if let Some(units_str) = s.strip_prefix('@') { + let units: i64 = units_str.trim().parse().ok()?; + let time_res = if time_res > 0 { time_res } else { 30 }; + return Some(units.saturating_mul(1000) / time_res); + } + + let (h_str, rest) = s.split_once(':')?; + let (m_str, rest) = rest.split_once(':')?; + let (s_str, f_str) = rest.split_once('.')?; + + let h: i64 = h_str.trim().parse().ok()?; + let m: i64 = m_str.trim().parse().ok()?; + let sec: i64 = s_str.trim().parse().ok()?; + let frac: i64 = f_str.trim().parse().ok()?; + + let time_res = if time_res > 0 { time_res } else { 30 }; + let frac_ms = frac.saturating_mul(1000) / time_res; + + let total_secs = h.saturating_mul(3600).saturating_add(m.saturating_mul(60)).saturating_add(sec); + Some(total_secs.saturating_mul(1000).saturating_add(frac_ms)) +} + +/// Format milliseconds into JACOsub timestamp string `H:MM:SS.cc` (at 100 centiseconds/sec). +pub(crate) fn format_time(ms: i64) -> String { + let ms = ms.max(0); + let total_secs = ms / 1000; + let centis = (ms % 1000) / 10; + let hours = total_secs / 3600; + let mins = (total_secs % 3600) / 60; + let secs = total_secs % 60; + + format!("{hours}:{mins:02}:{secs:02}.{centis:02}") +} + +/// Check if a line is a JACOsub timed caption line: `H:MM:SS.FF H:MM:SS.FF ...` or `@units @units ...`. +pub(crate) fn is_timed_line(line: &str) -> bool { + let line = line.trim(); + if line.starts_with('#') { + return false; + } + let mut parts = line.split_whitespace(); + let Some(t0) = parts.next() else { return false }; + let Some(t1) = parts.next() else { return false }; + (t0.contains(':') && t0.contains('.') && t1.contains(':') && t1.contains('.')) || (t0.starts_with('@') && t1.starts_with('@')) +} + +pub fn parse(text: &str) -> Result { + let mut doc = Document::default(); + let mut time_res: i64 = 30; // standard default + let mut shift_ms: i64 = 0; + let mut found = false; + + for (n, line) in text.lines().enumerate() { + let line = line.trim(); + if line.is_empty() { + continue; + } + + if let Some(cmd) = line.strip_prefix('#') { + let cmd_lower = cmd.to_ascii_lowercase(); + if cmd_lower.starts_with("timeres") || cmd_lower.starts_with('t') { + let parts: Vec<&str> = cmd.split_whitespace().collect(); + if parts.len() >= 2 + && let Ok(res) = parts[1].parse::() + && res > 0 + { + time_res = res; + } + } else if cmd_lower.starts_with("shift") || cmd_lower.starts_with('s') { + let parts: Vec<&str> = cmd.split_whitespace().collect(); + if parts.len() >= 2 { + let s_val = parts[1].trim(); + let (neg, s_raw) = if let Some(stripped) = s_val.strip_prefix('-') { + (true, stripped) + } else if let Some(stripped) = s_val.strip_prefix('+') { + (false, stripped) + } else { + (false, s_val) + }; + if let Some(ms) = parse_time(s_raw, time_res) { + shift_ms = if neg { -ms } else { ms }; + } + } + } + continue; + } + + if !is_timed_line(line) { + continue; + } + + let mut parts = line.split_whitespace(); + let Some(start_str) = parts.next() else { continue }; + let Some(end_str) = parts.next() else { continue }; + + let start_ms = match parse_time(start_str, time_res) { + Some(ms) => ms.saturating_add(shift_ms).max(0), + None => { + doc.warnings.push(format!("line {}: invalid start timestamp, skipped", n + 1)); + continue; + } + }; + let end_ms = match parse_time(end_str, time_res) { + Some(ms) => ms.saturating_add(shift_ms).max(0), + None => { + doc.warnings.push(format!("line {}: invalid end timestamp, skipped", n + 1)); + continue; + } + }; + + found = true; + + // Skip to after the end timestamp in the original line to preserve exact spaces + let after_end_pos = match line.find(end_str) { + Some(idx) => idx + end_str.len(), + None => continue, + }; + let remainder = &line[after_end_pos..]; + let trimmed_rem = remainder.trim_start(); + + // Check if there is a directive prefix: either [Directive] or a single non-space word starting with D + let text_part = if let Some(stripped) = trimmed_rem.strip_prefix('[') { + match stripped.find(']') { + Some(end_bracket) => stripped[end_bracket + 1..].trim_start(), + None => trimmed_rem, + } + } else if let Some(space_idx) = trimmed_rem.find(' ') { + let first_token = &trimmed_rem[..space_idx]; + if first_token.starts_with('D') && first_token[1..].chars().all(|c| c.is_ascii_digit()) { + trimmed_rem[space_idx + 1..].trim_start() + } else { + trimmed_rem + } + } else { + trimmed_rem + }; + + // Text parsing: `\n` represents newline + let raw_lines: Vec<&str> = text_part.split(r"\n").map(str::trim).filter(|l| !l.is_empty()).collect(); + let lines: Vec = raw_lines.into_iter().map(escape_markup).collect(); + + let start = ms_to_tick(start_ms); + let end = ms_to_tick(end_ms); + if end <= start || lines.is_empty() { + doc.warnings.push(format!("line {}: empty or ends before it starts; skipped", n + 1)); + continue; + } + + doc.cues.push(Cue { start, end, text: lines.join("\n"), ..Default::default() }); + } + + if !found && !text.trim().is_empty() { + return Err(Error::NotFormat("JACOsub")); + } + + Ok(doc) +} + +pub fn write(doc: &Document) -> String { + let mut out = String::new(); + out.push_str("# JACOsub file\n"); + out.push_str("#TimeRes 100\n\n"); + + for c in &doc.cues { + let start_ms = tick_to_ms(c.start); + let end_ms = tick_to_ms(c.end).max(start_ms + 10); + let raw_lines = plain_text(&c.text); + let lines: Vec<&str> = raw_lines.lines().map(str::trim).filter(|l| !l.is_empty()).collect(); + + if !lines.is_empty() { + let text = lines.join(r"\n"); + out.push_str(&format!("{} {} D0 {}\n", format_time(start_ms), format_time(end_ms), text)); + } + } + + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_and_write_roundtrip() { + let jss = "# JACOsub file\n#TimeRes 100\n\n0:00:01.00 0:00:03.00 D0 First line\\nsecond line\n0:00:04.50 0:00:06.00 D0 Second cue\n"; + let doc = parse(jss).expect("parse"); + assert_eq!(doc.cues.len(), 2); + assert_eq!(doc.cues[0].start, ms_to_tick(1000)); + assert_eq!(doc.cues[0].end, ms_to_tick(3000)); + assert_eq!(doc.cues[0].text, "First line\nsecond line"); + assert_eq!(doc.cues[1].start, ms_to_tick(4500)); + assert_eq!(doc.cues[1].end, ms_to_tick(6000)); + assert_eq!(doc.cues[1].text, "Second cue"); + + let written = write(&doc); + assert_eq!(written, jss); + } + + #[test] + fn parse_bracketed_directive() { + let jss = "#TimeRes 100\n0:00:01.00 0:00:03.00 [Style1] Bracketed style text\n"; + let doc = parse(jss).expect("parse"); + assert_eq!(doc.cues.len(), 1); + assert_eq!(doc.cues[0].text, "Bracketed style text"); + } + + #[test] + fn negative_shift_under_one_hour() { + let jss = "#TimeRes 100\n#Shift -0:00:01.00\n0:00:02.00 0:00:04.00 D0 Shifted line\n"; + let doc = parse(jss).expect("parse"); + assert_eq!(doc.cues.len(), 1); + assert_eq!(doc.cues[0].start, ms_to_tick(1000)); + assert_eq!(doc.cues[0].end, ms_to_tick(3000)); + } + + #[test] + fn rejects_non_jacosub() { + assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nHello\n").is_err()); + } +} diff --git a/crates/captions/src/lib.rs b/crates/captions/src/lib.rs index 44789541a..9d085854d 100644 --- a/crates/captions/src/lib.rs +++ b/crates/captions/src/lib.rs @@ -34,7 +34,10 @@ struct ReadmeExamples; pub mod ass; pub mod cea608; +pub mod jacosub; pub mod mcc; +pub mod pjs; +pub mod realtext; pub mod scc; pub mod srt; pub mod stl; @@ -72,10 +75,28 @@ pub enum Format { Dfxp, /// Advanced SubStation Alpha (also reads SubStation Alpha). Ass, + /// Phoenix Japanimation Society (PJS) subtitles. + Pjs, + /// RealText streaming timed text markup. + RealText, + /// JACOsub scriptable subtitles. + JacoSub, } impl Format { - pub const ALL: [Format; 8] = [Format::Srt, Format::WebVtt, Format::Scc, Format::Mcc, Format::Stl, Format::Ttml, Format::Dfxp, Format::Ass]; + pub const ALL: [Format; 11] = [ + Format::Srt, + Format::WebVtt, + Format::Scc, + Format::Mcc, + Format::Stl, + Format::Ttml, + Format::Dfxp, + Format::Ass, + Format::Pjs, + Format::RealText, + Format::JacoSub, + ]; pub fn name(self) -> &'static str { match self { Format::Srt => "SubRip (SRT)", @@ -86,6 +107,9 @@ impl Format { Format::Ttml => "TTML (IMSC1)", Format::Dfxp => "DFXP (TTML1)", Format::Ass => "Advanced SubStation Alpha (ASS)", + Format::Pjs => "PJS", + Format::RealText => "RealText", + Format::JacoSub => "JACOsub", } } pub fn extension(self) -> &'static str { @@ -98,6 +122,9 @@ impl Format { Format::Ttml => "ttml", Format::Dfxp => "dfxp", Format::Ass => "ass", + Format::Pjs => "pjs", + Format::RealText => "rt", + Format::JacoSub => "jss", } } pub fn from_name(s: &str) -> Option { @@ -110,6 +137,9 @@ impl Format { "ttml" | "imsc" | "imsc1" | "xml" => Some(Format::Ttml), "dfxp" => Some(Format::Dfxp), "ass" | "ssa" => Some(Format::Ass), + "pjs" => Some(Format::Pjs), + "rt" | "realtext" => Some(Format::RealText), + "jss" | "js" | "jacosub" => Some(Format::JacoSub), _ => None, } } @@ -260,6 +290,16 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option { if head.starts_with("[Script Info]") { return Some(Format::Ass); } + if realtext::is_realtext(head) { + return Some(Format::RealText); + } + let first_lines: Vec<&str> = head.lines().map(str::trim).filter(|l| !l.is_empty()).take(3).collect(); + if first_lines.first().is_some_and(|l| pjs::split_cue(l).is_some()) { + return Some(Format::Pjs); + } + if first_lines.iter().any(|l| jacosub::is_timed_line(l)) || (head.starts_with('#') && (head.contains("TimeRes") || head.contains("#T"))) { + return Some(Format::JacoSub); + } let ext = ext.map(|e| e.trim_start_matches('.').to_ascii_lowercase()); if (head.starts_with('<') && (head.contains("") || head.contains(":tt "))) && (head.contains("ns/ttml") || head.contains("ttaf1")) @@ -271,7 +311,7 @@ pub fn detect(bytes: &[u8], ext: Option<&str>) -> Option { match ext.as_deref() { Some("srt") if has_arrow_timing || head.is_empty() => Some(Format::Srt), Some("vtt") if has_arrow_timing => Some(Format::WebVtt), - Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa") => None, + Some("srt" | "vtt" | "scc" | "mcc" | "stl" | "ttml" | "dfxp" | "xml" | "ass" | "ssa" | "pjs" | "rt" | "jss" | "js") => None, _ if has_arrow_timing => Some(Format::Srt), _ => None, } @@ -306,6 +346,9 @@ fn parse_unbounded(bytes: &[u8], format: Format) -> Result { Format::Mcc => mcc::parse(&text), Format::Ttml | Format::Dfxp => ttml::parse(&text), Format::Ass => ass::parse(&text), + Format::Pjs => pjs::parse(&text), + Format::RealText => realtext::parse(&text), + Format::JacoSub => jacosub::parse(&text), Format::Stl => stl::parse(bytes), } } @@ -344,6 +387,9 @@ pub fn write(doc: &Document, format: Format, opts: WriteOptions) -> Vec { Format::Ttml => ttml::write(doc, ttml::Flavor::Imsc1, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(), Format::Dfxp => ttml::write(doc, ttml::Flavor::Dfxp, opts.rate, doc.language.as_deref().unwrap_or("")).into_bytes(), Format::Ass => ass::write(doc).into_bytes(), + Format::Pjs => pjs::write(doc).into_bytes(), + Format::RealText => realtext::write(doc).into_bytes(), + Format::JacoSub => jacosub::write(doc).into_bytes(), } } diff --git a/crates/captions/src/pjs.rs b/crates/captions/src/pjs.rs new file mode 100644 index 000000000..91874941b --- /dev/null +++ b/crates/captions/src/pjs.rs @@ -0,0 +1,102 @@ +//! Phoenix Japanimation Society (PJS) subtitles (`.pjs`): times in tenths of a second. +//! +//! ```text +//! 10,50,"First line" +//! 60,120,"Second line|with break" +//! ``` +//! +//! Lines are comma-separated `start,end,"text"` with times in deciseconds (tenths of a second). +//! Inside the quotes, `|` breaks a line. + +use crate::text::{escape_markup, ms_to_tick, number, plain_lines, tick_to_ms}; +use crate::{Cue, Document, Error, Result}; + +/// `start,end,"text"` as its parts. +pub(crate) fn split_cue(line: &str) -> Option<(i64, i64, &str)> { + let (start_str, rest) = line.split_once(',')?; + let (end_str, text_str) = rest.split_once(',')?; + let start = number(start_str.trim())?; + let end = number(end_str.trim())?; + let text = text_str.trim(); + let text = if let Some(stripped) = text.strip_prefix('"').and_then(|t| t.strip_suffix('"')) { stripped } else { text }; + Some((start, end, text)) +} + +/// Parse a PJS subtitle document. +pub fn parse(text: &str) -> Result { + let mut doc = Document::default(); + let mut found = false; + for (n, line) in text.lines().enumerate() { + let line = line.trim(); + if line.is_empty() { + continue; + } + let Some((start_ds, end_ds, raw)) = split_cue(line) else { + doc.warnings.push(format!("line {}: not a PJS cue, skipped", n + 1)); + continue; + }; + found = true; + let mut lines = Vec::new(); + for l in raw.split('|') { + let trimmed = l.trim(); + if !trimmed.is_empty() { + let unquoted = trimmed.replace("\"\"", "\""); + lines.push(escape_markup(&unquoted)); + } + } + let start = ms_to_tick(start_ds.saturating_mul(100)); + let end = ms_to_tick(end_ds.saturating_mul(100)); + if end <= start || lines.is_empty() { + doc.warnings.push(format!("line {}: empty or ends before it starts; skipped", n + 1)); + continue; + } + doc.cues.push(Cue { start, end, text: lines.join("\n"), ..Default::default() }); + } + if !found && !text.trim().is_empty() { + return Err(Error::NotFormat("PJS")); + } + Ok(doc) +} + +/// Serialize a document into PJS format. +pub fn write(doc: &Document) -> String { + let mut out = String::new(); + let ds = |t| (tick_to_ms(t) + 50) / 100; + for c in &doc.cues { + let start = ds(c.start); + let end = ds(c.end).max(start + 1); + let lines: Vec = + plain_lines(&c.text).into_iter().map(|l| l.replace('"', "\"\"").replace('|', "|")).filter(|l| !l.is_empty()).collect(); + if !lines.is_empty() { + out.push_str(&format!("{start},{end},\"{}\"\n", lines.join("|"))); + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_and_write_roundtrip() { + let pjs = "10,50,\"First line\"\n60,120,\"Second line|with break\"\n130,150,\"Say \"\"Hi\"\"\"\n"; + let doc = parse(pjs).expect("parse"); + assert_eq!(doc.cues.len(), 3); + assert_eq!(doc.cues[0].start, ms_to_tick(1000)); + assert_eq!(doc.cues[0].end, ms_to_tick(5000)); + assert_eq!(doc.cues[0].text, "First line"); + assert_eq!(doc.cues[1].start, ms_to_tick(6000)); + assert_eq!(doc.cues[1].end, ms_to_tick(12000)); + assert_eq!(doc.cues[1].text, "Second line\nwith break"); + assert_eq!(doc.cues[2].text, "Say \"Hi\""); + + let written = write(&doc); + assert_eq!(written, pjs); + } + + #[test] + fn rejects_non_pjs() { + assert!(parse("1\n00:00:01,000 --> 00:00:02,000\nHello\n").is_err()); + } +} diff --git a/crates/captions/src/realtext.rs b/crates/captions/src/realtext.rs new file mode 100644 index 000000000..1c4328a3b --- /dev/null +++ b/crates/captions/src/realtext.rs @@ -0,0 +1,247 @@ +//! RealText (`.rt`): streaming HTML-like timed text markup. +//! +//! ```text +//! +//! +//! ``` +//! +//! RealText documents encapsulate timed text inside a `` container with `