diff --git a/.config/nextest.toml b/.config/nextest.toml index 1037b23108..0bc679e473 100644 --- a/.config/nextest.toml +++ b/.config/nextest.toml @@ -1,7 +1,6 @@ # 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`. +# binaries. Every leg of the Workspace gate uses it (.github/workflows/workspace.yml); local runs may +# keep `cargo test`. Nextest does not run doctests, so the gate also runs `cargo test --doc`. # # cargo nextest run --workspace --profile ci # cargo test --workspace --doc @@ -22,6 +21,9 @@ 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 } +# craft-video integration tests on Linux and macOS: the binaries generate and read the same fixture +# directories (see the `ci` overrides below). +video-integration = { 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/))' @@ -42,3 +44,18 @@ test-group = 'print' fail-fast = false slow-timeout = { period = "60s" } failure-output = "immediate-final" + + +# Linux and macOS (the resource audit in craft-libs#2058): every GPU consumer shares the one `gpu` +# slot, and the craft-video integration binaries share their fixture directories. Conservative on +# purpose: the in-process mutexes these tests rely on do not reach across nextest's processes. +# Windows keeps the default groups above, which its leg has passed with since it moved to nextest. +[[profile.ci.overrides]] +platform = 'cfg(unix)' +filter = 'package(craft-ui) | package(craft-keymap) | package(craft-update)' +test-group = 'gpu' + +[[profile.ci.overrides]] +platform = 'cfg(unix)' +filter = 'package(craft-video) & kind(test)' +test-group = 'video-integration' diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock index 02cdf71c82..5996d0d2f7 100644 --- a/.github/workflows/actions.lock +++ b/.github/workflows/actions.lock @@ -18,7 +18,6 @@ workflows: - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' '.github/workflows/cms.yml': - 'actions/checkout@v4.4.0' - 'dtolnay/rust-toolchain@stable' @@ -27,7 +26,6 @@ workflows: - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' '.github/workflows/dialogs.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' @@ -36,10 +34,6 @@ workflows: - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - '.github/workflows/docpath.yml': - - 'actions/checkout@v7.0.1' - - 'step-security/dtolnay-rust-toolchain@v1.0.0' - - 'step-security/harden-runner@v2.22.4' '.github/workflows/ecproj-format.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' @@ -52,22 +46,16 @@ workflows: - 'actions/checkout@v7.0.1' - 'dtolnay/rust-toolchain@stable' - 'step-security/harden-runner@v2.22.4' - - 'swatinem/rust-cache@v2.9.3' + '.github/workflows/full-validation.yml': [] '.github/workflows/house-rules.yml': [] - '.github/workflows/i18n.yml': - - 'actions/checkout@v7.0.1' - - 'step-security/dtolnay-rust-toolchain@v1.0.0' - - 'step-security/harden-runner@v2.22.4' '.github/workflows/jpeg.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' '.github/workflows/keymap.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' '.github/workflows/lockfile.yml': - 'actions/checkout@v7.0.1' - 'step-security/harden-runner@v2.22.4' @@ -75,11 +63,10 @@ workflows: - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' '.github/workflows/menu.yml': - 'actions/checkout@v7.0.1' - 'dtolnay/rust-toolchain@stable' - - 'swatinem/rust-cache@v2.9.3' + '.github/workflows/nightly.yml': [] '.github/workflows/pcraft-format.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' @@ -89,23 +76,12 @@ workflows: - 'actions/setup-python@v7.1.0' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' - 'step-security/taiki-e-install-action@v2.87.32' - '.github/workflows/print.yml': - - 'actions/checkout@v7.0.1' - - 'step-security/dtolnay-rust-toolchain@v1.0.0' - - 'step-security/harden-runner@v2.22.4' '.github/workflows/proof.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' '.github/workflows/sonarcloud.yml': [] - '.github/workflows/ui.yml': - - 'actions/checkout@v7.0.1' - - 'step-security/dtolnay-rust-toolchain@v1.0.0' - - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' '.github/workflows/update.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' @@ -118,7 +94,6 @@ workflows: - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' '.github/workflows/web-api.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' @@ -128,13 +103,15 @@ workflows: - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - - 'step-security/rust-cache@v2.9.2' - 'step-security/taiki-e-install-action@v2.87.32' '.github/workflows/window.yml': - 'actions/checkout@v7.0.1' - 'step-security/dtolnay-rust-toolchain@v1.0.0' - 'step-security/harden-runner@v2.22.4' - '.github/workflows/workspace.yml': [] + '.github/workflows/workspace.yml': + - 'actions/checkout@v7.0.1' + - 'step-security/harden-runner@v2.22.4' + - 'storytold/craft-common-actions@d65d611f0ef67eaa94039351be5afce61102dbb0' dependencies: 'actions/cache@v6.1.0': ref: 'v6.1.0' @@ -171,18 +148,13 @@ dependencies: commit: 'sha1-1d88384fd173af6ecb012dfd0264a306b711241a' owner_id: 88700172 repo_id: 422287306 - 'step-security/rust-cache@v2.9.2': - ref: 'v2.9.2' - commit: 'sha1-8871c8ba063416da7c32246932ea6ae08700453e' - owner_id: 88700172 - repo_id: 822636713 'step-security/taiki-e-install-action@v2.87.32': ref: 'v2.87.32' commit: 'sha1-76747d22a7567cc4438546a84f11bdf18e670ab8' owner_id: 88700172 repo_id: 1382753260 - 'swatinem/rust-cache@v2.9.3': - ref: 'v2.9.3' - commit: 'sha1-d1527c8f67ef9af357290475d4af4b0fab8f12e0' - owner_id: 580492 - repo_id: 298565987 + 'storytold/craft-common-actions@d65d611f0ef67eaa94039351be5afce61102dbb0': + ref: 'main' + commit: 'sha1-d65d611f0ef67eaa94039351be5afce61102dbb0' + owner_id: 76897702 + repo_id: 1414429108 diff --git a/.github/workflows/ai-core.yml b/.github/workflows/ai-core.yml index 5471bf1ec0..2be123b233 100644 --- a/.github/workflows/ai-core.yml +++ b/.github/workflows/ai-core.yml @@ -12,20 +12,17 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: ai-core-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: ai-core-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests run in the Workspace gate on every OS; this checks what it does not. jobs: ai-core: + name: AI model WASM timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -35,10 +32,6 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy targets: wasm32-unknown-unknown - - run: cargo fmt --check - - run: cargo clippy -p craft-ai-core --all-targets --locked -- -D warnings - - run: cargo test -p craft-ai-core --locked # The model promises to compile for the web. - run: cargo check -p craft-ai-core --target wasm32-unknown-unknown --locked diff --git a/.github/workflows/atomic.yml b/.github/workflows/atomic.yml index f72a64f1e1..23de7f9f4f 100644 --- a/.github/workflows/atomic.yml +++ b/.github/workflows/atomic.yml @@ -12,20 +12,17 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: atomic-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: atomic-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests run in the Workspace gate on every OS; this runs what it does not. jobs: atomic: + name: Atomic inherited Linux ACLs timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -35,15 +32,9 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy - name: Install ACL tools - if: runner.os == 'Linux' run: sudo apt-get update && sudo apt-get install -y acl - - run: cargo fmt --check - - run: cargo clippy -p craft-atomic -p craft-settings --all-targets --locked -- -D warnings - - run: cargo test -p craft-atomic -p craft-settings --locked -- --nocapture - name: Check inherited Linux ACLs - if: runner.os == 'Linux' run: cargo test -p craft-atomic --locked -- --ignored --nocapture wasm: diff --git a/.github/workflows/audio.yml b/.github/workflows/audio.yml index d6a7ff5e25..5fd971c629 100644 --- a/.github/workflows/audio.yml +++ b/.github/workflows/audio.yml @@ -11,17 +11,22 @@ on: permissions: contents: read +# A fraction of the fuzz-style cases, as in the Workspace gate; nightly.yml runs them in full. +env: + CRAFT_HOSTILE_DIVISOR: "8" + PROPTEST_CASES: "64" + concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: shared-audio-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: shared-audio-${{ github.ref }} + cancel-in-progress: true jobs: lint: timeout-minutes: 15 - name: Audio lint, feature sets, docs, WASM + # fmt, clippy with all features and the minimum Rust version run in the Workspace gate. + name: Audio feature sets, docs, WASM runs-on: ubuntu-latest steps: - name: Harden runner @@ -34,13 +39,8 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy + components: clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-audio-lint - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo fmt -p craft-audio --check # Each feature on its own, so code that needs another feature fails here. - name: Clippy, each feature alone run: | @@ -49,7 +49,6 @@ jobs: cargo clippy -p craft-audio --all-targets --locked --no-default-features ${f:+--features "$f"} -- -D warnings echo "::endgroup::" done - - run: cargo clippy -p craft-audio --all-targets --locked --all-features -- -D warnings # missing_docs is a warning in the crate; -D warnings makes it (and broken links) fail here. - run: cargo doc -p craft-audio --all-features --no-deps --locked env: @@ -63,25 +62,6 @@ jobs: RUSTDOCFLAGS: "-D warnings" - run: cargo check -p craft-audio --all-features --locked --target wasm32-unknown-unknown - run: cargo check -p craft-audio --no-default-features --features lossless-encode --locked --target wasm32-unknown-unknown - msrv: - timeout-minutes: 10 - name: Audio on the minimum Rust version - runs-on: ubuntu-latest - steps: - - name: Harden runner - uses: step-security/harden-runner@v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@v7.0.1 - with: - persist-credentials: false - # The workspace's rust-version. - - run: rustup toolchain install 1.95.0 --profile minimal - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-audio-msrv - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo +1.95.0 check -p craft-audio -p craft-media --all-features --locked features: timeout-minutes: 10 name: Audio tests without symphonia and lossless-encode only @@ -97,10 +77,6 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-audio-features - save-if: ${{ github.ref == 'refs/heads/main' }} - run: cargo test -p craft-audio --no-default-features --features lossless-encode --locked - run: cargo test -p craft-audio --no-default-features --features pcm-encode --locked --lib - run: cargo test -p craft-audio --no-default-features --features flac-encode --locked --lib @@ -122,10 +98,6 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-audio-test - save-if: ${{ github.ref == 'refs/heads/main' }} - uses: actions/cache@v6.1.0 with: path: ${{ runner.temp }}/media-fixtures/opus/vectors diff --git a/.github/workflows/cms.yml b/.github/workflows/cms.yml index 74ce0815fa..9220d8e098 100644 --- a/.github/workflows/cms.yml +++ b/.github/workflows/cms.yml @@ -11,6 +11,13 @@ on: permissions: contents: read +concurrency: + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: cms-${{ github.ref }} + cancel-in-progress: true + +# clippy and the debug tests run in the Workspace gate; this runs the release build of the tests. jobs: cms: timeout-minutes: 20 @@ -18,9 +25,6 @@ jobs: steps: - uses: actions/checkout@v4.4.0 - uses: dtolnay/rust-toolchain@stable - with: - components: clippy - - run: cargo clippy -p craft-cms --all-targets --all-features --locked -- -D warnings # Release: the FOGRA39 fit and ink-limit tests take about 35 s in debug and 4 s here. Tests that need # installed system profiles print "skipped:" when none are present. - run: cargo test -p craft-cms --all-features --release --locked -- --nocapture diff --git a/.github/workflows/crw.yml b/.github/workflows/crw.yml index 54be7ba3ba..466b6c05d7 100644 --- a/.github/workflows/crw.yml +++ b/.github/workflows/crw.yml @@ -12,41 +12,13 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: shared-crw-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: shared-crw-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests run in the Workspace gate on every OS; this checks what it does not. jobs: - native: - timeout-minutes: 20 - name: CRW lint and tests (${{ matrix.os }}) - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, windows-latest] - runs-on: ${{ matrix.os }} - steps: - - name: Harden runner - uses: step-security/harden-runner@v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@v7.0.1 - with: - persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@v1.0.0 - with: - toolchain: stable - components: rustfmt, clippy - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-crw-${{ matrix.os }} - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo fmt -p craft-crw --check - - run: cargo clippy -p craft-crw --all-targets --locked -- -D warnings - - run: cargo test -p craft-crw --locked - wasm: timeout-minutes: 10 name: CRW WASM @@ -63,10 +35,6 @@ jobs: with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-crw-wasm - save-if: ${{ github.ref == 'refs/heads/main' }} - run: cargo check -p craft-crw --locked --target wasm32-unknown-unknown samples: @@ -84,10 +52,6 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-crw-samples - save-if: ${{ github.ref == 'refs/heads/main' }} - name: Cache the CC0 sample files uses: actions/cache@v6.1.0 with: diff --git a/.github/workflows/dialogs.yml b/.github/workflows/dialogs.yml index c8d43d725e..23def9feb0 100644 --- a/.github/workflows/dialogs.yml +++ b/.github/workflows/dialogs.yml @@ -12,10 +12,10 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: dialogs-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: dialogs-${{ github.ref }} + cancel-in-progress: true jobs: dialogs: @@ -37,6 +37,5 @@ jobs: components: clippy # The macOS code is linted on Linux: it compiles for the target without the SDK. targets: aarch64-apple-darwin - - run: cargo clippy -p craft-dialogs --all-targets --locked -- -D warnings + # Native clippy and the tests run in the Workspace gate. - run: cargo clippy -p craft-dialogs --target aarch64-apple-darwin --locked -- -D warnings - - run: cargo test -p craft-dialogs --locked diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index 6caa4f0eea..6275433817 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -12,20 +12,17 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: doc-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: doc-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests run in the Workspace gate on every OS; this checks what it does not. jobs: doc: + name: Craft documents WASM timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -37,11 +34,5 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy targets: wasm32-unknown-unknown - - run: cargo fmt --check - - run: cargo clippy -p craft-doc --all-targets --locked -- -D warnings - - run: cargo clippy -p craft-doc --all-targets --all-features --locked -- -D warnings - - run: cargo test -p craft-doc --locked -- --nocapture - - run: cargo test -p craft-doc --all-features --locked -- --nocapture - run: cargo check -p craft-doc --all-features --locked --target wasm32-unknown-unknown diff --git a/.github/workflows/docpath.yml b/.github/workflows/docpath.yml deleted file mode 100644 index e0115f6f58..0000000000 --- a/.github/workflows/docpath.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Document paths - -on: - workflow_dispatch: - push: - branches: [main] - paths: ['crates/docpath/**', 'Cargo.toml', 'Cargo.lock', '.github/workflows/docpath.yml'] - pull_request: - paths: ['crates/docpath/**', 'Cargo.toml', 'Cargo.lock', '.github/workflows/docpath.yml'] - -permissions: - contents: read - -concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: docpath-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} - -jobs: - docpath: - timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} - steps: - - name: Harden runner - uses: step-security/harden-runner@v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@v7.0.1 - - uses: step-security/dtolnay-rust-toolchain@v1.0.0 - with: - toolchain: stable - components: rustfmt, clippy - - run: cargo fmt --check - - run: cargo clippy -p craft-docpath --all-targets --locked -- -D warnings - - run: cargo test -p craft-docpath --locked -- --nocapture diff --git a/.github/workflows/ecproj-format.yml b/.github/workflows/ecproj-format.yml index 24a3350af8..048c897b3c 100644 --- a/.github/workflows/ecproj-format.yml +++ b/.github/workflows/ecproj-format.yml @@ -12,20 +12,17 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: ecproj-format-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: ecproj-format-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests run in the Workspace gate on every OS; this checks what it does not. jobs: ecproj-format: + name: EffectCraft project format WASM timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -37,9 +34,5 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy targets: wasm32-unknown-unknown - - run: cargo fmt --check - - run: cargo clippy -p craft-ecproj-format --all-targets --locked -- -D warnings - - run: cargo test -p craft-ecproj-format --locked -- --nocapture - run: cargo check -p craft-ecproj-format --locked --target wasm32-unknown-unknown diff --git a/.github/workflows/font-store.yml b/.github/workflows/font-store.yml index 0578f18ec4..e655697b3d 100644 --- a/.github/workflows/font-store.yml +++ b/.github/workflows/font-store.yml @@ -12,20 +12,17 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: font-store-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: font-store-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests run in the Workspace gate on every OS; this checks what it does not. jobs: font-store: + name: Font store WASM timeout-minutes: 20 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -35,13 +32,6 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy targets: wasm32-unknown-unknown - - run: cargo fmt --check - - run: cargo clippy -p craft-font-store --all-targets --locked -- -D warnings - - run: cargo test -p craft-font-store --locked - - run: cargo clippy -p craft-font-catalog --all-targets --locked -- -D warnings - - run: cargo test -p craft-font-catalog --locked - name: Check the web build - if: runner.os == 'Linux' run: cargo check -p craft-font-store --target wasm32-unknown-unknown --locked diff --git a/.github/workflows/formats.yml b/.github/workflows/formats.yml index 84d2c0d7e9..12478d2c95 100644 --- a/.github/workflows/formats.yml +++ b/.github/workflows/formats.yml @@ -42,11 +42,10 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: shared-formats-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: shared-formats-${{ github.ref }} + cancel-in-progress: true jobs: check: @@ -68,7 +67,9 @@ jobs: - { crate: craft-tiff, features: "" } - { crate: craft-heif, features: "testdata" } - { crate: craft-cms, features: "" } - name: ${{ matrix.crate }} lint, tests, WASM + # fmt, clippy with default and with all features, and the tests run in the Workspace gate on + # every OS; this lints each feature alone and checks the web build. + name: ${{ matrix.crate }} feature sets, WASM runs-on: ubuntu-latest steps: - name: Harden runner @@ -80,32 +81,19 @@ jobs: persist-credentials: false - uses: dtolnay/rust-toolchain@stable with: - components: rustfmt, clippy + components: clippy targets: wasm32-unknown-unknown - - uses: Swatinem/rust-cache@v2.9.3 - with: - key: ${{ matrix.crate }} - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo fmt -p "$CRATE" --check - env: - CRATE: ${{ matrix.crate }} - - name: Clippy, default features and each feature alone + - name: Clippy, each feature alone + if: matrix.features != '' env: CRATE: ${{ matrix.crate }} FEATURES: ${{ matrix.features }} run: | - cargo clippy -p "$CRATE" --all-targets --locked -- -D warnings for f in $FEATURES; do echo "::group::feature: $f" cargo clippy -p "$CRATE" --all-targets --locked --no-default-features --features "$f" -- -D warnings echo "::endgroup::" done - - run: cargo clippy -p "$CRATE" --all-targets --locked --all-features -- -D warnings - env: - CRATE: ${{ matrix.crate }} - - run: cargo test -p "$CRATE" --all-features --locked - env: - CRATE: ${{ matrix.crate }} - run: cargo check -p "$CRATE" --all-features --locked --target wasm32-unknown-unknown env: CRATE: ${{ matrix.crate }} diff --git a/.github/workflows/full-validation.yml b/.github/workflows/full-validation.yml new file mode 100644 index 0000000000..85ad96f2fe --- /dev/null +++ b/.github/workflows/full-validation.yml @@ -0,0 +1,64 @@ +name: Full validation + +# Add full-validation to a PR for uncapped validation of its merge ref. Keep the label across pushes. +# Fast Workspace checks are not substitutes for these six completed native/PDF gate checks. +on: + pull_request: + types: [opened, synchronize, reopened, labeled] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + # An unrelated label event must not cancel an active full run. + group: full-validation-${{ github.event.action == 'labeled' && github.event.label.name != 'full-validation' && github.run_id || github.event.pull_request.number || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + native: + if: >- + github.event_name == 'workflow_dispatch' || + (contains(github.event.pull_request.labels.*.name, 'full-validation') && + (github.event.action != 'labeled' || github.event.label.name == 'full-validation')) + name: Full native + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + os: '["ubuntu-latest","windows-latest","macos-latest"]' + select-os: '[]' + timeout-minutes: 120 + tools: "" + packages: mesa-vulkan-drivers libvulkan1 + craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 + linux-env: WGPU_BACKEND=vulkan + windows-env: CRAFT_PRINT_REQUIRE_QUEUE=1 + env: CRAFT_PRINT_REQUIRE_QUEUE=0 + setup-command: bash scripts/ci-test-inputs.sh + command: | + python scripts/test-full-validation.py + bash scripts/ci-full-validation.sh + + pdf-contracts: + if: >- + github.event_name == 'workflow_dispatch' || + (contains(github.event.pull_request.labels.*.name, 'full-validation') && + (github.event.action != 'labeled' || github.event.label.name == 'full-validation')) + name: Full PDF contracts + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + os: '["ubuntu-latest","windows-latest","macos-latest"]' + select-os: '[]' + timeout-minutes: 120 + tools: "" + craft-fonts-ref: "" + require-craft-fonts: false + env: RUSTFLAGS=--cfg craft_pdf_unicode_fixtures + setup-command: | + python scripts/test-fetch-pdf-test-fonts.py + python scripts/fetch-pdf-test-fonts.py "$RUNNER_TEMP/craft-pdf-test-fonts" + echo "CRAFT_PDF_TEST_FONT_DIR=$RUNNER_TEMP/craft-pdf-test-fonts" >> "$GITHUB_ENV" + command: | + unset CRAFT_HOSTILE_DIVISOR PROPTEST_CASES + cargo clippy -p craft-pdf --all-targets --all-features --locked -- -D warnings + cargo test -p craft-pdf --all-features --locked + cargo test --manifest-path crates/pdf/read-only-contract/Cargo.toml --locked diff --git a/.github/workflows/i18n.yml b/.github/workflows/i18n.yml deleted file mode 100644 index 7e69031884..0000000000 --- a/.github/workflows/i18n.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Translations - -on: - workflow_dispatch: - push: - branches: [main] - paths: ['crates/i18n/**', 'Cargo.toml', 'Cargo.lock', '.github/workflows/i18n.yml'] - pull_request: - paths: ['crates/i18n/**', 'Cargo.toml', 'Cargo.lock', '.github/workflows/i18n.yml'] - -permissions: - contents: read - -concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: i18n-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} - -jobs: - i18n: - timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} - steps: - - name: Harden runner - uses: step-security/harden-runner@v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@v7.0.1 - - uses: step-security/dtolnay-rust-toolchain@v1.0.0 - with: - toolchain: stable - components: rustfmt, clippy - - run: cargo fmt --check - - run: cargo clippy -p craft-i18n --all-targets --features collate --locked -- -D warnings - - run: cargo test -p craft-i18n --features collate --locked -- --nocapture diff --git a/.github/workflows/jpeg.yml b/.github/workflows/jpeg.yml index 21db0ab794..495af8b232 100644 --- a/.github/workflows/jpeg.yml +++ b/.github/workflows/jpeg.yml @@ -12,16 +12,17 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: shared-jpeg-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: shared-jpeg-${{ github.ref }} + cancel-in-progress: true jobs: check: - timeout-minutes: 10 - name: JPEG lint, tests, WASM + # Uncached: the wasm check builds its dependencies on every run (about 10 minutes with setup). + timeout-minutes: 20 + # fmt, clippy and the tests run in the Workspace gate on every OS. + name: JPEG WASM runs-on: ubuntu-latest steps: - name: Harden runner @@ -34,13 +35,5 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-jpeg - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo fmt -p craft-jpeg --check - - run: cargo clippy -p craft-jpeg --all-targets --locked -- -D warnings - - run: cargo test -p craft-jpeg --locked - run: cargo check -p craft-jpeg --locked --target wasm32-unknown-unknown diff --git a/.github/workflows/keymap.yml b/.github/workflows/keymap.yml index 41fcc0502d..bd4ef280a3 100644 --- a/.github/workflows/keymap.yml +++ b/.github/workflows/keymap.yml @@ -12,16 +12,17 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: keymap-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: keymap-${{ github.ref }} + cancel-in-progress: true jobs: linux: timeout-minutes: 15 - name: Keymap (Linux) + # fmt, and clippy and the tests with default and with all features, run in the Workspace gate + # on every OS; this runs the feature subsets. + name: Keymap feature sets runs-on: ubuntu-latest env: CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts @@ -43,12 +44,7 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-keymap-linux - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo fmt -p craft-keymap --check + components: clippy - run: cargo clippy -p craft-keymap --all-targets --no-default-features --locked -- -D warnings # `snapshot` covers `ui` and `egui` and compiles the dialog screenshots, which need a GPU # to run, so the test steps leave them out. @@ -58,40 +54,6 @@ jobs: run: cargo test -p craft-keymap --no-default-features --lib --tests --locked - name: Test egui dispatch and the shortcuts dialog run: cargo test -p craft-keymap --features ui --locked - native: - timeout-minutes: 15 - name: Keymap (${{ matrix.os }}) - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["macos-latest","windows-latest"]') || fromJSON('["windows-latest"]') }} - runs-on: ${{ matrix.os }} - env: - CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts - steps: - - name: Harden runner - uses: step-security/harden-runner@v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@v7.0.1 - with: - persist-credentials: false - - name: Checkout test fonts - uses: actions/checkout@v7.0.1 - with: - repository: storytold/craft-fonts - ref: b076417b5880bd4154bd22f00695b1d28c6bea83 - path: .test-inputs/craft-fonts - persist-credentials: false - - uses: step-security/dtolnay-rust-toolchain@v1.0.0 - with: - toolchain: stable - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-keymap-${{ matrix.os }} - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo test -p craft-keymap --features ui --locked wasm: timeout-minutes: 10 name: Keymap WASM @@ -108,8 +70,4 @@ jobs: with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-keymap-wasm - save-if: ${{ github.ref == 'refs/heads/main' }} - run: cargo check -p craft-keymap --features ui --locked --target wasm32-unknown-unknown diff --git a/.github/workflows/media.yml b/.github/workflows/media.yml index 4814e35ca8..1b5afaebdf 100644 --- a/.github/workflows/media.yml +++ b/.github/workflows/media.yml @@ -11,17 +11,22 @@ on: permissions: contents: read +# A fraction of the fuzz-style cases, as in the Workspace gate; nightly.yml runs them in full. +env: + CRAFT_HOSTILE_DIVISOR: "8" + PROPTEST_CASES: "64" + concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: shared-media-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: shared-media-${{ github.ref }} + cancel-in-progress: true jobs: lint: timeout-minutes: 10 - name: Media lint, feature sets, WASM + # fmt, and clippy with default and with all features, run in the Workspace gate on every OS. + name: Media feature sets, WASM runs-on: ubuntu-latest steps: - name: Harden runner @@ -34,20 +39,12 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy + components: clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-media-lint - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo fmt -p craft-media --check - run: cargo clippy -p craft-media --all-targets --locked --no-default-features -- -D warnings - run: cargo clippy -p craft-media --all-targets --locked --no-default-features --features bitstream -- -D warnings - - run: cargo clippy -p craft-media --all-targets --locked -- -D warnings - run: cargo clippy -p craft-media --all-targets --locked --no-default-features --features matroska -- -D warnings - run: cargo clippy -p craft-media --all-targets --locked --no-default-features --features ogg -- -D warnings - - run: cargo clippy -p craft-media --all-targets --locked --all-features -- -D warnings - - run: cargo clippy -p craft-media-testkit --all-targets --locked -- -D warnings - run: cargo clippy -p craft-media --all-targets --locked --no-default-features --features serde -- -D warnings - run: cargo check -p craft-media --all-features --locked --target wasm32-unknown-unknown test: @@ -65,10 +62,6 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-media-test - save-if: ${{ github.ref == 'refs/heads/main' }} - name: Build tests while installing ffmpeg run: | # The man-db index rebuild after dpkg is slow on hosted runners. diff --git a/.github/workflows/menu.yml b/.github/workflows/menu.yml index 8d10587313..b12c8bcc6a 100644 --- a/.github/workflows/menu.yml +++ b/.github/workflows/menu.yml @@ -18,7 +18,8 @@ concurrency: jobs: linux: timeout-minutes: 15 - name: Menu (Linux) + # fmt, native clippy and the tests run in the Workspace gate on every OS. + name: Menu macOS and WASM lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v7.0.1 @@ -26,14 +27,8 @@ jobs: persist-credentials: false - uses: dtolnay/rust-toolchain@stable with: - components: rustfmt, clippy + components: clippy targets: aarch64-apple-darwin, wasm32-unknown-unknown - - uses: Swatinem/rust-cache@v2.9.3 - with: - key: craft-menu-linux - - run: cargo fmt -p craft-menu --check - - run: cargo clippy -p craft-menu --all-targets --locked -- -D warnings # The macOS backend type-checks from Linux; it needs no linker or SDK. - run: cargo clippy -p craft-menu --lib --locked --target aarch64-apple-darwin -- -D warnings - run: cargo clippy -p craft-menu --lib --locked --target wasm32-unknown-unknown -- -D warnings - - run: cargo test -p craft-menu --locked diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml new file mode 100644 index 0000000000..ed9d361279 --- /dev/null +++ b/.github/workflows/nightly.yml @@ -0,0 +1,34 @@ +name: Nightly full tests + +# The tests the Workspace gate shortens, in full, once a day: every hostile-input hammer and sweep +# without CRAFT_HOSTILE_DIVISOR, and every proptest at its own case count (no PROPTEST_CASES). The +# rest of the workspace's tests run with them, on Linux, with the gate's setup. +on: + schedule: + - cron: "37 2 * * *" + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: nightly-${{ github.ref }} + cancel-in-progress: false + +jobs: + full: + name: Full tests + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + os: '["ubuntu-latest"]' + timeout-minutes: 120 + tools: cargo-nextest@0.9.150 + packages: mesa-vulkan-drivers libvulkan1 + craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 + linux-env: WGPU_BACKEND=vulkan + env: CRAFT_PRINT_REQUIRE_QUEUE=0 + setup-command: bash scripts/ci-test-inputs.sh + cache-paths: .test-inputs/photocraft/corpus + cache-key: photocraft-corpus + cache-key-files: scripts/ci-test-inputs.sh + command: cargo nextest run --workspace --all-features --locked --profile ci diff --git a/.github/workflows/pcraft-format.yml b/.github/workflows/pcraft-format.yml index 9c66b5a10d..0e4feabc92 100644 --- a/.github/workflows/pcraft-format.yml +++ b/.github/workflows/pcraft-format.yml @@ -12,20 +12,18 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: pcraft-format-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: pcraft-format-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests run in the Workspace gate on every OS, and its msrv job checks the +# workspace on 1.95; this checks the web build on 1.95. jobs: pcraft-format: + name: PhotoCraft format WASM timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -37,9 +35,5 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" - components: rustfmt, clippy targets: wasm32-unknown-unknown - - run: cargo fmt --check - - run: cargo clippy -p craft-pcraft-format --all-targets --locked -- -D warnings - - run: cargo test -p craft-pcraft-format --locked -- --nocapture - run: cargo check -p craft-pcraft-format --locked --target wasm32-unknown-unknown diff --git a/.github/workflows/pdf.yml b/.github/workflows/pdf.yml index 09ddfabcab..7b73977570 100644 --- a/.github/workflows/pdf.yml +++ b/.github/workflows/pdf.yml @@ -11,22 +11,23 @@ on: permissions: contents: read +# A fraction of the proptest cases, as in the Workspace gate; nightly.yml runs them in full. +env: + PROPTEST_CASES: "64" + concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: shared-pdf-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: shared-pdf-${{ github.ref }} + cancel-in-progress: true jobs: native: - name: PDF (${{ matrix.os }}) - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + # fmt, clippy and the tests run in the Workspace gate on every OS, with the licensed fonts and + # the plate tests; this runs the tests behind craft_pdf_unicode_fixtures and the read-only + # contract, which is its own workspace. + name: PDF Unicode fixtures and read-only contract + runs-on: ubuntu-latest env: RUSTFLAGS: --cfg craft_pdf_unicode_fixtures steps: @@ -38,11 +39,6 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-pdf - save-if: ${{ github.ref == 'refs/heads/main' }} - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" @@ -52,9 +48,7 @@ jobs: python scripts/test-fetch-pdf-test-fonts.py python scripts/fetch-pdf-test-fonts.py "$RUNNER_TEMP/craft-pdf-test-fonts" echo "CRAFT_PDF_TEST_FONT_DIR=$RUNNER_TEMP/craft-pdf-test-fonts" >> "$GITHUB_ENV" - - run: cargo fmt --check - - run: cargo clippy -p craft-pdf --all-targets --all-features --locked -- -D warnings - - run: cargo test -p craft-pdf --all-features --locked + - run: cargo test -p craft-pdf --all-features --locked --test unicode_writing --test upstream_compatibility --test upstream_reading - run: cargo test --manifest-path crates/pdf/read-only-contract/Cargo.toml --locked backends: name: PDF vendored contracts @@ -68,10 +62,6 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-pdf-backends - save-if: ${{ github.ref == 'refs/heads/main' }} - run: bash scripts/test-pdf-backends.sh wasm: name: PDF WASM runtime @@ -89,10 +79,6 @@ jobs: with: toolchain: stable targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-pdf-wasm - save-if: ${{ github.ref == 'refs/heads/main' }} - uses: actions/setup-python@v7.1.0 with: python-version: "3.12" @@ -117,31 +103,3 @@ jobs: fallback: cargo-install - run: cargo test -p craft-pdf --all-features --locked --target wasm32-unknown-unknown - run: cargo test --manifest-path crates/pdf/read-only-contract/Cargo.toml --locked --target wasm32-unknown-unknown - plate-test: - name: Plate tests and craft-prepress - runs-on: ubuntu-latest - steps: - - name: Harden runner - uses: step-security/harden-runner@v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@v7.0.1 - - uses: step-security/dtolnay-rust-toolchain@v1.0.0 - with: - toolchain: stable - components: clippy - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-plate-test - save-if: ${{ github.ref == 'refs/heads/main' }} - - uses: actions/setup-python@v7.1.0 - with: - python-version: "3.12" - - name: Provision licensed PDF test fonts - shell: bash - run: | - python scripts/test-fetch-pdf-test-fonts.py - python scripts/fetch-pdf-test-fonts.py "$RUNNER_TEMP/craft-pdf-test-fonts" - echo "CRAFT_PDF_TEST_FONT_DIR=$RUNNER_TEMP/craft-pdf-test-fonts" >> "$GITHUB_ENV" - - run: cargo clippy -p craft-plate-test -p craft-prepress --all-targets --all-features --locked -- -D warnings - - run: cargo test -p craft-plate-test -p craft-prepress --all-features --locked diff --git a/.github/workflows/print.yml b/.github/workflows/print.yml deleted file mode 100644 index 8e2108e096..0000000000 --- a/.github/workflows/print.yml +++ /dev/null @@ -1,46 +0,0 @@ -name: Print spooler -on: - workflow_dispatch: - push: - branches: [main] - paths: ['crates/print/**', 'Cargo.toml', 'Cargo.lock', '.github/workflows/print.yml'] - pull_request: - paths: ['crates/print/**', 'Cargo.toml', 'Cargo.lock', '.github/workflows/print.yml'] -permissions: - contents: read -concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: print-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} -jobs: - print: - timeout-minutes: 30 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} - steps: - - name: Harden runner - uses: step-security/harden-runner@v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@v7.0.1 - - uses: step-security/dtolnay-rust-toolchain@v1.0.0 - with: - toolchain: stable - components: rustfmt, clippy - - run: cargo fmt --check - - run: cargo clippy -p craft-print --all-targets -- -D warnings - - name: Prepare the Windows test driver - if: runner.os == 'Windows' - shell: powershell - run: | - Start-Service Spooler - Add-PrinterDriver -Name 'Generic / Text Only' - - run: cargo test -p craft-print --locked -- --nocapture - timeout-minutes: 10 - env: - CRAFT_PRINT_REQUIRE_QUEUE: ${{ runner.os == 'Windows' && '1' || '0' }} diff --git a/.github/workflows/proof.yml b/.github/workflows/proof.yml index 10365a2306..ea5edb8a56 100644 --- a/.github/workflows/proof.yml +++ b/.github/workflows/proof.yml @@ -12,16 +12,16 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: craft-proof-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: craft-proof-${{ github.ref }} + cancel-in-progress: true jobs: check: timeout-minutes: 20 - name: Proofing lint, tests with and without Language data, WASM + # fmt, and clippy and the tests without Language data, run in the Workspace gate on every OS. + name: Proofing with Language data, WASM runs-on: ubuntu-latest steps: - name: Harden runner @@ -43,18 +43,10 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy + components: clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-proof - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo fmt -p craft-proof --check - - name: Without Language data (a fork, docs.rs, offline) - run: | - cargo clippy -p craft-proof --all-targets --locked -- -D warnings - cargo test -p craft-proof --locked - cargo check -p craft-proof --locked --target wasm32-unknown-unknown + - name: The web build without Language data (a fork, docs.rs, offline) + run: cargo check -p craft-proof --locked --target wasm32-unknown-unknown - name: A release without Language data fails run: | if CRAFT_I18N_REQUIRED=1 cargo check -p craft-proof --locked 2>build.log; then diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index e40c1b4bc8..c03a3a0768 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -20,6 +20,9 @@ jobs: uses: storytold/craft-common-actions/.github/workflows/sonarcloud.yml@main with: pdf-test-fonts: true + # The instrumented build runs without a dependency cache: its 1.2 GB entry does not fit the + # repository's 10 GB cache budget beside the Workspace gate's. It runs on main only. + build-cache: false # The codec and format code behind features that are off by default. Without these the # coverage build never compiles it, and new code there reads as untested. cargo-features: >- diff --git a/.github/workflows/ui.yml b/.github/workflows/ui.yml deleted file mode 100644 index f15ef6ddc8..0000000000 --- a/.github/workflows/ui.yml +++ /dev/null @@ -1,74 +0,0 @@ -name: Shared UI - -on: - push: - branches: [main] - paths: ["crates/ui/**", "crates/i18n/**", "Cargo.toml", "Cargo.lock", ".github/workflows/ui.yml"] - pull_request: - paths: ["crates/ui/**", "crates/i18n/**", "Cargo.toml", "Cargo.lock", ".github/workflows/ui.yml"] - workflow_dispatch: - -permissions: - contents: read - -concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: shared-ui-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} - -jobs: - native: - timeout-minutes: 30 - name: UI (${{ matrix.os }}) - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} - steps: - - name: Harden runner - uses: step-security/harden-runner@v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@v7.0.1 - - name: Install software Vulkan renderer - if: runner.os == 'Linux' - run: | - sudo apt-get update - sudo apt-get install -y mesa-vulkan-drivers libvulkan1 - echo 'WGPU_BACKEND=vulkan' >> "$GITHUB_ENV" - - uses: step-security/dtolnay-rust-toolchain@v1.0.0 - with: - toolchain: stable - components: rustfmt, clippy - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-ui - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo fmt --check - - run: cargo clippy -p craft-ui --all-targets --locked -- -D warnings - - run: cargo clippy -p craft-ui --all-targets --all-features --locked -- -D warnings - - run: cargo test -p craft-ui --locked - - run: cargo test -p craft-ui --all-features --locked - wasm: - timeout-minutes: 15 - name: UI WASM - runs-on: ubuntu-latest - steps: - - name: Harden runner - uses: step-security/harden-runner@v2.22.4 - with: - egress-policy: audit - - uses: actions/checkout@v7.0.1 - - uses: step-security/dtolnay-rust-toolchain@v1.0.0 - with: - toolchain: stable - targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-ui-wasm - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo check -p craft-ui --all-features --locked --target wasm32-unknown-unknown diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml index 9a7139026d..96ab4d6ad2 100644 --- a/.github/workflows/update.yml +++ b/.github/workflows/update.yml @@ -12,20 +12,18 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: update-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: update-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests with default and with all features run in the Workspace gate on every +# OS; this runs the egui feature alone. jobs: update: + name: Update with egui alone timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -39,10 +37,6 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy - - run: cargo fmt --check - # Default (no egui) and the egui feature both build and lint cleanly. - - run: cargo clippy -p craft-update --all-targets --locked -- -D warnings + components: clippy - run: cargo clippy -p craft-update --all-targets --locked --features egui -- -D warnings - - run: cargo test -p craft-update --locked -- --nocapture - run: cargo test -p craft-update --locked --features egui -- --nocapture diff --git a/.github/workflows/vectorcraft-format.yml b/.github/workflows/vectorcraft-format.yml index 74144f30f4..cd66615a50 100644 --- a/.github/workflows/vectorcraft-format.yml +++ b/.github/workflows/vectorcraft-format.yml @@ -12,20 +12,18 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: vectorcraft-format-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: vectorcraft-format-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests run in the Workspace gate on every OS, and its msrv job checks the +# workspace on 1.95; this checks the web build on 1.95. jobs: vectorcraft-format: + name: VectorCraft format WASM timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -37,9 +35,5 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" - components: rustfmt, clippy targets: wasm32-unknown-unknown - - run: cargo fmt --check - - run: cargo clippy -p craft-vectorcraft-format --all-targets --locked -- -D warnings - - run: cargo test -p craft-vectorcraft-format --locked -- --nocapture - run: cargo check -p craft-vectorcraft-format --locked --target wasm32-unknown-unknown diff --git a/.github/workflows/video.yml b/.github/workflows/video.yml index fa667bdfca..20afe0d700 100644 --- a/.github/workflows/video.yml +++ b/.github/workflows/video.yml @@ -11,6 +11,11 @@ on: permissions: contents: read +# A fraction of the fuzz-style cases, as in the Workspace gate; nightly.yml runs them in full. +env: + CRAFT_HOSTILE_DIVISOR: "8" + PROPTEST_CASES: "64" + concurrency: group: shared-video-${{ github.ref }} cancel-in-progress: true @@ -18,7 +23,8 @@ concurrency: jobs: lint: timeout-minutes: 20 - name: Video lint, feature sets, docs, WASM + # fmt and clippy with all features run in the Workspace gate on every OS. + name: Video feature sets, docs, WASM runs-on: ubuntu-latest steps: - name: Harden runner @@ -31,12 +37,8 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" - components: rustfmt, clippy + components: clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-video-lint - - run: cargo fmt -p craft-video --check # Each feature on its own, so code that needs another feature fails here. - name: Clippy, each feature alone run: | @@ -45,19 +47,15 @@ jobs: cargo clippy -p craft-video --all-targets --locked --no-default-features ${f:+--features "$f"} -- -D warnings echo "::endgroup::" done - - run: cargo clippy -p craft-video --all-targets --locked --all-features -- -D warnings - run: cargo doc -p craft-video --all-features --no-deps --locked env: RUSTDOCFLAGS: "-D warnings" - run: cargo check -p craft-video --all-features --locked --target wasm32-unknown-unknown test: timeout-minutes: 30 - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, windows-latest] - runs-on: ${{ matrix.os }} - name: Video tests (${{ matrix.os }}) + # The Workspace gate runs these tests on every OS without ffmpeg; this runs them with it. + runs-on: ubuntu-latest + name: Video tests with ffmpeg steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -69,11 +67,7 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: "1.95" - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-video-${{ matrix.os }} - - name: Install ffmpeg (Linux) - if: runner.os == 'Linux' + - name: Install ffmpeg run: | sudo rm -f /var/lib/man-db/auto-update sudo apt-get update -qq && sudo apt-get install -y -qq --no-install-recommends ffmpeg diff --git a/.github/workflows/web-api.yml b/.github/workflows/web-api.yml index 4bf1a30fe8..f0f8e22da7 100644 --- a/.github/workflows/web-api.yml +++ b/.github/workflows/web-api.yml @@ -12,10 +12,10 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: web-api-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: web-api-${{ github.ref }} + cancel-in-progress: true jobs: web-api: @@ -35,7 +35,7 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy + components: clippy targets: wasm32-unknown-unknown - name: Read the wasm-bindgen version from Cargo.lock id: wasm-bindgen @@ -50,8 +50,6 @@ jobs: with: tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }} fallback: cargo-install - - run: cargo fmt --check - - run: cargo clippy -p craft-web-api --all-targets --locked -- -D warnings + # fmt, native clippy and the native tests run in the Workspace gate. - run: cargo clippy -p craft-web-api --all-targets --locked --target wasm32-unknown-unknown -- -D warnings - - run: cargo test -p craft-web-api --locked - run: cargo test -p craft-web-api --locked --target wasm32-unknown-unknown diff --git a/.github/workflows/web-host.yml b/.github/workflows/web-host.yml index 76e6701e53..dc0254850a 100644 --- a/.github/workflows/web-host.yml +++ b/.github/workflows/web-host.yml @@ -12,16 +12,16 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets its own group: it is never cancelled or replaced while queued, so it - # always gets to save the cache. - group: shared-web-host-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: shared-web-host-${{ github.ref }} + cancel-in-progress: true jobs: check: timeout-minutes: 20 - name: Web host lint, tests, WASM tests + # fmt, native clippy and the native tests run in the Workspace gate. + name: Web host WASM lint and tests runs-on: ubuntu-latest env: CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner @@ -36,16 +36,9 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy + components: clippy targets: wasm32-unknown-unknown - - uses: step-security/rust-cache@v2.9.2 - with: - key: craft-web-host - save-if: ${{ github.ref == 'refs/heads/main' }} - - run: cargo fmt -p craft-web-host --check - - run: cargo clippy -p craft-web-host --all-targets --locked -- -D warnings - run: cargo clippy -p craft-web-host --all-targets --locked --target wasm32-unknown-unknown -- -D warnings - - run: cargo test -p craft-web-host --locked - name: Read the wasm-bindgen version from Cargo.lock id: wasm-bindgen shell: bash diff --git a/.github/workflows/window.yml b/.github/workflows/window.yml index fdd28e2030..c13b1ac3fe 100644 --- a/.github/workflows/window.yml +++ b/.github/workflows/window.yml @@ -12,20 +12,18 @@ permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (main, manual) gets a group of its own, so it is neither cancelled nor replaced while queued. - group: window-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # One group per pull request and per branch: a newer push cancels the run it supersedes, on main + # too, since the newest run checks everything before it. + group: window-${{ github.ref }} + cancel-in-progress: true +# fmt, clippy and the tests with default and with all features (egui and restore together) run in +# the Workspace gate on every OS; this runs each feature alone. jobs: window: + name: Window with each feature alone timeout-minutes: 15 - strategy: - fail-fast: false - matrix: - # macOS runners are kept for releases: macOS runs only when started by hand. - os: ${{ github.event_name == 'workflow_dispatch' && fromJSON('["ubuntu-latest","windows-latest","macos-latest"]') || fromJSON('["ubuntu-latest","windows-latest"]') }} - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest steps: - name: Harden runner uses: step-security/harden-runner@v2.22.4 @@ -39,12 +37,8 @@ jobs: - uses: step-security/dtolnay-rust-toolchain@v1.0.0 with: toolchain: stable - components: rustfmt, clippy - - run: cargo fmt --check - # Default (no egui) and the egui feature both build and lint cleanly. - - run: cargo clippy -p craft-window --all-targets --locked -- -D warnings + components: clippy - run: cargo clippy -p craft-window --all-targets --locked --features egui -- -D warnings - - run: cargo test -p craft-window --locked -- --nocapture - run: cargo test -p craft-window --locked --features egui -- --nocapture - run: cargo clippy -p craft-window --all-targets --locked --features restore -- -D warnings - - run: cargo test -p craft-window --locked --features restore,egui -- --nocapture + - run: cargo test -p craft-window --locked --features restore -- --nocapture diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index 4d717fed82..e13d290a66 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -6,70 +6,194 @@ on: tags: ["v*"] pull_request: workflow_dispatch: + # A full run on main every 6 hours that is never cancelled, so the caches are saved while pushes + # to main cancel each other. The midnight run builds the Linux leg from source (`native`). + schedule: + - cron: "17 0 * * *" + - cron: "17 6,12,18 * * *" permissions: contents: read concurrency: - # Pull requests share one group per branch, so a newer push cancels the older run. Every other - # run (push to main, a release tag, a manual run) gets a group of its own: it is neither - # cancelled nor replaced while queued, so a main run always finishes and saves the dependency - # cache, and a release run is never cancelled. - group: workspace-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} + # A pull request, and main, each have one group, so a newer push cancels the run it supersedes; + # the newest push's run still tests everything before it. Every other run (a release tag, a + # manual run, the scheduled run) gets a group of its own and is neither cancelled nor replaced + # while queued: a release run always finishes, and the scheduled run saves the caches. + group: workspace-${{ (github.event_name == 'pull_request' || github.event_name == 'push' && github.ref == 'refs/heads/main') && github.ref || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' || github.event_name == 'push' && github.ref == 'refs/heads/main' }} jobs: + # Whether this run needs the macOS and Windows legs. On a pull request they run only when it can + # change platform behaviour: a crate with platform cfg code or one that depends on it, the build + # setup, or a file outside the crates (craft-common-actions' select-platforms). The full-ci label + # runs them anyway from the next push or manual run; every other event runs them. + platforms: + name: select platforms + runs-on: ubuntu-latest + timeout-minutes: 5 + outputs: + platform: ${{ steps.select.outputs.platform }} + steps: + - name: Harden runner + uses: step-security/harden-runner@v2.22.4 + with: + egress-policy: audit + # The merge commit and its first parent. + - uses: actions/checkout@v7.0.1 + if: github.event_name == 'pull_request' + with: + persist-credentials: false + fetch-depth: 2 + - id: select + uses: storytold/craft-common-actions/actions/select-platforms@d65d611f0ef67eaa94039351be5afce61102dbb0 + with: + os: '["ubuntu-latest","windows-latest","macos-latest"]' + select-os: '["windows-latest","macos-latest"]' + label: full-ci + + # The Linux leg keeps the compiled workspace crates between runs (`cache-workspace-crates`): each + # main run saves a rolling cache entry of its own, every run restores the newest one, and Cargo + # rebuilds only the crates a change reaches. `cache-prune` keeps the newest entry. The midnight + # scheduled run builds from source (`cold-build`) and starts the entry afresh. The macOS and + # Windows legs keep a dependency-only entry: their workspace crates would not fit the 10 GB cache + # budget beside Linux's. The lint legs keep no cache (`lint-cache: false`) for the same reason; a + # cold lint leg is still shorter than its gate leg. native: name: Workspace uses: storytold/craft-common-actions/.github/workflows/gate.yml@main with: - os: '["ubuntu-latest","windows-latest","macos-latest"]' - timeout-minutes: 120 - # The Windows leg runs its tests with nextest (.config/nextest.toml). - tools: cargo-nextest@0.9.150 + os: '["ubuntu-latest"]' + cache-workspace-crates: true + cold-build: ${{ github.event.schedule == '17 0 * * *' }} + lint-cache: false + timeout-minutes: &timeout 120 + # Every leg runs its tests with nextest, and its doctests with cargo test (.config/nextest.toml). + tools: &tools cargo-nextest@0.9.150 packages: mesa-vulkan-drivers libvulkan1 - craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 + craft-fonts-ref: &craft-fonts b076417b5880bd4154bd22f00695b1d28c6bea83 linux-env: WGPU_BACKEND=vulkan - windows-env: CRAFT_PRINT_REQUIRE_QUEUE=1 - env: CRAFT_PRINT_REQUIRE_QUEUE=0 - setup-command: | - set -euo pipefail - corpus_fetcher=.test-inputs/photocraft - mkdir -p "$corpus_fetcher" - git -C "$corpus_fetcher" init - git -C "$corpus_fetcher" remote add origin https://github.com/storytold/PhotoCraft.git - git -C "$corpus_fetcher" fetch --quiet --depth=1 origin 74330157cb601573ae4523a95bb9132822fc3918 - git -C "$corpus_fetcher" checkout --quiet --detach FETCH_HEAD - (cd "$corpus_fetcher" && cargo xtask corpus --psd --psd-tools --atn --exr --heif --pngsuite) - mkdir -p corpus - cp -a "$corpus_fetcher/corpus/." corpus/ - python scripts/test-fetch-pdf-test-fonts.py - python scripts/fetch-pdf-test-fonts.py "$RUNNER_TEMP/craft-pdf-test-fonts" - printf 'CRAFT_PSD_CORPUS=%s\n' "$GITHUB_WORKSPACE/$corpus_fetcher/corpus" >> "$GITHUB_ENV" - printf 'CRAFT_PDF_TEST_FONT_DIR=%s\n' "$RUNNER_TEMP/craft-pdf-test-fonts" >> "$GITHUB_ENV" - command: | - python3 scripts/test-workspace-concurrency.py - cargo fmt --check + # Test binaries without debug info or symbols: about a third of the size, and a panic still + # names its file and line. + dev-strip: true + # The fuzz-style tests run a fraction of their cases here: every hostile-input hammer and + # sweep 1/8 (craft-testkit's CRAFT_HOSTILE_DIVISOR), every proptest without its own case + # count 64 cases instead of 256. nightly.yml runs them all in full. + env: &env | + CRAFT_PRINT_REQUIRE_QUEUE=0 + CRAFT_HOSTILE_DIVISOR=8 + PROPTEST_CASES=64 + setup-command: &setup bash scripts/ci-test-inputs.sh + # The PhotoCraft corpora, keyed on the script that pins them: a hit skips the PhotoCraft + # checkout and its xtask build (1:51 on Windows). + cache-paths: &cache-paths .test-inputs/photocraft/corpus + cache-key: &cache-key photocraft-corpus + cache-key-files: &cache-key-files scripts/ci-test-inputs.sh + # A `lint ()` leg beside each `gate ()` leg, so clippy is off the tests' critical path. + lint-command: &lint | + if [ "$RUNNER_OS" = Linux ]; then + python3 scripts/test-workspace-concurrency.py + python3 scripts/test-workspace-tests.py + fi + # One rustfmt call gets every source root of the workspace, which is longer than + # Windows allows for a command line (os error 206). rustfmt's result is the same on + # every system, so the Linux and macOS legs check it. + if [ "$RUNNER_OS" != Windows ]; then + cargo fmt --check + fi cargo clippy --workspace --all-targets --locked -- -D warnings cargo clippy --workspace --all-targets --all-features --locked -- -D warnings - cargo test --workspace --locked - cargo test --workspace --all-features --locked + command: &command | + # The tests run once, with every feature, in parallel across test binaries; nextest skips + # doctests, so `cargo test --doc` runs them. Examples are not built, except the two whose + # tests run (stubicc, h264_h264enc_bench); clippy in the lint leg covers the rest. The default-feature compile is the lint leg's + # first clippy run; the last command runs the crates whose tests check behaviour with a + # feature off, which an all-features build cannot reach. + cargo nextest run --workspace --all-features --locked --profile ci --lib --bins --tests + cargo nextest run -p craft-aep -p craft-video --all-features --locked --profile ci --example stubicc --example h264_h264enc_bench + cargo test --workspace --doc --all-features --locked + cargo nextest run -p craft-image-codecs -p craft-text --locked --profile ci --lib --bins --tests + + # The macOS and Windows legs, when `platforms` says this run needs them, with the Linux leg's + # inputs. + native-platforms: + name: Workspace + needs: platforms + if: needs.platforms.outputs.platform == 'true' + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + os: '["windows-latest","macos-latest"]' + lint-cache: false + timeout-minutes: *timeout + tools: *tools + craft-fonts-ref: *craft-fonts + dev-strip: true + # No debuginfo on Windows: writing PDBs for every test binary is a large part of its link + # time. A panic still names its file and line; only backtrace frames lose theirs, and the + # Linux and macOS legs keep line tables. + windows-env: | + CRAFT_PRINT_REQUIRE_QUEUE=1 + CARGO_PROFILE_DEV_DEBUG=0 + env: *env + setup-command: *setup + cache-paths: *cache-paths + cache-key: *cache-key + cache-key-files: *cache-key-files + lint-command: *lint + command: *command windows-command: | powershell.exe -NoProfile -Command '$ErrorActionPreference = "Stop"; Start-Service Spooler; Add-PrinterDriver -Name "Generic / Text Only"' - cargo fmt --check - cargo clippy --workspace --all-targets --locked -- -D warnings - cargo clippy --workspace --all-targets --all-features --locked -- -D warnings - # 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 nextest run --workspace --all-features --locked --profile ci --lib --bins --tests + cargo nextest run -p craft-aep -p craft-video --all-features --locked --profile ci --example stubicc --example h264_h264enc_bench cargo test --workspace --doc --all-features --locked + cargo nextest run -p craft-image-codecs -p craft-text --locked --profile ci --lib --bins --tests + + # Each main run's Linux leg saves a rolling cache entry of its own (`cache-workspace-crates`), and + # every run restores only the newest. This deletes the older ones as soon as the Linux leg is + # done, instead of leaving them to the 10 GB budget's eviction, which would evict other entries + # too. The only job with `actions: write`; it checks out and runs nothing from the repository. + cache-prune: + name: cache prune + needs: native + if: ${{ !cancelled() && github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + actions: write + steps: + - name: Harden runner + uses: step-security/harden-runner@v2.22.4 + with: + egress-policy: audit + - name: Delete all but the newest rolling entry of each job and runner + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + run: | + set -euo pipefail + # `-rust---crates-----...`, newest first. + gh cache list --repo "$REPO" --ref refs/heads/main --limit 100 --sort created_at --order desc --json id,key \ + --jq '.[] | "\(.id) \(.key)"' > "$RUNNER_TEMP/caches" + declare -A kept + while read -r id key; do + [[ "$key" =~ ^.+-rust-[0-9a-f]{64}-(.+-crates-[A-Za-z_]+-[a-z0-9]+)-[0-9]+-[0-9]+- ]] || continue + entry=${BASH_REMATCH[1]} + if [ -n "${kept[$entry]:-}" ]; then + echo "delete $key" + gh cache delete "$id" --repo "$REPO" + else + kept[$entry]=1 + echo "keep $key" + fi + done < "$RUNNER_TEMP/caches" msrv: name: Rust 1.95 uses: storytold/craft-common-actions/.github/workflows/gate.yml@main with: + # Its own dependency-cache entry: the calls in this file share one job id, so without a name + # they would write the same key. + cache-name: msrv os: '["ubuntu-latest"]' toolchain: 1.95.0 targets: "" @@ -84,6 +208,7 @@ jobs: name: UI and settings WASM uses: storytold/craft-common-actions/.github/workflows/gate.yml@main with: + cache-name: wasm os: '["ubuntu-latest"]' targets: wasm32-unknown-unknown tools: "" diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 9d260300c7..5805271e03 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -11,6 +11,7 @@ author, source and license. Library code and original assets are under the repos | `licenses/dwg/acadrust-MPL-2.0.txt`, `licenses/dwg/acadrust-THIRD_PARTY_NOTICES.md` | `acadrust` contributors; ACadSharp authors for the noted ported routines | [acadrust 0.6.6](https://github.com/hakanaktt/acadrust), used unmodified as `craft-dwg`'s optional native dependency | MPL-2.0; embedded ACadSharp notice is MIT; see both retained files | | `crates/cms/profiles/photocraft-coated-cmyk.icc` | PhotoCraft contributors; generated by `craft_cms::synth::coated_cmyk` | [PhotoCraft `crates/cms`](https://github.com/storytold/photocraft/tree/b3222ee8e7e6bcb4799c37f904c595f20f9baf5e/crates/cms) | CC0-1.0 | | `crates/cms/profiles/craft-coated-fogra39.icc` | craft-libs contributors; generated by `crates/cms/tests/fogra39.rs` from the FOGRA39 data below | this repository | CC0-1.0 | +| `crates/ppt-binary/tests/fixtures/old-talk.pps` | DeckCraft contributors; a one-slide PowerPoint 97-2003 show built from records by `crates/ppt-binary/src/tests.rs` (`the_pps_fixture_is_a_built_presentation`, with `CRAFT_WRITE_PPT_FIXTURE=1`); no PowerPoint involved | [storytold/deckcraft#1137](https://github.com/storytold/deckcraft/pull/1137) | CC0-1.0 | | `crates/font-store/index/sources.json` (unmodified) | craft-font-index contributors | [aiongg/craft-font-index@5a9a7f8](https://github.com/aiongg/craft-font-index/tree/5a9a7f8038c8f3e2e1583a79c92a816e8e71b34f) | Apache-2.0 | | `crates/font-store/index/indexes/google-fonts.json.gz` (unmodified) | craft-font-index contributors; generated from the METADATA.pb files of [google/fonts](https://github.com/google/fonts) at `45e3d5269e4a17d823f6d84470bc084dc5ae46f6` | [aiongg/craft-font-index@5a9a7f8](https://github.com/aiongg/craft-font-index/tree/5a9a7f8038c8f3e2e1583a79c92a816e8e71b34f) | Apache-2.0 | | `crates/font-store/index/indexes/noto.json.gz` (unmodified) | craft-font-index contributors; generated from [notofonts/notofonts.github.io](https://github.com/notofonts/notofonts.github.io) at `93fac8abebe6c7fed0c5b7d2456d8fa484fa146c` and [noto-cjk](https://github.com/notofonts/noto-cjk) releases | [aiongg/craft-font-index@5a9a7f8](https://github.com/aiongg/craft-font-index/tree/5a9a7f8038c8f3e2e1583a79c92a816e8e71b34f) | Apache-2.0 | @@ -104,6 +105,10 @@ 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. @@ -114,3 +119,60 @@ The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper ## 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 legacy ZIP Zstandard method-20 mapping and bounded-read regression in `crates/zip/src/lenient` are ported from RRFZ's Craft Anything commit `29badae7c9280fdf4c24795927bf3a91f51694c6` (MIT OR Apache-2.0). RRFZ remains the primary Git author. Firmware scanner detection remains app-owned and is not part of this port. +## TAR/CPIO compatibility updates + +The old GNU sparse-extension traversal, GNU CRC-symlink compatibility and PAX-size precedence fixes and their regression tests in `crates/tar` are ported from RRFZ's Craft Anything PRs #1973 (`8ea2ec66a1bc80f0420f3695baac877a0cb9b9a5`), #1984 (`b226a475f7dce9710e3af7a069386fe7a9720df7`) and #2007 (`d29465c20b0d570ea4973be512a0cb4e212bb60f`). Source is MIT OR Apache-2.0; RRFZ remains the primary Git author of each port. The PAX update adapts to the shared parser's existing `PaxOverrides` structure without changing the source policy. + +## Aligned stored ZIP entries + +The USDZ alignment requirement and compatibility fixture for `Writer::add_stored_aligned` come from TextureCraft `crates/formats/src/mesh_export/usd.rs`, introduced by bflatastic in `bdbb52c9` and maintained through `9eb20086` (MIT OR Apache-2.0). The shared API and alignment validation are subsequent Dylan M. Taylor work. Scene/material mapping remains in TextureCraft. + +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. + +## Source Map v3 parser and lookup + +`crates/sourcemap` extracts the bounded parser, source inventory and generated-position lookup from Craft Anything `crates/formats/src/sourcemap.rs` at `e5c9c94106747ed44b9a3fb0decce867c2285f93` (MIT OR Apache-2.0). Core-history ports preserve Brandon Thomas, BFlat, Afloat, RRFZ, Nicolas Bohn and Alex Joaquim Pereira as primary authors, with exact source revisions in each commit. Six regression files preserve their source authors; nine core unit tests retain the original tests. App annotation scanning, data URLs, base64 and file I/O are intentionally not imported. Workspace wiring is a subsequent change; existing limits and errors are unchanged. +## JSONC span-preserving editing + +`crates/jsonc` is extracted from Craft Anything `crates/formats/src/jsonc.rs` at `e5c9c94106747ed44b9a3fb0decce867c2285f93` (MIT OR Apache-2.0). The original implementation and hardening by BFlat, newline preservation by NetterSilver, and subsequent RRFZ/Afloat correctness fixes retain separate primary-author port commits with immutable source revisions. Shared API wiring and optional input-byte admission are subsequent changes. Compatibility parsing and all existing formatting/editing tests are retained; application file I/O and policy are not imported. + +## XFA XML DOM + +`crates/xfa/src/xml.rs` is extracted from PDFCraft at `271e479c53723b1b294c866d5cb2f27604601b29`, originally by Dylan M. Taylor (`eead4541`, `7d44e654`), MIT OR Apache-2.0. The initial extraction changes visibility and module boundaries; subsequent namespace-retention budget hardening intentionally tightens resource admission. PDF/layout/form policy remains in the app. + +## Java class inspection + +`crates/java-class` extracts Craft Anything `java_class.rs` and `java_ops.rs` at `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2` (MIT OR Apache-2.0), preserving separate primary-author source-history ports by Brandon Thomas, BFlat, Afloat and RRFZ. The checked byte-reader, modified UTF-8 unit decoder, descriptor display helper and synthetic class fixtures come from the same source snapshot. Seven regression files retain their source authors. Neutral error wiring and shared crate integration are subsequent changes. No JAR archive, application I/O or bytecode execution is imported. + +## WebAssembly inspection + +`crates/wasm` extracts Craft Anything `crates/formats/src/wasm.rs` at `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2`, with its checked byte-reader subset and original synthetic WASM/testkit fixtures (MIT OR Apache-2.0). Eight core-history ports preserve Brandon Thomas, Afloat and RRFZ as primary authors; component, repeated-custom-section and referenced-string regression imports retain source authors and commit references. Fixtures come from `crates/testkit/src/formats.rs` and deterministic mutation helpers from `crates/testkit/src/lib.rs`; no third-party binaries. Shared error naming and workspace wiring are subsequent changes. Existing inspection limits and report behavior are unchanged; this crate validates/inspects/renders, never executes modules. + +## Packet capture and protocol inspection + +`crates/pcap` extracts Craft Anything `crates/formats/src/pcap.rs`, its checked byte reader primitives and synthetic packet fixtures/regressions at `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2` (MIT OR Apache-2.0). Brandon Thomas introduced the implementation, byte reader and fixtures in `f64ae3bf3c8863a982af1304e06de20653803968`. Subsequent fixes by phatloi0010, GGWA, BFlat, Afloat, NetterSilver, Max Mittelstädt and RRFZ retain separate primary-author source-history commits with immutable source revisions. Shared error/API wiring and chrono-based timestamp formatting with exhaustive day-boundary parity tests are subsequent Dylan M. Taylor changes. No packet reassembly, decryption, live capture or app analysis policy is imported. +## HTTP Archive reader + +`crates/har` preserves Craft Anything `crates/formats/src/har.rs` from `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2` (MIT OR Apache-2.0). Brandon Thomas introduced the typed reader in `f64ae3bf3c8863a982af1304e06de20653803968`; BFlat added UTF-8 BOM compatibility in `cb0bb9d8fa4536ba44ef2c86f5ed759b879cc2d3`. Both source snapshots retain primary author attribution. Shared wiring, explicit byte admission and its tests are subsequent Dylan M. Taylor changes. Redaction and analysis remain app-owned. + +## Dalvik DEX inspection + +`crates/dex` extracts Craft Anything `crates/formats/src/dex.rs` and `dex_ops.rs` at `5979f9b13f16dcf1df3c7ae7f8e7735f452866f2` (MIT OR Apache-2.0). Five source-history ports preserve Brandon Thomas, BFlat and Afloat as primary authors. Five regression imports retain BFlat/Afloat authors and exact source revisions. The checked byte-reader/MUTF-8 subset and synthetic DEX fixture come from that app's `bytes.rs` and `testkit/src/android.rs`; deterministic noise/mutations come from its testkit. No application file I/O, APK logic or execution is imported. Shared neutral errors and workspace wiring are subsequent changes; existing per-file strings, member/handler and bytecode payload budgets are retained. + +## EVM bytecode inspection + +`crates/evm` extracts Craft Anything `crates/formats/src/evm.rs` at `a31e995858f4312e0f1d59a007112da191ef2d26` (MIT OR Apache-2.0). Eight separate source-history ports preserve Brandon Thomas, Afloat, RRFZ and NetterSilver as primary authors with immutable source references. Synthetic EVM fixtures and deterministic mutation helpers come from the same app source; regression files retain their source provenance. Neutral errors and workspace integration are subsequent changes. No code execution or transaction behavior is imported. + +## XFDF field reader + +`crates/xfdf` extracts PDFCraft field reading from #5937 refresh `cb564fd827f572818e9ddb0ea5989a432b1f39fd` (MIT OR Apache-2.0). App-side annotation and form policy remains local; source history is retained in the original repository. + +Retained XFDF source contributors (move-aware blame): Brandon Thomas, Marco Arnulfo, pkibiego and storybored379; module split by aiongg. Subsequent budget/integration work by Dylan M. Taylor. diff --git a/Cargo.lock b/Cargo.lock index 1ac11475f3..3c7c6a1e91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -232,6 +232,17 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "apple-native-keyring-store" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b350bfd03649e07aa05c0a81b3e15934374e585c98204a57e20b9d49f49bb9a" +dependencies = [ + "keyring-core", + "log", + "security-framework", +] + [[package]] name = "arbitrary" version = "1.5.0" @@ -1074,6 +1085,18 @@ dependencies = [ "winx", ] +[[package]] +name = "cap-std" +version = "4.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1ec78e242cfa2cfe276807ac2ecc00315a6c97786977414bcd1c3963b6c91b8" +dependencies = [ + "cap-primitives", + "io-extras", + "io-lifetimes 3.0.1", + "rustix 1.1.5", +] + [[package]] name = "cast" version = "0.3.0" @@ -1545,6 +1568,16 @@ dependencies = [ "ureq", ] +[[package]] +name = "craft-ai-agents" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-ai-integrations?rev=d94a3593adcff504eaaad9e5aee4c84fd2f07a72#d94a3593adcff504eaaad9e5aee4c84fd2f07a72" +dependencies = [ + "dirs", + "serde", + "serde_json", +] + [[package]] name = "craft-ai-core" version = "0.1.0" @@ -1601,6 +1634,7 @@ version = "0.1.0" name = "craft-captions" version = "0.1.0" dependencies = [ + "craft-media", "craft-media-testkit", "craft-timeline", "craft-xml", @@ -1616,6 +1650,25 @@ dependencies = [ "craft-media-testkit", ] +[[package]] +name = "craft-cloud" +version = "0.1.0" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-cloud-local" +version = "0.1.0" +dependencies = [ + "cap-std", + "craft-cloud", + "craft-ui", + "egui", + "tempfile", +] + [[package]] name = "craft-cms" version = "0.1.0" @@ -1640,14 +1693,29 @@ dependencies = [ "serde", ] +[[package]] +name = "craft-color-transform" +version = "0.1.0" +dependencies = [ + "craft-cms", + "craft-color", + "serde", + "serde_json", +] + [[package]] name = "craft-control" version = "0.1.0" dependencies = [ + "craft-ai-agents", + "craft-atomic", + "craft-flags", "getrandom 0.3.4", + "log", "proptest", "serde", "serde_json", + "tempfile", ] [[package]] @@ -1722,6 +1790,40 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-develop" +version = "0.1.0" +dependencies = [ + "craft-color-transform", + "craft-develop-model", + "craft-render-parity", + "proptest", + "rayon", + "serde", + "serde_json", +] + +[[package]] +name = "craft-develop-model" +version = "0.1.0" +dependencies = [ + "craft-geom", + "craft-metadata", + "miniz_oxide 0.9.1", + "proptest", + "serde", + "serde_json", +] + +[[package]] +name = "craft-dex" +version = "0.1.0" +dependencies = [ + "craft-testkit", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-dialogs" version = "0.1.0" @@ -1761,6 +1863,7 @@ dependencies = [ "craft-wordmodel", "image", "log", + "md4", "miniz_oxide 0.9.1", "proptest", "serde_json", @@ -1881,6 +1984,26 @@ dependencies = [ "zip 9.1.3", ] +[[package]] +name = "craft-esign" +version = "0.1.0" +dependencies = [ + "craft-cloud", + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-evm" +version = "0.1.0" +dependencies = [ + "craft-testkit", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-ews" version = "0.1.0" @@ -1968,6 +2091,7 @@ dependencies = [ "craft-dxf", "craft-eps", "craft-file-formats", + "craft-image-codecs", "craft-pdf 0.1.0", "craft-raster-codecs", "craft-svg", @@ -2098,6 +2222,14 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-geom" +version = "0.1.0" +dependencies = [ + "proptest", + "serde", +] + [[package]] name = "craft-gif" version = "0.1.0" @@ -2106,6 +2238,40 @@ dependencies = [ "image", ] +[[package]] +name = "craft-gpu" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-gpu-null" +version = "0.1.0" +dependencies = [ + "craft-gpu", +] + +[[package]] +name = "craft-gpu-providers" +version = "0.1.0" +dependencies = [ + "craft-gpu", + "craft-gpu-null", +] + +[[package]] +name = "craft-gpu-wgpu" +version = "0.1.0" +dependencies = [ + "craft-gpu", + "craft-gpu-providers", + "pollster 0.4.0", + "wgpu", +] + [[package]] name = "craft-gridcraft-format" version = "0.1.0" @@ -2122,6 +2288,14 @@ dependencies = [ "flate2", ] +[[package]] +name = "craft-har" +version = "0.1.0" +dependencies = [ + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-hdr" version = "0.1.0" @@ -2204,6 +2378,7 @@ dependencies = [ "craft-dds", "craft-heif", "craft-jpeg", + "craft-jxl", "craft-media-testkit", "craft-metadata", "craft-tiff", @@ -2253,6 +2428,19 @@ dependencies = [ "widestring", ] +[[package]] +name = "craft-ipp" +version = "0.1.0" + +[[package]] +name = "craft-java-class" +version = "0.1.0" +dependencies = [ + "craft-testkit", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-jmap" version = "0.1.0" @@ -2272,6 +2460,13 @@ dependencies = [ "zune-jpeg", ] +[[package]] +name = "craft-jsonc" +version = "0.1.0" +dependencies = [ + "serde_json", +] + [[package]] name = "craft-jsonrpc" version = "0.1.0" @@ -2284,6 +2479,7 @@ dependencies = [ name = "craft-jxl" version = "0.1.0" dependencies = [ + "brotli-decompressor 5.0.3", "craft-cms", "jixel", "jxl-oxide", @@ -2596,6 +2792,16 @@ dependencies = [ name = "craft-palette" version = "0.1.0" +[[package]] +name = "craft-pcap" +version = "0.1.0" +dependencies = [ + "chrono", + "serde", + "serde_json", + "thiserror 2.0.21", +] + [[package]] name = "craft-pcraft-bundle" version = "0.1.0" @@ -2762,10 +2968,24 @@ dependencies = [ name = "craft-plotter" version = "0.1.0" dependencies = [ + "kurbo 0.13.1", "serde", "thiserror 2.0.21", ] +[[package]] +name = "craft-ppt-binary" +version = "0.1.0" +dependencies = [ + "craft-cfb", + "craft-drawingml", + "craft-pptx", + "image", + "log", + "miniz_oxide 0.8.9", + "thiserror 2.0.21", +] + [[package]] name = "craft-pptx" version = "0.1.0" @@ -2896,6 +3116,7 @@ name = "craft-raster-codecs" version = "0.1.0" dependencies = [ "base64 0.23.1", + "craft-dds", "craft-image-codecs", "image", "thiserror 2.0.21", @@ -2943,6 +3164,13 @@ dependencies = [ "proptest", ] +[[package]] +name = "craft-score" +version = "0.1.0" +dependencies = [ + "craft-xml", +] + [[package]] name = "craft-scraft-format" version = "0.1.0" @@ -3039,6 +3267,19 @@ dependencies = [ name = "craft-sniff" version = "0.1.0" +[[package]] +name = "craft-sourcemap" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-sqlite" +version = "0.1.0" + [[package]] name = "craft-ssip" version = "0.1.0" @@ -3077,6 +3318,21 @@ dependencies = [ "weezl 0.2.1", ] +[[package]] +name = "craft-suite-account" +version = "0.1.0" +dependencies = [ + "dirs", + "keyring", + "keyring-core", + "log", + "serde", + "serde_json", + "sha2 0.10.9", + "tempfile", + "zeroize", +] + [[package]] name = "craft-svg" version = "0.1.0" @@ -3085,11 +3341,14 @@ dependencies = [ "craft-xml", "data-url", "flate2", + "gif", + "image-webp", "kurbo 0.13.1", "quick-xml 0.41.0", "resvg 0.48.1", "roxmltree 0.21.2", "thiserror 2.0.21", + "toml", "usvg 0.48.1", "vectorcraft-geom", "vectorcraft-pathops", @@ -3107,6 +3366,7 @@ dependencies = [ name = "craft-tar" version = "0.1.0" dependencies = [ + "craft-testkit", "serde", ] @@ -3191,6 +3451,8 @@ dependencies = [ name = "craft-ui" version = "0.1.0" dependencies = [ + "craft-cloud", + "craft-esign", "craft-fonts-build", "craft-i18n", "eframe", @@ -3247,6 +3509,7 @@ dependencies = [ name = "craft-usd" version = "0.1.0" dependencies = [ + "half", "lz4_flex", "thiserror 2.0.21", ] @@ -3286,11 +3549,23 @@ dependencies = [ "craft-jpeg", "craft-media", "craft-media-testkit", + "miniz_oxide 0.8.9", "rav1e", "rayon", "thiserror 2.0.21", ] +[[package]] +name = "craft-wasm" +version = "0.1.0" +dependencies = [ + "craft-testkit", + "serde", + "thiserror 2.0.21", + "wasmparser 0.261.0", + "wasmprinter", +] + [[package]] name = "craft-web-api" version = "0.1.0" @@ -3365,6 +3640,21 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xfa" +version = "0.1.0" +dependencies = [ + "craft-xml", +] + +[[package]] +name = "craft-xfdf" +version = "0.1.0" +dependencies = [ + "craft-xml", + "thiserror 2.0.21", +] + [[package]] name = "craft-xfl" version = "0.1.0" @@ -4320,7 +4610,7 @@ dependencies = [ "ff 0.13.1", "generic-array", "group 0.13.0", - "hkdf", + "hkdf 0.12.4", "pem-rfc7468 0.7.0", "pkcs8 0.10.2", "rand_core 0.6.4", @@ -4969,6 +5259,17 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "futures-macro" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "futures-sink" version = "0.3.34" @@ -4989,6 +5290,7 @@ checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -5598,6 +5900,15 @@ dependencies = [ "hmac 0.12.1", ] +[[package]] +name = "hkdf" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" +dependencies = [ + "hmac 0.13.0", +] + [[package]] name = "hmac" version = "0.12.1" @@ -6579,6 +6890,27 @@ dependencies = [ "serde", ] +[[package]] +name = "keyring" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2270074a3d26bcac93c1dc5d2845eb4c089e8d761ccf6e0ea266a16004640627" +dependencies = [ + "apple-native-keyring-store", + "keyring-core", + "windows-native-keyring-store", + "zbus-secret-service-keyring-store", +] + +[[package]] +name = "keyring-core" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29fbf4b551298c67925f38cf341a1ada797a75f1837bdf8ae1fc8e2ebc6efb59" +dependencies = [ + "log", +] + [[package]] name = "khronos-egl" version = "6.0.0" @@ -6874,6 +7206,15 @@ dependencies = [ "digest 0.11.3", ] +[[package]] +name = "md4" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd76fb0fd6b2e4be62a73f8e0858ca97f81babcb1af322dcaca196f735f17f80" +dependencies = [ + "digest 0.11.3", +] + [[package]] name = "memchr" version = "2.8.3" @@ -8053,7 +8394,7 @@ dependencies = [ "generic-array", "getrandom 0.2.17", "hex", - "hkdf", + "hkdf 0.12.4", "idea", "k256", "log", @@ -9618,6 +9959,25 @@ dependencies = [ "zeroize", ] +[[package]] +name = "secret-service" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5107b24b91445dd2aa449a258a1807b63240942157292354dc5bfdbeb8bc6db8" +dependencies = [ + "aes 0.9.4", + "cbc", + "futures-util", + "getrandom 0.4.3", + "hkdf 0.13.0", + "hybrid-array", + "num", + "once_cell", + "serde", + "sha2 0.11.1", + "zbus", +] + [[package]] name = "security-framework" version = "3.7.0" @@ -11974,7 +12334,7 @@ dependencies = [ "wasmi_collections", "wasmi_core", "wasmi_ir", - "wasmparser", + "wasmparser 0.228.0", ] [[package]] @@ -12014,6 +12374,28 @@ dependencies = [ "indexmap", ] +[[package]] +name = "wasmparser" +version = "0.261.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f20f20e44f7e8aeb6744823ea9d869ede51e51be4fdaedede2852282e54d2d8" +dependencies = [ + "bitflags 2.13.2", + "indexmap", + "semver", +] + +[[package]] +name = "wasmprinter" +version = "0.261.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86a21c9e3325655ba02de3e645bc8357a8b3651a2e3ada25b6dbc37560deb9b7" +dependencies = [ + "anyhow", + "termcolor", + "wasmparser 0.261.0", +] + [[package]] name = "wayland-backend" version = "0.3.17" @@ -12405,6 +12787,19 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-native-keyring-store" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063426e76fdec7438d56bb777f67e318a84a25c707b07e575cb8b78e10c028f8" +dependencies = [ + "byteorder", + "keyring-core", + "regex", + "windows-sys 0.61.2", + "zeroize", +] + [[package]] name = "windows-numerics" version = "0.3.1" @@ -12948,6 +13343,17 @@ dependencies = [ "zvariant", ] +[[package]] +name = "zbus-secret-service-keyring-store" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74801d001b9e7729adb4f1825b67b398185fed424749aa3d8bacf70417137d9a" +dependencies = [ + "keyring-core", + "secret-service", + "zbus", +] + [[package]] name = "zbus_macros" version = "5.19.0" diff --git a/Cargo.toml b/Cargo.toml index 2c0f4dfeaf..7ef038f990 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,17 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-xfa = { path = "crates/xfa" } +craft-evm = { path = "crates/evm" } +craft-java-class = { path = "crates/java-class" } +craft-sourcemap = { path = "crates/sourcemap" } +craft-pcap = { path = "crates/pcap" } +craft-har = { path = "crates/har" } +craft-xfdf = { path = "crates/xfdf" } +craft-jsonc = { path = "crates/jsonc" } craft-palette = { path = "crates/palette" } +craft-score = { path = "crates/score" } +craft-sqlite = { path = "crates/sqlite" } craft-shx = { path = "crates/shx" } craft-http-url = { path = "crates/http-url" } craft-usd = { path = "crates/usd" } @@ -24,6 +34,7 @@ acadrust = "=0.6.6" log = "0.4" # internal: add each crate here as `craft- = { path = "crates/" }` craft-plist = { path = "crates/plist" } +craft-wasm = { path = "crates/wasm" } craft-platform = { path = "crates/platform" } craft-aep = { path = "crates/aep" } craft-affinity = { path = "crates/affinity" } @@ -50,12 +61,18 @@ craft-gzip = { path = "crates/gzip" } craft-gridcraft-format = { path = "crates/gridcraft-format" } craft-aaf = { path = "crates/aaf" } craft-atomic = { path = "crates/atomic" } +# The ArtCraft Launcher's app list for AI agents (storytold/craft-ai-integrations, crates/app/ai-agents). +craft-ai-agents = { git = "https://github.com/storytold/craft-ai-integrations", rev = "d94a3593adcff504eaaad9e5aee4c84fd2f07a72" } +craft-dex = { path = "crates/dex" } 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-cloud = { path = "crates/cloud" } +craft-cloud-local = { path = "crates/cloud-local" } craft-color = { path = "crates/color" } +craft-color-transform = { path = "crates/color-transform" } craft-frame = { path = "crates/frame" } craft-crs = { path = "crates/crs" } craft-crw = { path = "crates/crw" } @@ -84,6 +101,7 @@ craft-dpx = { path = "crates/dpx" } craft-dwg = { path = "crates/dwg" } craft-fcproj-format = { path = "crates/fcproj-format" } craft-ecproj-format = { path = "crates/ecproj-format" } +craft-esign = { path = "crates/esign" } craft-figma-rest = { path = "crates/figma-rest" } craft-exr = { path = "crates/exr" } craft-flags = { path = "crates/flags" } @@ -97,9 +115,14 @@ craft-gif = { path = "crates/gif" } craft-hdr = { path = "crates/hdr" } craft-mesh = { path = "crates/mesh" } craft-html = { path = "crates/html" } +craft-ipp = { path = "crates/ipp" } craft-heif = { path = "crates/heif" } craft-fig = { path = "crates/fig" } craft-fonts-build = { path = "crates/fonts-build" } +craft-gpu = { path = "crates/gpu" } +craft-gpu-null = { path = "crates/gpu-null" } +craft-gpu-providers = { path = "crates/gpu-providers" } +craft-gpu-wgpu = { path = "crates/gpu-wgpu" } craft-i18n = { path = "crates/i18n" } craft-ipc = { path = "crates/ipc" } craft-idml = { path = "crates/idml" } @@ -110,7 +133,7 @@ craft-jpeg = { path = "crates/jpeg" } craft-control-protocol = { path = "crates/control-protocol" } craft-jsonrpc = { path = "crates/jsonrpc" } craft-mcp = { path = "crates/mcp" } -craft-jxl = { path = "crates/jxl" } +craft-jxl = { path = "crates/jxl", default-features = false } craft-kiwi = { path = "crates/kiwi" } craft-keymap = { path = "crates/keymap" } craft-assuan = { path = "crates/assuan" } @@ -139,6 +162,7 @@ craft-opc = { path = "crates/opc" } craft-openpgp = { path = "crates/openpgp" } craft-ora = { path = "crates/ora" } craft-pptx = { path = "crates/pptx" } +craft-ppt-binary = { path = "crates/ppt-binary" } craft-penpot = { path = "crates/penpot" } craft-pcraft-format = { path = "crates/pcraft-format" } craft-pcraft-bundle = { path = "crates/pcraft-bundle" } @@ -156,12 +180,16 @@ craft-pst = { path = "crates/pst" } craft-quit = { path = "crates/quit" } craft-rtf = { path = "crates/rtf" } craft-raw = { path = "crates/raw" } +craft-geom = { path = "crates/geom" } +craft-develop = { path = "crates/develop" } +craft-develop-model = { path = "crates/develop-model" } craft-sheet = { path = "crates/sheet" } craft-sketch = { path = "crates/sketch" } craft-settings = { path = "crates/settings" } craft-scraft-format = { path = "crates/scraft-format" } craft-time = { path = "crates/time" } craft-silhouette-studio = { path = "crates/silhouette-studio" } +craft-suite-account = { path = "crates/suite-account" } craft-svg = { path = "crates/svg" } craft-stencilcraft-format = { path = "crates/stencilcraft-format" } craft-stencilcraft-document = { path = "crates/stencilcraft-document" } @@ -210,6 +238,9 @@ proptest = "1" encoding_rs = "0.8" # All date/time conversion goes through chrono (no hand-rolled calendar code beyond the 400-year range extension; AGENTS.md). chrono = { version = "0.4.45", default-features = false, features = ["std"] } +# Foundation crates for craft-gpu-wgpu. The versions match the ones the app repos already pin. +pollster = "0.4" +wgpu = { version = "30", default-features = false, features = ["std", "wgsl"] } [workspace.lints.rust] unsafe_code = "forbid" diff --git a/NOTICE b/NOTICE index 35085dcb30..49bb9c0e45 100644 --- a/NOTICE +++ b/NOTICE @@ -108,6 +108,14 @@ Copyright (c) 2026 ArtCraft Team and the PhotoCraft contributors. The format imp clean-room work based on their public specifications; dependency licences and X.Org's `rgb.txt` attribution are listed in ATTRIBUTION.md. +crates/develop (craft-develop) is moved from LightCraft, module by module: +- src/inpaint.rs and src/inpaint/ from crates/inpaint (lightcraft-inpaint), with its tests, revision a75497d6. + https://github.com/storytold/lightcraft + Copyright (c) 2026 ArtCraft Team and the LightCraft contributors. + That crate ports the inpainting (PatchMatch completion) and Poisson membrane fill code of PhotoCraft + (https://github.com/storytold/photocraft, crates/algo/src/inpaint.rs and poisson.rs), Copyright the PhotoCraft + contributors, MIT OR Apache-2.0; the port was modified by the LightCraft contributors (2026). + crates/raw (craft-raw) is moved from LightCraft, with behaviour ported from two other readers: - LightCraft, crates/raw (src/lib.rs, dng.rs, dngwrite.rs, tiffraw.rs, unpack.rs, ljpeg.rs, jxl.rs, opcodes.rs, profile.rs, gaintable.rs, semantic.rs, preview.rs, vendor/crw.rs, vendor/hasselblad.rs, diff --git a/README.md b/README.md index 5650bb9320..9512980917 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ Craft App Libraries =================== Scanned by SonarCloud +Reviewed by CodeRabbit This repo contains libraries used by the various Craft Apps. It's a Rust monorepo workspace, and the libraries can be included by any Craft Apps that need them. @@ -274,7 +275,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit - [`craft-math`](crates/math/README.md): equations as one tree (the Office Math object model) with its syntaxes, read and written: OMML, presentation MathML, LaTeX math and the UnicodeMath linear format. - [`craft-media`](crates/media/README.md): bit-level I/O (`bitstream`), clean-room MP4/MOV (`isobmff`), Matroska/WebM (`matroska`) and MXF (`mxf`) demuxers and muxers, Ogg (`ogg`), AVI (`avi`) and - MPEG-TS/PS (`mpegts`) demuxers, from FilmCraft, and an ISO BMFF box reader. Containers only; codecs are in `craft-audio`, and + MPEG-TS/PS (`mpegts`) demuxers, from FilmCraft, a Sierra VMD (`vmd`) demuxer, and an ISO BMFF box reader. Containers only; codecs are in `craft-audio`, and video codecs will go in a separate crate. - [`craft-lottie`](crates/lottie/README.md): Lottie (Bodymovin) JSON: the document model of lottie-spec 1.0 (animation, assets, layers by type, shape items, masks, effects, text, markers) @@ -323,6 +324,8 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit - [`craft-plotter`](crates/plotter/README.md): cutter and pen-plotter command languages: HP-GL, DMPL and GP-GL (plain and the Silhouette dialect, three generations, registration marks) from a job in millimetres, plus the Silhouette device queries and reply parsers. No I/O. +- [`craft-ppt-binary`](crates/ppt-binary/README.md): the PowerPoint 97-2003 binary format (`.ppt`, `.pot`, `.pps`; + [MS-PPT], [MS-ODRAW]) read into `craft-pptx`, on `craft-cfb`. Import only. DeckCraft's reader. - [`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`. diff --git a/crates/anicraft-format/src/lib.rs b/crates/anicraft-format/src/lib.rs index 7cc5dc2617..6ff5d81e19 100644 --- a/crates/anicraft-format/src/lib.rs +++ b/crates/anicraft-format/src/lib.rs @@ -34,7 +34,12 @@ struct EnvelopeOut<'a, P, T> { /// Serializes the envelope in its stable wire order and uses AniCraft's compact/pretty threshold. pub fn write(generator: &str, paths: &[P], document: &T) -> Result { - let envelope = EnvelopeOut { format: FORMAT_NAME, version: FORMAT_VERSION, generator, paths, document }; + write_version(FORMAT_VERSION, generator, paths, document) +} + +/// Serializes the envelope with an explicit format version and AniCraft's compact/pretty threshold. +pub fn write_version(version: u32, generator: &str, paths: &[P], document: &T) -> Result { + let envelope = EnvelopeOut { format: FORMAT_NAME, version, generator, paths, document }; let compact = serde_json::to_string(&envelope)?; if compact.len() < PRETTY_LIMIT { serde_json::to_string_pretty(&envelope) } else { Ok(compact) } } diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index 607fb00b9f..ae633a05d7 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -36,15 +36,23 @@ flac-encode = ["craft-media/bitstream"] # The pure-Rust Vorbis encoder (`vorbis::encoder`) and Ogg Vorbis files through `encode` and # `encode_ogg_vorbis`. vorbis-encode = ["craft-media/ogg"] +# The pure-Rust Vorbis decoder (`vorbis::decoder`), packets and whole Ogg Vorbis files. +vorbis-decode = ["craft-media/ogg"] # Codec modules on their own, for packet-level use. aac-decode = ["craft-media/bitstream"] # The AAC encoder (`aac::Encoder`), and AAC in M4A through `encode` and `FileEncoder`. aac-encode = ["craft-media/bitstream", "craft-media/isobmff"] opus-decode = [] +# ADPCM (`adpcm`): IMA WAV, Microsoft, Yamaha and Flash (SWF) decoders and encoders. +adpcm = ["craft-media/bitstream"] # QOA (Quite OK Audio) files: decoder and encoder in the `qoa` module, each on its own. qoa = ["qoa-decode", "qoa-encode"] qoa-decode = [] qoa-encode = [] +# ITU-T G.722 wideband sub-band ADPCM speech codec (64 kbit/s). +g722 = [] +# ITU-T G.726 ADPCM speech codec (16/24/32/40 kbit/s). +g726 = [] opus-encode = [] # GSM 06.10 full-rate speech: decoder and encoder in the `gsm` module, each on its own. gsm = ["gsm-decode", "gsm-encode"] @@ -62,8 +70,16 @@ ac3-encode = ["craft-media/bitstream"] eac3-encode = ["craft-media/bitstream"] # MPEG-1/2 Layer III (MP3) encoder (`mp3`). mp3-encode = ["craft-media/bitstream"] +# Direct Stream Digital (DSD) files: DSF and DSDIFF decoders and DSD-to-PCM decimation. +dsd = [] + +# MPEG-1/2 Layer II (MP2) encoder (`mpa::encoder`). It shares the polyphase analysis filter of the +# MP3 encoder and the tables of the MP1/MP2 decoder. +mpa-encode = ["mpa-decode", "mp3-encode"] # Bluetooth Low Complexity Subband Coding (SBC) audio codec (`sbc`). sbc = [] +# Sierra VMD sound decoder (`vmd::Decoder`): 8-bit PCM and 16-bit DPCM. +vmd-decode = [] # Multi-resolution waveform overviews (`peaks`). peaks = [] # Encode AAC channels in parallel (with `aac-encode`). @@ -83,6 +99,8 @@ symphonia = { version = "0.6.1", optional = true, default-features = false, feat [dev-dependencies] craft-media-testkit = { workspace = true } +# The VMD demuxer, for the VMD sound decoder's tests. +craft-media = { workspace = true, features = ["vmd"] } proptest = { workspace = true } serde_json = { workspace = true } @@ -90,14 +108,38 @@ serde_json = { workspace = true } name = "audio" required-features = ["decode", "lossless-encode", "peaks"] +[[test]] +name = "vmd" +required-features = ["vmd-decode"] + [[test]] name = "codecs" required-features = ["decode", "encode"] +[[test]] +name = "mp2_encoder_oracle" +required-features = ["mpa-encode"] + +[[test]] +name = "adpcm_oracle" +required-features = ["adpcm"] + [[test]] name = "flac_decode" required-features = ["flac-decode", "flac-encode"] +[[test]] +name = "vorbis_decode_oracle" +required-features = ["vorbis-decode", "vorbis-encode", "decode"] + +[[test]] +name = "vorbis_decode_hostile" +required-features = ["vorbis-decode", "vorbis-encode"] + +[[test]] +name = "vorbis_decode_synthetic" +required-features = ["vorbis-decode"] + [[test]] name = "gsm_oracle" required-features = ["gsm"] @@ -106,6 +148,18 @@ required-features = ["gsm"] name = "qoa_oracle" required-features = ["qoa"] +[[test]] +name = "dsd_oracle" +required-features = ["dsd"] + +[[test]] +name = "g722_oracle" +required-features = ["g722"] + +[[test]] +name = "g726_oracle" +required-features = ["g726"] + [[test]] name = "sbc_oracle" required-features = ["sbc"] @@ -125,3 +179,11 @@ required-features = ["decode"] [[example]] name = "encode" required-features = ["lossless-encode", "aac-encode"] + +[[test]] +name = "dsd_bounds" +required-features = ["dsd"] + +[[test]] +name = "dsd_metadata" +required-features = ["dsd"] diff --git a/crates/audio/README.md b/crates/audio/README.md index 10080ebbe4..bd80b54069 100644 --- a/crates/audio/README.md +++ b/crates/audio/README.md @@ -39,12 +39,16 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou | `alac-encode` | `alac::Encoder`; `encode`, `FileEncoder` for ALAC in M4A | craft-media `isobmff` | | `opus-decode`, `opus-encode` | `opus::Decoder`/`StreamDecoder`, `opus::Encoder` | none | | `vorbis-encode` | `vorbis::encoder::Encoder` (pure Rust); `encode_ogg_vorbis`, `encode_ogg` and `encode` for Ogg Vorbis files (whole buffer; not `FileEncoder`) | craft-media `ogg` | +| `vorbis-decode` | `vorbis::decoder::Decoder` (packets), `vorbis::decoder::decode_ogg` (whole Ogg Vorbis files); native Vorbis I: floors 0 and 1, residues 0 to 2, channel coupling, both block sizes. `decode` still reads Vorbis files through symphonia | craft-media `ogg` | | `ac3-decode` | `ac3::Decoder`, `ac3::ProgrammeDecoder` | craft-media `bitstream` | | `ac3-encode`, `eac3-encode` | `ac3::encoder::Encoder`; `encode`, `FileEncoder` for AC-3 and E-AC-3 streams | craft-media `bitstream` | | `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` | +| `mpa-encode` | `mpa::encoder::Encoder` (Layer II / MP2 frames) | `mpa-decode`, `mp3-encode` | +| `adpcm` | `adpcm::{ima, ms, yamaha, swf}`: decoders and encoders for IMA WAV, Microsoft, Yamaha and Flash ADPCM | 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 | +| `vmd-decode` | `vmd::Decoder`: Sierra VMD sound (8-bit PCM and 16-bit DPCM), frame by frame from `craft_media::vmd` | none | | `peaks` | `peaks::Peaks` | none | | `rayon` | parallel AAC encoding (with `aac-encode`) | `rayon` | | `serde` | `Serialize`/`Deserialize` on the shared types | `serde` | @@ -566,6 +570,7 @@ Each codec module's rustdoc lists the coding tools it implements. | Opus (`opus`) | RFC 6716, 8251, 7845 | decoder: SILK, hybrid, CELT, all frame sizes, PLC and FEC, multistream (`Decoder`) and single stream (`StreamDecoder`), 8–48 kHz output; encoder (`Encoder`): SILK, hybrid, CELT, 48 kHz, 20 ms frames, mono or stereo | all 36 RFC 8251 conformance cases; encoder output through ffmpeg's two Opus decoders | | AC-3/E-AC-3 (`ac3`) | decoder: ATSC A/52:2012; encoder/programme API: ETSI TS 102 366 V1.4.1 | single-syncframe and complete-programme decoding; packet encoding through discrete 7.1 (see below) | decoder comparisons and encoder output through ffmpeg, CRC/channel/timing/bit-budget regressions; synthetic quality limitations below | | MP1/MP2 (`mpa`) | ISO/IEC 11172-3, 13818-3 | Layer I and II, MPEG-1/2/2.5, 8–48 kHz; also the Layer II rate/mode pairs the spec disallows (ffmpeg's default mono 384 kbit/s); no free format | ffmpeg-encoded streams, 73–84 dB SNR | +| MP2 encoder (`mpa::encoder`) | ISO/IEC 11172-3 (§2.4, Annex B and C), 13818-3 | Layer II, constant bit rate, mono or stereo (no joint stereo), 32/44.1/48 kHz at the spec's rate/mode pairs and 16/22.05/24 kHz at 8–160 kbit/s; no CRC | our decoder and ffmpeg decode the frames; SNR of 20–37 dB on tone mixes, and the 481-sample filter delay matches | | MP3 decoder (`mp3`) | ISO/IEC 11172-3, 13818-3 | Layer III, MPEG-1/2/2.5, 8–48 kHz, every bit rate, CBR and VBR; mono, stereo, dual channel, joint stereo (mid/side and intensity); long, short and mixed blocks; bit reservoir; CRC read, not checked; Xing / Info / VBRI and LAME tags; no free format (read through symphonia) | ffmpeg's float decoder: 127–131 dB SNR on LAME streams of every sampling frequency (CBR, VBR, CRC, no reservoir, transients with short blocks) and on this crate's encoder's; intensity stereo and mixed blocks through LAME frames edited to use them (112–131 dB) | | MP3 encoder (`mp3`) | ISO/IEC 11172-3, 13818-3 | Layer III, constant bit rate: 32–320 kbit/s at 32/44.1/48 kHz, 8–160 kbit/s at 16/22.05/24 kHz; mono, stereo, joint stereo (mid/side); bit reservoir; long blocks only; no Xing/Info tag, VBR, CRC or free format | ffmpeg decodes every Huffman code as intended; full encodes at every sampling frequency checked with ffprobe and by SNR of ffmpeg's decode | | ALAC (`alac`) | Apple's reference implementation | decode and encode, 1–8 channels, 16/20/24/32 bits (decode: any depth 1–32), fast mode, magic cookie in MP4/Matroska and CAF form | Apple's reference: same packets from the encoder, same samples from both decoders on 766 configurations; ffmpeg: bit-exact both ways | diff --git a/crates/audio/src/adpcm/ima.rs b/crates/audio/src/adpcm/ima.rs new file mode 100644 index 0000000000..dcf6958f7a --- /dev/null +++ b/crates/audio/src/adpcm/ima.rs @@ -0,0 +1,247 @@ +//! IMA ADPCM in WAV blocks (format tag 0x0011). +//! +//! A block is, per channel, a 4-byte header (the first sample as `i16` and the step index as `u8`, +//! then a zero byte), then groups of 4 bytes per channel in channel order, each holding eight +//! 4-bit codes, low nibble first. A block of `block_align` bytes holds +//! `(block_align - 4 * channels) * 2 / channels + 1` samples per channel. A code is a sign bit and +//! three magnitude bits: the decoder adds `step/8` plus `step`, `step/2` and `step/4` for each set +//! magnitude bit from the top, then moves the step index by `[-1, -1, -1, -1, 2, 4, 6, 8]`. + +use super::{Error, Result, clip16, step_at}; + +const INDEX_ADJUST: [i32; 8] = [-1, -1, -1, -1, 2, 4, 6, 8]; + +/// The state of one channel's decoder. +#[derive(Clone, Copy)] +struct State { + predictor: i32, + index: i32, +} + +impl State { + /// The sample after code `nibble`. + fn step(&mut self, nibble: u8) -> i16 { + let step = step_at(self.index); + let mut diff = step >> 3; + if nibble & 1 != 0 { + diff += step >> 2; + } + if nibble & 2 != 0 { + diff += step >> 1; + } + if nibble & 4 != 0 { + diff += step; + } + if nibble & 8 != 0 { + diff = -diff; + } + self.predictor = clip16(self.predictor + diff); + self.index = (self.index + INDEX_ADJUST.get(usize::from(nibble & 7)).copied().unwrap_or(0)).clamp(0, 88); + self.predictor as i16 + } + + /// The code that brings the predictor closest to `sample`, applied to the state. + fn encode(&mut self, sample: i16) -> u8 { + let step = step_at(self.index); + let mut diff = i32::from(sample) - self.predictor; + let mut code = 0u8; + if diff < 0 { + code = 8; + diff = -diff; + } + let mut s = step; + for bit in [4u8, 2, 1] { + if diff >= s { + code |= bit; + diff -= s; + } + s >>= 1; + } + self.step(code); + code + } +} + +/// Samples per channel in a block of `block_align` bytes; `None` when the layout is impossible +/// (a block must hold the headers and whole groups of eight samples, and there are 1 to 8 +/// channels). +pub fn samples_per_block(block_align: usize, channels: usize) -> Option { + if !(1..=8).contains(&channels) { + return None; + } + let header = 4 * channels; + let body = block_align.checked_sub(header)?; + if body == 0 || !body.is_multiple_of(4 * channels) { + return None; + } + let per = (body / channels).checked_mul(2)?.checked_add(1)?; + // Callers size interleaved buffers from this value; the full count must fit too. + per.checked_mul(channels)?; + Some(per) +} + +/// Decode one block, appending its samples interleaved by channel to `out`. +pub fn decode_block(block: &[u8], channels: usize, out: &mut Vec) -> Result<()> { + let per = samples_per_block(block.len(), channels).ok_or(Error::Malformed("block size does not fit the channel count"))?; + let mut states = Vec::with_capacity(channels); + for ch in 0..channels { + let h = block.get(4 * ch..4 * ch + 4).ok_or(Error::Malformed("truncated block header"))?; + let index = i32::from(h[2]); + if index > 88 { + return Err(Error::Malformed("step index above 88")); + } + states.push(State { predictor: i32::from(i16::from_le_bytes([h[0], h[1]])), index }); + } + let start = out.len(); + out.resize(start + per * channels, 0); + let samples = out.get_mut(start..).ok_or(Error::Malformed("output"))?; + for (ch, s) in states.iter().enumerate() { + if let Some(o) = samples.get_mut(ch) { + *o = s.predictor as i16; + } + } + let body = block.get(4 * channels..).ok_or(Error::Malformed("truncated block"))?; + for (g, group) in body.chunks_exact(4 * channels).enumerate() { + for (ch, bytes) in group.as_chunks::<4>().0.iter().enumerate() { + let Some(state) = states.get_mut(ch) else { continue }; + for (k, b) in bytes.iter().enumerate() { + for (half, nibble) in [b & 0xF, b >> 4].into_iter().enumerate() { + let t = 1 + g * 8 + k * 2 + half; + if let Some(o) = samples.get_mut(t * channels + ch) { + *o = state.step(nibble); + } + } + } + } + } + Ok(()) +} + +/// Decode a stream of blocks of `block_align` bytes. A final partial block is decoded as far as it +/// holds whole groups. +pub fn decode(data: &[u8], channels: usize, block_align: usize) -> Result> { + samples_per_block(block_align, channels).ok_or(Error::Malformed("block size does not fit the channel count"))?; + let mut out = Vec::new(); + for block in data.chunks(block_align) { + if block.len() == block_align { + decode_block(block, channels, &mut out)?; + } else { + let (header, group) = (4 * channels, 4 * channels); + let whole = block.len().saturating_sub(header) / group * group; + if whole > 0 { + decode_block(block.get(..header + whole).unwrap_or(block), channels, &mut out)?; + } + } + } + Ok(out) +} + +/// The starting step index for a channel whose first samples are `x`: the index whose first +/// codes follow `x` most closely. +fn start_index(first: i16, x: &[i16]) -> i32 { + let mut best = (i64::MAX, 0); + for index in 0..=88 { + let mut st = State { predictor: i32::from(first), index }; + let mut err = 0i64; + for &s in x.iter().take(16) { + let code = st.encode(s); + let _ = code; + err += (i64::from(s) - i64::from(st.predictor)).pow(2); + } + if err < best.0 { + best = (err, index); + } + } + best.1 +} + +/// Encode one block from exactly `samples_per_block * channels` interleaved samples. +pub fn encode_block(samples: &[i16], channels: usize, block_align: usize) -> Result> { + let per = samples_per_block(block_align, channels).ok_or(Error::InvalidInput("block size does not fit the channel count"))?; + if samples.len() != per * channels { + return Err(Error::InvalidInput("a block needs exactly samples_per_block samples per channel")); + } + let mut block = vec![0u8; block_align]; + let mut states = Vec::with_capacity(channels); + for ch in 0..channels { + let col: Vec = samples.iter().skip(ch).step_by(channels).copied().collect(); + let first = col.first().copied().unwrap_or(0); + let st = State { predictor: i32::from(first), index: start_index(first, col.get(1..).unwrap_or(&[])) }; + if let Some(h) = block.get_mut(4 * ch..4 * ch + 4) { + h[..2].copy_from_slice(&first.to_le_bytes()); + h[2] = st.index as u8; + } + states.push(st); + } + let groups = (block_align - 4 * channels) / (4 * channels); + for g in 0..groups { + for ch in 0..channels { + let Some(state) = states.get_mut(ch) else { continue }; + for k in 0..4 { + let mut byte = 0u8; + for half in 0..2 { + let t = 1 + g * 8 + k * 2 + half; + let nibble = state.encode(samples.get(t * channels + ch).copied().unwrap_or(0)); + byte |= nibble << (4 * half); + } + if let Some(b) = block.get_mut(4 * channels + g * 4 * channels + ch * 4 + k) { + *b = byte; + } + } + } + } + Ok(block) +} + +/// Encode interleaved samples as blocks of `block_align` bytes. The last block is padded with +/// copies of the last sample, so a decoder returns up to one block more than was given. +pub fn encode(samples: &[i16], channels: usize, block_align: usize) -> Result> { + let per = samples_per_block(block_align, channels).ok_or(Error::InvalidInput("block size does not fit the channel count"))?; + if !samples.len().is_multiple_of(channels) { + return Err(Error::InvalidInput("the sample count is not a multiple of the channel count")); + } + let mut out = Vec::new(); + for chunk in samples.chunks(per * channels) { + if chunk.len() == per * channels { + out.extend(encode_block(chunk, channels, block_align)?); + } else { + let mut padded = chunk.to_vec(); + let last: Vec = chunk.get(chunk.len().saturating_sub(channels)..).unwrap_or(&[]).to_vec(); + while padded.len() < per * channels { + padded.extend(last.iter().copied().chain(std::iter::repeat(0)).take(channels)); + } + out.extend(encode_block(&padded, channels, block_align)?); + } + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn layout() { + assert_eq!(samples_per_block(256, 1), Some(505)); + assert_eq!(samples_per_block(512, 2), Some(505)); + assert_eq!(samples_per_block(4, 1), None); + assert_eq!(samples_per_block(100, 0), None); + assert_eq!(samples_per_block(258, 2), None); + } + + #[test] + fn unrepresentable_interleaved_layout_is_rejected() { + let align = usize::MAX - 7; + assert_eq!(samples_per_block(align, 2), None); + assert!(encode(&[], 2, align).is_err()); + assert!(encode_block(&[], 2, align).is_err()); + } + + #[test] + fn silence_stays_silent() { + let block = encode_block(&vec![0; 505], 1, 256).unwrap(); + let mut out = Vec::new(); + decode_block(&block, 1, &mut out).unwrap(); + assert!(out.iter().all(|&s| s == 0)); + } +} diff --git a/crates/audio/src/adpcm/mod.rs b/crates/audio/src/adpcm/mod.rs new file mode 100644 index 0000000000..da552d3cdf --- /dev/null +++ b/crates/audio/src/adpcm/mod.rs @@ -0,0 +1,56 @@ +//! ADPCM: a decoder and an encoder for four families, each on whole blocks or streams of `i16` +//! samples interleaved by channel (feature `adpcm`). +//! +//! - [`ima`]: IMA / DVI ADPCM in WAV blocks (format tag 0x0011): 4 bits per sample, a 4-byte header +//! per channel and groups of eight samples per channel. +//! - [`ms`]: Microsoft ADPCM in WAV blocks (format tag 0x0002): 4 bits per sample, adaptive +//! two-tap prediction with the seven standard coefficient pairs. +//! - [`yamaha`]: Yamaha ADPCM (the YM2608 / AICA family): a raw stream of 4-bit codes. +//! - [`swf`]: the ADPCM of Flash (SWF) audio: 2 to 5 bits per sample, in blocks of 4096 samples. +//! +//! Written from the public descriptions of the algorithms: the IMA Digital Audio Focus and +//! Technical Working Groups' *Recommended Practices for Enhancing Digital Audio Compatibility* +//! (IMA ADPCM), Microsoft's *Multimedia Standards Update* (the WAVE format tags and the block +//! layout of Microsoft ADPCM), the SWF file format specification (the ADPCM section) and the +//! Yamaha ADPCM-B description. ffmpeg is only run as a black-box oracle in the tests. +//! +//! Decoders return errors, never panic, on damaged blocks. Encoders are greedy: they choose each +//! code to bring the decoder's output closest to the sample. + +pub mod ima; +pub mod ms; +pub mod swf; +pub mod yamaha; + +/// What went wrong decoding or encoding ADPCM. +#[non_exhaustive] +#[derive(Debug, thiserror::Error, PartialEq, Eq)] +pub enum Error { + /// The data is damaged, truncated or does not fit the stated block layout. + #[error("malformed ADPCM: {0}")] + Malformed(&'static str), + /// The settings or samples given to an encoder cannot be written. + #[error("ADPCM cannot encode: {0}")] + InvalidInput(&'static str), +} + +/// Result with this module's [`Error`]. +pub type Result = std::result::Result; + +/// IMA ADPCM step sizes (the 89-entry table of the IMA recommended practices). +pub(crate) const STEP_TABLE: [i32; 89] = [ + 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, + 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, + 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, + 22385, 24623, 27086, 29794, 32767, +]; + +/// Clamp to the 16-bit sample range. +pub(crate) fn clip16(v: i32) -> i32 { + v.clamp(i32::from(i16::MIN), i32::from(i16::MAX)) +} + +/// The step size at `index` (clamped to the table). +pub(crate) fn step_at(index: i32) -> i32 { + STEP_TABLE.get(index.clamp(0, 88) as usize).copied().unwrap_or(7) +} diff --git a/crates/audio/src/adpcm/ms.rs b/crates/audio/src/adpcm/ms.rs new file mode 100644 index 0000000000..057f93549f --- /dev/null +++ b/crates/audio/src/adpcm/ms.rs @@ -0,0 +1,244 @@ +//! Microsoft ADPCM in WAV blocks (format tag 0x0002). +//! +//! A block starts, for all channels in turn, with one predictor index (a byte, 0 to 6), then one +//! initial step (`delta`, `i16`), then each channel's second-last sample (`sample1`, `i16`) and +//! last sample (`sample2`, `i16`) before the block; the block's first two samples per channel are +//! `sample2` then `sample1`. The rest is 4-bit two's-complement codes, the first channel in the +//! high nibble. A block of `block_align` bytes holds `(block_align - 7 * channels) * 2 / channels +//! + 2` samples per channel. Each sample is predicted as `(sample1 * c1 + sample2 * c2) / 256` (rounding toward zero) +//! from the block's coefficient pair, plus `code * delta`; then `delta` becomes +//! `max(16, delta * a / 256)`, where `a` is the table entry for the code (`ADAPT`). + +use super::{Error, Result, clip16}; + +/// The seven coefficient pairs `(c1, c2)` every Microsoft ADPCM stream uses. +pub const COEFFICIENTS: [(i32, i32); 7] = [(256, 0), (512, -256), (0, 0), (192, 64), (240, 0), (460, -208), (392, -232)]; +/// How the step moves with the code's unsigned value (the index is the 4-bit pattern). +/// The largest step. The reference keeps it in 16 bits and no real stream gets near this bound; it +/// keeps every product in range however long a damaged block runs. +const MAX_DELTA: i32 = 1 << 24; +const ADAPT: [i32; 16] = [230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230]; + +#[derive(Clone, Copy)] +struct State { + c1: i32, + c2: i32, + delta: i32, + s1: i32, + s2: i32, +} + +impl State { + fn step(&mut self, nibble: u8) -> i16 { + let signed = i32::from(nibble) - if nibble & 8 != 0 { 16 } else { 0 }; + let predicted = (self.s1 * self.c1 + self.s2 * self.c2) / 256; + let sample = clip16(predicted + signed * self.delta); + self.s2 = self.s1; + self.s1 = sample; + let adapt = i64::from(ADAPT.get(usize::from(nibble)).copied().unwrap_or(256)); + let new_delta = (adapt * i64::from(self.delta)) >> 8; + self.delta = (new_delta.min(i64::from(MAX_DELTA)) as i32).max(16); + sample as i16 + } + + /// The code that brings the output closest to `sample`, applied to the state. + fn encode(&mut self, sample: i16) -> u8 { + let predicted = (self.s1 * self.c1 + self.s2 * self.c2) / 256; + let err = i32::from(sample) - predicted; + // round to the nearest multiple of delta + let q = if err >= 0 { (err + self.delta / 2) / self.delta } else { -((-err + self.delta / 2) / self.delta) }; + let code = (q.clamp(-8, 7) & 0xF) as u8; + self.step(code); + code + } +} + +/// Samples per channel in a block of `block_align` bytes; `None` for an impossible layout (1 or 2 +/// channels, and room for the header and at least one code byte). +pub fn samples_per_block(block_align: usize, channels: usize) -> Option { + if !(1..=2).contains(&channels) { + return None; + } + let body = block_align.checked_sub(7 * channels).filter(|&b| b > 0)?; + // codes pair up across channels + let per = (body.checked_mul(2)? / channels).checked_add(2)?; + // Include the header samples and all channels before callers size their buffers. + per.checked_mul(channels)?; + Some(per) +} + +/// Decode one block, appending its samples interleaved by channel to `out`. +pub fn decode_block(block: &[u8], channels: usize, out: &mut Vec) -> Result<()> { + let per = samples_per_block(block.len(), channels).ok_or(Error::Malformed("block size does not fit the channel count"))?; + let header = block.get(..7 * channels).ok_or(Error::Malformed("truncated block header"))?; + let mut states = Vec::with_capacity(channels); + let word = |i: usize| i32::from(i16::from_le_bytes([header.get(i).copied().unwrap_or(0), header.get(i + 1).copied().unwrap_or(0)])); + for ch in 0..channels { + let pred = usize::from(header.get(ch).copied().unwrap_or(0)); + let &(c1, c2) = COEFFICIENTS.get(pred).ok_or(Error::Malformed("predictor index above 6"))?; + states.push(State { c1, c2, delta: word(channels + 2 * ch), s1: word(3 * channels + 2 * ch), s2: word(5 * channels + 2 * ch) }); + } + let start = out.len(); + out.resize(start + per * channels, 0); + let samples = out.get_mut(start..).ok_or(Error::Malformed("output"))?; + for (ch, s) in states.iter().enumerate() { + if let Some(a) = samples.get_mut(ch) { + *a = s.s2 as i16; + } + if let Some(b) = samples.get_mut(channels + ch) { + *b = s.s1 as i16; + } + } + let codes = block.get(7 * channels..).ok_or(Error::Malformed("truncated block"))?; + let mut t = 2 * channels; + for byte in codes { + for nibble in [byte >> 4, byte & 0xF] { + let ch = t % channels; + if let (Some(o), Some(state)) = (samples.get_mut(t), states.get_mut(ch)) { + *o = state.step(nibble); + } + t += 1; + } + } + Ok(()) +} + +/// Decode a stream of blocks of `block_align` bytes. A final partial block is decoded in full if +/// it holds a header and a code byte. +pub fn decode(data: &[u8], channels: usize, block_align: usize) -> Result> { + samples_per_block(block_align, channels).ok_or(Error::Malformed("block size does not fit the channel count"))?; + let mut out = Vec::new(); + for block in data.chunks(block_align) { + if block.len() == block_align || samples_per_block(block.len(), channels).is_some() { + decode_block(block, channels, &mut out)?; + } + } + Ok(out) +} + +/// The squared error of encoding `col` (one channel) from state `st`. +fn trial(mut st: State, col: &[i16]) -> i64 { + let mut err = 0i64; + for &s in col { + st.encode(s); + err += (i64::from(s) - i64::from(st.s1)).pow(2); + } + err +} + +/// Encode one block from exactly `samples_per_block * channels` interleaved samples. Each channel +/// gets the coefficient pair and initial step that code it with the least error. +pub fn encode_block(samples: &[i16], channels: usize, block_align: usize) -> Result> { + let per = samples_per_block(block_align, channels).ok_or(Error::InvalidInput("block size does not fit the channel count"))?; + if samples.len() != per * channels { + return Err(Error::InvalidInput("a block needs exactly samples_per_block samples per channel")); + } + let mut block = vec![0u8; block_align]; + let mut states = Vec::with_capacity(channels); + for ch in 0..channels { + let col: Vec = samples.iter().skip(ch).step_by(channels).copied().collect(); + let (x0, x1) = (col.first().copied().unwrap_or(0), col.get(1).copied().unwrap_or(0)); + let rest = col.get(2..).unwrap_or(&[]); + let mut best: Option<(i64, usize, i32)> = None; + for (p, &(c1, c2)) in COEFFICIENTS.iter().enumerate() { + for delta in [16, 64, 256, 1024] { + let st = State { c1, c2, delta, s1: i32::from(x1), s2: i32::from(x0) }; + let e = trial(st, rest); + if best.is_none_or(|(b, _, _)| e < b) { + best = Some((e, p, delta)); + } + } + } + let (_, p, delta) = best.unwrap_or((0, 0, 16)); + let &(c1, c2) = COEFFICIENTS.get(p).unwrap_or(&(256, 0)); + states.push(State { c1, c2, delta, s1: i32::from(x1), s2: i32::from(x0) }); + let (d, a, b) = (delta as i16, x1, x0); + let put = |block: &mut Vec, at: usize, v: i16| { + if let Some(w) = block.get_mut(at..at + 2) { + w.copy_from_slice(&v.to_le_bytes()); + } + }; + if let Some(h) = block.get_mut(ch) { + *h = p as u8; + } + put(&mut block, channels + 2 * ch, d); + put(&mut block, 3 * channels + 2 * ch, a); + put(&mut block, 5 * channels + 2 * ch, b); + } + let mut t = 2 * channels; + for byte in block.iter_mut().skip(7 * channels) { + let mut b = 0u8; + for half in 0..2 { + let ch = t % channels; + let nibble = match states.get_mut(ch) { + Some(st) => st.encode(samples.get(t).copied().unwrap_or(0)), + None => 0, + }; + b |= nibble << (4 * (1 - half)); + t += 1; + } + *byte = b; + } + Ok(block) +} + +/// Encode interleaved samples as blocks of `block_align` bytes. The last block is padded with +/// copies of the last frame, so a decoder returns up to one block more than was given. +pub fn encode(samples: &[i16], channels: usize, block_align: usize) -> Result> { + let per = samples_per_block(block_align, channels).ok_or(Error::InvalidInput("block size does not fit the channel count"))?; + if !samples.len().is_multiple_of(channels) { + return Err(Error::InvalidInput("the sample count is not a multiple of the channel count")); + } + let mut out = Vec::new(); + for chunk in samples.chunks(per * channels) { + let mut padded = chunk.to_vec(); + let last: Vec = chunk.get(chunk.len().saturating_sub(channels)..).unwrap_or(&[]).to_vec(); + while padded.len() < per * channels { + padded.extend(last.iter().copied().chain(std::iter::repeat(0)).take(channels)); + } + out.extend(encode_block(&padded, channels, block_align)?); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn layout() { + assert_eq!(samples_per_block(256, 1), Some(500)); + assert_eq!(samples_per_block(512, 2), Some(500)); + assert_eq!(samples_per_block(7, 1), None); + assert_eq!(samples_per_block(100, 3), None); + } + + #[test] + fn unrepresentable_layouts_are_rejected() { + for (align, channels) in [(usize::MAX / 2 + 7, 1), (usize::MAX / 2 + 14, 2)] { + assert_eq!(samples_per_block(align, channels), None); + assert!(encode(&[], channels, align).is_err()); + assert!(encode_block(&[], channels, align).is_err()); + } + } + + #[test] + fn a_long_run_of_large_codes_cannot_overflow() { + // a step that starts at its largest 16-bit value and grows with every large code + let mut block = vec![0x77u8; 7 + 2000]; + block[..7].copy_from_slice(&[0, 255, 127, 0, 0, 0, 0]); + for b in block.iter_mut().skip(7) { + *b = 0x88; + } + let mut out = Vec::new(); + decode_block(&block, 1, &mut out).unwrap(); + assert_eq!(out.len(), 2000 * 2 + 2); + } + + #[test] + fn bad_predictor_index_is_an_error() { + let mut block = vec![0u8; 64]; + block[0] = 7; + assert!(decode_block(&block, 1, &mut Vec::new()).is_err()); + } +} diff --git a/crates/audio/src/adpcm/swf.rs b/crates/audio/src/adpcm/swf.rs new file mode 100644 index 0000000000..2fc780b1d0 --- /dev/null +++ b/crates/audio/src/adpcm/swf.rs @@ -0,0 +1,168 @@ +//! The ADPCM of Flash (SWF) audio, 2 to 5 bits per sample. +//! +//! The stream starts with two bits, the code size minus 2. It continues in blocks of 4096 samples +//! per channel: each block starts, for every channel, with the first sample (16 bits, signed) and +//! the step index (6 bits, 0 to 88), then 4095 codes per channel, interleaved by sample. All fields +//! are packed most significant bit first. A code is a sign bit and `bits - 1` magnitude bits; the +//! decoder adds `step >> (bits - 1)` plus `step >> k` for each magnitude bit set `k` places below +//! the top one, negated when the sign is set, and moves the step index by the adjustment table of +//! the code size (negative for small magnitudes, positive for large ones). The last block may +//! be shorter. + +use super::{Error, Result, clip16, step_at}; +use craft_media::bitstream::{BitReader, BitWriter}; + +/// Samples per channel in a full block. +pub const BLOCK_SAMPLES: usize = 4096; + +/// The index adjustment for each magnitude, per code size. +fn adjust(bits: u32) -> &'static [i32] { + match bits { + 2 => &[-1, 2], + 3 => &[-1, -1, 2, 4], + 4 => &[-1, -1, -1, -1, 2, 4, 6, 8], + _ => &[-1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16], + } +} + +#[derive(Clone, Copy)] +struct State { + predictor: i32, + index: i32, +} + +impl State { + fn apply(&mut self, code: u32, bits: u32) -> i16 { + let step = step_at(self.index); + let mag = code & ((1 << (bits - 1)) - 1); + let mut diff = step >> (bits - 1); + for k in 0..bits - 1 { + if mag & (1 << (bits - 2 - k)) != 0 { + diff += step >> k; + } + } + if code & (1 << (bits - 1)) != 0 { + diff = -diff; + } + self.predictor = clip16(self.predictor + diff); + self.index = (self.index + adjust(bits).get(mag as usize).copied().unwrap_or(0)).clamp(0, 88); + self.predictor as i16 + } + + fn encode(&mut self, sample: i16, bits: u32) -> u32 { + let step = step_at(self.index); + let mut diff = i32::from(sample) - self.predictor; + let mut code = 0u32; + if diff < 0 { + code = 1 << (bits - 1); + diff = -diff; + } + let mut s = step; + for k in 0..bits - 1 { + if diff >= s { + code |= 1 << (bits - 2 - k); + diff -= s; + } + s >>= 1; + } + self.apply(code, bits); + code + } +} + +/// Decode a stream. `channels` is 1 or 2; the code size comes from the stream's first two bits. +/// The result holds every sample up to the end of the data (a last block cut short decodes as +/// far as it goes). +pub fn decode(data: &[u8], channels: usize) -> Result> { + if !(1..=2).contains(&channels) { + return Err(Error::Malformed("Flash ADPCM has one or two channels")); + } + let mut r = BitReader::new(data); + let bits = r.read_bits(2).map_err(|_| Error::Malformed("empty stream"))? + 2; + let mut out = Vec::new(); + let header_bits = channels * 22; + while r.bits_left() >= header_bits { + let mut states = Vec::with_capacity(channels); + for _ in 0..channels { + let sample = r.read_bits(16).map_err(|_| Error::Malformed("truncated block"))? as u16 as i16; + let index = r.read_bits(6).map_err(|_| Error::Malformed("truncated block"))? as i32; + if index > 88 { + return Err(Error::Malformed("step index above 88")); + } + out.push(sample); + states.push(State { predictor: i32::from(sample), index }); + } + 'block: for _ in 1..BLOCK_SAMPLES { + if r.bits_left() < channels * (bits as usize) { + break 'block; + } + for st in states.iter_mut() { + match r.read_bits(bits) { + Ok(code) => out.push(st.apply(code, bits)), + Err(_) => break 'block, + } + } + } + } + Ok(out) +} + +/// Encode interleaved samples with `bits` (2 to 5) bits per sample. Blocks hold 4096 samples per +/// channel; the stream ends at a byte boundary. +pub fn encode(samples: &[i16], channels: usize, bits: u32) -> Result> { + if !(1..=2).contains(&channels) { + return Err(Error::InvalidInput("Flash ADPCM has one or two channels")); + } + if !(2..=5).contains(&bits) { + return Err(Error::InvalidInput("Flash ADPCM codes have 2 to 5 bits")); + } + if !samples.len().is_multiple_of(channels) { + return Err(Error::InvalidInput("the sample count is not a multiple of the channel count")); + } + let mut w = BitWriter::new(); + w.write_bits(bits - 2, 2); + for block in samples.chunks(BLOCK_SAMPLES * channels) { + let mut states = Vec::with_capacity(channels); + for ch in 0..channels { + let col: Vec = block.iter().skip(ch).step_by(channels).copied().collect(); + let first = col.first().copied().unwrap_or(0); + // the index whose first codes follow the signal most closely + let mut best = (i64::MAX, 0); + for index in 0..=63 { + let mut st = State { predictor: i32::from(first), index }; + let mut err = 0i64; + for &s in col.iter().skip(1).take(16) { + st.encode(s, bits); + err += (i64::from(s) - i64::from(st.predictor)).pow(2); + } + if err < best.0 { + best = (err, index); + } + } + w.write_bits(u32::from(first as u16), 16); + w.write_bits(best.1 as u32, 6); + states.push(State { predictor: i32::from(first), index: best.1 }); + } + for frame in block.chunks(channels).skip(1) { + for (st, &s) in states.iter_mut().zip(frame) { + w.write_bits(st.encode(s, bits), bits); + } + } + } + w.align_zero(); + Ok(w.finish()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn settings_are_checked() { + assert!(encode(&[0], 3, 4).is_err()); + assert!(encode(&[0], 1, 1).is_err()); + assert!(encode(&[0], 1, 6).is_err()); + assert!(decode(&[], 1).is_err()); + assert!(decode(&[0], 0).is_err()); + } +} diff --git a/crates/audio/src/adpcm/yamaha.rs b/crates/audio/src/adpcm/yamaha.rs new file mode 100644 index 0000000000..22ea0e77f4 --- /dev/null +++ b/crates/audio/src/adpcm/yamaha.rs @@ -0,0 +1,113 @@ +//! Yamaha ADPCM (the ADPCM-B of the YM2608 and the AICA): a raw stream of 4-bit codes, no header. +//! +//! Each channel starts at predictor 0 and step 127. A code is a sign bit and three magnitude bits; +//! the sample moves by `step * [1, 3, 5, 7, 9, 11, 13, 15][magnitude] / 8` (negative when the sign +//! is set) and the step is multiplied by `[230, 230, 230, 230, 307, 409, 512, 614][magnitude] / 256`, +//! kept within 127 and 24576. One channel: the low nibble of a byte is the first sample. Two +//! channels: the low nibble is the left channel and the high nibble the right one of the same +//! frame. + +use super::{Error, Result, clip16}; + +const SCALE: [i32; 8] = [230, 230, 230, 230, 307, 409, 512, 614]; + +#[derive(Clone, Copy)] +struct State { + predictor: i32, + step: i32, +} + +impl State { + fn new() -> State { + State { predictor: 0, step: 127 } + } + + fn apply(&mut self, nibble: u8) -> i16 { + let m = usize::from(nibble & 7); + let diff = self.step * (2 * m as i32 + 1) / 8; + self.predictor = clip16(self.predictor + if nibble & 8 != 0 { -diff } else { diff }); + self.step = ((self.step * SCALE.get(m).copied().unwrap_or(256)) >> 8).clamp(127, 24_576); + self.predictor as i16 + } + + fn encode(&mut self, sample: i16) -> u8 { + let err = i32::from(sample) - self.predictor; + let m = (err.abs() * 8 / self.step / 2).clamp(0, 7); + // the magnitude whose reconstruction (2m + 1) * step / 8 is nearest + let mut best = (i32::MAX, 0u8); + for cand in [m - 1, m, m + 1] { + let c = cand.clamp(0, 7); + let recon = self.step * (2 * c + 1) / 8; + let e = (err.abs() - recon).abs(); + if e < best.0 { + best = (e, c as u8); + } + } + let nibble = best.1 | if err < 0 { 8 } else { 0 }; + self.apply(nibble); + nibble + } +} + +/// Decode a stream of `channels` (1 or 2) interleaved channels. +pub fn decode(data: &[u8], channels: usize) -> Result> { + if !(1..=2).contains(&channels) { + return Err(Error::Malformed("Yamaha ADPCM has one or two channels")); + } + let mut states = vec![State::new(); channels]; + let mut out = Vec::with_capacity(data.len() * 2); + let mut t = 0usize; + for byte in data { + for nibble in [byte & 0xF, byte >> 4] { + if let Some(st) = states.get_mut(t % channels) { + out.push(st.apply(nibble)); + } + t += 1; + } + } + Ok(out) +} + +/// Encode interleaved samples. With two channels the frame count must be whole bytes, which it +/// always is; with one channel an odd count is padded with a copy of the last sample. +pub fn encode(samples: &[i16], channels: usize) -> Result> { + if !(1..=2).contains(&channels) { + return Err(Error::InvalidInput("Yamaha ADPCM has one or two channels")); + } + if !samples.len().is_multiple_of(channels) { + return Err(Error::InvalidInput("the sample count is not a multiple of the channel count")); + } + let mut states = vec![State::new(); channels]; + let mut out = Vec::with_capacity(samples.len().div_ceil(2)); + let mut t = 0usize; + let mut padded = samples.to_vec(); + if padded.len() % 2 == 1 { + padded.push(padded.last().copied().unwrap_or(0)); + } + for pair in padded.chunks(2) { + let mut byte = 0u8; + for (half, &s) in pair.iter().enumerate() { + let code = match states.get_mut(t % channels) { + Some(st) => st.encode(s), + None => 0, + }; + byte |= code << (4 * half); + t += 1; + } + out.push(byte); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn channel_count_is_checked() { + assert!(decode(&[0], 0).is_err()); + assert!(decode(&[0], 3).is_err()); + assert!(encode(&[0], 3).is_err()); + assert!(encode(&[0, 0, 0], 2).is_err()); + } +} diff --git a/crates/audio/src/dsd/dsdiff.rs b/crates/audio/src/dsd/dsdiff.rs new file mode 100644 index 0000000000..f9f12d04d3 --- /dev/null +++ b/crates/audio/src/dsd/dsdiff.rs @@ -0,0 +1,378 @@ +//! Philips DSDIFF (Direct Stream Digital Interchange File Format, `.dff`) parser and writer. +//! +//! DSDIFF files use an IFF-style big-endian chunk format starting with `FRM8` of type `DSD `: +//! - `FRM8` container chunk containing: +//! - `FVER`: format version (typically 1.5.0.0). +//! - `PROP`: properties chunk with subtype `SND `, containing: +//! - `FS `: sampling frequency (e.g. 2,822,400 Hz for DSD64). +//! - `CHNL`: channel count and 4-character channel ID codes (`SLFT`, `SRGT`, etc.). +//! - `CMPR`: compression type (`DSD ` for uncompressed, `DST ` for compressed). +//! - `DSD `: audio data chunk containing channel-interleaved 1-bit samples (MSB-first). +//! - Optional `DIIN`: disc and track information (title, artist). + +use super::{BitOrder, Error, Format, MAX_CHANNELS, MAX_DECODE_BYTES, MAX_SAMPLE_RATE, MIN_SAMPLE_RATE, Metadata, RawAudio}; + +/// Decodes 4-byte ASCII channel identifiers into human-readable channel names. +fn channel_id_to_name(id: &[u8; 4]) -> String { + match id { + b"SLFT" => "FrontLeft".to_string(), + b"SRGT" => "FrontRight".to_string(), + b"MLFT" => "Mono/Left".to_string(), + b"MRGT" => "Right".to_string(), + b"C " => "Center".to_string(), + b"LFE " => "LFE".to_string(), + b"LS " => "LeftSurround".to_string(), + b"RS " => "RightSurround".to_string(), + b"CO " => "CenterOverhead".to_string(), + b"LH " => "LeftHeight".to_string(), + b"RH " => "RightHeight".to_string(), + _ => { + if let Ok(s) = std::str::from_utf8(id) { + let trimmed = s.trim(); + if !trimmed.is_empty() { + return trimmed.to_string(); + } + } + format!("Channel {:02X}{:02X}{:02X}{:02X}", id[0], id[1], id[2], id[3]) + } + } +} + +/// The text of a `DITI` or `DIAR` chunk: a four-byte big-endian character count, then that many +/// bytes. `None` when the count is missing or longer than the chunk. +fn diin_text(sub_data: &[u8]) -> Option { + let count = usize::try_from(u32::from_be_bytes(sub_data.get(..4)?.try_into().ok()?)).ok()?; + let text = sub_data.get(4..)?.get(..count)?; + Some(String::from_utf8_lossy(text).trim().to_string()) +} + +/// The channel identifiers DSDIFF 1.5 defines for a channel count (stereo, 5 and 5.1 channels); +/// other counts use the generic `C001`, `C002`, ... identifiers. +fn channel_ids(num_channels: usize) -> Vec<[u8; 4]> { + let named: &[&[u8; 4]] = match num_channels { + 1 => &[b"SLFT"], + 2 => &[b"SLFT", b"SRGT"], + 5 => &[b"MLFT", b"MRGT", b"C ", b"LS ", b"RS "], + 6 => &[b"MLFT", b"MRGT", b"C ", b"LFE ", b"LS ", b"RS "], + _ => &[], + }; + if !named.is_empty() { + return named.iter().map(|id| **id).collect(); + } + (1..=num_channels) + .map(|n| { + let s = format!("C{n:03}"); + let mut id = [b' '; 4]; + id.copy_from_slice(&s.as_bytes()[..4]); + id + }) + .collect() +} + +/// Parses the metadata from a DSDIFF file. +pub fn parse_dsdiff_header(bytes: &[u8]) -> Result<(Metadata, usize, usize), Error> { + if bytes.len() < 16 { + return Err(Error::UnexpectedEof("DSDIFF file is too short")); + } + + if &bytes[0..4] != b"FRM8" { + return Err(Error::InvalidHeader("missing 'FRM8' magic chunk")); + } + let form_size = u64::from_be_bytes(bytes[4..12].try_into().map_err(|_| Error::UnexpectedEof("FRM8 size"))?); + if &bytes[12..16] != b"DSD " { + return Err(Error::InvalidHeader("invalid FRM8 form type (expected 'DSD ')")); + } + + let total_file_data = bytes.len().saturating_sub(12); + let form_size = usize::try_from(form_size).map_err(|_| Error::MemoryLimitExceeded)?; + if form_size > total_file_data { + return Err(Error::UnexpectedEof("FRM8 chunk extends beyond file size")); + } + + let mut pos = 16; + let end = 12 + form_size; + + let mut sample_rate = None; + let mut channel_names = Vec::new(); + let mut num_channels = None; + let mut is_compressed_dst = false; + let mut dsd_data_offset = None; + let mut dsd_data_len = None; + let mut title = None; + let mut artist = None; + + while pos + 12 <= end { + let chunk_id: [u8; 4] = bytes[pos..pos + 4].try_into().map_err(|_| Error::UnexpectedEof("chunk ID"))?; + let chunk_size = usize::try_from(u64::from_be_bytes(bytes[pos + 4..pos + 12].try_into().map_err(|_| Error::UnexpectedEof("chunk size"))?)) + .map_err(|_| Error::MemoryLimitExceeded)?; + pos += 12; + + if chunk_size > end.saturating_sub(pos) { + return Err(Error::UnexpectedEof("subchunk extends beyond container bounds")); + } + + let chunk_data = &bytes[pos..pos + chunk_size]; + let pad = chunk_size & 1; + pos = pos.saturating_add(chunk_size).saturating_add(pad); + + match &chunk_id { + b"PROP" => { + if chunk_data.len() < 4 { + continue; + } + if &chunk_data[0..4] != b"SND " { + continue; + } + let mut prop_pos = 4; + while prop_pos + 12 <= chunk_data.len() { + let sub_id: [u8; 4] = chunk_data[prop_pos..prop_pos + 4].try_into().map_err(|_| Error::UnexpectedEof("PROP subchunk ID"))?; + let sub_size = usize::try_from(u64::from_be_bytes( + chunk_data[prop_pos + 4..prop_pos + 12].try_into().map_err(|_| Error::UnexpectedEof("PROP subchunk size"))?, + )) + .map_err(|_| Error::MemoryLimitExceeded)?; + prop_pos += 12; + + if sub_size > chunk_data.len().saturating_sub(prop_pos) { + return Err(Error::UnexpectedEof("PROP subchunk extends beyond bounds")); + } + + let sub_data = &chunk_data[prop_pos..prop_pos + sub_size]; + let sub_pad = sub_size & 1; + prop_pos = prop_pos.saturating_add(sub_size).saturating_add(sub_pad); + + match &sub_id { + b"FS " => { + if sub_data.len() < 4 { + return Err(Error::UnexpectedEof("truncated FS chunk")); + } + if sample_rate.is_some() { + return Err(Error::InvalidHeader("duplicate FS chunk")); + } + let sr = u32::from_be_bytes([sub_data[0], sub_data[1], sub_data[2], sub_data[3]]); + if !(MIN_SAMPLE_RATE..=MAX_SAMPLE_RATE).contains(&sr) { + return Err(Error::InvalidHeader("sampling frequency out of range")); + } + sample_rate = Some(sr); + } + b"CHNL" => { + if sub_data.len() < 2 { + return Err(Error::UnexpectedEof("truncated CHNL chunk")); + } + if num_channels.is_some() { + return Err(Error::InvalidHeader("duplicate CHNL chunk")); + } + let n = u16::from_be_bytes([sub_data[0], sub_data[1]]); + if n == 0 || n > MAX_CHANNELS as u16 { + return Err(Error::Unsupported("channel count must be between 1 and 8")); + } + num_channels = Some(n); + channel_names.clear(); + let mut ch_pos = 2; + while ch_pos + 4 <= sub_data.len() && channel_names.len() < n as usize { + let id: [u8; 4] = [sub_data[ch_pos], sub_data[ch_pos + 1], sub_data[ch_pos + 2], sub_data[ch_pos + 3]]; + channel_names.push(channel_id_to_name(&id)); + ch_pos += 4; + } + } + b"CMPR" if sub_data.len() >= 4 => { + if &sub_data[0..4] == b"DST " { + is_compressed_dst = true; + } else if &sub_data[0..4] != b"DSD " { + return Err(Error::Unsupported("unknown DSDIFF compression type")); + } + } + _ => {} + } + } + } + b"DSD " => { + dsd_data_offset = Some(pos - chunk_size - pad); + dsd_data_len = Some(chunk_size); + } + b"DST " => { + is_compressed_dst = true; + } + b"DIIN" => { + // Parse optional disc info for title and artist. + let mut diin_pos = 0; + while diin_pos + 12 <= chunk_data.len() { + let sub_id = &chunk_data[diin_pos..diin_pos + 4]; + let sub_size = usize::try_from(u64::from_be_bytes( + chunk_data[diin_pos + 4..diin_pos + 12].try_into().map_err(|_| Error::UnexpectedEof("DIIN subchunk size"))?, + )) + .map_err(|_| Error::MemoryLimitExceeded)?; + diin_pos += 12; + if sub_size > chunk_data.len().saturating_sub(diin_pos) { + break; + } + let sub_data = &chunk_data[diin_pos..diin_pos + sub_size]; + let sub_pad = sub_size & 1; + diin_pos = diin_pos.saturating_add(sub_size).saturating_add(sub_pad); + + if sub_id == b"DITI" && title.is_none() { + title = diin_text(sub_data); + } else if sub_id == b"DIAR" && artist.is_none() { + artist = diin_text(sub_data); + } + } + } + _ => {} + } + } + + if is_compressed_dst { + return Err(Error::Unsupported("DST compression in DSDIFF is not yet supported")); + } + + let sample_rate = sample_rate.ok_or(Error::InvalidHeader("missing sample rate ('FS ' chunk)"))?; + if !(MIN_SAMPLE_RATE..=MAX_SAMPLE_RATE).contains(&sample_rate) { + return Err(Error::InvalidHeader("sampling frequency out of range")); + } + + let channels = num_channels.ok_or(Error::InvalidHeader("missing channel info ('CHNL' chunk)"))?; + if channels == 0 || channels > MAX_CHANNELS as u16 { + return Err(Error::Unsupported("channel count must be between 1 and 8")); + } + + let data_offset = dsd_data_offset.ok_or(Error::InvalidHeader("missing audio data ('DSD ' chunk)"))?; + let data_len = dsd_data_len.ok_or(Error::InvalidHeader("missing audio data length"))?; + + let bytes_per_channel = data_len / (channels as usize); + let sample_count = (bytes_per_channel as u64).saturating_mul(8); + + if channel_names.len() < channels as usize { + for c in channel_names.len()..(channels as usize) { + channel_names.push(format!("Channel {}", c + 1)); + } + } + + let metadata = Metadata { format: Format::Dsdiff, sample_rate, channels, sample_count, channel_names, title, artist }; + + Ok((metadata, data_offset, data_len)) +} + +/// Parses a DSDIFF file into raw 1-bit planar channel streams. +pub fn parse_dsdiff(bytes: &[u8]) -> Result { + let (metadata, data_offset, data_len) = parse_dsdiff_header(bytes)?; + let channels = metadata.channels as usize; + let bytes_per_channel = data_len / channels; + + if bytes_per_channel.saturating_mul(channels) > MAX_DECODE_BYTES { + return Err(Error::MemoryLimitExceeded); + } + + let end_offset = data_offset + data_len; + if end_offset > bytes.len() { + return Err(Error::UnexpectedEof("DSD sound data extends beyond file bounds")); + } + let data_slice = &bytes[data_offset..end_offset]; + + let mut channel_bytes: Vec> = Vec::with_capacity(channels); + for _ in 0..channels { + let mut ch_vec = Vec::new(); + ch_vec.try_reserve_exact(bytes_per_channel).map_err(|_| Error::MemoryLimitExceeded)?; + channel_bytes.push(ch_vec); + } + + // De-interleave byte-by-byte: Ch0, Ch1, ... Ch(N-1), Ch0, Ch1 ... + for chunk in data_slice.chunks_exact(channels) { + for (c, &byte) in chunk.iter().enumerate() { + if let Some(ch_vec) = channel_bytes.get_mut(c) { + ch_vec.push(byte); + } + } + } + + Ok(RawAudio { metadata, bit_order: BitOrder::MsbFirst, channel_bytes }) +} + +/// Encodes 1-bit planar audio channels into a valid Philips DSDIFF (`.dff`) file. +pub fn write_dsdiff(sample_rate: u32, channels: &[&[u8]], sample_count: u64) -> Result, Error> { + if channels.is_empty() || channels.len() > MAX_CHANNELS { + return Err(Error::Unsupported("channel count must be between 1 and 8")); + } + if !(MIN_SAMPLE_RATE..=MAX_SAMPLE_RATE).contains(&sample_rate) { + return Err(Error::InvalidData("sample rate out of bounds")); + } + + let num_channels = channels.len(); + let bytes_per_ch = usize::try_from(sample_count.div_ceil(8)).map_err(|_| Error::MemoryLimitExceeded)?; + let data_size = bytes_per_ch.checked_mul(num_channels).ok_or(Error::MemoryLimitExceeded)?; + // Fixed headers plus four bytes for each channel identifier. + // An odd-sized chunk payload is followed by a pad byte that its size field does not count. + let data_pad = data_size & 1; + let padded_size = data_size.checked_add(data_pad).ok_or(Error::MemoryLimitExceeded)?; + let file_size = super::encoded_size(padded_size, 110 + 4 * num_channels)?; + + // Build PROP subchunks: + // 1. 'FS ' (4 bytes + 12 chunk header) + let mut fs_chunk = Vec::with_capacity(16); + fs_chunk.extend_from_slice(b"FS "); + fs_chunk.extend_from_slice(&4u64.to_be_bytes()); + fs_chunk.extend_from_slice(&sample_rate.to_be_bytes()); + + // 2. 'CHNL' (2 + 4 * num_channels + 12 chunk header) + let chnl_data_len = 2 + 4 * num_channels; + let mut chnl_chunk = Vec::with_capacity(12 + chnl_data_len + 1); + chnl_chunk.extend_from_slice(b"CHNL"); + chnl_chunk.extend_from_slice(&(chnl_data_len as u64).to_be_bytes()); + chnl_chunk.extend_from_slice(&(num_channels as u16).to_be_bytes()); + for id in channel_ids(num_channels) { + chnl_chunk.extend_from_slice(&id); + } + if !chnl_data_len.is_multiple_of(2) { + chnl_chunk.push(0); + } + + // 3. 'CMPR' (4 + 1 + 3 + pad = 8 bytes + 12 chunk header) + let mut cmpr_chunk = Vec::with_capacity(20); + cmpr_chunk.extend_from_slice(b"CMPR"); + cmpr_chunk.extend_from_slice(&8u64.to_be_bytes()); + cmpr_chunk.extend_from_slice(b"DSD "); + cmpr_chunk.push(3); // name len + cmpr_chunk.extend_from_slice(b"DSD"); + + let prop_payload_len = 4 + fs_chunk.len() + chnl_chunk.len() + cmpr_chunk.len(); + let mut prop_chunk = Vec::with_capacity(12 + prop_payload_len); + prop_chunk.extend_from_slice(b"PROP"); + prop_chunk.extend_from_slice(&(prop_payload_len as u64).to_be_bytes()); + prop_chunk.extend_from_slice(b"SND "); + prop_chunk.extend_from_slice(&fs_chunk); + prop_chunk.extend_from_slice(&chnl_chunk); + prop_chunk.extend_from_slice(&cmpr_chunk); + + // FVER chunk (4 bytes payload + 12 chunk header) + let mut fver_chunk = Vec::with_capacity(16); + fver_chunk.extend_from_slice(b"FVER"); + fver_chunk.extend_from_slice(&4u64.to_be_bytes()); + fver_chunk.extend_from_slice(&0x01050000u32.to_be_bytes()); + + // DSD chunk (data_size payload + 12 chunk header) + let mut dsd_chunk_hdr = Vec::with_capacity(12); + dsd_chunk_hdr.extend_from_slice(b"DSD "); + dsd_chunk_hdr.extend_from_slice(&(data_size as u64).to_be_bytes()); + + let frm8_payload_len = file_size - 12; + let mut out = Vec::new(); + out.try_reserve_exact(file_size).map_err(|_| Error::MemoryLimitExceeded)?; + + out.extend_from_slice(b"FRM8"); + out.extend_from_slice(&(frm8_payload_len as u64).to_be_bytes()); + out.extend_from_slice(b"DSD "); + out.extend_from_slice(&fver_chunk); + out.extend_from_slice(&prop_chunk); + out.extend_from_slice(&dsd_chunk_hdr); + + // Interleave channels byte-by-byte. + for i in 0..bytes_per_ch { + for ch_data in channels { + let byte = ch_data.get(i).copied().unwrap_or(super::filter::SILENCE_MSBF); + out.push(byte); + } + } + if data_pad == 1 { + out.push(0); + } + + Ok(out) +} diff --git a/crates/audio/src/dsd/dsf.rs b/crates/audio/src/dsd/dsf.rs new file mode 100644 index 0000000000..1fb36c18da --- /dev/null +++ b/crates/audio/src/dsd/dsf.rs @@ -0,0 +1,391 @@ +//! Sony DSF (Direct Stream Digital File) parser and writer. +//! +//! A DSF file consists of three contiguous little-endian chunks: +//! - `'DSD '` chunk (28 bytes): file identification, total size, and metadata offset. +//! - `'fmt '` chunk (52 bytes): sample rate, channel count, sample count, block size. +//! - `'data'` chunk (12 bytes + data): 1-bit audio organized into channel blocks. +//! - Optional ID3v2 metadata chunk located at `metadata_offset`. + +use super::{BitOrder, Error, Format, MAX_BLOCK_SIZE, MAX_CHANNELS, MAX_DECODE_BYTES, MAX_SAMPLE_RATE, MIN_SAMPLE_RATE, Metadata, RawAudio}; + +/// Channel configuration names for DSF channel types 1..=7. +fn dsf_channel_names(channel_type: u32, channel_count: u16) -> Vec { + match channel_type { + 1 => vec!["Mono".to_string()], + 2 => vec!["Left".to_string(), "Right".to_string()], + 3 => vec!["Left".to_string(), "Right".to_string(), "Center".to_string()], + 4 => vec!["FrontLeft".to_string(), "FrontRight".to_string(), "BackLeft".to_string(), "BackRight".to_string()], + 5 => vec!["FrontLeft".to_string(), "FrontRight".to_string(), "Center".to_string(), "LFE".to_string()], + 6 => vec!["FrontLeft".to_string(), "FrontRight".to_string(), "Center".to_string(), "BackLeft".to_string(), "BackRight".to_string()], + 7 => vec![ + "FrontLeft".to_string(), + "FrontRight".to_string(), + "Center".to_string(), + "LFE".to_string(), + "BackLeft".to_string(), + "BackRight".to_string(), + ], + _ => (0..channel_count).map(|c| format!("Channel {}", c + 1)).collect(), + } +} + +/// Attempts to parse basic ID3v2 title and artist tags if present at `offset`. +fn parse_id3v2(bytes: &[u8], offset: usize) -> (Option, Option) { + let Some(id3) = bytes.get(offset..) else { + return (None, None); + }; + if id3.len() < 10 || &id3[..3] != b"ID3" { + return (None, None); + } + + let mut title = None; + let mut artist = None; + + // ID3v2 size is 4 synchsafe bytes at offset 6. + let tag_size = ((id3[6] as usize & 0x7F) << 21) | ((id3[7] as usize & 0x7F) << 14) | ((id3[8] as usize & 0x7F) << 7) | (id3[9] as usize & 0x7F); + + let end = (10 + tag_size).min(id3.len()); + let mut pos = 10; + + // Scan ID3v2.3 / ID3v2.4 frames. + while pos + 10 <= end { + let frame_id = &id3[pos..pos + 4]; + if frame_id[0] == 0 { + break; + } + let raw = [id3[pos + 4], id3[pos + 5], id3[pos + 6], id3[pos + 7]]; + // ID3v2.4 frame sizes are synchsafe; v2.3 ones are plain big-endian. + let frame_size = if id3[3] >= 4 { + if raw.iter().any(|b| b & 0x80 != 0) { + break; + } + ((raw[0] as usize) << 21) | ((raw[1] as usize) << 14) | ((raw[2] as usize) << 7) | raw[3] as usize + } else { + u32::from_be_bytes(raw) as usize + }; + pos += 10; + if frame_size > end.saturating_sub(pos) { + break; + } + + let frame_data = &id3[pos..pos + frame_size]; + pos += frame_size; + + if frame_id == b"TIT2" && title.is_none() { + title = parse_text_frame(frame_data); + } else if frame_id == b"TPE1" && artist.is_none() { + artist = parse_text_frame(frame_data); + } + } + + (title, artist) +} + +fn parse_text_frame(data: &[u8]) -> Option { + if data.is_empty() { + return None; + } + let encoding = data[0]; + let content = &data[1..]; + let text = match encoding { + // ISO-8859-1: every byte is the code point of the same value. + 0 => content.iter().map(|&b| char::from(b)).collect::(), + 3 => String::from_utf8_lossy(content).into_owned(), + // UTF-16 with a byte order mark (which is removed); without one, assume big-endian. + 1 => { + let (big_endian, body) = match content { + [0xFE, 0xFF, rest @ ..] => (true, rest), + [0xFF, 0xFE, rest @ ..] => (false, rest), + _ => (true, content), + }; + utf16(body, big_endian) + } + // UTF-16BE without a byte order mark. + 2 => utf16(content, true), + _ => return None, + }; + let s = text.trim_end_matches('\0').trim().to_string(); + if s.is_empty() { None } else { Some(s) } +} + +fn utf16(bytes: &[u8], big_endian: bool) -> String { + let units: Vec = bytes + .as_chunks::<2>() + .0 + .iter() + .map(|c| if big_endian { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }) + .collect(); + String::from_utf16_lossy(&units) +} + +/// Parses DSF file metadata, returning the metadata, audio data offset, and block size per channel. +pub fn parse_dsf_header(bytes: &[u8]) -> Result<(Metadata, usize, u32), Error> { + let (metadata, offset, _, block_size) = parse_dsf_header_bounded(bytes)?; + Ok((metadata, offset, block_size)) +} + +fn parse_dsf_header_bounded(bytes: &[u8]) -> Result<(Metadata, usize, usize, u32), Error> { + if bytes.len() < 28 + 52 + 12 { + return Err(Error::UnexpectedEof("DSF file header is truncated")); + } + + // 1. 'DSD ' chunk + if &bytes[..4] != b"DSD " { + return Err(Error::InvalidHeader("missing 'DSD ' magic header")); + } + let dsd_chunk_size = u64::from_le_bytes(bytes[4..12].try_into().map_err(|_| Error::UnexpectedEof("DSD chunk size"))?); + if dsd_chunk_size != 28 { + return Err(Error::InvalidHeader("invalid 'DSD ' chunk size (must be 28)")); + } + let file_size = u64::from_le_bytes(bytes[12..20].try_into().map_err(|_| Error::UnexpectedEof("file size"))?); + let metadata_offset = u64::from_le_bytes(bytes[20..28].try_into().map_err(|_| Error::UnexpectedEof("metadata offset"))?); + + if file_size > bytes.len() as u64 { + return Err(Error::UnexpectedEof("declared file size extends beyond buffer")); + } + let file_len = usize::try_from(file_size).map_err(|_| Error::MemoryLimitExceeded)?; + if file_len < 28 + 52 + 12 { + return Err(Error::InvalidHeader("declared file size is smaller than required headers")); + } + let file_bytes = &bytes[..file_len]; + + // 2. 'fmt ' chunk + let fmt_offset = 28; + if &file_bytes[fmt_offset..fmt_offset + 4] != b"fmt " { + return Err(Error::InvalidHeader("missing 'fmt ' chunk header")); + } + let fmt_chunk_size = + u64::from_le_bytes(file_bytes[fmt_offset + 4..fmt_offset + 12].try_into().map_err(|_| Error::UnexpectedEof("fmt chunk size"))?); + if fmt_chunk_size < 52 { + return Err(Error::InvalidHeader("invalid 'fmt ' chunk size (must be at least 52)")); + } + let fmt_size = usize::try_from(fmt_chunk_size).map_err(|_| Error::MemoryLimitExceeded)?; + if fmt_size > file_bytes.len().saturating_sub(fmt_offset) { + return Err(Error::UnexpectedEof("fmt chunk extends beyond file bounds")); + } + + let format_version = + u32::from_le_bytes(file_bytes[fmt_offset + 12..fmt_offset + 16].try_into().map_err(|_| Error::UnexpectedEof("format version"))?); + if format_version != 1 { + return Err(Error::Unsupported("unsupported DSF format version (only version 1 is supported)")); + } + + let format_id = u32::from_le_bytes(file_bytes[fmt_offset + 16..fmt_offset + 20].try_into().map_err(|_| Error::UnexpectedEof("format id"))?); + if format_id != 0 { + return Err(Error::Unsupported("unsupported DSF format id (only raw DSD id=0 is supported)")); + } + + let channel_type = u32::from_le_bytes(file_bytes[fmt_offset + 20..fmt_offset + 24].try_into().map_err(|_| Error::UnexpectedEof("channel type"))?); + let channel_count = + u32::from_le_bytes(file_bytes[fmt_offset + 24..fmt_offset + 28].try_into().map_err(|_| Error::UnexpectedEof("channel count"))?); + if channel_count == 0 || channel_count > MAX_CHANNELS as u32 { + return Err(Error::Unsupported("DSF channel count must be between 1 and 8")); + } + let channels = channel_count as u16; + + let sample_rate = + u32::from_le_bytes(file_bytes[fmt_offset + 28..fmt_offset + 32].try_into().map_err(|_| Error::UnexpectedEof("sampling frequency"))?); + if !(MIN_SAMPLE_RATE..=MAX_SAMPLE_RATE).contains(&sample_rate) { + return Err(Error::InvalidHeader("DSF sampling frequency out of valid range")); + } + + let bits_per_sample = + u32::from_le_bytes(file_bytes[fmt_offset + 32..fmt_offset + 36].try_into().map_err(|_| Error::UnexpectedEof("bits per sample"))?); + if bits_per_sample != 1 { + return Err(Error::Unsupported("DSF bits per sample must be 1")); + } + + let sample_count = u64::from_le_bytes(file_bytes[fmt_offset + 36..fmt_offset + 44].try_into().map_err(|_| Error::UnexpectedEof("sample count"))?); + + let block_size_per_channel = + u32::from_le_bytes(file_bytes[fmt_offset + 44..fmt_offset + 48].try_into().map_err(|_| Error::UnexpectedEof("block size per channel"))?); + if block_size_per_channel == 0 || block_size_per_channel > MAX_BLOCK_SIZE { + return Err(Error::InvalidHeader("DSF block size per channel is invalid or too large")); + } + + // 3. 'data' chunk + let data_offset = fmt_offset.saturating_add(fmt_size); + if data_offset.saturating_add(12) > file_bytes.len() { + return Err(Error::UnexpectedEof("missing 'data' chunk header")); + } + if &file_bytes[data_offset..data_offset + 4] != b"data" { + return Err(Error::InvalidHeader("missing 'data' chunk header")); + } + let data_chunk_size = + u64::from_le_bytes(file_bytes[data_offset + 4..data_offset + 12].try_into().map_err(|_| Error::UnexpectedEof("data chunk size"))?); + if data_chunk_size < 12 { + return Err(Error::InvalidHeader("invalid 'data' chunk size (must be at least 12)")); + } + + let payload_offset = data_offset + 12; + let payload_size = usize::try_from(data_chunk_size - 12).map_err(|_| Error::MemoryLimitExceeded)?; + if payload_size > file_bytes.len().saturating_sub(payload_offset) { + return Err(Error::UnexpectedEof("data chunk payload extends beyond file bounds")); + } + let data_end = payload_offset.saturating_add(payload_size); + + // Parse ID3v2 metadata if pointer is valid. + let (title, artist) = if metadata_offset == 0 { + (None, None) + } else { + let meta_offset = usize::try_from(metadata_offset).map_err(|_| Error::MemoryLimitExceeded)?; + if meta_offset < data_end || meta_offset >= file_len { + return Err(Error::InvalidHeader("DSF metadata offset outside declared file bounds")); + } + parse_id3v2(file_bytes, meta_offset) + }; + + let channel_names = dsf_channel_names(channel_type, channels); + + let metadata = Metadata { format: Format::Dsf, sample_rate, channels, sample_count, channel_names, title, artist }; + + Ok((metadata, payload_offset, payload_size, block_size_per_channel)) +} + +/// Parses a DSF file into raw 1-bit planar channel streams. +pub fn parse_dsf(bytes: &[u8]) -> Result { + let (metadata, payload_offset, payload_size, block_size) = parse_dsf_header_bounded(bytes)?; + let block_size = block_size as usize; + let channels = metadata.channels as usize; + + let target_bytes_per_channel = usize::try_from(metadata.sample_count.div_ceil(8)).map_err(|_| Error::MemoryLimitExceeded)?; + if target_bytes_per_channel.saturating_mul(channels) > MAX_DECODE_BYTES { + return Err(Error::MemoryLimitExceeded); + } + + let slice_size = channels.saturating_mul(block_size); + if slice_size == 0 { + return Err(Error::InvalidHeader("zero slice size")); + } + + // The header validated this range. Trailing ID3 or other bytes are not audio, + // even if sample_count asks for more samples than the data chunk contains. + let payload = &bytes[payload_offset..payload_offset + payload_size]; + let num_slices = payload.len() / slice_size; + + let mut channel_bytes: Vec> = Vec::with_capacity(channels); + for _ in 0..channels { + let mut ch_vec = Vec::new(); + ch_vec.try_reserve_exact(target_bytes_per_channel).map_err(|_| Error::MemoryLimitExceeded)?; + channel_bytes.push(ch_vec); + } + + for s in 0..num_slices { + let slice_base = s * slice_size; + for (c, ch_vec) in channel_bytes.iter_mut().enumerate() { + let ch_start = slice_base + c * block_size; + let ch_end = ch_start + block_size; + if let Some(block) = payload.get(ch_start..ch_end) { + let remaining = target_bytes_per_channel.saturating_sub(ch_vec.len()); + let take = block.len().min(remaining); + ch_vec.extend_from_slice(&block[..take]); + } + } + } + + // Handle any trailing partial slice. + let remainder = payload.len() % slice_size; + if remainder > 0 { + let slice_base = num_slices * slice_size; + for (c, ch_vec) in channel_bytes.iter_mut().enumerate() { + let ch_start = slice_base + c * block_size; + if ch_start < payload.len() { + let ch_end = (ch_start + block_size).min(payload.len()); + let block = &payload[ch_start..ch_end]; + let remaining = target_bytes_per_channel.saturating_sub(ch_vec.len()); + let take = block.len().min(remaining); + ch_vec.extend_from_slice(&block[..take]); + } + } + } + + // Pad with silence (0x69) if the file had fewer bytes than required by sample_count. + for ch_vec in &mut channel_bytes { + if ch_vec.len() < target_bytes_per_channel { + ch_vec.resize(target_bytes_per_channel, super::filter::SILENCE_LSBF); + } + } + + Ok(RawAudio { metadata, bit_order: BitOrder::LsbFirst, channel_bytes }) +} + +fn unsupported_channel_layout() -> Result { + Err(Error::Unsupported("DSF has no channel type for this channel count (1 to 6 are supported)")) +} + +/// Encodes 1-bit planar audio channels into a valid Sony DSF file. +pub fn write_dsf(sample_rate: u32, channels: &[&[u8]], sample_count: u64, block_size: u32) -> Result, Error> { + if channels.is_empty() || channels.len() > MAX_CHANNELS { + return Err(Error::Unsupported("channel count must be between 1 and 8")); + } + if !(MIN_SAMPLE_RATE..=MAX_SAMPLE_RATE).contains(&sample_rate) { + return Err(Error::InvalidData("sample rate out of bounds")); + } + if block_size > MAX_BLOCK_SIZE { + return Err(Error::InvalidData("DSF block size exceeds maximum")); + } + let block_size = if block_size == 0 { 4096 } else { block_size as usize }; + + let num_channels = channels.len(); + let bytes_per_ch = usize::try_from(sample_count.div_ceil(8)).map_err(|_| Error::MemoryLimitExceeded)?; + let num_blocks = bytes_per_ch.div_ceil(block_size); + let payload_size = num_blocks.checked_mul(num_channels).and_then(|n| n.checked_mul(block_size)).ok_or(Error::MemoryLimitExceeded)?; + let file_size = super::encoded_size(payload_size, 92)?; + + let data_chunk_size = 12u64 + payload_size as u64; + + let mut out = Vec::new(); + out.try_reserve_exact(file_size).map_err(|_| Error::MemoryLimitExceeded)?; + + // 1. 'DSD ' chunk (28 bytes) + out.extend_from_slice(b"DSD "); + out.extend_from_slice(&28u64.to_le_bytes()); + out.extend_from_slice(&(file_size as u64).to_le_bytes()); + out.extend_from_slice(&0u64.to_le_bytes()); // metadata offset = 0 + + // 2. 'fmt ' chunk (52 bytes) + out.extend_from_slice(b"fmt "); + out.extend_from_slice(&52u64.to_le_bytes()); + out.extend_from_slice(&1u32.to_le_bytes()); // format_version = 1 + out.extend_from_slice(&0u32.to_le_bytes()); // format_id = 0 (raw DSD) + // DSF channel types: 1 mono, 2 stereo, 3 L R C, 4 quad, 6 five channels, 7 5.1. + let channel_type = match num_channels { + 1 => 1u32, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 6, + 6 => 7, + _ => unsupported_channel_layout()?, + }; + out.extend_from_slice(&channel_type.to_le_bytes()); + out.extend_from_slice(&(num_channels as u32).to_le_bytes()); + out.extend_from_slice(&sample_rate.to_le_bytes()); + out.extend_from_slice(&1u32.to_le_bytes()); // bits_per_sample = 1 + out.extend_from_slice(&sample_count.to_le_bytes()); + out.extend_from_slice(&(block_size as u32).to_le_bytes()); + out.extend_from_slice(&0u32.to_le_bytes()); // reserved + + // 3. 'data' chunk + out.extend_from_slice(b"data"); + out.extend_from_slice(&data_chunk_size.to_le_bytes()); + + for b in 0..num_blocks { + let blk_start = b * block_size; + let blk_end = blk_start + block_size; + for ch_data in channels { + let available = if blk_start < ch_data.len() { + let end = blk_end.min(ch_data.len()); + &ch_data[blk_start..end] + } else { + &[] + }; + out.extend_from_slice(available); + if available.len() < block_size { + out.resize(out.len() + (block_size - available.len()), super::filter::SILENCE_LSBF); + } + } + } + + Ok(out) +} diff --git a/crates/audio/src/dsd/filter.rs b/crates/audio/src/dsd/filter.rs new file mode 100644 index 0000000000..ec815afd1c --- /dev/null +++ b/crates/audio/src/dsd/filter.rs @@ -0,0 +1,288 @@ +//! DSD-to-PCM decimation filter. +//! +//! Direct Stream Digital (DSD) uses 1-bit Sigma-Delta modulation sampled at high rates +//! (2,822,400 Hz for DSD64, 5,644,800 Hz for DSD128, etc.). Decimation to multi-bit PCM +//! involves low-pass filtering to attenuate high-frequency quantization noise followed +//! by downsampling. +//! +//! This module provides a 96-tap symmetric FIR decimation filter that downsamples 1-bit DSD +//! to multi-bit PCM at 1/8th of the DSD sampling rate (e.g. 352,800 Hz for DSD64). Further +//! integer decimation (to 176.4 kHz, 88.2 kHz, or 44.1 kHz) is supported via subsequent +//! downsampling stages. + +use super::{BitOrder, Error, MAX_DECODE_BYTES}; + +/// 96-tap symmetric FIR decimation filter coefficients for decimation by 8. +/// +/// Output is normalized so that alternating silence bits (0x69 or 0xAA) decode near 0.0, +/// and full-scale runs of 1s or 0s decode near +1.0 and -1.0. +#[allow(clippy::excessive_precision)] +pub const TAPS_96: [f32; 96] = [ + 2.9802322388e-08_f32, + 1.1920928955e-07_f32, + 3.5762786865e-07_f32, + 7.4505805969e-07_f32, + 1.3113021851e-06_f32, + 1.9371509552e-06_f32, + 2.1755695343e-06_f32, + 1.2516975403e-06_f32, + -1.9967556000e-06_f32, + -9.1493129730e-06_f32, + -2.1725893021e-05_f32, + -4.0739774704e-05_f32, + -6.5773725510e-05_f32, + -9.3966722488e-05_f32, + -1.1900067329e-04_f32, + -1.3050436974e-04_f32, + -1.1405348778e-04_f32, + -5.2779912949e-05_f32, + 7.0035457611e-05_f32, + 2.6643276215e-04_f32, + 5.3817033768e-04_f32, + 8.7043642998e-04_f32, + 1.2266933918e-03_f32, + 1.5456080437e-03_f32, + 1.7420351505e-03_f32, + 1.7137229443e-03_f32, + 1.3538300991e-03_f32, + 5.7008862495e-04_f32, + -6.9221854210e-04_f32, + -2.4250447750e-03_f32, + -4.5351684093e-03_f32, + -6.8288743496e-03_f32, + -9.0079009533e-03_f32, + -1.0681360960e-02_f32, + -1.1394292116e-02_f32, + -1.0672420263e-02_f32, + -8.0802738667e-03_f32, + -3.2847225666e-03_f32, + 3.8830339909e-03_f32, + 1.3377487659e-02_f32, + 2.4909228086e-02_f32, + 3.7942945957e-02_f32, + 5.1726281643e-02_f32, + 6.5348774195e-02_f32, + 7.7825516462e-02_f32, + 8.8196486235e-02_f32, + 9.5628470182e-02_f32, + 9.9507331848e-02_f32, + 9.9507331848e-02_f32, + 9.5628470182e-02_f32, + 8.8196486235e-02_f32, + 7.7825516462e-02_f32, + 6.5348774195e-02_f32, + 5.1726281643e-02_f32, + 3.7942945957e-02_f32, + 2.4909228086e-02_f32, + 1.3377487659e-02_f32, + 3.8830339909e-03_f32, + -3.2847225666e-03_f32, + -8.0802738667e-03_f32, + -1.0672420263e-02_f32, + -1.1394292116e-02_f32, + -1.0681360960e-02_f32, + -9.0079009533e-03_f32, + -6.8288743496e-03_f32, + -4.5351684093e-03_f32, + -2.4250447750e-03_f32, + -6.9221854210e-04_f32, + 5.7008862495e-04_f32, + 1.3538300991e-03_f32, + 1.7137229443e-03_f32, + 1.7420351505e-03_f32, + 1.5456080437e-03_f32, + 1.2266933918e-03_f32, + 8.7043642998e-04_f32, + 5.3817033768e-04_f32, + 2.6643276215e-04_f32, + 7.0035457611e-05_f32, + -5.2779912949e-05_f32, + -1.1405348778e-04_f32, + -1.3050436974e-04_f32, + -1.1900067329e-04_f32, + -9.3966722488e-05_f32, + -6.5773725510e-05_f32, + -4.0739774704e-05_f32, + -2.1725893021e-05_f32, + -9.1493129730e-06_f32, + -1.9967556000e-06_f32, + 1.2516975403e-06_f32, + 2.1755695343e-06_f32, + 1.9371509552e-06_f32, + 1.3113021851e-06_f32, + 7.4505805969e-07_f32, + 3.5762786865e-07_f32, + 1.1920928955e-07_f32, + 2.9802322388e-08_f32, +]; + +/// The number of filter history bytes (96 taps / 8 bits per byte = 12 bytes). +pub const FILTER_BYTES: usize = 12; + +/// Standard DSD silence pattern byte for LSB-first ordering (Sony DSF). +pub const SILENCE_LSBF: u8 = 0x69; + +/// Standard DSD silence pattern byte for MSB-first ordering (Philips DSDIFF). +pub const SILENCE_MSBF: u8 = 0xAA; + +/// Precomputes lookup table entries for byte position `k` (0..12) and byte value `v` (0..256). +/// +/// In LSB-first order, bit `b` of input byte `k` corresponds to tap `k * 8 + b`. +#[inline] +fn byte_lut(k: usize, byte_val: u8) -> f32 { + let mut sum = 0.0_f32; + let base_tap = k * 8; + for b in 0..8 { + if ((byte_val >> b) & 1) != 0 + && let Some(tap) = TAPS_96.get(base_tap + b) + { + sum += 2.0 * *tap; + } + } + sum +} + +// Both the first-stage samples and the secondary output coexist during filtering. +pub(super) fn allocation_sizes(len: usize, factor: u32) -> Result<(usize, usize), Error> { + if !matches!(factor, 8 | 16 | 32 | 64) { + return Err(Error::Unsupported("decimation factor must be 8, 16, 32, or 64")); + } + let stage = len.checked_mul(std::mem::size_of::()).ok_or(Error::MemoryLimitExceeded)?; + let output = (len / (factor / 8) as usize).checked_mul(std::mem::size_of::()).ok_or(Error::MemoryLimitExceeded)?; + let working = if factor == 8 { stage } else { stage.checked_add(output).ok_or(Error::MemoryLimitExceeded)? }; + if working > MAX_DECODE_BYTES { + return Err(Error::MemoryLimitExceeded); + } + Ok((working, output)) +} + +/// Unity-DC-gain low-pass taps for decimating by `ratio` (cutoff 0.45 / ratio cycles per sample). +fn decimation_taps(ratio: usize) -> Vec { + let n = 24 * ratio + 1; + let mid = (n / 2) as f64; + let fc = 0.45 / ratio as f64; + let mut taps: Vec = (0..n) + .map(|k| { + let x = k as f64 - mid; + let sinc = if x == 0.0 { 2.0 * fc } else { (2.0 * std::f64::consts::PI * fc * x).sin() / (std::f64::consts::PI * x) }; + let t = k as f64 / (n - 1) as f64; + let window = 0.42 - 0.5 * (2.0 * std::f64::consts::PI * t).cos() + 0.08 * (4.0 * std::f64::consts::PI * t).cos(); + sinc * window + }) + .collect(); + let sum: f64 = taps.iter().sum(); + for t in &mut taps { + *t /= sum; + } + taps.into_iter().map(|t| t as f32).collect() +} + +/// Decimates 1-bit DSD audio bytes for a single channel to `f32` PCM. +/// +/// The default decimation factor is 8 (1 output float per input byte). +/// When `factor` is 16, 32, or 64, the 1/8 output is further downsampled +/// by 2, 4, or 8 respectively. +pub fn decimate_channel(channel_bytes: &[u8], bit_order: BitOrder, factor: u32) -> Result, Error> { + allocation_sizes(channel_bytes.len(), factor)?; + let num_bytes = channel_bytes.len(); + + // Build the 12 x 256 lookup table for fast convolution. + // 12 * 256 * 4 bytes = 12 KB, sits comfortably in L1 cache. + let mut lut = [[0.0_f32; 256]; FILTER_BYTES]; + for (k, lut_k) in lut.iter_mut().enumerate() { + for (v, cell) in lut_k.iter_mut().enumerate() { + *cell = byte_lut(k, v as u8); + } + } + + let mut stage1 = Vec::new(); + stage1.try_reserve_exact(num_bytes).map_err(|_| Error::MemoryLimitExceeded)?; + + // History buffer holds the most recent 12 bytes in LSB-first order. + // Pre-filled with silence (0x69). + let mut history = [SILENCE_LSBF; FILTER_BYTES]; + + for &raw_byte in channel_bytes { + let byte = match bit_order { + BitOrder::LsbFirst => raw_byte, + BitOrder::MsbFirst => raw_byte.reverse_bits(), + }; + + // Shift history left by 1 and push current byte to the end. + history.copy_within(1.., 0); + if let Some(last) = history.last_mut() { + *last = byte; + } + + let mut sample = -1.0_f32; + for (k, &hist_byte) in history.iter().enumerate() { + if let Some(lut_k) = lut.get(k) + && let Some(&contrib) = lut_k.get(hist_byte as usize) + { + sample += contrib; + } + } + + stage1.push(sample); + } + + if factor == 8 { + return Ok(stage1); + } + + // Secondary decimation by ratio = factor / 8 (2, 4, or 8). A plain average attenuates the band + // between the new Nyquist rate and the stage-1 rate too little (about -7.6 dB at 29.4 kHz for + // factor 64), so low-pass first: a Blackman-windowed sinc with its cutoff at 90% of the new + // Nyquist frequency, evaluated only at the kept samples. + let ratio = (factor / 8) as usize; + let out_len = stage1.len() / ratio; + let mut out = Vec::new(); + out.try_reserve_exact(out_len).map_err(|_| Error::MemoryLimitExceeded)?; + + let taps = decimation_taps(ratio); + let half = (taps.len() / 2) as isize; + let last = stage1.len() as isize - 1; + for i in 0..out_len { + let center = (i * ratio + ratio / 2) as isize; + let mut acc = 0.0_f32; + for (k, &w) in taps.iter().enumerate() { + let idx = (center + k as isize - half).clamp(0, last); + if let Some(&x) = stage1.get(idx as usize) { + acc += w * x; + } + } + out.push(acc); + } + + Ok(out) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod decimation_tests { + use super::*; + + /// A full-scale 29.4 kHz square wave in DSD64 (six bytes of ones, six of zeros) lies between + /// the output Nyquist frequency and the sampling rate of a 44.1 kHz result: it must be + /// attenuated well, not folded down at 0.42 of its amplitude. + #[test] + fn factor_64_attenuates_29_4_khz() { + let mut bytes = Vec::new(); + for _ in 0..2000 { + bytes.extend_from_slice(&[0xFF; 6]); + bytes.extend_from_slice(&[0x00; 6]); + } + let out = decimate_channel(&bytes, BitOrder::MsbFirst, 64).unwrap(); + let mid = &out[out.len() / 4..out.len() * 3 / 4]; + let peak = mid.iter().fold(0.0_f32, |m, v| m.max(v.abs())); + assert!(peak < 0.05, "peak {peak}"); + } + + #[test] + fn decimation_keeps_dc_level() { + let bytes = vec![0xFF; 4096]; + let out = decimate_channel(&bytes, BitOrder::MsbFirst, 64).unwrap(); + let mid = out[out.len() / 2]; + assert!((mid - 1.0).abs() < 0.05, "{mid}"); + } +} diff --git a/crates/audio/src/dsd/mod.rs b/crates/audio/src/dsd/mod.rs new file mode 100644 index 0000000000..e782b50f05 --- /dev/null +++ b/crates/audio/src/dsd/mod.rs @@ -0,0 +1,275 @@ +//! Direct Stream Digital (DSD) audio: Sony DSF and Philips DSDIFF (`.dff`) readers and +//! DSD-to-PCM decimation. +//! +//! DSD stores 1-bit Pulse Density Modulation (PDM) sampled at high rates (e.g. 2,822,400 Hz +//! for DSD64, 5,644,800 Hz for DSD128, etc.). +//! +//! This module provides: +//! - File parsers for Sony DSF (`.dsf`) and Philips DSDIFF (`.dff`) containers. +//! - Raw 1-bit audio extraction with preservation of native bit ordering (LSB-first for DSF, +//! MSB-first for DSDIFF). +//! - Clean-room 96-tap symmetric FIR decimation filter converting 1-bit PDM to multi-bit PCM +//! (decimation by 8 to 352,800 Hz for DSD64, or downsampled to 176.4, 88.2, or 44.1 kHz). +//! - Reference encoders for generating synthetic DSF and DSDIFF files. + +pub mod dsdiff; +pub mod dsf; +pub mod filter; + +use crate::AudioBuffer; + +/// Maximum number of audio channels supported (1 to 8). +pub const MAX_CHANNELS: usize = 8; + +/// Minimum allowable DSD sample rate in Hz. +pub const MIN_SAMPLE_RATE: u32 = 44_100; + +/// Maximum allowable DSD sample rate in Hz (e.g. DSD1024 at 45.1584 MHz). +pub const MAX_SAMPLE_RATE: u32 = 45_158_400; + +/// Maximum safe block size for DSF channel blocks (16 MiB). +pub const MAX_BLOCK_SIZE: u32 = 16 * 1024 * 1024; + +/// Maximum audio-buffer allocation budget (256 MiB). +/// +/// Raw readers limit their channel bytes; PCM conversion separately limits the +/// simultaneously live PCM output and scratch buffers. Caller-owned raw input, +/// metadata, and the small filter lookup table are not included. +pub const MAX_DECODE_BYTES: usize = 256 * 1024 * 1024; + +/// Maximum encoded container size, including headers and block padding (256 MiB). +pub const MAX_ENCODE_BYTES: usize = 256 * 1024 * 1024; + +fn encoded_size(payload: usize, header: usize) -> Result { + payload.checked_add(header).filter(|&n| n <= MAX_ENCODE_BYTES).ok_or(Error::MemoryLimitExceeded) +} + +// Account for retained channel output plus the current filter's two live buffers. +fn pcm_budget(lengths: impl Iterator, factor: u32) -> Result<(), Error> { + let mut retained = 0usize; + for len in lengths { + let (working, output) = filter::allocation_sizes(len, factor)?; + if retained.checked_add(working).is_none_or(|n| n > MAX_DECODE_BYTES) { + return Err(Error::MemoryLimitExceeded); + } + retained = retained.checked_add(output).ok_or(Error::MemoryLimitExceeded)?; + } + Ok(()) +} + +/// Supported DSD container formats. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Format { + /// Sony DSF (Direct Stream Digital File) format. + Dsf, + /// Philips DSDIFF (Direct Stream Digital Interchange File Format, `.dff`). + Dsdiff, +} + +/// Bit ordering of 1-bit DSD audio samples within bytes. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum BitOrder { + /// Least-significant bit first (LSB-first, as used in DSF). + /// Bit 0 is the earliest temporal sample; bit 7 is the latest. + LsbFirst, + /// Most-significant bit first (MSB-first, as used in DSDIFF). + /// Bit 7 is the earliest temporal sample; bit 0 is the latest. + MsbFirst, +} + +/// Stream properties and metadata parsed from a DSD container header. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Metadata { + /// Container format (DSF or DSDIFF). + pub format: Format, + /// DSD sampling frequency in Hz (e.g. 2,822,400 for DSD64). + pub sample_rate: u32, + /// Number of audio channels (1 to 8). + pub channels: u16, + /// Total number of 1-bit audio samples per channel. + pub sample_count: u64, + /// Channel identification labels (e.g. "SLFT", "SRGT", "Center"). + pub channel_names: Vec, + /// Optional track title if present in container metadata. + pub title: Option, + /// Optional artist name if present in container metadata. + pub artist: Option, +} + +/// Raw 1-bit DSD audio extracted from a file without decimation. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct RawAudio { + /// Stream metadata from the file header. + pub metadata: Metadata, + /// Bit ordering of the bytes in `channel_bytes`. + pub bit_order: BitOrder, + /// Planar 1-bit audio bytes per channel. + pub channel_bytes: Vec>, +} + +impl RawAudio { + /// Converts all channel byte streams to the requested bit ordering in place. + pub fn convert_bit_order(&mut self, target: BitOrder) { + if self.bit_order == target { + return; + } + for ch in &mut self.channel_bytes { + for b in ch.iter_mut() { + *b = b.reverse_bits(); + } + } + self.bit_order = target; + } + + /// Decimates the raw 1-bit DSD audio to multi-bit PCM `f32` samples. + /// + /// The default decimation factor is 8 (e.g. 352,800 Hz for DSD64). + pub fn to_pcm(&self) -> Result { + self.to_pcm_with_factor(8) + } + + /// Decimates the raw 1-bit DSD audio to multi-bit PCM `f32` samples with the given factor (8, 16, 32, or 64). + pub fn to_pcm_with_factor(&self, factor: u32) -> Result { + // Validate before iterating: public RawAudio may contain no channels. + if !matches!(factor, 8 | 16 | 32 | 64) { + return Err(Error::Unsupported("decimation factor must be 8, 16, 32, or 64")); + } + if self.channel_bytes.len() > MAX_CHANNELS { + return Err(Error::Unsupported("channel count must be between 1 and 8")); + } + pcm_budget(self.channel_bytes.iter().map(Vec::len), factor)?; + let mut channels = Vec::with_capacity(self.channel_bytes.len()); + for ch_bytes in &self.channel_bytes { + let pcm = filter::decimate_channel(ch_bytes, self.bit_order, factor)?; + channels.push(pcm); + } + let sample_rate = self.metadata.sample_rate / factor; + Ok(DecodedAudio { metadata: self.metadata.clone(), sample_rate, channels }) + } +} + +/// Decoded multi-bit PCM audio produced by DSD-to-PCM decimation. +#[derive(Debug, Clone, PartialEq)] +pub struct DecodedAudio { + /// Stream metadata from the DSD file header. + pub metadata: Metadata, + /// PCM sample rate in Hz (e.g. 352,800 Hz for DSD64 decimated by 8). + pub sample_rate: u32, + /// Planar normalized PCM samples in `[-1.0, 1.0]`, one `Vec` per channel. + pub channels: Vec>, +} + +impl DecodedAudio { + /// Converts the decoded PCM audio into an [`AudioBuffer`]. + pub fn to_audio_buffer(&self) -> AudioBuffer { + AudioBuffer { sample_rate: self.sample_rate, channels: self.channels.clone() } + } +} + +/// Errors returned when parsing or decoding DSD audio. +#[non_exhaustive] +#[derive(Debug, thiserror::Error, PartialEq, Eq)] +pub enum Error { + /// Input data ended prematurely while parsing a header or chunk. + #[error("unexpected end of file: {0}")] + UnexpectedEof(&'static str), + /// Invalid magic bytes or malformed chunk header. + #[error("invalid DSD header or chunk: {0}")] + InvalidHeader(&'static str), + /// An unsupported feature, compression, or channel configuration. + #[error("unsupported DSD feature: {0}")] + Unsupported(&'static str), + /// Audio data length or structure does not match the header specification. + #[error("invalid audio data: {0}")] + InvalidData(&'static str), + /// Header or data requested exceeds maximum safe allocation budget. + #[error("memory budget exceeded")] + MemoryLimitExceeded, +} + +/// Inspects the magic bytes of `bytes` and detects whether it is a DSF or DSDIFF file. +pub fn probe_format(bytes: &[u8]) -> Result { + if bytes.len() < 4 { + return Err(Error::UnexpectedEof("input is too short to determine DSD format")); + } + if &bytes[0..4] == b"DSD " { + Ok(Format::Dsf) + } else if bytes.len() >= 16 && &bytes[0..4] == b"FRM8" && &bytes[12..16] == b"DSD " { + Ok(Format::Dsdiff) + } else { + Err(Error::InvalidHeader("unknown or unsupported DSD container format")) + } +} + +/// Parses the metadata from a DSF or DSDIFF file without decoding audio samples. +pub fn read_metadata(bytes: &[u8]) -> Result { + match probe_format(bytes)? { + Format::Dsf => { + let (meta, _, _) = dsf::parse_dsf_header(bytes)?; + Ok(meta) + } + Format::Dsdiff => { + let (meta, _, _) = dsdiff::parse_dsdiff_header(bytes)?; + Ok(meta) + } + } +} + +/// Reads raw 1-bit planar audio streams from a DSF or DSDIFF file without decimation. +pub fn read_raw(bytes: &[u8]) -> Result { + match probe_format(bytes)? { + Format::Dsf => dsf::parse_dsf(bytes), + Format::Dsdiff => dsdiff::parse_dsdiff(bytes), + } +} + +/// Decodes a DSF or DSDIFF file to multi-bit planar `f32` PCM audio using decimation by 8. +/// +/// For DSD64 (2,822,400 Hz), the output sample rate is 352,800 Hz. +pub fn decode(bytes: &[u8]) -> Result { + decode_with_factor(bytes, 8) +} + +/// Decodes a DSF or DSDIFF file to multi-bit planar `f32` PCM audio with the specified decimation factor. +/// +/// Supported decimation factors are: +/// - 8: 1/8th of DSD rate (e.g. 352,800 Hz for DSD64). +/// - 16: 1/16th of DSD rate (e.g. 176,400 Hz for DSD64). +/// - 32: 1/32th of DSD rate (e.g. 88,200 Hz for DSD64). +/// - 64: 1/64th of DSD rate (e.g. 44,100 Hz for DSD64). +pub fn decode_with_factor(bytes: &[u8], factor: u32) -> Result { + let raw = read_raw(bytes)?; + raw.to_pcm_with_factor(factor) +} + +/// Encodes planar 1-bit audio channels into a Sony DSF file. +pub fn encode_dsf(sample_rate: u32, channels: &[&[u8]], sample_count: u64, block_size: u32) -> Result, Error> { + dsf::write_dsf(sample_rate, channels, sample_count, block_size) +} + +/// Encodes planar 1-bit audio channels into a Philips DSDIFF file. +pub fn encode_dsdiff(sample_rate: u32, channels: &[&[u8]], sample_count: u64) -> Result, Error> { + dsdiff::write_dsdiff(sample_rate, channels, sample_count) +} + +#[cfg(test)] +mod budget_tests { + use super::*; + + #[test] + fn pcm_budget_counts_all_channels_and_live_scratch() { + let quarter = MAX_DECODE_BYTES / 4; + assert!(pcm_budget([quarter / 2, quarter / 2].into_iter(), 8).is_ok()); + assert_eq!(pcm_budget([quarter / 2, quarter / 2 + 1].into_iter(), 8), Err(Error::MemoryLimitExceeded)); + assert!(pcm_budget([quarter / 2].into_iter(), 16).is_ok()); + assert_eq!(pcm_budget([quarter].into_iter(), 16), Err(Error::MemoryLimitExceeded)); + assert_eq!(pcm_budget([usize::MAX].into_iter(), 8), Err(Error::MemoryLimitExceeded)); + } + + #[test] + fn encoded_budget_includes_headers_without_large_allocations() { + assert_eq!(encoded_size(MAX_ENCODE_BYTES - 92, 92), Ok(MAX_ENCODE_BYTES)); + assert_eq!(encoded_size(MAX_ENCODE_BYTES - 91, 92), Err(Error::MemoryLimitExceeded)); + assert_eq!(encoded_size(usize::MAX, 92), Err(Error::MemoryLimitExceeded)); + } +} diff --git a/crates/audio/src/g722.rs b/crates/audio/src/g722.rs new file mode 100644 index 0000000000..030b87d14f --- /dev/null +++ b/crates/audio/src/g722.rs @@ -0,0 +1,693 @@ +//! ITU-T Recommendation G.722 wideband audio coding (64 kbit/s). +//! +//! Pure safe Rust clean-room implementation of the G.722 sub-band adaptive differential +//! pulse code modulation (SB-ADPCM) speech and audio codec. +//! +//! Input audio is 16 kHz 16-bit linear PCM. An analysis Quadrature Mirror Filter (QMF) splits +//! the 16 kHz audio into a lower sub-band (0–4 kHz) and a higher sub-band (4–8 kHz), each +//! sampled at 8 kHz. The lower sub-band is encoded at 48 kbit/s (6 bits/sample) and the higher +//! sub-band at 16 kbit/s (2 bits/sample), multiplexed into an 8-bit octet per 8 kHz cycle +//! (64 kbit/s total stream rate). +//! +//! On decoding, inverse adaptive quantizers reconstruct each sub-band signal, and a synthesis +//! QMF interpolates and combines them back to 16 kHz 16-bit linear PCM. + +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +/// Maximum allowed channels for multi-channel processing. +pub const MAX_CHANNELS: usize = 8; + +/// Maximum input byte buffer bound to prevent memory exhaustion on hostile inputs (64 MiB). +pub const MAX_INPUT_BYTES: usize = 64 * 1024 * 1024; + +/// Maximum input sample count bound (128 million samples). +pub const MAX_SAMPLES: usize = 128 * 1024 * 1024; + +/// Error conditions encountered during G.722 encoding or decoding. +#[derive(Debug, thiserror::Error, PartialEq, Eq)] +pub enum Error { + /// The channel count is invalid. + #[error("invalid channel count: {0} (must be 1..={MAX_CHANNELS})")] + InvalidChannels(usize), + + /// The input size exceeds safety bounds. + #[error("input exceeds maximum size bound")] + InputTooLarge, + + /// Encoding requires an even number of samples per channel (16 kHz sample pairs). + #[error("odd number of samples: {0} (G.722 encodes pairs of 16 kHz samples)")] + OddSampleCount(usize), + + /// System allocation failed when reserving buffer memory. + #[error("allocation failed")] + AllocationFailed, +} + +// --------------------------------------------------------------------------- +// Specification Tables from ITU-T Recommendation G.722 +// --------------------------------------------------------------------------- + +/// 24-tap QMF filter coefficients scaled by 2^13 (Table 11/G.722). +const H: [i16; 24] = [3, -11, -11, 53, 12, -156, 32, 362, -210, -805, 951, 3876, 3876, 951, -805, -210, 362, 32, -156, 12, 53, -11, -11, 3]; + +/// Lower sub-band 60-level inverse quantizer reconstruction table QQ6 (Table 14/G.722). +const QQ6: [i16; 31] = [ + 0, 17, 54, 91, 130, 170, 211, 254, 300, 347, 396, 447, 501, 558, 618, 682, 750, 822, 899, 982, 1072, 1170, 1279, 1399, 1535, 1689, 1873, 2088, + 2376, 2738, 3101, +]; + +/// Lower sub-band quantizer decision levels Q6 (Table 14/G.722). +const Q6: [i16; 30] = [ + 0, 35, 72, 110, 150, 190, 233, 276, 323, 370, 422, 473, 530, 587, 650, 714, 786, 858, 940, 1023, 1121, 1219, 1339, 1458, 1612, 1765, 1980, 2195, + 2557, 2919, +]; + +/// Lower sub-band 15-level inverse quantizer reconstruction table QQ4 (Table 14/G.722). +const QQ4: [i16; 8] = [0, 150, 323, 530, 786, 1121, 1612, 2557]; + +/// Lower sub-band logarithmic scale factor multipliers WL (Table 14/G.722). +const WL: [i16; 8] = [-60, -30, 58, 172, 334, 538, 1198, 3042]; + +/// Higher sub-band 4-level inverse quantizer reconstruction table QQ2 (Table 14/G.722). +const QQ2: [i16; 3] = [0, 202, 926]; + +/// Higher sub-band logarithmic scale factor multipliers WH (Table 14/G.722). +const WH: [i16; 3] = [0, -214, 798]; + +/// 32-entry inverse logarithmic scale table ILB (Table 15/G.722). +const ILB: [i32; 32] = [ + 2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383, 2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834, 2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371, + 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008, +]; + +/// Table 17/G.722: 4-bit codeword (RIL = IL >> 2) to (SIL, IL4). +const IL4_MAP: [(i16, usize); 16] = + [(0, 0), (-1, 7), (-1, 6), (-1, 5), (-1, 4), (-1, 3), (-1, 2), (-1, 1), (0, 7), (0, 6), (0, 5), (0, 4), (0, 3), (0, 2), (0, 1), (0, 0)]; + +/// Table 21/G.722: 2-bit codeword IH to (SIH, IH2). +const IH2_MAP: [(i16, usize); 4] = [(-1, 2), (-1, 1), (0, 2), (0, 1)]; + +#[inline] +fn mult(a: i16, b: i16) -> i16 { + (((a as i32) * (b as i32)) >> 15) as i16 +} + +#[inline] +fn scalel(nbpl: i32) -> i32 { + let wd1 = ((nbpl >> 6) & 31) as usize; + let wd2 = nbpl >> 11; + let shift = 8 - wd2; + let wd3 = if shift >= 0 { ILB[wd1] >> shift } else { ILB[wd1] << (-shift) }; + wd3 << 2 +} + +#[inline] +fn scaleh(nbph: i32) -> i32 { + let wd1 = ((nbph >> 6) & 31) as usize; + let wd2 = nbph >> 11; + let shift = 10 - wd2; + let wd3 = if shift >= 0 { ILB[wd1] >> shift } else { ILB[wd1] << (-shift) }; + wd3 << 2 +} + +// --------------------------------------------------------------------------- +// Sub-band ADPCM State (Pole/Zero Predictors & Adaptations) +// --------------------------------------------------------------------------- + +#[derive(Debug, Clone)] +struct BandState { + s: i16, + sp: i16, + sz: i16, + d: [i16; 7], + r: [i16; 3], + p: [i16; 3], + a: [i16; 3], + b: [i16; 7], + nb: i32, + det: i32, +} + +impl BandState { + fn new(is_lower: bool) -> Self { + Self { s: 0, sp: 0, sz: 0, d: [0; 7], r: [0; 3], p: [0; 3], a: [0; 3], b: [0; 7], nb: 0, det: if is_lower { 32 } else { 8 } } + } + + fn reset(&mut self, is_lower: bool) { + self.s = 0; + self.sp = 0; + self.sz = 0; + self.d = [0; 7]; + self.r = [0; 3]; + self.p = [0; 3]; + self.a = [0; 3]; + self.b = [0; 7]; + self.nb = 0; + self.det = if is_lower { 32 } else { 8 }; + } + + fn update_predictor(&mut self, d: i16, p_sig: i16, r_sig: i16) { + // UPZERO: update 6th order zero predictor coefficients + let wd1: i16 = if d != 0 { 128 } else { 0 }; + let sg0: i16 = if d < 0 { -1 } else { 0 }; + for i in 1..=6 { + let sgi = if self.d[i] < 0 { -1 } else { 0 }; + let wd2 = if sg0 == sgi { wd1 } else { wd1.saturating_neg() }; + let wd3 = mult(self.b[i], 32640); + self.b[i] = wd2.saturating_add(wd3); + } + + // UPPOL2: update 2nd pole coefficient + let sg0 = if p_sig < 0 { -1 } else { 0 }; + let sg1 = if self.p[1] < 0 { -1 } else { 0 }; + let sg2 = if self.p[2] < 0 { -1 } else { 0 }; + let wd1 = self.a[1].saturating_add(self.a[1]).saturating_add(self.a[1].saturating_add(self.a[1])); + let wd2 = if sg0 == sg1 { wd1.saturating_neg() } else { wd1 } >> 7; + let wd3: i16 = if sg0 == sg2 { 128 } else { -128 }; + let wd4 = wd2.saturating_add(wd3); + let wd5 = mult(self.a[2], 32512); + let apl2 = wd4.saturating_add(wd5).clamp(-12288, 12288); + + // UPPOL1: update 1st pole coefficient + let wd1: i16 = if sg0 == sg1 { 192 } else { -192 }; + let wd2 = mult(self.a[1], 32640); + let apl1 = wd1.saturating_add(wd2); + let wd3 = 15360 - apl2; + let apl1 = apl1.clamp(-wd3, wd3); + + self.a[1] = apl1; + self.a[2] = apl2; + + // Shift history delay lines + for i in (2..=6).rev() { + self.d[i] = self.d[i - 1]; + } + self.d[1] = d; + + self.p[2] = self.p[1]; + self.p[1] = p_sig; + + self.r[2] = self.r[1]; + self.r[1] = r_sig; + + // FILTEZ: compute zero section output + let mut sz: i16 = 0; + for i in (1..=6).rev() { + let part = mult(self.b[i], self.d[i].saturating_add(self.d[i])); + sz = sz.saturating_add(part); + } + self.sz = sz; + + // FILTEP: compute pole section output + let sp1 = mult(self.a[1], self.r[1].saturating_add(self.r[1])); + let sp2 = mult(self.a[2], self.r[2].saturating_add(self.r[2])); + self.sp = sp1.saturating_add(sp2); + + // PREDIC: compute full predictor output value + self.s = self.sp.saturating_add(self.sz); + } +} + +// --------------------------------------------------------------------------- +// Quadrature Mirror Filters (QMF) +// --------------------------------------------------------------------------- + +/// Analysis QMF (transmit filter bank): splits pairs of 16 kHz samples into lower and higher sub-bands. +#[derive(Debug, Clone, Default)] +pub struct QmfTx { + x: [i16; 24], +} + +impl QmfTx { + /// Create a new analysis QMF filter. + pub fn new() -> Self { + Self { x: [0; 24] } + } + + /// Reset filter history. + pub fn reset(&mut self) { + self.x = [0; 24]; + } + + /// Process a pair of 16 kHz input samples `(s0, s1)` into `(xl, xh)`. + pub fn filter(&mut self, s0: i16, s1: i16) -> (i16, i16) { + for i in (2..24).rev() { + self.x[i] = self.x[i - 2]; + } + self.x[0] = s1 >> 1; + self.x[1] = s0 >> 1; + + let mut xa: i32 = 0; + let mut xb: i32 = 0; + for i in 0..12 { + xa = xa.saturating_add((self.x[2 * i] as i32) * (H[2 * i] as i32)); + xb = xb.saturating_add((self.x[2 * i + 1] as i32) * (H[2 * i + 1] as i32)); + } + + let xl = ((xa.saturating_add(xb)) >> 13).clamp(-16384, 16383) as i16; + let xh = ((xa.saturating_sub(xb)) >> 13).clamp(-16384, 16383) as i16; + (xl, xh) + } +} + +/// Synthesis QMF (receive filter bank): synthesizes lower and higher sub-band signals into 16 kHz output. +#[derive(Debug, Clone, Default)] +pub struct QmfRx { + xd: [i16; 12], + xs: [i16; 12], +} + +impl QmfRx { + /// Create a new synthesis QMF filter. + pub fn new() -> Self { + Self { xd: [0; 12], xs: [0; 12] } + } + + /// Reset filter history. + pub fn reset(&mut self) { + self.xd = [0; 12]; + self.xs = [0; 12]; + } + + /// Reconstruct a pair of 16 kHz output samples from sub-band samples `(rl, rh)`. + pub fn filter(&mut self, rl: i16, rh: i16) -> (i16, i16) { + let xd_curr = rl.saturating_sub(rh); + let xs_curr = rl.saturating_add(rh); + + for i in (1..12).rev() { + self.xd[i] = self.xd[i - 1]; + self.xs[i] = self.xs[i - 1]; + } + self.xd[0] = xd_curr; + self.xs[0] = xs_curr; + + let mut wd_c: i32 = 0; + let mut wd_d: i32 = 0; + for i in 0..12 { + wd_c = wd_c.saturating_add((self.xd[i] as i32) * (H[2 * i] as i32)); + wd_d = wd_d.saturating_add((self.xs[i] as i32) * (H[2 * i + 1] as i32)); + } + + let xout1 = (wd_c >> 12).clamp(-16384, 16383); + let xout2 = (wd_d >> 12).clamp(-16384, 16383); + + let s0 = (xout1 << 1).clamp(-32768, 32767) as i16; + let s1 = (xout2 << 1).clamp(-32768, 32767) as i16; + (s0, s1) + } +} + +// --------------------------------------------------------------------------- +// G.722 Decoder & Encoder +// --------------------------------------------------------------------------- + +/// ITU-T G.722 64 kbit/s single-channel decoder. +#[derive(Debug, Clone)] +pub struct Decoder { + band_l: BandState, + band_h: BandState, + qmf: QmfRx, +} + +impl Default for Decoder { + fn default() -> Self { + Self::new() + } +} + +impl Decoder { + /// Create a new G.722 decoder initialized to standard reset state. + pub fn new() -> Self { + Self { band_l: BandState::new(true), band_h: BandState::new(false), qmf: QmfRx::new() } + } + + /// Reset decoder internal state. + pub fn reset(&mut self) { + self.band_l.reset(true); + self.band_h.reset(false); + self.qmf.reset(); + } + + /// Decode a single 8-bit octet into two 16 kHz 16-bit PCM samples. + pub fn decode_octet(&mut self, octet: u8) -> (i16, i16) { + let ih = ((octet >> 6) & 3) as usize; + let il = (octet & 0x3F) as usize; + + // INVQBL: 6-bit lower sub-band inverse quantization + let (sil6, il6) = table18_lookup(il); + let wd1 = (QQ6[il6] as i32) << 3; + let wd2 = if sil6 == 0 { wd1 } else { -wd1 }; + let dl = mult(self.band_l.det as i16, wd2 as i16); + let yl = self.band_l.s.saturating_add(dl); + let rl = yl.clamp(-16384, 16383); + + // INVQAL: lower sub-band predictor update (4-bit feedback path) + let ril = il >> 2; + let (sil4, il4) = IL4_MAP[ril & 0x0F]; + let wd1 = (QQ4[il4] as i32) << 3; + let wd2 = if sil4 == 0 { wd1 } else { -wd1 }; + let dlt = mult(self.band_l.det as i16, wd2 as i16); + let plt = dlt.saturating_add(self.band_l.sz); + let rlt = self.band_l.s.saturating_add(dlt); + + // Lower sub-band scale factor adaptation (LOGSCL) + let wd_scale = mult(self.band_l.nb as i16, 32512); + let nbpl = (wd_scale as i32 + WL[il4] as i32).clamp(0, 18432); + self.band_l.nb = nbpl; + self.band_l.det = scalel(nbpl); + self.band_l.update_predictor(dlt, plt, rlt); + + // INVQAH: higher sub-band inverse quantization + let (sih_dec, ih2) = IH2_MAP[ih & 0x03]; + let wd1 = (QQ2[ih2] as i32) << 3; + let wd2 = if sih_dec == 0 { wd1 } else { -wd1 }; + let dh = mult(self.band_h.det as i16, wd2 as i16); + let yh = self.band_h.s.saturating_add(dh); + let rh = yh.clamp(-16384, 16383); + + // Higher sub-band scale factor adaptation (LOGSCH) + let wd_scale = mult(self.band_h.nb as i16, 32512); + let nbph = (wd_scale as i32 + WH[ih2] as i32).clamp(0, 22528); + self.band_h.nb = nbph; + self.band_h.det = scaleh(nbph); + let ph = dh.saturating_add(self.band_h.sz); + self.band_h.update_predictor(dh, ph, yh); + + // Synthesis QMF + self.qmf.filter(rl, rh) + } + + /// Decode G.722 octets into 16 kHz 16-bit linear PCM samples, appending to `dst`. + pub fn decode(&mut self, src: &[u8], dst: &mut Vec) -> Result<(), Error> { + if src.len() > MAX_INPUT_BYTES { + return Err(Error::InputTooLarge); + } + let needed_samples = src.len().saturating_mul(2); + if needed_samples > MAX_SAMPLES { + return Err(Error::InputTooLarge); + } + dst.try_reserve(needed_samples).map_err(|_| Error::AllocationFailed)?; + for &octet in src { + let (s0, s1) = self.decode_octet(octet); + dst.push(s0); + dst.push(s1); + } + Ok(()) + } +} + +/// ITU-T G.722 64 kbit/s single-channel encoder. +#[derive(Debug, Clone)] +pub struct Encoder { + qmf: QmfTx, + band_l: BandState, + band_h: BandState, +} + +impl Default for Encoder { + fn default() -> Self { + Self::new() + } +} + +impl Encoder { + /// Create a new G.722 encoder initialized to standard reset state. + pub fn new() -> Self { + Self { qmf: QmfTx::new(), band_l: BandState::new(true), band_h: BandState::new(false) } + } + + /// Reset encoder internal state. + pub fn reset(&mut self) { + self.qmf.reset(); + self.band_l.reset(true); + self.band_h.reset(false); + } + + /// Encode a pair of 16 kHz 16-bit PCM samples into one 8-bit G.722 octet. + pub fn encode_sample_pair(&mut self, s0: i16, s1: i16) -> u8 { + // Analysis QMF + let (xl, xh) = self.qmf.filter(s0, s1); + + // Lower sub-band quantizer (QUANTL) + let el = xl.saturating_sub(self.band_l.s); + let sil = if el < 0 { -1 } else { 0 }; + let wd = if sil == 0 { el } else { 32767 - (el & 32767) }; + let mut mil: usize = 30; + for (k, &thresh_q) in Q6.iter().enumerate().skip(1) { + let thresh = (((thresh_q as i32) << 3) * self.band_l.det) >> 15; + if (wd as i32) < thresh { + mil = k; + break; + } + } + + // Table 16 conversion to IL + let il: u8 = if sil == 0 { + (62 - mil) as u8 + } else if mil == 1 { + 63 + } else if mil == 2 { + 62 + } else { + (34 - mil) as u8 + }; + + // Lower sub-band feedback and predictor update (INVQAL) + let ril = (il >> 2) as usize; + let (sil4, il4) = IL4_MAP[ril & 0x0F]; + let wd1 = (QQ4[il4] as i32) << 3; + let wd2 = if sil4 == 0 { wd1 } else { -wd1 }; + let dlt = mult(self.band_l.det as i16, wd2 as i16); + let plt = dlt.saturating_add(self.band_l.sz); + let rlt = self.band_l.s.saturating_add(dlt); + + // Lower sub-band scale factor adaptation (LOGSCL) + let wd_scale = mult(self.band_l.nb as i16, 32512); + let nbpl = (wd_scale as i32 + WL[il4] as i32).clamp(0, 18432); + self.band_l.nb = nbpl; + self.band_l.det = scalel(nbpl); + self.band_l.update_predictor(dlt, plt, rlt); + + // Higher sub-band quantizer (QUANTH) + let eh = xh.saturating_sub(self.band_h.s); + let sih = if eh < 0 { -1 } else { 0 }; + let wd = if sih == 0 { eh } else { 32767 - (eh & 32767) }; + let thresh = ((564 << 3) * self.band_h.det) >> 15; + let mih = if (wd as i32) < thresh { 1 } else { 2 }; + + // Table 20 conversion to IH + let ih: u8 = if sih == -1 { + if mih == 2 { 0 } else { 1 } + } else if mih == 1 { + 3 + } else { + 2 + }; + + // Higher sub-band feedback and predictor update (INVQAH) + let (sih_dec, ih2) = IH2_MAP[ih as usize]; + let wd1 = (QQ2[ih2] as i32) << 3; + let wd2 = if sih_dec == 0 { wd1 } else { -wd1 }; + let dh = mult(self.band_h.det as i16, wd2 as i16); + let yh = self.band_h.s.saturating_add(dh); + + // Higher sub-band scale factor adaptation (LOGSCH) + let wd_scale = mult(self.band_h.nb as i16, 32512); + let nbph = (wd_scale as i32 + WH[ih2] as i32).clamp(0, 22528); + self.band_h.nb = nbph; + self.band_h.det = scaleh(nbph); + let ph = dh.saturating_add(self.band_h.sz); + self.band_h.update_predictor(dh, ph, yh); + + // Multiplexer (MUX) + (ih << 6) | (il & 0x3F) + } + + /// Encode 16 kHz 16-bit linear PCM samples into G.722 octets, appending to `dst`. + pub fn encode(&mut self, src: &[i16], dst: &mut Vec) -> Result<(), Error> { + if !src.len().is_multiple_of(2) { + return Err(Error::OddSampleCount(src.len())); + } + if src.len() > MAX_SAMPLES { + return Err(Error::InputTooLarge); + } + let needed_octets = src.len() / 2; + dst.try_reserve(needed_octets).map_err(|_| Error::AllocationFailed)?; + for &[s0, s1] in src.as_chunks::<2>().0 { + let octet = self.encode_sample_pair(s0, s1); + dst.push(octet); + } + Ok(()) + } +} + +// --------------------------------------------------------------------------- +// Helpers +// --------------------------------------------------------------------------- + +#[inline] +fn table18_lookup(il: usize) -> (i16, usize) { + let il = il & 0x3F; + if (4..=31).contains(&il) { + (-1, 34 - il) + } else if il == 62 { + (-1, 2) + } else if il == 63 { + (-1, 1) + } else if (32..=61).contains(&il) { + (0, 62 - il) + } else { + // Transmission error fallbacks (0..=3) + (-1, 1) + } +} + +// --------------------------------------------------------------------------- +// Public Top-Level Functions +// --------------------------------------------------------------------------- + +/// Decode a mono 64 kbit/s G.722 stream into 16 kHz 16-bit linear PCM samples. +pub fn decode(bytes: &[u8]) -> Result, Error> { + let mut decoder = Decoder::new(); + let mut out = Vec::new(); + decoder.decode(bytes, &mut out)?; + Ok(out) +} + +/// Encode mono 16 kHz 16-bit linear PCM samples into a 64 kbit/s G.722 stream. +pub fn encode(samples: &[i16]) -> Result, Error> { + let mut encoder = Encoder::new(); + let mut out = Vec::new(); + encoder.encode(samples, &mut out)?; + Ok(out) +} + +/// Decode an interleaved multi-channel G.722 stream into interleaved 16 kHz 16-bit PCM samples. +pub fn decode_interleaved(bytes: &[u8], channels: usize) -> Result, Error> { + if channels == 0 || channels > MAX_CHANNELS { + return Err(Error::InvalidChannels(channels)); + } + if channels == 1 { + return decode(bytes); + } + if bytes.len() > MAX_INPUT_BYTES { + return Err(Error::InputTooLarge); + } + let frames = bytes.len() / channels; + let mut decoders: Vec = Vec::with_capacity(channels); + for _ in 0..channels { + decoders.push(Decoder::new()); + } + let total_samples = frames.saturating_mul(channels).saturating_mul(2); + let mut out = Vec::new(); + out.try_reserve(total_samples).map_err(|_| Error::AllocationFailed)?; + + // Each frame has `channels` bytes. Decode each byte with its channel's decoder, + // producing sample 0 for each channel, then sample 1 for each channel. + for chunk in bytes.chunks_exact(channels) { + let mut pairs = [(0i16, 0i16); MAX_CHANNELS]; + for (ch, &b) in chunk.iter().enumerate() { + pairs[ch] = decoders[ch].decode_octet(b); + } + for pair in pairs.iter().take(channels) { + out.push(pair.0); + } + for pair in pairs.iter().take(channels) { + out.push(pair.1); + } + } + Ok(out) +} + +/// Encode interleaved 16 kHz 16-bit PCM samples into an interleaved multi-channel G.722 stream. +pub fn encode_interleaved(samples: &[i16], channels: usize) -> Result, Error> { + if channels == 0 || channels > MAX_CHANNELS { + return Err(Error::InvalidChannels(channels)); + } + if channels == 1 { + return encode(samples); + } + if samples.len() > MAX_SAMPLES { + return Err(Error::InputTooLarge); + } + let frame_len = channels.saturating_mul(2); + if !samples.len().is_multiple_of(frame_len) { + return Err(Error::OddSampleCount(samples.len())); + } + let frames = samples.len() / frame_len; + let mut encoders: Vec = Vec::with_capacity(channels); + for _ in 0..channels { + encoders.push(Encoder::new()); + } + let total_bytes = frames.saturating_mul(channels); + let mut out = Vec::new(); + out.try_reserve(total_bytes).map_err(|_| Error::AllocationFailed)?; + + for chunk in samples.chunks_exact(frame_len) { + for ch in 0..channels { + let s0 = chunk[ch]; + let s1 = chunk[ch + channels]; + out.push(encoders[ch].encode_sample_pair(s0, s1)); + } + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reject_invalid_channels() { + assert_eq!(decode_interleaved(&[0; 4], 0), Err(Error::InvalidChannels(0))); + assert_eq!(decode_interleaved(&[0; 4], 9), Err(Error::InvalidChannels(9))); + assert_eq!(encode_interleaved(&[0; 4], 0), Err(Error::InvalidChannels(0))); + assert_eq!(encode_interleaved(&[0; 4], 9), Err(Error::InvalidChannels(9))); + } + + #[test] + fn reject_odd_sample_count() { + assert_eq!(encode(&[0; 3]), Err(Error::OddSampleCount(3))); + assert_eq!(encode_interleaved(&[0; 5], 2), Err(Error::OddSampleCount(5))); + } + + #[test] + fn roundtrip_silence() { + let input = vec![0i16; 320]; + let encoded = encode(&input).expect("encode silence"); + assert_eq!(encoded.len(), 160); + let decoded = decode(&encoded).expect("decode silence"); + assert_eq!(decoded.len(), 320); + for &s in &decoded[40..] { + assert!(s.abs() <= 6, "silence sample too large: {s}"); + } + } + + #[test] + fn multichannel_stereo_roundtrip() { + let mut stereo_pcm = Vec::new(); + for i in 0..400 { + stereo_pcm.push((i * 10) as i16); + stereo_pcm.push((-i * 10) as i16); + } + let enc = encode_interleaved(&stereo_pcm, 2).expect("encode stereo"); + assert_eq!(enc.len(), 400); + let dec = decode_interleaved(&enc, 2).expect("decode stereo"); + assert_eq!(dec.len(), 800); + } + + #[test] + fn hostile_input_does_not_panic() { + let garbage = [0xFF, 0x00, 0x55, 0xAA, 0x12, 0x34, 0x7F, 0x80]; + let dec = decode(&garbage).expect("decode garbage"); + assert_eq!(dec.len(), 16); + } + + #[test] + fn uppol2_overflow_does_not_panic() { + let mut state = BandState::new(true); + state.a[1] = -8192; + state.p = [0, 0, 0]; + state.update_predictor(0, 0, 0); + } +} diff --git a/crates/audio/src/g726.rs b/crates/audio/src/g726.rs new file mode 100644 index 0000000000..6ac02359df --- /dev/null +++ b/crates/audio/src/g726.rs @@ -0,0 +1,996 @@ +//! ITU-T Recommendation G.726 ADPCM speech codec (16, 24, 32, 40 kbit/s). +//! +//! Clean-room implementation based on ITU-T Recommendation G.726 (12/1990) +//! "40, 32, 24, 16 kbit/s Adaptive Differential Pulse Code Modulation (ADPCM)". +//! +//! Provides encoding from 8 kHz 16-bit linear PCM to G.726 bitstreams, and decoding +//! from G.726 bitstreams to 8 kHz 16-bit linear PCM. +//! +//! Supports both standard big-endian bit packing (network order / RFC 3551) and +//! little-endian bit packing (RIFF WAV / AVID format tag 0x0045 / 0x0014). + +const MAX_CHANNELS: usize = 8; +const MAX_INPUT_BYTES: usize = 64 * 1024 * 1024; +const MAX_SAMPLES: usize = 64 * 1024 * 1024; + +/// Bit rate for G.726 ADPCM. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BitRate { + /// 16 kbit/s (2 bits per sample). + Rate16k, + /// 24 kbit/s (3 bits per sample). + Rate24k, + /// 32 kbit/s (4 bits per sample). + Rate32k, + /// 40 kbit/s (5 bits per sample). + Rate40k, +} + +impl BitRate { + /// Number of bits per code (sample). + #[inline] + #[must_use] + pub const fn code_size(self) -> usize { + match self { + Self::Rate16k => 2, + Self::Rate24k => 3, + Self::Rate32k => 4, + Self::Rate40k => 5, + } + } + + /// Bit rate in bits per second. + #[inline] + #[must_use] + pub const fn bit_rate_bps(self) -> u32 { + match self { + Self::Rate16k => 16_000, + Self::Rate24k => 24_000, + Self::Rate32k => 32_000, + Self::Rate40k => 40_000, + } + } +} + +/// Bit packing order within bytes. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum BitPacking { + /// Big-endian packing: MSB to LSB (standard RFC 3551 / network order). + #[default] + BigEndian, + /// Little-endian packing: LSB to MSB (WAV / RIFF order). + LittleEndian, +} + +/// Errors returned by G.726 operations. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// Channel count must be between 1 and 8. + #[error("unsupported channel count: {0} (must be 1..=8)")] + InvalidChannels(usize), + /// Input exceeds safe maximum memory bound. + #[error("input exceeds safe maximum processing limit")] + InputTooLarge, + /// Output buffer allocation failed. + #[error("memory allocation failed")] + AllocationFailed, +} + +// --------------------------------------------------------------------------- +// G.726 Tables +// --------------------------------------------------------------------------- + +// Table 11..14 (I -> DQS, DQLN) +static OUT_16: [(u8, u16); 4] = [(0, 116), (0, 365), (1, 365), (1, 116)]; + +static OUT_24: [(u8, u16); 8] = [(0, 2048), (0, 135), (0, 273), (0, 373), (1, 373), (1, 273), (1, 135), (1, 2048)]; + +static OUT_32: [(u8, u16); 16] = [ + (0, 2048), + (0, 4), + (0, 135), + (0, 213), + (0, 273), + (0, 323), + (0, 373), + (0, 425), + (1, 425), + (1, 373), + (1, 323), + (1, 273), + (1, 213), + (1, 135), + (1, 4), + (1, 2048), +]; + +static OUT_40: [(u8, u16); 32] = [ + (0, 2048), + (0, 4030), + (0, 28), + (0, 104), + (0, 169), + (0, 224), + (0, 274), + (0, 318), + (0, 358), + (0, 395), + (0, 429), + (0, 459), + (0, 488), + (0, 514), + (0, 539), + (0, 566), + (1, 566), + (1, 539), + (1, 514), + (1, 488), + (1, 459), + (1, 429), + (1, 395), + (1, 358), + (1, 318), + (1, 274), + (1, 224), + (1, 169), + (1, 104), + (1, 28), + (1, 4030), + (1, 2048), +]; + +// FUNCTW: scale factor multipliers WI +static WI_16: [u16; 2] = [4074, 439]; +static WI_24: [u16; 4] = [4092, 30, 137, 582]; +static WI_32: [u16; 8] = [4084, 18, 41, 64, 112, 198, 355, 1122]; +static WI_40: [u16; 16] = [14, 14, 24, 39, 40, 41, 58, 100, 141, 179, 219, 280, 358, 440, 529, 696]; + +#[inline] +fn get_wi(rate: BitRate, code: u8) -> u16 { + match rate { + BitRate::Rate16k => { + let im = if (code >> 1) == 0 { (code & 1) as usize } else { ((3 - code) & 1) as usize }; + WI_16[im] + } + BitRate::Rate24k => { + let im = if (code >> 2) == 0 { (code & 3) as usize } else { ((7 - code) & 3) as usize }; + WI_24[im] + } + BitRate::Rate32k => { + let im = if (code >> 3) == 0 { (code & 7) as usize } else { ((15 - code) & 7) as usize }; + WI_32[im] + } + BitRate::Rate40k => { + let im = if (code >> 4) == 0 { (code & 15) as usize } else { ((31 - code) & 15) as usize }; + WI_40[im] + } + } +} + +#[inline] +fn get_fi(rate: BitRate, code: u8) -> u8 { + match rate { + BitRate::Rate16k => { + let im = if (code >> 1) == 0 { code & 1 } else { (3 - code) & 1 }; + if im == 1 { 7 } else { 0 } + } + BitRate::Rate24k => { + let im = if (code >> 2) == 0 { code & 3 } else { (7 - code) & 3 }; + match im { + 0 => 0, + 1 => 1, + 2 => 2, + _ => 7, + } + } + BitRate::Rate32k => { + let im = if (code >> 3) == 0 { code & 7 } else { (15 - code) & 7 }; + if im <= 2 { + 0 + } else if im <= 5 { + 1 + } else if im == 6 { + 3 + } else { + 7 + } + } + BitRate::Rate40k => { + let im = if (code >> 4) == 0 { code & 15 } else { (31 - code) & 15 }; + if im <= 4 { + 0 + } else if im <= 9 { + 1 + } else { + match im { + 10 => 2, + 11 => 3, + 12 => 4, + 13 => 5, + _ => 6, + } + } + } + } +} + +// Quantizer decision intervals (lo, hi, positive_code, negative_code) +static THRESH_16: [(i32, i32, u8, u8); 1] = [(261, 2047, 1, 2)]; + +static THRESH_24: [(i32, i32, u8, u8); 3] = [(331, 2047, 3, 4), (218, 330, 2, 5), (8, 217, 1, 6)]; + +static THRESH_32: [(i32, i32, u8, u8); 8] = [ + (400, 2047, 7, 8), + (349, 399, 6, 9), + (300, 348, 5, 10), + (246, 299, 4, 11), + (178, 245, 3, 12), + (80, 177, 2, 13), + (0, 79, 1, 14), + (3972, 4095, 1, 14), +]; + +static THRESH_40: [(i32, i32, u8, u8); 16] = [ + (553, 2047, 15, 16), + (528, 552, 14, 17), + (502, 527, 13, 18), + (475, 501, 12, 19), + (445, 474, 11, 20), + (413, 444, 10, 21), + (378, 412, 9, 22), + (339, 377, 8, 23), + (298, 338, 7, 24), + (250, 297, 6, 25), + (198, 249, 5, 26), + (139, 197, 4, 27), + (68, 138, 3, 28), + (0, 67, 2, 29), + (4080, 4095, 2, 29), + (3974, 4079, 1, 30), +]; + +#[inline] +fn quantize_16(dln: i32, ds: u8) -> u8 { + for &(lo, hi, pos, neg) in &THRESH_16 { + if (lo..=hi).contains(&dln) { + return if ds == 0 { pos } else { neg }; + } + } + if ds == 0 { 0 } else { 3 } +} + +#[inline] +fn quantize_24(dln: i32, ds: u8) -> u8 { + for &(lo, hi, pos, neg) in &THRESH_24 { + if (lo..=hi).contains(&dln) { + return if ds == 0 { pos } else { neg }; + } + } + 7 +} + +#[inline] +fn quantize_32(dln: i32, ds: u8) -> u8 { + for &(lo, hi, pos, neg) in &THRESH_32 { + if (lo..=hi).contains(&dln) { + return if ds == 0 { pos } else { neg }; + } + } + 15 +} + +#[inline] +fn quantize_40(dln: i32, ds: u8) -> u8 { + for &(lo, hi, pos, neg) in &THRESH_40 { + if (lo..=hi).contains(&dln) { + return if ds == 0 { pos } else { neg }; + } + } + 31 +} + +// --------------------------------------------------------------------------- +// G.726 Core State +// --------------------------------------------------------------------------- + +#[derive(Debug, Clone)] +struct Core { + rate: BitRate, + is_40k: bool, + a: [u16; 2], + b: [u16; 6], + dms: i16, + dml: i16, + ap: i16, + yu: i16, + yl: i32, + sr: [u16; 2], + dq: [u16; 6], + pk: [u8; 2], + td: i16, +} + +impl Core { + fn new(rate: BitRate) -> Self { + let is_40k = matches!(rate, BitRate::Rate40k); + let mut core = + Self { rate, is_40k, a: [0; 2], b: [0; 6], dms: 0, dml: 0, ap: 0, yu: 544, yl: 34816, sr: [32, 32], dq: [32; 6], pk: [0; 2], td: 0 }; + core.reset(); + core + } + + fn reset(&mut self) { + self.a = [0; 2]; + self.b = [0; 6]; + self.dms = 0; + self.dml = 0; + self.ap = 0; + self.yu = 544; + self.yl = 34816; + self.sr = [32, 32]; + self.dq = [32; 6]; + self.pk = [0; 2]; + self.td = 0; + } + + #[inline] + fn fmult(an: u16, srn: u16) -> u16 { + let an_s = (an >> 15) & 1; + let an_mag = if an_s == 0 { (an >> 2) & 0x1FFF } else { (16384_u32.wrapping_sub((an >> 2) as u32) & 0x1FFF) as u16 }; + let an_exp = if an_mag > 0 { (16 - an_mag.leading_zeros()).min(13) as u16 } else { 0 }; + let an_mant = if an_mag == 0 { 1 << 5 } else { (((an_mag as u32) << 6) >> an_exp) as u16 & 0x3F }; + + let srn_s = (srn >> 10) & 1; + let srn_exp = (srn >> 6) & 15; + let srn_mant = srn & 63; + + let wan_s = srn_s ^ an_s; + let wan_exp = srn_exp + an_exp; + let wan_mant = (((srn_mant as u32 * an_mant as u32) + 48) >> 4) as u16; + + let wan_mag = + if wan_exp <= 26 { ((wan_mant as u32) << 7) >> (26 - wan_exp) } else { (((wan_mant as u32) << 7) << (wan_exp - 26)) & 0x7FFF } as u16; + + if wan_s == 0 { wan_mag } else { (65536_u32.wrapping_sub(wan_mag as u32) & 0xFFFF) as u16 } + } + + #[inline] + fn quantize(&self, dln: i32, ds: u8) -> u8 { + match self.rate { + BitRate::Rate16k => quantize_16(dln, ds), + BitRate::Rate24k => quantize_24(dln, ds), + BitRate::Rate32k => quantize_32(dln, ds), + BitRate::Rate40k => quantize_40(dln, ds), + } + } + + #[inline] + fn table_lookup(&self, code: u8) -> (u8, u16) { + match self.rate { + BitRate::Rate16k => OUT_16[(code & 3) as usize], + BitRate::Rate24k => OUT_24[(code & 7) as usize], + BitRate::Rate32k => OUT_32[(code & 15) as usize], + BitRate::Rate40k => OUT_40[(code & 31) as usize], + } + } + + #[inline] + fn compute_accum(&self) -> (i16, i16) { + let wa0 = Self::fmult(self.a[0], self.sr[0]); + let wa1 = Self::fmult(self.a[1], self.sr[1]); + let mut sezi: u32 = 0; + for i in 0..6 { + sezi = (sezi + Self::fmult(self.b[i], self.dq[i]) as u32) & 0xFFFF; + } + let sei = (sezi + wa1 as u32 + wa0 as u32) & 0xFFFF; + let sez = (sezi as i16) >> 1; + let se = (sei as i16) >> 1; + (sez, se) + } + + #[inline] + fn compute_mix(&self) -> i32 { + let al = if self.ap >= 256 { 64 } else { self.ap >> 2 } as i32; + let dif = (self.yu as i32 + 16384 - (self.yl >> 6)) & 16383; + let difs = (dif >> 13) & 1; + let difm = if difs == 0 { dif } else { (16384 - dif) & 8191 }; + let prodm = (difm * al) >> 6; + let prod = if difs == 0 { prodm } else { (16384 - prodm) & 8191 }; + ((self.yl >> 6) + prod) & 8191 + } + + #[inline] + fn quantize_sample(&self, sample: i16, se: i16, y: i32) -> u8 { + let sl = (sample >> 2) as i32; + let sl_ext = if sl >= 0 { sl } else { 65536 + sl }; + let se_ext = if se >= 0 { se as i32 } else { 65536 + se as i32 }; + let d = ((sl_ext + 65536 - se_ext) & 0xFFFF) as u16; + + let ds = ((d >> 15) & 1) as u8; + let dqm = if ds == 0 { d } else { (65536_u32.wrapping_sub(d as u32) & 0x7FFF) as u16 }; + let exp = if dqm > 1 { (15 - dqm.leading_zeros()).min(14) } else { 0 }; + let mant = (((dqm as u32) << 7) >> exp) & 127; + let dl = ((exp << 7) + mant) as u16; + let dln = (dl as i32 + 4096 - (y >> 2)) & 4095; + self.quantize(dln, ds) + } + + #[inline] + fn step(&mut self, sample: i16, is_encoder: bool, code_in: u8) -> (u8, i16) { + let (sez, se) = self.compute_accum(); + let y = self.compute_mix(); + + let code = if is_encoder { self.quantize_sample(sample, se, y) } else { code_in }; + + // 4. RECONST & ADDA & ANTILOG + let (dqs, dqln) = self.table_lookup(code); + let dql = (dqln as i32 + (y >> 2)) & 4095; + let dex = (dql >> 7) & 15; + let dmn = dql & 127; + let dqt = 128 + dmn; + let dqmag = if (dql >> 11) == 0 { ((dqt << 7) >> (14 - dex)) as u16 } else { 0 }; + let dq = if self.is_40k { ((dqs as u16) << 15) | (dqmag & 0x7FFF) } else { ((dqs as u16) << 14) | (dqmag & 0x3FFF) }; + + // 5. ADDB & ADDC + let dqi = if dqs == 0 { + dq as u32 + } else if self.is_40k { + 65536_u32.wrapping_sub((dq & 0x7FFF) as u32) & 0xFFFF + } else { + 65536_u32.wrapping_sub((dq & 0x3FFF) as u32) & 0xFFFF + }; + let sei_ext = if se >= 0 { se as u32 } else { (65536 + se as i32) as u32 }; + let sr = ((dqi + sei_ext) & 0xFFFF) as u16; + + let sezi_ext = if sez >= 0 { sez as u32 } else { (65536 + sez as i32) as u32 }; + let dqsez = ((dqi + sezi_ext) & 0xFFFF) as u16; + let pk0 = ((dqsez >> 15) & 1) as u8; + let sigpk = if dqsez == 0 { 1 } else { 0 }; + + // 6. FLOATA & FLOATB + let dq_mag = if self.is_40k { dq & 0x7FFF } else { dq & 0x3FFF }; + let (dq_exp, dq_mant) = if dq_mag == 0 { + (0, 32) + } else { + let max_exp = if self.is_40k { 15 } else { 14 }; + let exp = (16 - dq_mag.leading_zeros()).min(max_exp); + let mant = (((dq_mag as u32) << 6) >> exp) as u16; + (exp as u16, mant) + }; + let dq0 = ((dqs as u16) << 10) | (dq_exp << 6) | (dq_mant & 63); + + let sr_s = ((sr >> 15) & 1) as u8; + let sr_mag = if sr_s == 0 { sr } else { (65536_u32.wrapping_sub(sr as u32) & 0x7FFF) as u16 }; + let (sr_exp, sr_mant) = if sr_mag == 0 { + (0, 32) + } else { + let exp = (16 - sr_mag.leading_zeros()).min(15); + let mant = (((sr_mag as u32) << 6) >> exp) as u16; + (exp as u16, mant) + }; + let sr0 = ((sr_s as u16) << 10) | (sr_exp << 6) | (sr_mant & 63); + + self.update_adaptation_and_predictors(code, y, pk0, sigpk, dqs, dq_mag); + + self.sr = [sr0, self.sr[0]]; + self.dq = [dq0, self.dq[0], self.dq[1], self.dq[2], self.dq[3], self.dq[4]]; + self.pk = [pk0, self.pk[0]]; + + let sr_signed = if sr < 32768 { sr as i32 } else { sr as i32 - 65536 }; + let out_pcm = (sr_signed << 2).clamp(-32768, 32767) as i16; + (code, out_pcm) + } + + #[inline] + fn adapt_scale(&mut self, code: u8, y: i32) { + let wi = get_wi(self.rate, code); + let dif = (((wi as i32) << 5) + 131072 - y) & 131071; + let difs = (dif >> 16) & 1; + let difsx = if difs == 0 { dif >> 5 } else { (dif >> 5) + 4096 }; + let yut = (y + difsx) & 8191; + let yup = yut.clamp(544, 5120); + + let dif = (yup + ((1048576 - self.yl) >> 6)) & 16383; + let difs = (dif >> 13) & 1; + let difsx = if difs == 0 { dif } else { dif + 507904 }; + self.yu = yup as i16; + self.yl = (self.yl + difsx) & 524287; + } + + #[inline] + fn adapt_speed(&mut self, code: u8, y: i32, dq_mag: u16) -> (i16, i16, u8) { + let fi = get_fi(self.rate, code); + let dif = (((fi as i32) << 9) + 8192 - self.dms as i32) & 8191; + let difs = (dif >> 12) & 1; + let difsx = if difs == 0 { dif >> 5 } else { (dif >> 5) + 3840 }; + let dmsp = ((difsx + self.dms as i32) & 4095) as i16; + + let dif = (((fi as i32) << 11) + 32768 - self.dml as i32) & 32767; + let difs = (dif >> 14) & 1; + let difsx = if difs == 0 { dif >> 7 } else { (dif >> 7) + 16128 }; + let dmlp = ((difsx + self.dml as i32) & 16383) as i16; + + let dif = (((dmsp as i32) << 2) + 32768 - dmlp as i32) & 32767; + let difs = (dif >> 14) & 1; + let difm = if difs == 0 { dif } else { (32768 - dif) & 16383 }; + let dthr = (dmlp as i32) >> 3; + + let tdp = if (32768..53760).contains(&self.a[1]) { 1 } else { 0 }; + let ax = if y >= 1536 && tdp == 0 && difm < dthr { 0 } else { 1 }; + + let dif = ((ax << 9) + 2048 - self.ap as i32) & 2047; + let difs = (dif >> 10) & 1; + let difsx = if difs == 0 { dif >> 4 } else { (dif >> 4) + 1920 }; + let app = ((difsx + self.ap as i32) & 1023) as i16; + + let ylint = (self.yl >> 15) as u32; + let ylfrac = ((self.yl >> 10) & 31) as u32; + let thr1 = (32 + ylfrac) << ylint; + let thr2 = if self.is_40k && ylint > 9 { + 31 << 10 + } else if !self.is_40k && ylint > 8 { + 31 << 9 + } else { + thr1 + }; + let dqthr = (thr2 + (thr2 >> 1)) >> 1; + let tr = if (dq_mag as u32) > dqthr && self.td == 1 { 1 } else { 0 }; + let apr = if tr == 1 { 256 } else { app }; + + self.dms = dmsp; + self.dml = dmlp; + self.ap = apr; + self.td = if tr == 1 { 0 } else { tdp }; + + (apr, tdp, tr) + } + + #[inline] + fn update_predictors(&mut self, pk0: u8, sigpk: u8, dqs: u8, dq_mag: u16, tr: u8) { + let pks = pk0 ^ self.pk[0]; + let uga1 = if sigpk == 1 { + 0 + } else if pks == 0 { + 192 + } else { + 65344 + }; + let a1s = (self.a[0] >> 15) & 1; + let ula1 = if a1s == 0 { + (65536_u32.wrapping_sub((self.a[0] >> 8) as u32) & 0xFFFF) as u16 + } else { + (65536_u32.wrapping_sub(((self.a[0] >> 8) + 65280) as u32) & 0xFFFF) as u16 + }; + let ua1 = (uga1 as u32 + ula1 as u32) & 0xFFFF; + let a1t = ((self.a[0] as u32 + ua1) & 0xFFFF) as u16; + + let pks1 = pk0 ^ self.pk[0]; + let pks2 = pk0 ^ self.pk[1]; + let uga2a = if pks2 == 0 { 16384_u32 } else { 114688_u32 }; + let a1_u = self.a[0]; + let fa1 = if a1s == 0 { + if a1_u <= 8191 { (a1_u as u32) << 2 } else { 8191 << 2 } + } else if a1_u >= 57345 { + ((a1_u as u32) << 2) & 131071 + } else { + 24577 << 2 + }; + let fa = if pks1 == 1 { fa1 } else { (131072_u32.wrapping_sub(fa1)) & 131071 }; + let uga2b = (uga2a + fa) & 131071; + let uga2s = (uga2b >> 16) & 1; + let uga2 = if sigpk == 1 { + 0 + } else if uga2s == 0 { + uga2b >> 7 + } else { + (uga2b >> 7) + 64512 + } as u16; + let a2s = (self.a[1] >> 15) & 1; + let ula2 = if a2s == 0 { + (65536_u32.wrapping_sub((self.a[1] >> 7) as u32) & 0xFFFF) as u16 + } else { + (65536_u32.wrapping_sub(((self.a[1] >> 7) + 65024) as u32) & 0xFFFF) as u16 + }; + let ua2 = (uga2 as u32 + ula2 as u32) & 0xFFFF; + let a2t = ((self.a[1] as u32 + ua2) & 0xFFFF) as u16; + + let a2p = if (32768..=53248).contains(&a2t) { + 53248 + } else if (12288..=32767).contains(&a2t) { + 12288 + } else { + a2t + }; + + let ome = 15360_u32; + let a1ul = ((ome + 65536 - a2p as u32) & 0xFFFF) as u16; + let a1ll = ((a2p as u32 + 65536 - ome) & 0xFFFF) as u16; + let a1p = if a1t >= 32768 && a1t <= a1ll { + a1ll + } else if a1t >= a1ul && a1t <= 32767 { + a1ul + } else { + a1t + }; + + let a1r = if tr == 1 { 0 } else { a1p }; + let a2r = if tr == 1 { 0 } else { a2p }; + + let mut new_b = [0u16; 6]; + for (i, slot) in new_b.iter_mut().enumerate() { + let un = (dqs ^ ((self.dq[i] >> 10) as u8)) & 1; + let ugbn = if dq_mag == 0 { + 0 + } else if un == 0 { + 128 + } else { + 65408 + }; + let bns = (self.b[i] >> 15) & 1; + let ulbn = if self.is_40k { + if bns == 0 { + (65536_u32.wrapping_sub((self.b[i] >> 9) as u32) & 0xFFFF) as u16 + } else { + (65536_u32.wrapping_sub(((self.b[i] >> 9) + 65408) as u32) & 0xFFFF) as u16 + } + } else if bns == 0 { + (65536_u32.wrapping_sub((self.b[i] >> 8) as u32) & 0xFFFF) as u16 + } else { + (65536_u32.wrapping_sub(((self.b[i] >> 8) + 65280) as u32) & 0xFFFF) as u16 + }; + let ubn = (ugbn as u32 + ulbn as u32) & 0xFFFF; + let bnp = ((self.b[i] as u32 + ubn) & 0xFFFF) as u16; + *slot = if tr == 1 { 0 } else { bnp }; + } + + self.a[0] = a1r; + self.a[1] = a2r; + self.b = new_b; + } + + #[inline] + fn update_adaptation_and_predictors(&mut self, code: u8, y: i32, pk0: u8, sigpk: u8, dqs: u8, dq_mag: u16) { + self.adapt_scale(code, y); + let (_, _, tr) = self.adapt_speed(code, y, dq_mag); + self.update_predictors(pk0, sigpk, dqs, dq_mag, tr); + } +} + +// --------------------------------------------------------------------------- +// Decoder & Encoder structs +// --------------------------------------------------------------------------- + +/// G.726 ADPCM decoder. +#[derive(Debug, Clone)] +pub struct Decoder { + core: Core, + packing: BitPacking, + bit_buf: u32, + num_bits: usize, +} + +impl Decoder { + /// Create a new G.726 decoder at the specified bit rate and bit packing. + #[must_use] + pub fn new(rate: BitRate, packing: BitPacking) -> Self { + Self { core: Core::new(rate), packing, bit_buf: 0, num_bits: 0 } + } + + /// Reset decoder internal predictor state. + pub fn reset(&mut self) { + self.core.reset(); + self.bit_buf = 0; + self.num_bits = 0; + } + + /// Decode a single quantized ADPCM code into an 8 kHz 16-bit linear PCM sample. + /// Bits above the configured code width are ignored, as in packed-stream decoding. + #[inline] + pub fn decode_sample(&mut self, code: u8) -> i16 { + // Adaptation must see the same bounded codeword as the reconstruction table; + // otherwise unused high bits can underflow its unsigned inverse-code arithmetic. + let code = code & ((1u8 << self.core.rate.code_size()) - 1); + self.core.step(0, false, code).1 + } + + /// Decode G.726 bitstream bytes into 8 kHz 16-bit linear PCM samples, appending to `dst`. + pub fn decode(&mut self, src: &[u8], dst: &mut Vec) -> Result<(), Error> { + if src.len() > MAX_INPUT_BYTES { + return Err(Error::InputTooLarge); + } + let cs = self.core.rate.code_size(); + let est_samples = (src.len() * 8) / cs; + dst.try_reserve(est_samples).map_err(|_| Error::AllocationFailed)?; + + match self.packing { + BitPacking::BigEndian => { + for &b in src { + self.bit_buf = ((self.bit_buf & ((1 << self.num_bits) - 1)) << 8) | (b as u32); + self.num_bits += 8; + while self.num_bits >= cs { + let code = ((self.bit_buf >> (self.num_bits - cs)) & ((1 << cs) - 1)) as u8; + self.num_bits -= cs; + let s = self.decode_sample(code); + dst.push(s); + } + self.bit_buf &= (1 << self.num_bits) - 1; + } + } + BitPacking::LittleEndian => { + for &b in src { + self.bit_buf |= (b as u32) << self.num_bits; + self.num_bits += 8; + while self.num_bits >= cs { + let code = (self.bit_buf & ((1 << cs) - 1)) as u8; + self.bit_buf >>= cs; + self.num_bits -= cs; + let s = self.decode_sample(code); + dst.push(s); + } + } + } + } + Ok(()) + } +} + +/// G.726 ADPCM encoder. +#[derive(Debug, Clone)] +pub struct Encoder { + core: Core, + packing: BitPacking, + bit_buf: u32, + num_bits: usize, +} + +impl Encoder { + /// Create a new G.726 encoder at the specified bit rate and bit packing. + #[must_use] + pub fn new(rate: BitRate, packing: BitPacking) -> Self { + Self { core: Core::new(rate), packing, bit_buf: 0, num_bits: 0 } + } + + /// Reset encoder internal predictor state. + pub fn reset(&mut self) { + self.core.reset(); + self.bit_buf = 0; + self.num_bits = 0; + } + + /// Encode a single 8 kHz 16-bit linear PCM sample into an ADPCM code word. + #[inline] + pub fn encode_sample(&mut self, sample: i16) -> u8 { + self.core.step(sample, true, 0).0 + } + + /// Encode 8 kHz 16-bit linear PCM samples into G.726 bitstream bytes, appending to `dst`. + pub fn encode(&mut self, src: &[i16], dst: &mut Vec) -> Result<(), Error> { + if src.len() > MAX_SAMPLES { + return Err(Error::InputTooLarge); + } + let cs = self.core.rate.code_size(); + let est_bytes = (src.len() * cs).div_ceil(8); + dst.try_reserve(est_bytes).map_err(|_| Error::AllocationFailed)?; + + match self.packing { + BitPacking::BigEndian => { + for &s in src { + let code = self.encode_sample(s); + self.bit_buf = ((self.bit_buf & ((1 << self.num_bits) - 1)) << cs) | ((code as u32) & ((1 << cs) - 1)); + self.num_bits += cs; + while self.num_bits >= 8 { + dst.push(((self.bit_buf >> (self.num_bits - 8)) & 0xFF) as u8); + self.num_bits -= 8; + } + self.bit_buf &= (1 << self.num_bits) - 1; + } + } + BitPacking::LittleEndian => { + for &s in src { + let code = self.encode_sample(s); + self.bit_buf |= (code as u32) << self.num_bits; + self.num_bits += cs; + while self.num_bits >= 8 { + dst.push((self.bit_buf & 0xFF) as u8); + self.bit_buf >>= 8; + self.num_bits -= 8; + } + } + } + } + Ok(()) + } + + /// Flush any remaining buffered bits into `dst`, padding with zeroes. + pub fn flush(&mut self, dst: &mut Vec) -> Result<(), Error> { + if self.num_bits > 0 { + dst.try_reserve(1).map_err(|_| Error::AllocationFailed)?; + match self.packing { + BitPacking::BigEndian => { + let b = ((self.bit_buf << (8 - self.num_bits)) & 0xFF) as u8; + dst.push(b); + } + BitPacking::LittleEndian => { + let b = (self.bit_buf & 0xFF) as u8; + dst.push(b); + } + } + self.bit_buf = 0; + self.num_bits = 0; + } + Ok(()) + } +} + +// --------------------------------------------------------------------------- +// Public Top-Level Functions +// --------------------------------------------------------------------------- + +/// Decode a mono G.726 bitstream into 8 kHz 16-bit linear PCM samples. +pub fn decode(bytes: &[u8], rate: BitRate, packing: BitPacking) -> Result, Error> { + let mut decoder = Decoder::new(rate, packing); + let mut out = Vec::new(); + decoder.decode(bytes, &mut out)?; + Ok(out) +} + +/// Encode mono 8 kHz 16-bit linear PCM samples into a G.726 bitstream. +pub fn encode(samples: &[i16], rate: BitRate, packing: BitPacking) -> Result, Error> { + let mut encoder = Encoder::new(rate, packing); + let mut out = Vec::new(); + encoder.encode(samples, &mut out)?; + encoder.flush(&mut out)?; + Ok(out) +} + +/// Decode an interleaved multi-channel G.726 stream into interleaved 8 kHz 16-bit PCM samples. +pub fn decode_interleaved(bytes: &[u8], channels: usize, rate: BitRate, packing: BitPacking) -> Result, Error> { + if channels == 0 || channels > MAX_CHANNELS { + return Err(Error::InvalidChannels(channels)); + } + if channels == 1 { + return decode(bytes, rate, packing); + } + if bytes.len() > MAX_INPUT_BYTES { + return Err(Error::InputTooLarge); + } + + let mut decoders: Vec = Vec::with_capacity(channels); + for _ in 0..channels { + decoders.push(Decoder::new(rate, packing)); + } + + let cs = rate.code_size(); + let est_samples = (bytes.len() * 8) / cs; + let mut out = Vec::new(); + out.try_reserve(est_samples).map_err(|_| Error::AllocationFailed)?; + + let mut ch_outs: Vec> = Vec::with_capacity(channels); + for _ in 0..channels { + ch_outs.push(Vec::new()); + } + + for (i, &b) in bytes.iter().enumerate() { + let ch = i % channels; + decoders[ch].decode(&[b], &mut ch_outs[ch])?; + } + + let min_len = ch_outs.iter().map(Vec::len).min().unwrap_or(0); + for i in 0..min_len { + for ch_out in ch_outs.iter().take(channels) { + out.push(ch_out[i]); + } + } + + Ok(out) +} + +/// Encode interleaved 8 kHz 16-bit PCM samples into an interleaved multi-channel G.726 stream. +pub fn encode_interleaved(samples: &[i16], channels: usize, rate: BitRate, packing: BitPacking) -> Result, Error> { + if channels == 0 || channels > MAX_CHANNELS { + return Err(Error::InvalidChannels(channels)); + } + if channels == 1 { + return encode(samples, rate, packing); + } + if samples.len() > MAX_SAMPLES { + return Err(Error::InputTooLarge); + } + + let mut encoders: Vec = Vec::with_capacity(channels); + for _ in 0..channels { + encoders.push(Encoder::new(rate, packing)); + } + + let cs = rate.code_size(); + let est_bytes = (samples.len() * cs).div_ceil(8); + let mut out = Vec::new(); + out.try_reserve(est_bytes).map_err(|_| Error::AllocationFailed)?; + + let mut ch_samples: Vec> = Vec::with_capacity(channels); + for _ in 0..channels { + ch_samples.push(Vec::new()); + } + + for (i, &s) in samples.iter().enumerate() { + let ch = i % channels; + ch_samples[ch].push(s); + } + + let mut ch_bytes: Vec> = Vec::with_capacity(channels); + for ch in 0..channels { + let mut b = Vec::new(); + encoders[ch].encode(&ch_samples[ch], &mut b)?; + encoders[ch].flush(&mut b)?; + ch_bytes.push(b); + } + + let min_bytes = ch_bytes.iter().map(Vec::len).min().unwrap_or(0); + for i in 0..min_bytes { + for ch_b in ch_bytes.iter().take(channels) { + out.push(ch_b[i]); + } + } + + Ok(out) +} + +// --------------------------------------------------------------------------- +// Unit tests +// --------------------------------------------------------------------------- + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sample_api_ignores_unused_codeword_bits() { + for rate in [BitRate::Rate16k, BitRate::Rate24k, BitRate::Rate32k, BitRate::Rate40k] { + let mask = (1u8 << rate.code_size()) - 1; + let mut actual = Decoder::new(rate, BitPacking::BigEndian); + let mut expected = Decoder::new(rate, BitPacking::BigEndian); + for code in 0..=u8::MAX { + assert_eq!(actual.decode_sample(code), expected.decode_sample(code & mask)); + } + // High bits must not leave a different predictor state for later valid codes. + for code in 0..=mask { + assert_eq!(actual.decode_sample(code), expected.decode_sample(code)); + } + } + } + + #[test] + fn reject_invalid_channels() { + assert!(decode_interleaved(&[], 0, BitRate::Rate32k, BitPacking::BigEndian).is_err()); + assert!(decode_interleaved(&[], 9, BitRate::Rate32k, BitPacking::BigEndian).is_err()); + assert!(encode_interleaved(&[], 0, BitRate::Rate32k, BitPacking::BigEndian).is_err()); + assert!(encode_interleaved(&[], 9, BitRate::Rate32k, BitPacking::BigEndian).is_err()); + } + + #[test] + fn roundtrip_silence_all_rates() { + let pcm = vec![0i16; 64]; + for rate in [BitRate::Rate16k, BitRate::Rate24k, BitRate::Rate32k, BitRate::Rate40k] { + for packing in [BitPacking::BigEndian, BitPacking::LittleEndian] { + let encoded = encode(&pcm, rate, packing).expect("encode silence"); + let decoded = decode(&encoded, rate, packing).expect("decode silence"); + assert_eq!(decoded.len(), pcm.len()); + for &s in &decoded { + assert!(s.abs() <= 16, "silence sample too large: {s}"); + } + } + } + } + + #[test] + fn hostile_input_does_not_panic() { + let mut decoder = Decoder::new(BitRate::Rate32k, BitPacking::BigEndian); + let mut out = Vec::new(); + let hostile = vec![0xFF; 1024]; + let _ = decoder.decode(&hostile, &mut out); + + let mut decoder = Decoder::new(BitRate::Rate16k, BitPacking::LittleEndian); + let mut out = Vec::new(); + let _ = decoder.decode(&[0x00, 0xAA, 0x55, 0x12, 0x34], &mut out); + } +} diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 8aa3af36df..fcaf2897e1 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -2,7 +2,7 @@ #![warn(missing_docs)] // The docs link items of every feature; built with fewer features, those links have no target. // CI and docs.rs build the docs with all features, where every link must resolve. -#![cfg_attr(not(all(feature = "decode", feature = "encode", feature = "peaks")), allow(rustdoc::broken_intra_doc_links))] +#![cfg_attr(not(all(feature = "decode", feature = "encode", feature = "peaks", feature = "dsd")), allow(rustdoc::broken_intra_doc_links))] //! Audio file I/O and codecs for the Craft apps. Input and output are byte slices or a //! [`craft_media::ByteSource`] (no filesystem access of its own); builds for //! `wasm32-unknown-unknown`. @@ -13,6 +13,7 @@ //! demuxers and [`TrackDecoder`]; everything else (FLAC, Vorbis, CAF, ADPCM, A-law, µ-law) through //! `symphonia`, which is to be replaced by native decoders. Feature `decode-native` is the //! same without symphonia. +//! - [`vorbis::decoder`] (feature `vorbis-decode`): the pure-Rust Vorbis I decoder, packets and Ogg files. //! - [`encode_ogg_vorbis`] and [`vorbis::encoder`] (feature `vorbis-encode`): Ogg Vorbis files //! and Vorbis packets from a pure-Rust encoder, 1 to 8 channels, quality -1 to 10, with tags. //! - [`TrackDecoder`] (any decoder feature): one track's packets from any demuxer to planar @@ -28,10 +29,12 @@ //! - [`peaks`] (feature `peaks`): min/max/RMS pyramid for waveform drawing. //! - Codec modules for packet-level use, each with its own features: [`aac`] (`aac-decode`, //! `aac-encode`), [`alac`] (`alac-decode`, `alac-encode`), [`opus`] (`opus-decode`, and -//! [`opus::encoder`] with `opus-encode`), [`ac3`] (`ac3-decode`), [`mpa`] (`mpa-decode`), [`mp3`] +//! [`opus::encoder`] with `opus-encode`), [`ac3`] (`ac3-decode`), [`mpa`] (`mpa-decode`; `mpa-encode`: the Layer II encoder), [`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), -//! [`gsm`] (`gsm-decode`, `gsm-encode`: GSM 06.10 full-rate speech). +//! `flac-encode`: the packet encoder), [`qoa`] (`qoa-decode`, `qoa-encode`: whole QOA files), [`adpcm`] +//! (`adpcm`: IMA WAV, Microsoft, Yamaha and Flash ADPCM), [`dsd`] (`dsd`: DSF and DSDIFF files to PCM), +//! [`gsm`] (`gsm-decode`, `gsm-encode`: GSM 06.10 full-rate speech), [`vmd`] (`vmd-decode`: +//! Sierra VMD sound). //! //! The shared types below build with no features. @@ -39,9 +42,17 @@ pub mod aac; #[cfg(any(feature = "ac3-decode", feature = "ac3-encode", feature = "eac3-encode"))] pub mod ac3; +#[cfg(feature = "adpcm")] +pub mod adpcm; #[cfg(any(feature = "alac-decode", feature = "alac-encode"))] pub mod alac; mod buffer; +#[cfg(feature = "dsd")] +pub mod dsd; +#[cfg(feature = "g722")] +pub mod g722; +#[cfg(feature = "g726")] +pub mod g726; #[cfg(any(feature = "gsm-decode", feature = "gsm-encode"))] pub mod gsm; mod layout; @@ -63,7 +74,9 @@ mod read; 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")] +#[cfg(feature = "vmd-decode")] +pub mod vmd; +#[cfg(any(feature = "vorbis-decode", feature = "vorbis-encode"))] pub mod vorbis; #[cfg(any(feature = "decode-native", feature = "pcm-encode"))] mod wav; @@ -267,6 +280,16 @@ impl From for AudioError { } } +#[cfg(feature = "vmd-decode")] +impl From for AudioError { + fn from(e: vmd::Error) -> Self { + match e { + vmd::Error::Unsupported(_) => AudioError::Unsupported(e.to_string()), + _ => AudioError::Malformed(e.to_string()), + } + } +} + #[cfg(feature = "opus-encode")] impl From for AudioError { fn from(e: opus::encoder::Error) -> Self { diff --git a/crates/audio/src/mp3/mod.rs b/crates/audio/src/mp3/mod.rs index d286071105..e273e903ca 100644 --- a/crates/audio/src/mp3/mod.rs +++ b/crates/audio/src/mp3/mod.rs @@ -39,13 +39,13 @@ mod decoder; #[cfg(feature = "mp3-encode")] mod encoder; #[cfg(feature = "mp3-encode")] -mod filterbank; +pub(crate) mod filterbank; #[cfg(feature = "mp3-encode")] mod huffman; mod huffman_tables; #[cfg(feature = "mp3-encode")] mod quantize; -mod tables; +pub(crate) mod tables; #[cfg(feature = "mp3-decode")] pub use decoder::{Decoder, InfoTag}; diff --git a/crates/audio/src/mpa/encoder.rs b/crates/audio/src/mpa/encoder.rs new file mode 100644 index 0000000000..000f86fe72 --- /dev/null +++ b/crates/audio/src/mpa/encoder.rs @@ -0,0 +1,493 @@ +//! The Layer II encoder; the module docs are in `mpa/mod.rs`. + +// Index loops follow the specification's pseudo-code (FilmCraft allows this lint workspace-wide). +#![allow(clippy::needless_range_loop)] + +use craft_media::bitstream::BitWriter; + +use super::tables::{self, AllocTable, SUBBANDS}; +use crate::mp3::filterbank::Polyphase; +use crate::mp3::tables::ath_db; + +/// Settings an [`Encoder`] refuses. +#[non_exhaustive] +#[derive(Debug, thiserror::Error, PartialEq, Eq)] +pub enum EncodeError { + /// A sample rate outside [`SAMPLE_RATES`]. + #[error("MP2: unsupported sample rate {0} Hz (32, 44.1, 48 kHz, or 16, 22.05, 24 kHz)")] + SampleRate(u32), + /// A bit rate that is not one of [`bitrates`] for the sample rate and channel count. + #[error("MP2: unsupported bit rate {kbps} kbit/s at {rate} Hz with {channels} channel(s)")] + Bitrate { + /// The bit rate asked for, kbit/s. + kbps: u32, + /// The sample rate, Hz. + rate: u32, + /// The channel count. + channels: usize, + }, + /// More than two channels, or none. + #[error("MP2: unsupported channel count {0} (1 or 2)")] + Channels(usize), +} + +/// Result with this module's [`EncodeError`]. +pub type EncodeResult = std::result::Result; + +/// Encoder settings. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Config { + /// One of [`SAMPLE_RATES`]. + pub sample_rate: u32, + /// 1 or 2. Two channels are coded as stereo (`mode` 00), each with its own allocation. + pub channels: usize, + /// One of [`bitrates`] for the sample rate and channel count, kbit/s. + pub bitrate_kbps: u32, +} + +impl Config { + /// `channels` at `sample_rate`, constant `bitrate_kbps`. + pub fn cbr(sample_rate: u32, channels: usize, bitrate_kbps: u32) -> Config { + Config { sample_rate, channels, bitrate_kbps } + } +} + +/// The sample rates the encoder accepts. +pub const SAMPLE_RATES: [u32; 6] = [32_000, 44_100, 48_000, 16_000, 22_050, 24_000]; + +/// Samples by which a standard decoder's output lags the input: the polyphase analysis and +/// synthesis filters. +pub const DELAY: u32 = 481; + +/// Samples per channel in a frame. +pub const FRAME_SAMPLES: usize = 1152; + +/// Bit rates ISO/IEC 11172-3 Table 3-B.2 allows for a single channel at 32 / 44.1 / 48 kHz. +const MPEG1_MONO: [u32; 10] = [32, 48, 56, 64, 80, 96, 112, 128, 160, 192]; +/// Bit rates it allows for two channels. +const MPEG1_STEREO: [u32; 10] = [64, 96, 112, 128, 160, 192, 224, 256, 320, 384]; + +/// Whether `rate` is one of the MPEG-2 lower sampling frequencies; `None` if it is unsupported. +fn is_lsf(rate: u32) -> Option { + match rate { + 32_000 | 44_100 | 48_000 => Some(false), + 16_000 | 22_050 | 24_000 => Some(true), + _ => None, + } +} + +/// The bit rates (kbit/s) a Layer II stream can use at `sample_rate` with `channels` channels: +/// at 32 / 44.1 / 48 kHz the combinations ISO/IEC 11172-3 allows (32-192 for one channel, 64-384 +/// for two), at the lower sampling frequencies 8-160 for either. Empty for an unsupported rate. +pub fn bitrates(sample_rate: u32, channels: usize) -> &'static [u32] { + match is_lsf(sample_rate) { + Some(false) if channels == 1 => &MPEG1_MONO, + Some(false) => &MPEG1_STEREO, + Some(true) => &tables::BITRATES_LSF_L23[1..], + None => &[], + } +} + +/// The highest bit rate at `sample_rate` and `channels` not above `kbps` (the lowest one when +/// `kbps` is below all of them); `None` for an unsupported rate. +pub fn nearest_bitrate(sample_rate: u32, channels: usize, kbps: u32) -> Option { + let rates = bitrates(sample_rate, channels); + rates.iter().rev().find(|&&r| r <= kbps).or(rates.first()).copied() +} + +/// SNR-free noise model: the power of the error of rounding a value in ±1 to one of `steps` +/// levels spaced `2 / steps` apart, is `(2 / steps)² / 12`. +fn quantization_noise(steps: u16) -> f32 { + let d = 2.0 / f32::from(steps); + d * d / 12.0 +} + +/// Index of the scalefactor for a peak of `peak` (Table 3-B.1): the smallest scalefactor that is +/// at least `peak`, i.e. the largest index. +fn scalefactor_index(peak: f32) -> u32 { + if peak.is_nan() || peak <= 0.0 { + return 62; + } + let mut i = (3.0 * (1.0 - f64::from(peak).log2())).floor().clamp(0.0, 62.0) as u32; + while i > 0 && tables::scalefactor(i) < peak { + i -= 1; + } + while i < 62 && tables::scalefactor(i + 1) >= peak { + i += 1; + } + i +} + +/// Which scalefactors of a subband's three parts are sent (`scfsi`, §2.4.1.6) and what they are. +fn share(sf: [u32; 3]) -> (u32, [u32; 3]) { + let near = |a: u32, b: u32| a.abs_diff(b) <= 1; + let [a, b, c] = sf; + if near(a, b) && near(b, c) && near(a, c) { + let m = a.min(b).min(c); + (2, [m, m, m]) + } else if near(a, b) { + let m = a.min(b); + (1, [m, m, c]) + } else if near(b, c) { + let m = b.min(c); + (3, [a, m, m]) + } else { + (0, sf) + } +} + +/// Scalefactors sent for an `scfsi` class. +fn scalefactors_sent(scfsi: u32) -> u64 { + match scfsi { + 0 => 3, + 2 => 1, + _ => 2, + } +} + +/// Bits of the 12 granules of samples in a subband with `steps` quantization steps. +fn sample_bits(steps: u16) -> u64 { + 12 * match tables::grouping(steps) { + Some(bits) => u64::from(bits), + None => 3 * u64::from((u32::from(steps) + 1).trailing_zeros()), + } +} + +/// What the encoder decided for one channel of a frame. +#[derive(Clone)] +struct ChannelPlan { + /// The allocation value (index into the table's row) per subband. + alloc: [usize; SUBBANDS], + /// `scfsi` per subband. + scfsi: [u32; SUBBANDS], + /// The scalefactor index of each part of each subband, after sharing. + sf: [[u32; 3]; SUBBANDS], + /// Mean square of each subband's samples. + energy: [f32; SUBBANDS], +} + +/// A Layer II encoder. Feed planar `f32` samples (full scale ±1) to [`Encoder::encode`]; it +/// returns whole frames as they are completed. [`Encoder::flush`] pads the end with silence and +/// returns the rest. +/// +/// ``` +/// use craft_audio::mpa::encoder::{Config, DELAY, Encoder}; +/// +/// let left: Vec = (0..48_000).map(|i| (i as f32 * 0.05).sin() * 0.5).collect(); +/// let right = left.clone(); +/// let mut enc = Encoder::new(Config::cbr(48_000, 2, 192))?; +/// let mut mp2 = enc.encode(&[&left, &right]); // whole frames as they complete +/// mp2.extend(enc.flush()); // pads with silence to the last input sample +/// assert_eq!(mp2.len() % 576, 0); // 192 kbit/s at 48 kHz: 576 bytes per frame +/// assert_eq!(DELAY, 481); // a decoder's output lags the input by this many samples +/// # Ok::<(), craft_audio::mpa::encoder::EncodeError>(()) +/// ``` +pub struct Encoder { + cfg: Config, + lsf: bool, + sr_index: u32, + br_index: u32, + table: AllocTable, + sblimit: usize, + /// The masking offset floor per subband (power), from the threshold in quiet. + ath: [f32; SUBBANDS], + poly: Vec, + /// Buffered input per channel, and how much of it is consumed. + input: Vec>, + consumed: usize, + /// Remainder accumulator for the padding slot (44.1 / 22.05 kHz). + pad_acc: u64, + samples_in: u64, + frames: u64, +} + +impl Encoder { + /// An encoder for `cfg`, or the setting it cannot carry. + pub fn new(cfg: Config) -> EncodeResult { + let lsf = is_lsf(cfg.sample_rate).ok_or(EncodeError::SampleRate(cfg.sample_rate))?; + if !(1..=2).contains(&cfg.channels) { + return Err(EncodeError::Channels(cfg.channels)); + } + let bad = EncodeError::Bitrate { kbps: cfg.bitrate_kbps, rate: cfg.sample_rate, channels: cfg.channels }; + if !bitrates(cfg.sample_rate, cfg.channels).contains(&cfg.bitrate_kbps) { + return Err(bad); + } + let (rates, bitrate_table): (&[u32; 3], &[u32; 15]) = + if lsf { (&[22_050, 24_000, 16_000], &tables::BITRATES_LSF_L23) } else { (&tables::SAMPLE_RATES, &tables::BITRATES_MPEG1_L2) }; + let sr_index = rates.iter().position(|&r| r == cfg.sample_rate).ok_or(EncodeError::SampleRate(cfg.sample_rate))?; + let br_index = bitrate_table.iter().position(|&b| b == cfg.bitrate_kbps).ok_or(bad)?; + let per_channel = cfg.bitrate_kbps / cfg.channels as u32; + let table = AllocTable::select(lsf, cfg.sample_rate, per_channel); + let sblimit = table.sblimit().min(SUBBANDS); + let mut ath = [0f32; SUBBANDS]; + for (sb, a) in ath.iter_mut().enumerate() { + let centre = (sb as f64 + 0.5) * f64::from(cfg.sample_rate) / 64.0; + // a full-scale sine has a mean square of 1/2 and plays back at about 96 dB SPL + *a = (0.5 * 10f64.powf((ath_db(centre) - 96.0) / 10.0)) as f32; + } + Ok(Encoder { + cfg, + lsf, + sr_index: sr_index as u32, + br_index: br_index as u32, + table, + sblimit, + ath, + poly: vec![Polyphase::new(); cfg.channels], + input: vec![Vec::new(); cfg.channels], + consumed: 0, + pad_acc: 0, + samples_in: 0, + frames: 0, + }) + } + + /// The settings. + pub fn config(&self) -> &Config { + &self.cfg + } + + /// Samples per channel in a frame: 1152. + pub fn frame_samples(&self) -> usize { + FRAME_SAMPLES + } + + /// Samples by which a standard decoder's output lags the input. + pub fn delay(&self) -> u32 { + DELAY + } + + /// Frames written so far. + pub fn frames(&self) -> u64 { + self.frames + } + + /// Encode planar samples (one slice per channel; a missing channel is silence, extra ones + /// are ignored, every channel takes the first channel's length). Returns the completed frames. + pub fn encode(&mut self, planar: &[&[f32]]) -> Vec { + let n = planar.first().map_or(0, |p| p.len()); + for (c, buf) in self.input.iter_mut().enumerate() { + let src = planar.get(c).copied().unwrap_or(&[]); + buf.extend((0..n).map(|i| src.get(i).copied().filter(|v| v.is_finite()).unwrap_or(0.0).clamp(-4.0, 4.0))); + } + self.samples_in = self.samples_in.saturating_add(n as u64); + let mut out = Vec::new(); + self.drain(&mut out); + out + } + + /// Encode the buffered rest padded with silence, plus enough silence that a decoder outputs + /// every input sample, and return the remaining frames. + pub fn flush(&mut self) -> Vec { + let fs = FRAME_SAMPLES as u64; + let needed = if self.samples_in == 0 { self.frames } else { self.samples_in.saturating_add(u64::from(DELAY)).div_ceil(fs) }; + let have = self.input.first().map_or(0, |b| b.len().saturating_sub(self.consumed)); + let want = (needed.saturating_sub(self.frames) as usize).saturating_mul(FRAME_SAMPLES); + if want > have { + for buf in &mut self.input { + buf.resize(self.consumed + want, 0.0); + } + } + let mut out = Vec::new(); + self.drain(&mut out); + self.input.iter_mut().for_each(Vec::clear); + self.consumed = 0; + out + } + + /// Encode every whole frame of buffered input. + fn drain(&mut self, out: &mut Vec) { + while self.input.first().is_some_and(|b| b.len() - self.consumed >= FRAME_SAMPLES) { + let mut sub = vec![[[0f32; SUBBANDS]; 36]; self.cfg.channels]; + for (ch, slots) in sub.iter_mut().enumerate() { + let (Some(buf), Some(poly)) = (self.input.get(ch), self.poly.get_mut(ch)) else { continue }; + let frame = buf.get(self.consumed..self.consumed + FRAME_SAMPLES).unwrap_or(&[]); + for (slot, chunk) in slots.iter_mut().zip(frame.as_chunks::<32>().0) { + poly.filter(chunk, slot); + } + } + self.consumed += FRAME_SAMPLES; + self.write_frame(&sub, out); + self.frames += 1; + } + // keep the buffer small + if self.consumed >= 1 << 16 { + for buf in &mut self.input { + buf.drain(..self.consumed); + } + self.consumed = 0; + } + } + + /// The scalefactors and energies of a channel's 36 time slots. + fn analyse(&self, slots: &[[f32; SUBBANDS]; 36]) -> ChannelPlan { + let mut plan = ChannelPlan { alloc: [0; SUBBANDS], scfsi: [0; SUBBANDS], sf: [[62; 3]; SUBBANDS], energy: [0.0; SUBBANDS] }; + for sb in 0..self.sblimit { + let mut sf = [62u32; 3]; + for (part, s) in sf.iter_mut().enumerate() { + let peak = slots[12 * part..12 * part + 12].iter().map(|slot| slot[sb].abs()).fold(0f32, f32::max); + *s = scalefactor_index(peak); + } + (plan.scfsi[sb], plan.sf[sb]) = share(sf); + plan.energy[sb] = slots.iter().map(|slot| slot[sb] * slot[sb]).sum::() / 36.0; + } + plan + } + + /// Choose the allocation of every subband of every channel with the bits the frame has. + fn allocate(&self, plans: &mut [ChannelPlan], mut bits_left: i64) { + let nch = plans.len(); + // power the quantization noise of each subband may have before it is audible: a spread + // of the band energies, never below the threshold in quiet + let mut mask = vec![[0f32; SUBBANDS]; nch]; + for (m, plan) in mask.iter_mut().zip(plans.iter()) { + for sb in 0..self.sblimit { + let mut level = self.ath[sb]; + for j in 0..self.sblimit { + let atten_db = 14.0 + 20.0 * sb.abs_diff(j) as f32; + level = level.max(plan.energy[j] * 10f32.powf(-atten_db / 10.0)); + } + m[sb] = level; + } + } + // the fixed cost: every subband's allocation field + for sb in 0..self.sblimit { + bits_left -= (nch * self.table.row(sb).len().trailing_zeros() as usize) as i64; + } + // the noise a subband has with allocation `a`: dropped, or the mean over its parts + let noise = |plan: &ChannelPlan, sb: usize, a: usize| -> f32 { + let row = self.table.row(sb); + let steps = row.get(a).copied().unwrap_or(0); + if steps == 0 { + return plan.energy[sb]; + } + let mean_sf2 = plan.sf[sb].iter().map(|&i| tables::scalefactor(i).powi(2)).sum::() / 3.0; + (quantization_noise(steps) * mean_sf2).min(plan.energy[sb]) + }; + let cost = |plan: &ChannelPlan, sb: usize, a: usize| -> i64 { + let row = self.table.row(sb); + let step = |a: usize| row.get(a).copied().unwrap_or(0); + let (from, to) = (step(a), step(a + 1)); + let mut bits = sample_bits(to) as i64 - if from == 0 { 0 } else { sample_bits(from) as i64 }; + if from == 0 { + bits += 2 + 6 * scalefactors_sent(plan.scfsi[sb]) as i64; + } + bits + }; + let mut blocked = vec![[false; SUBBANDS]; nch]; + loop { + // the subband whose noise is furthest above its mask + let mut best: Option<(usize, usize, f32)> = None; + for (ch, plan) in plans.iter().enumerate() { + for sb in 0..self.sblimit { + let a = plan.alloc[sb]; + if a + 1 >= self.table.row(sb).len() || blocked[ch][sb] || plan.energy[sb] <= 0.0 { + continue; + } + let ratio = noise(plan, sb, a) / mask[ch][sb].max(1e-20); + if ratio > 1e-3 && best.is_none_or(|(_, _, r)| ratio > r) { + best = Some((ch, sb, ratio)); + } + } + } + let Some((ch, sb, _)) = best else { break }; + let plan = &mut plans[ch]; + let c = cost(plan, sb, plan.alloc[sb]); + if c <= bits_left { + bits_left -= c; + plan.alloc[sb] += 1; + } else { + blocked[ch][sb] = true; + } + } + } + + fn write_frame(&mut self, sub: &[[[f32; SUBBANDS]; 36]], out: &mut Vec) { + let nch = self.cfg.channels; + // frame length: 144 bytes per kbit/s over the sampling rate, plus a slot when the + // remainder has added up to one + let rate = u64::from(self.cfg.sample_rate); + let exact = 144 * u64::from(self.cfg.bitrate_kbps) * 1000; + self.pad_acc += exact % rate; + let pad = self.pad_acc >= rate; + if pad { + self.pad_acc -= rate; + } + let frame_bytes = (exact / rate) as usize + usize::from(pad); + + let mut plans: Vec = sub.iter().map(|s| self.analyse(s)).collect(); + self.allocate(&mut plans, frame_bytes as i64 * 8 - 32); + + let mut w = BitWriter::new(); + w.write_bits(0xFFF, 12); + w.write_bits(u32::from(!self.lsf), 1); + w.write_bits(0b10, 2); // Layer II + w.write_bits(1, 1); // no CRC + w.write_bits(self.br_index, 4); + w.write_bits(self.sr_index, 2); + w.write_bits(u32::from(pad), 1); + w.write_bits(0, 1); // private + w.write_bits(if nch == 1 { 3 } else { 0 }, 2); // mono or stereo + w.write_bits(0, 2); // mode extension + w.write_bits(0, 1); // copyright + w.write_bits(1, 1); // original + w.write_bits(0, 2); // emphasis + for sb in 0..self.sblimit { + let nbal = self.table.row(sb).len().trailing_zeros(); + for plan in &plans { + w.write_bits(plan.alloc[sb] as u32, nbal); + } + } + for sb in 0..self.sblimit { + for plan in &plans { + if plan.alloc[sb] != 0 { + w.write_bits(plan.scfsi[sb], 2); + } + } + } + for sb in 0..self.sblimit { + for plan in &plans { + if plan.alloc[sb] == 0 { + continue; + } + let [a, b, c] = plan.sf[sb]; + match plan.scfsi[sb] { + 0 => [a, b, c].iter().for_each(|&s| w.write_bits(s, 6)), + 1 => [a, c].iter().for_each(|&s| w.write_bits(s, 6)), + 2 => w.write_bits(a, 6), + _ => [a, b].iter().for_each(|&s| w.write_bits(s, 6)), + } + } + } + for gr in 0..12 { + let part = gr / 4; + for sb in 0..self.sblimit { + for (ch, plan) in plans.iter().enumerate() { + let a = plan.alloc[sb]; + let steps = self.table.row(sb).get(a).copied().unwrap_or(0); + if steps == 0 { + continue; + } + let scale = tables::scalefactor(plan.sf[sb][part]); + let n = u32::from(steps); + let code = |k: usize| -> u32 { + let x = (sub[ch][3 * gr + k][sb] / scale).clamp(-1.0, 1.0); + // the reconstruction points are (2v + 1 - n) / n + ((x * n as f32 + n as f32 - 1.0) / 2.0).round().clamp(0.0, (n - 1) as f32) as u32 + }; + let v = [code(0), code(1), code(2)]; + match tables::grouping(steps) { + Some(bits) => w.write_bits(v[0] + n * v[1] + n * n * v[2], bits), + None => { + let nb = (n + 1).trailing_zeros(); + v.iter().for_each(|&x| w.write_bits(x, nb)); + } + } + } + } + } + let mut bytes = w.finish(); + bytes.resize(frame_bytes, 0); + out.extend_from_slice(&bytes); + } +} diff --git a/crates/audio/src/mpa/mod.rs b/crates/audio/src/mpa/mod.rs index f1d5b6378d..3db2041768 100644 --- a/crates/audio/src/mpa/mod.rs +++ b/crates/audio/src/mpa/mod.rs @@ -13,12 +13,30 @@ //! format (bit rate index 0, whose frame length is only known from the next sync word) and the //! CRC (read, not checked, like most decoders). //! +//! # Encoder (feature `mpa-encode`) +//! +//! [`encoder::Encoder`] writes Layer II streams, written from ISO/IEC 11172-3 (§2.4.1.6 bit stream +//! syntax, Annex B tables, Annex C encoding process) and ISO/IEC 13818-3. +//! +//! - Constant bit rate: 32-192 kbit/s mono and 64-384 kbit/s stereo at 32 / 44.1 / 48 kHz (the +//! rate and mode pairs of Table 3-B.2), 8-160 kbit/s at 16 / 22.05 / 24 kHz. Mono, or stereo with +//! each channel allocated on its own (no joint stereo, no dual channel). +//! - The polyphase analysis filterbank of Annex C.1.3 (shared with the Layer III encoder), a +//! scalefactor per 12 samples with the `scfsi` sharing of §2.4.1.6, and a bit allocation chosen by +//! spending the frame's bits where the quantization noise is furthest above a simple masking +//! estimate (the band energies spread over the neighbouring subbands, never below the threshold +//! in quiet). It is not one of the psychoacoustic models of Annex D. +//! - Output: frames only, no CRC, no ID3 tag. Frames written one after another are a playable +//! `.mp2` file. A decoder's output lags the input by [`encoder::DELAY`] samples. +//! //! From FilmCraft's `filmcraft-mpa` (storytold/filmcraft@8fda2f2). Public items keep their names, so //! FilmCraft can re-export this module. // Index loops follow the specification's pseudo-code (FilmCraft allows this lint workspace-wide). #![allow(clippy::needless_range_loop)] +#[cfg(feature = "mpa-encode")] +pub mod encoder; pub(crate) mod tables; use craft_media::bitstream::BitReader; diff --git a/crates/audio/src/vmd.rs b/crates/audio/src/vmd.rs new file mode 100644 index 0000000000..9e45b1a5c6 --- /dev/null +++ b/crates/audio/src/vmd.rs @@ -0,0 +1,192 @@ +//! Sierra VMD sound decoder (feature `vmd-decode`): the 8-bit PCM and 16-bit DPCM sound of VMD +//! (Video and Music Data) files, as in Sierra's and Coktel Vision's 1990s games. +//! +//! Clean-room, from the public format description at +//! ("Description of the Sierra Video and Music Data (VMD) Format", Mike Melanson and Vladimir +//! Gneushev). The description gives the coding but not the size of a block; the sizes here are +//! what ffmpeg was seen to expect of files made by this crate's tests: +//! +//! | Sound | Bytes in a block of `n` samples a channel | +//! |---|---| +//! | 8-bit | `n` for each channel, interleaved | +//! | 16-bit | 2 for each channel's first sample, then one code for each further sample, interleaved | +//! +//! The header's frame length is `n`, except in old-style stereo (audio flag 0x8000), where it +//! counts both channels. Old-style 16-bit stereo blocks begin with their first samples like the +//! others, which is what ffmpeg reads; the description says they carry the last block's samples +//! over instead. No such file was at hand to settle it. +//! +//! A [`Decoder`] is made from the 816-byte file header. [`Decoder::decode`] takes a sound +//! frame's flags (from its record in the file's table of contents; `craft_media::vmd` reads it) +//! and its data, and returns interleaved `i16` samples: one block of sound or silence, or, for a +//! frame with a silence mask (the first sound frame of a file), several. 8-bit samples are widened to 16 bits. Blocks do not depend +//! on each other, so decoding can start at any frame. +//! +//! Not decoded: IMA ADPCM sound (audio flag 0x10) and the external codecs of later Coktel Vision +//! files ([`Error::Unsupported`]). +//! +//! Input is treated as hostile: a block is at most 65 535 samples a channel and is decoded only +//! if all of it is there; a frame with a silence mask yields at most 32 blocks (the width of +//! the mask), any other frame as many whole blocks as its data holds. No `unsafe`, no panics. + +#![warn(missing_docs)] + +/// Bytes of the VMD file header a [`Decoder`] is made from. +pub const HEADER_LEN: usize = 816; + +/// Errors from decoding VMD sound. +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The header or a frame is not valid. + #[error("invalid VMD sound: {0}")] + Invalid(&'static str), + /// Valid, but not something this decoder handles. + #[error("unsupported VMD sound: {0}")] + Unsupported(&'static str), +} + +/// The steps of the 16-bit coding: a code's low seven bits pick one, its top bit the sign. +const STEPS: [u16; 128] = [ + 0, 8, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 272, 288, 304, 320, 336, 352, 368, 384, 400, 416, 432, 448, 464, + 480, 496, 512, 520, 528, 536, 544, 552, 560, 568, 576, 584, 592, 600, 608, 616, 624, 632, 640, 648, 656, 664, 672, 680, 688, 696, 704, 712, 720, + 728, 736, 744, 752, 760, 768, 776, 784, 792, 800, 808, 816, 824, 832, 840, 848, 856, 864, 872, 880, 888, 896, 904, 912, 920, 928, 936, 944, 952, + 960, 968, 976, 984, 992, 1000, 1008, 1016, 1024, 1088, 1152, 1216, 1280, 1344, 1408, 1472, 1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984, 2048, + 2304, 2560, 2816, 3072, 3328, 3584, 3840, 4096, 5120, 6144, 7168, 8192, 12288, 16384, +]; + +/// A VMD sound decoder. +#[derive(Clone, Debug)] +pub struct Decoder { + sample_rate: u32, + channels: usize, + sixteen_bit: bool, + /// Samples of one block, all channels counted. + block_total: usize, +} + +impl Decoder { + /// A decoder for the file whose header is `header` (its first 816 bytes; more are ignored). + pub fn new(header: &[u8]) -> Result { + let h = header.get(..HEADER_LEN).ok_or(Error::Invalid("the header is shorter than 816 bytes"))?; + let u16_at = |at: usize| u16::from_le_bytes([h[at], h[at + 1]]); + if usize::from(u16_at(0)) != HEADER_LEN - 2 { + return Err(Error::Invalid("not a VMD header")); + } + if u16_at(16) & 0x1000 == 0 || u16_at(804) == 0 { + return Err(Error::Invalid("the file has no sound")); + } + let flags = u16_at(810); + if flags & 0x10 != 0 { + return Err(Error::Unsupported("IMA ADPCM sound")); + } + let length = u16_at(806) as i16; + let frame_length = usize::from(length.unsigned_abs()); + // old-style stereo counts both channels, new-style one + let (channels, block_total) = match flags { + f if f & 0x8000 != 0 => (2, frame_length & !1), + f if f & 0x200 != 0 => (2, frame_length * 2), + _ => (1, frame_length), + }; + Ok(Decoder { sample_rate: u32::from(u16_at(804)), channels, sixteen_bit: length < 0, block_total }) + } + + /// Samples a second. + pub fn sample_rate(&self) -> u32 { + self.sample_rate + } + + /// 1 or 2. + pub fn channels(&self) -> usize { + self.channels + } + + /// Bits a sample in the file: 8 (PCM) or 16 (DPCM). The decoder returns `i16` for both. + pub fn bits(&self) -> u8 { + if self.sixteen_bit { 16 } else { 8 } + } + + /// Samples of one block for each channel. + pub fn block_samples(&self) -> usize { + self.block_total / self.channels + } + + /// Bytes of one block of sound in a frame. + pub fn block_bytes(&self) -> usize { + if self.sixteen_bit { (self.block_total + self.channels).min(self.block_total * 2) } else { self.block_total } + } + + /// Decode one sound frame: `flags` is the frame's flag byte from its record, `data` the + /// frame data. Returns interleaved samples, [`Decoder::block_samples`] for each channel and + /// block. + /// + /// - flags 1: one block of sound (every whole block, if the frame holds more); + /// - flags 2: a 32-bit mask, then blocks of sound: the sound a player loads ahead at the + /// start of a file. From bit 0 up, a set bit is a block of silence and a clear bit the next + /// block of data, while there is one. ffmpeg plays the silent blocks first instead; + /// the order here is the description's; + /// - flags 3: one block of silence. + /// + /// An empty frame gives no samples. With flags 1, a first block cut short is an error. A part + /// of a block after the last whole one is left out, with flags 1 and 2. + /// + /// The sound of a file is its sound frames decoded in file order and played end to end from + /// the first picture on: see `craft_media::vmd` for how that lines up with the pictures. + pub fn decode(&self, flags: u8, data: &[u8]) -> Result, Error> { + let mut out = Vec::new(); + match flags { + 3 => out.resize(self.block_total, 0), + 1 | 2 if data.is_empty() => {} + 1 => { + // one block; a frame that holds more whole blocks plays them all, as in ffmpeg + self.block(data, &mut out)?; + let size = self.block_bytes(); + if size > 0 { + for more in data[size..].chunks_exact(size) { + self.block(more, &mut out)?; + } + } + } + 2 => { + let (mask, mut data) = data.split_first_chunk::<4>().ok_or(Error::Invalid("the silence mask is cut short"))?; + let mask = u32::from_le_bytes(*mask); + // bit by bit: a set bit is a silent block, a clear one takes the next block of data + // while there is one. The header's buffer count is not needed: game files hold + // one block fewer than it says. + for i in 0..32 { + if mask >> i & 1 == 1 { + out.resize(out.len() + self.block_total, 0); + } else if !data.is_empty() && data.len() >= self.block_bytes() { + self.block(data, &mut out)?; + data = &data[self.block_bytes()..]; + } + } + } + _ => return Err(Error::Unsupported("unknown sound frame flags")), + } + Ok(out) + } + + /// Decode the block at the start of `data` onto `out`. + fn block(&self, data: &[u8], out: &mut Vec) -> Result<(), Error> { + let data = data.get(..self.block_bytes()).ok_or(Error::Invalid("a block of sound is cut short"))?; + if !self.sixteen_bit { + out.extend(data.iter().map(|&b| (i16::from(b) - 0x80) << 8)); + return Ok(()); + } + // each channel's first sample as it is, then a signed step for each further sample + let mut last = [0i32; 2]; + let Some((first, codes)) = data.split_at_checked(self.channels * 2) else { return Ok(()) }; + for (ch, s) in first.as_chunks::<2>().0.iter().enumerate() { + last[ch] = i32::from(i16::from_le_bytes(*s)); + out.push(last[ch] as i16); + } + for (i, &code) in codes.iter().enumerate() { + let ch = i % self.channels; + let step = i32::from(STEPS[usize::from(code & 0x7F)]); + last[ch] = (if code & 0x80 != 0 { last[ch] - step } else { last[ch] + step }).clamp(-32768, 32767); + out.push(last[ch] as i16); + } + Ok(()) + } +} diff --git a/crates/audio/src/vorbis/decoder/bits.rs b/crates/audio/src/vorbis/decoder/bits.rs new file mode 100644 index 0000000000..6735d3d3d2 --- /dev/null +++ b/crates/audio/src/vorbis/decoder/bits.rs @@ -0,0 +1,96 @@ +//! The Vorbis bit reader (Vorbis I §2): bits are taken least significant first from each byte. + +/// Reading past the end of the packet ("end-of-packet", §2.1.8). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(super) struct Eop; + +pub(super) struct BitReader<'a> { + data: &'a [u8], + /// The position of the next bit. + pos: usize, +} + +impl<'a> BitReader<'a> { + pub(super) fn new(data: &'a [u8]) -> Self { + Self { data, pos: 0 } + } + + /// The bits not yet read. + pub(super) fn remaining(&self) -> usize { + (self.data.len() * 8).saturating_sub(self.pos) + } + + /// Read `n` (at most 32) bits as an unsigned integer. A read that does not fit takes nothing. + pub(super) fn read(&mut self, n: u32) -> Result { + if n == 0 { + return Ok(0); + } + if (n as usize) > self.remaining() { + self.pos = self.data.len() * 8; + return Err(Eop); + } + let mut value = 0u64; + let mut got = 0u32; + while got < n { + let byte = u64::from(self.data[self.pos / 8]); + let shift = (self.pos % 8) as u32; + let take = (8 - shift).min(n - got); + value |= ((byte >> shift) & ((1 << take) - 1)) << got; + got += take; + self.pos += take as usize; + } + Ok(value as u32) + } + + pub(super) fn bit(&mut self) -> Result { + Ok(self.read(1)? == 1) + } +} + +/// `ilog(x)` (§9.2.1): the position of the highest set bit, 0 for values below 1. +pub(super) fn ilog(x: i64) -> u32 { + if x <= 0 { 0 } else { 64 - (x as u64).leading_zeros() } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_least_significant_bit_first() { + // Fields come out of each byte from its least significant bit. + let mut r = BitReader::new(&[0xFC, 0x08]); + assert_eq!(r.read(4), Ok(0xC)); + assert_eq!(r.read(4), Ok(0xF)); + assert_eq!(r.read(1), Ok(0)); + assert_eq!(r.read(3), Ok(4)); + assert_eq!(r.read(4), Ok(0)); + assert_eq!(r.read(1), Err(Eop)); + } + + #[test] + fn wide_reads_span_bytes_and_zero_bits_are_free() { + let mut r = BitReader::new(&[0x01, 0x02, 0x03, 0x04, 0x05]); + assert_eq!(r.read(0), Ok(0)); + assert_eq!(r.read(32), Ok(0x0403_0201)); + assert_eq!(r.read(8), Ok(5)); + assert_eq!(r.read(0), Ok(0)); + assert_eq!(r.read(1), Err(Eop)); + assert_eq!(r.remaining(), 0); + } + + #[test] + fn a_failed_read_consumes_the_rest() { + let mut r = BitReader::new(&[0xFF]); + assert_eq!(r.read(3), Ok(7)); + assert_eq!(r.read(6), Err(Eop)); + assert_eq!(r.read(1), Err(Eop)); + } + + #[test] + fn ilog_matches_the_specification_examples() { + for (x, want) in [(0, 0), (1, 1), (2, 2), (3, 2), (4, 3), (7, 3), (-5, 0), (255, 8), (256, 9)] { + assert_eq!(ilog(x), want, "ilog({x})"); + } + } +} diff --git a/crates/audio/src/vorbis/decoder/codebook.rs b/crates/audio/src/vorbis/decoder/codebook.rs new file mode 100644 index 0000000000..9d083c9e47 --- /dev/null +++ b/crates/audio/src/vorbis/decoder/codebook.rs @@ -0,0 +1,409 @@ +//! Codebooks (Vorbis I §3): a Huffman code over entry numbers and an optional table of vectors. + +use super::Error; +use super::bits::{BitReader, Eop, ilog}; + +const NONE: u32 = u32::MAX; +const LEAF: u32 = 1 << 31; +/// Entries a codebook may declare. +const MAX_ENTRIES: u32 = 1 << 20; +/// Nodes of one codeword tree. +const MAX_NODES: usize = 1 << 22; + +#[derive(Clone, Copy)] +struct Node { + child: [u32; 2], + full: bool, +} + +pub(super) struct Codebook { + pub(super) dimensions: usize, + pub(super) entries: usize, + tree: Vec, + /// The only used entry of a single-entry book. + single: Option, + /// `entries * dimensions` values when the book has a lookup table. + vectors: Vec, +} + +/// `float32_unpack` (§9.2.2). +fn float32_unpack(x: u32) -> f64 { + let mantissa = f64::from(x & 0x001F_FFFF); + let exponent = ((x & 0x7FE0_0000) >> 21) as i32; + let m = if x & 0x8000_0000 != 0 { -mantissa } else { mantissa }; + m * 2f64.powi(exponent - 788) +} + +/// `lookup1_values` (§9.2.3): the greatest `v` with `v^dimensions <= entries`. +fn lookup1_values(entries: u32, dimensions: u32) -> u32 { + let mut v = 1u32; + loop { + let next = u128::from(v) + 1; + let mut p = 1u128; + for _ in 0..dimensions { + p *= next; + if p > u128::from(entries) { + return v; + } + } + v += 1; + } +} + +fn insert(tree: &mut Vec, at: usize, depth: u32, len: u32, entry: u32) -> bool { + for b in 0..2 { + let child = tree[at].child[b]; + if depth + 1 == len { + if child == NONE { + tree[at].child[b] = LEAF | entry; + refresh(tree, at); + return true; + } + continue; + } + if child == NONE { + if tree.len() >= MAX_NODES { + return false; + } + tree.push(Node { child: [NONE; 2], full: false }); + let new = tree.len() - 1; + tree[at].child[b] = new as u32; + if insert(tree, new, depth + 1, len, entry) { + refresh(tree, at); + return true; + } + return false; + } + if child & LEAF != 0 || tree[child as usize].full { + continue; + } + if insert(tree, child as usize, depth + 1, len, entry) { + refresh(tree, at); + return true; + } + } + false +} + +fn refresh(tree: &mut [Node], at: usize) { + let done = |c: u32, t: &[Node]| c != NONE && (c & LEAF != 0 || t[c as usize].full); + let full = done(tree[at].child[0], tree) && done(tree[at].child[1], tree); + tree[at].full = full; +} + +impl Codebook { + /// Read one codebook from a setup header. `budget` is the bytes the setup's codebooks may + /// still take in all. + pub(super) fn read(r: &mut BitReader<'_>, budget: &mut usize) -> Result { + let bad = |m: &'static str| Error::Setup(m); + let eop = |_: Eop| Error::Setup("setup header ends inside a codebook"); + if r.read(24).map_err(eop)? != 0x0056_4342 { + return Err(bad("codebook sync pattern missing")); + } + let dimensions = r.read(16).map_err(eop)?; + let entries = r.read(24).map_err(eop)?; + if dimensions == 0 || entries == 0 || entries > MAX_ENTRIES { + return Err(bad("codebook with no dimensions, no entries or too many")); + } + let mut lengths: Vec = Vec::with_capacity(entries as usize); + if r.bit().map_err(eop)? { + // Ordered: the number of codewords of each length. + let mut current = 0u32; + let mut len = r.read(5).map_err(eop)? + 1; + while current < entries { + let number = r.read(ilog(i64::from(entries - current))).map_err(eop)?; + if number > entries - current || len > 32 { + return Err(bad("ordered codebook lengths overrun the entries")); + } + lengths.extend(std::iter::repeat_n(len as u8, number as usize)); + current += number; + len += 1; + } + } else { + let sparse = r.bit().map_err(eop)?; + for _ in 0..entries { + if sparse && !r.bit().map_err(eop)? { + lengths.push(0); + } else { + lengths.push((r.read(5).map_err(eop)? + 1) as u8); + } + } + } + + // The codeword tree. + let used = lengths.iter().filter(|&&l| l != 0).count(); + let mut tree = vec![Node { child: [NONE; 2], full: false }]; + let mut single = None; + if used == 1 { + let entry = lengths.iter().position(|&l| l != 0).unwrap_or(0); + if lengths[entry] != 1 { + return Err(bad("a codebook with one used entry must give it length 1")); + } + single = Some(entry as u32); + } else if used > 1 { + for (entry, &len) in lengths.iter().enumerate() { + if len != 0 && !insert(&mut tree, 0, 0, u32::from(len), entry as u32) { + return Err(bad("overspecified codebook")); + } + } + if !tree[0].full { + return Err(bad("underspecified codebook")); + } + } + let tree_bytes = tree.len() * std::mem::size_of::(); + if tree_bytes > *budget { + return Err(Error::Limit("the codebooks are too large")); + } + *budget -= tree_bytes; + + // The lookup table. + let lookup_type = r.read(4).map_err(eop)?; + let mut vectors = Vec::new(); + if lookup_type > 2 { + return Err(bad("reserved codebook lookup type")); + } + if lookup_type != 0 { + let minimum = float32_unpack(r.read(32).map_err(eop)?); + let delta = float32_unpack(r.read(32).map_err(eop)?); + let value_bits = r.read(4).map_err(eop)? + 1; + let sequence = r.bit().map_err(eop)?; + // Check before multiplying or reading multiplicands: the product can overflow + // usize on 32-bit targets, and a type-2 table otherwise allocates before admission. + let total = (entries as usize) + .checked_mul(dimensions as usize) + .filter(|&n| n <= *budget / std::mem::size_of::()) + .ok_or(Error::Limit("the codebooks' vector tables are too large"))?; + let values = if lookup_type == 1 { lookup1_values(entries, dimensions) as usize } else { total }; + if !minimum.is_finite() || !delta.is_finite() || values > r.remaining() / value_bits as usize { + return Err(bad("codebook lookup table is damaged or truncated")); + } + let multiplicands: Vec = (0..values).map(|_| r.read(value_bits).unwrap_or(0)).collect(); + *budget -= total * std::mem::size_of::(); + vectors = vec![0f32; total]; + for e in 0..entries as usize { + let out = &mut vectors[e * dimensions as usize..(e + 1) * dimensions as usize]; + let mut last = 0f64; + let mut divisor = 1usize; + for (i, slot) in out.iter_mut().enumerate() { + let m = if lookup_type == 1 { + let at = (e / divisor) % values; + divisor = divisor.saturating_mul(values); + multiplicands[at] + } else { + multiplicands[e * dimensions as usize + i] + }; + let v = f64::from(m) * delta + minimum + last; + if sequence { + last = v; + } + *slot = v as f32; + } + } + } + Ok(Self { dimensions: dimensions as usize, entries: entries as usize, tree, single, vectors }) + } + + /// Whether the book has a lookup table. + pub(super) fn has_vectors(&self) -> bool { + !self.vectors.is_empty() + } + + /// Read a codeword; the entry number (scalar context). + pub(super) fn scalar(&self, r: &mut BitReader<'_>) -> Result { + if let Some(e) = self.single { + r.read(1)?; + return Ok(e); + } + let mut at = 0usize; + loop { + let c = self.tree[at].child[usize::from(r.bit()?)]; + if c == NONE { + return Err(Eop); + } + if c & LEAF != 0 { + return Ok(c & !LEAF); + } + at = c as usize; + } + } + + /// Read a codeword and return its vector (VQ context); `None` for a book without a table. + pub(super) fn vector(&self, r: &mut BitReader<'_>) -> Result, Eop> { + let e = self.scalar(r)? as usize; + Ok(self.vectors.get(e * self.dimensions..(e + 1) * self.dimensions)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Pack fields least significant bit first. + struct W(Vec, usize); + impl W { + fn put(&mut self, v: u64, n: u32) { + for i in 0..n { + if self.1.is_multiple_of(8) { + self.0.push(0); + } + let last = self.0.len() - 1; + self.0[last] |= (((v >> i) & 1) as u8) << (self.1 % 8); + self.1 += 1; + } + } + } + + fn book_bytes(lengths: &[u32], lookup: Option<(u32, u32, &[u32])>) -> Vec { + let mut w = W(Vec::new(), 0); + w.put(0x56_4342, 24); + w.put(1, 16); + w.put(lengths.len() as u64, 24); + w.put(0, 1); // not ordered + w.put(0, 1); // not sparse + for &l in lengths { + w.put(u64::from(l - 1), 5); + } + match lookup { + None => w.put(0, 4), + Some((ty, bits, mult)) => { + w.put(u64::from(ty), 4); + w.put(0, 32); // minimum: 0.0 + w.put(0, 32); + w.put(u64::from(bits - 1), 4); + w.put(0, 1); + for &m in mult { + w.put(u64::from(m), bits); + } + } + } + w.0 + } + + #[test] + fn the_specification_example_assigns_its_codewords() { + // §3.2.1: lengths 2 4 4 4 4 2 3 3 -> 00, 0100, 0101, 0110, 0111, 10, 110, 111. + let bytes = book_bytes(&[2, 4, 4, 4, 4, 2, 3, 3], None); + let mut budget = 1 << 26; + let book = Codebook::read(&mut BitReader::new(&bytes), &mut budget).expect("book"); + let code = |bits: &[u8]| { + let mut w = W(Vec::new(), 0); + for &b in bits { + w.put(u64::from(b), 1); + } + let mut r = BitReader::new(&w.0); + book.scalar(&mut r).expect("entry") + }; + assert_eq!(code(&[0, 0]), 0); + assert_eq!(code(&[0, 1, 0, 0]), 1); + assert_eq!(code(&[0, 1, 1, 1]), 4); + assert_eq!(code(&[1, 0]), 5); + assert_eq!(code(&[1, 1, 0]), 6); + assert_eq!(code(&[1, 1, 1]), 7); + } + + #[test] + fn over_and_underspecified_books_are_errors() { + let mut budget = 1 << 26; + let over = book_bytes(&[1, 1, 1], None); + assert!(Codebook::read(&mut BitReader::new(&over), &mut budget).is_err()); + let under = book_bytes(&[2, 2, 2], None); + assert!(Codebook::read(&mut BitReader::new(&under), &mut budget).is_err()); + } + + #[test] + fn a_single_entry_book_sinks_one_bit_of_either_value() { + let mut budget = 1 << 26; + let bytes = book_bytes(&[1], None); + let book = Codebook::read(&mut BitReader::new(&bytes), &mut budget).expect("book"); + for byte in [0u8, 1] { + let data = [byte]; + let mut r = BitReader::new(&data); + assert_eq!(book.scalar(&mut r), Ok(0)); + assert_eq!(r.remaining(), 7); + } + let bad = book_bytes(&[2], None); + assert!(Codebook::read(&mut BitReader::new(&bad), &mut budget).is_err()); + } + + #[test] + fn lookup_type_one_builds_a_lattice() { + // 4 entries, 2 dimensions, 2 values per dimension: minimum 0 and delta 1 in the packed + // float format are written below by hand (mantissa 1, exponent 788 -> 1.0). + let mut w = W(Vec::new(), 0); + w.put(0x56_4342, 24); + w.put(2, 16); + w.put(4, 24); + w.put(0, 1); + w.put(0, 1); + for _ in 0..4 { + w.put(1, 5); // length 2 each + } + w.put(1, 4); // lookup type 1 + w.put(0, 32); // minimum 0 + w.put((788u64 << 21) | 1, 32); // delta 1.0 + w.put(0, 4); // 1 bit per value + w.put(0, 1); + w.put(0b10, 2); // multiplicands 0, 1 + let mut budget = 1 << 26; + let book = Codebook::read(&mut BitReader::new(&w.0), &mut budget).expect("book"); + assert_eq!(lookup1_values(4, 2), 2); + // Entry e is (m[e % 2], m[(e / 2) % 2]). + assert_eq!(book.vectors, vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]); + } + + #[test] + fn vector_budget_is_checked_before_reading_lookup_values() { + // A valid single-entry tree, but two values exceed the remaining budget. + // No multiplicands follow: admission must reject it before reading them. + for lookup in [1, 2] { + let mut w = W(Vec::new(), 0); + w.put(0x56_4342, 24); + w.put(2, 16); + w.put(1, 24); + w.put(0, 1); + w.put(0, 1); + w.put(0, 5); + w.put(lookup, 4); + w.put(0, 32); + w.put(0, 32); + w.put(15, 4); + w.put(0, 1); + let mut budget = std::mem::size_of::() + 4; + assert!(matches!(Codebook::read(&mut BitReader::new(&w.0), &mut budget), Err(Error::Limit(_)))); + } + } + + #[test] + fn vector_size_that_overflows_32_bits_is_refused() { + for lookup in [1, 2] { + let mut w = W(Vec::new(), 0); + w.put(0x56_4342, 24); + w.put(65535, 16); + w.put(65538, 24); // product is above u32::MAX + w.put(0, 1); // unordered + w.put(1, 1); // sparse: only one entry is used + w.put(1, 1); + w.put(0, 5); // length 1 + for _ in 1..65538 { + w.put(0, 1); + } + w.put(lookup, 4); + w.put(0, 32); + w.put(0, 32); + w.put(0, 4); + w.put(0, 1); + w.put(0, 1); // lookup type 1's single multiplicand + let mut budget = 256 << 20; + assert!(matches!(Codebook::read(&mut BitReader::new(&w.0), &mut budget), Err(Error::Limit(_)))); + } + } + + #[test] + fn helper_functions() { + assert_eq!(float32_unpack((788u32 << 21) | 5), 5.0); + assert_eq!(float32_unpack(0x8000_0000 | (789u32 << 21) | 3), -6.0); + for (entries, dims, want) in [(8, 3, 2), (9, 2, 3), (1, 5, 1), (10, 1, 10), (1000, 3, 10), (4_000_000, 2, 2000)] { + assert_eq!(lookup1_values(entries, dims), want, "{entries} entries, {dims} dims"); + } + } +} diff --git a/crates/audio/src/vorbis/decoder/floor.rs b/crates/audio/src/vorbis/decoder/floor.rs new file mode 100644 index 0000000000..bc6ce4e365 --- /dev/null +++ b/crates/audio/src/vorbis/decoder/floor.rs @@ -0,0 +1,385 @@ +//! Floors (Vorbis I §6 and §7): the spectral envelope each channel's residue is multiplied by. + +use super::Error; +use super::bits::{BitReader, Eop, ilog}; +use super::codebook::Codebook; +use super::tables::INVERSE_DB; + +/// Why a floor could not be decoded from a packet. +pub(super) enum Fail { + /// The packet ended (a nominal event: the channel is silent). + Eop, + /// The packet names a codebook that does not exist: it cannot be decoded (§6.2.2). + Invalid, +} + +impl From for Fail { + fn from(_: Eop) -> Self { + Fail::Eop + } +} + +/// What a decoded floor gives to synthesis. +pub(super) enum FloorData { + One { y: Vec, step2: Vec }, + Zero { amplitude: u32, coefficients: Vec }, +} + +pub(super) struct Floor0 { + order: usize, + rate: u32, + bark_map_size: u32, + amplitude_bits: u32, + amplitude_offset: u32, + books: Vec, +} + +pub(super) struct Floor1 { + partition_class: Vec, + class_dimensions: Vec, + class_subclasses: Vec, + class_masterbook: Vec, + subclass_books: Vec>, + multiplier: i32, + range: i32, + xlist: Vec, + /// `(low, high)` neighbour positions of each X from index 2 on. + neighbours: Vec<(usize, usize)>, + /// Indices of `xlist` in ascending X order. + sorted: Vec, +} + +pub(super) enum Floor { + Zero(Floor0), + One(Floor1), +} + +fn eop(_: Eop) -> Error { + Error::Setup("setup header ends inside a floor") +} + +impl Floor { + pub(super) fn read(floor_type: u32, r: &mut BitReader<'_>, books: &[Codebook]) -> Result { + match floor_type { + 0 => { + let order = r.read(8).map_err(eop)? as usize; + let rate = r.read(16).map_err(eop)?; + let bark_map_size = r.read(16).map_err(eop)?; + let amplitude_bits = r.read(6).map_err(eop)?; + let amplitude_offset = r.read(8).map_err(eop)?; + let count = r.read(4).map_err(eop)? as usize + 1; + let mut list = Vec::with_capacity(count); + for _ in 0..count { + let b = r.read(8).map_err(eop)? as usize; + match books.get(b) { + Some(book) if book.has_vectors() => list.push(b), + _ => return Err(Error::Setup("floor 0 names a missing codebook or one without vectors")), + } + } + if order == 0 || rate == 0 || bark_map_size == 0 || amplitude_bits > 32 { + return Err(Error::Setup("floor 0 with a zero order, rate or map size, or more than 32 amplitude bits")); + } + Ok(Self::Zero(Floor0 { order, rate, bark_map_size, amplitude_bits, amplitude_offset, books: list })) + } + 1 => { + let partitions = r.read(5).map_err(eop)? as usize; + let mut partition_class = Vec::with_capacity(partitions); + for _ in 0..partitions { + partition_class.push(r.read(4).map_err(eop)? as usize); + } + let classes = partition_class.iter().max().map_or(0, |m| m + 1); + let (mut class_dimensions, mut class_subclasses, mut class_masterbook, mut subclass_books) = (vec![], vec![], vec![], vec![]); + for _ in 0..classes { + class_dimensions.push(r.read(3).map_err(eop)? as usize + 1); + let subs = r.read(2).map_err(eop)?; + class_subclasses.push(subs); + let master = if subs != 0 { r.read(8).map_err(eop)? as usize } else { 0 }; + if subs != 0 && master >= books.len() { + return Err(Error::Setup("floor 1 master book out of range")); + } + class_masterbook.push(master); + let mut list = Vec::new(); + for _ in 0..(1u32 << subs) { + let b = r.read(8).map_err(eop)? as i32 - 1; + if b >= books.len() as i32 { + return Err(Error::Setup("floor 1 subclass book out of range")); + } + list.push(b); + } + subclass_books.push(list); + } + let multiplier = r.read(2).map_err(eop)? as i32 + 1; + let rangebits = r.read(4).map_err(eop)?; + let mut xlist = vec![0, 1i32 << rangebits]; + for &c in &partition_class { + for _ in 0..class_dimensions[c] { + xlist.push(r.read(rangebits).map_err(eop)? as i32); + } + } + if xlist.len() > 65 { + return Err(Error::Setup("floor 1 has more than 65 points")); + } + let mut seen = xlist.clone(); + seen.sort_unstable(); + if seen.windows(2).any(|w| w[0] == w[1]) { + return Err(Error::Setup("floor 1 repeats an X position")); + } + // Low and high neighbours (§9.2.4, §9.2.5) of each point, among those before it. + let mut neighbours = Vec::new(); + for i in 2..xlist.len() { + let (mut lo, mut hi) = (0usize, 1usize); + for j in 0..i { + if xlist[j] < xlist[i] && xlist[j] > xlist[lo] { + lo = j; + } + if xlist[j] > xlist[i] && xlist[j] < xlist[hi] { + hi = j; + } + } + neighbours.push((lo, hi)); + } + let mut sorted: Vec = (0..xlist.len()).collect(); + sorted.sort_by_key(|&i| xlist[i]); + let range = [256, 128, 86, 64][multiplier as usize - 1]; + Ok(Self::One(Floor1 { + partition_class, + class_dimensions, + class_subclasses, + class_masterbook, + subclass_books, + multiplier, + range, + xlist, + neighbours, + sorted, + })) + } + _ => Err(Error::Setup("unknown floor type")), + } + } + + /// Decode one channel's floor from a packet: `None` when the channel is unused this block. + pub(super) fn decode(&self, r: &mut BitReader<'_>, books: &[Codebook]) -> Result, Fail> { + match self { + Self::One(f) => Ok(f.decode(r, books)?), + Self::Zero(f) => f.decode(r, books), + } + } + + /// The floor curve over `n` bins (`n` is half the block size). + pub(super) fn curve(&self, data: &FloorData, n: usize) -> Vec { + match (self, data) { + (Self::One(f), FloorData::One { y, step2 }) => f.curve(y, step2, n), + (Self::Zero(f), FloorData::Zero { amplitude, coefficients }) => f.curve(*amplitude, coefficients, n), + _ => vec![0.0; n], + } + } +} + +/// `render_point` (§9.2.6). +fn render_point(x0: i32, y0: i32, x1: i32, y1: i32, x: i32) -> i32 { + let dy = i64::from(y1 - y0); + let adx = i64::from(x1 - x0); + let err = dy.abs() * i64::from(x - x0); + let off = (err / adx) as i32; + if dy < 0 { y0 - off } else { y0 + off } +} + +/// `render_line` (§9.2.7), writing the bins that fall inside `v`. +fn render_line(x0: i32, y0: i32, x1: i32, y1: i32, v: &mut [i32]) { + let dy = y1 - y0; + let adx = x1 - x0; + let base = dy / adx; + let ady = dy.abs() - base.abs() * adx; + let sy = if dy < 0 { base - 1 } else { base + 1 }; + let (mut y, mut err) = (y0, 0); + if let Some(slot) = usize::try_from(x0).ok().and_then(|x| v.get_mut(x)) { + *slot = y; + } + for x in x0 + 1..x1 { + err += ady; + if err >= adx { + err -= adx; + y += sy; + } else { + y += base; + } + match usize::try_from(x).ok().and_then(|x| v.get_mut(x)) { + Some(slot) => *slot = y, + None => break, + } + } +} + +impl Floor1 { + fn decode(&self, r: &mut BitReader<'_>, books: &[Codebook]) -> Result, Eop> { + if !r.bit()? { + return Ok(None); + } + let bits = ilog(i64::from(self.range) - 1); + let mut y = vec![0i32; self.xlist.len()]; + y[0] = r.read(bits)? as i32; + y[1] = r.read(bits)? as i32; + let mut offset = 2usize; + for &class in &self.partition_class { + let cdim = self.class_dimensions[class]; + let cbits = self.class_subclasses[class]; + let csub = (1u32 << cbits) - 1; + let mut cval = 0u32; + if cbits > 0 { + cval = books[self.class_masterbook[class]].scalar(r)?; + } + for j in 0..cdim { + let book = self.subclass_books[class][(cval & csub) as usize]; + cval >>= cbits; + y[offset + j] = if book >= 0 { books[book as usize].scalar(r)? as i32 } else { 0 }; + } + offset += cdim; + } + + // Step 1: amplitude synthesis (§7.2.4). + let mut step2 = vec![false; self.xlist.len()]; + step2[0] = true; + step2[1] = true; + let mut fin = y.clone(); + for i in 2..self.xlist.len() { + let (lo, hi) = self.neighbours[i - 2]; + let predicted = render_point(self.xlist[lo], fin[lo], self.xlist[hi], fin[hi], self.xlist[i]); + let val = y[i]; + let highroom = self.range - predicted; + let lowroom = predicted; + let room = if highroom < lowroom { highroom * 2 } else { lowroom * 2 }; + if val != 0 { + step2[lo] = true; + step2[hi] = true; + step2[i] = true; + fin[i] = if val >= room { + if highroom > lowroom { val - lowroom + predicted } else { predicted - val + highroom - 1 } + } else if val % 2 == 1 { + predicted - (val + 1) / 2 + } else { + predicted + val / 2 + }; + } else { + fin[i] = predicted; + } + // Valid setups never leave the range; a damaged one must not carry out-of-range values on. + fin[i] = fin[i].clamp(0, self.range - 1); + } + Ok(Some(FloorData::One { y: fin, step2 })) + } + + fn curve(&self, fin: &[i32], step2: &[bool], n: usize) -> Vec { + let mut floor = vec![0i32; n]; + let (mut hx, mut hy) = (0i32, 0i32); + let (mut lx, mut ly) = (0i32, fin[self.sorted[0]] * self.multiplier); + for &i in &self.sorted[1..] { + if step2[i] { + hy = fin[i] * self.multiplier; + hx = self.xlist[i]; + render_line(lx, ly, hx, hy, &mut floor); + lx = hx; + ly = hy; + } + } + if (hx as usize) < n { + render_line(hx, hy, n as i32, hy, &mut floor); + } + floor.iter().map(|&v| INVERSE_DB[v.clamp(0, 255) as usize]).collect() + } +} + +impl Floor0 { + fn decode(&self, r: &mut BitReader<'_>, books: &[Codebook]) -> Result, Fail> { + let amplitude = r.read(self.amplitude_bits)?; + if amplitude == 0 { + return Ok(None); + } + let book = r.read(ilog(self.books.len() as i64))? as usize; + let Some(&book) = self.books.get(book) else { return Err(Fail::Invalid) }; + let book = &books[book]; + let mut coefficients: Vec = Vec::with_capacity(self.order + book.dimensions); + let mut last = 0f32; + while coefficients.len() < self.order { + let Some(v) = book.vector(r)? else { return Err(Fail::Invalid) }; + coefficients.extend(v.iter().map(|&x| x + last)); + last = coefficients[coefficients.len() - 1]; + } + Ok(Some(FloorData::Zero { amplitude, coefficients })) + } + + fn curve(&self, amplitude: u32, coefficients: &[f32], n: usize) -> Vec { + let bark = |x: f64| 13.1 * (0.00074 * x).atan() + 2.24 * (0.000_000_018_5 * x * x).atan() + 0.0001 * x; + let scale = f64::from(self.bark_map_size) / bark(0.5 * f64::from(self.rate)); + let map: Vec = (0..n) + .map(|i| { + let foobar = (bark(f64::from(self.rate) * i as f64 / (2.0 * n as f64)) * scale).floor() as i64; + foobar.min(i64::from(self.bark_map_size) - 1) + }) + .collect(); + let cos_c: Vec = coefficients.iter().take(self.order).map(|&c| f64::from(c).cos()).collect(); + let amp = f64::from(amplitude); + let offset = f64::from(self.amplitude_offset); + let top = (2f64).powi(self.amplitude_bits as i32) - 1.0; + let mut out = vec![0f32; n]; + let mut i = 0usize; + while i < n { + let omega = std::f64::consts::PI * map[i] as f64 / f64::from(self.bark_map_size); + let cw = omega.cos(); + let (p, q); + if self.order % 2 == 1 { + let mut pp = 1.0 - cw * cw; + for j in 0..(self.order - 1) / 2 { + let d = cos_c[2 * j + 1] - cw; + pp *= 4.0 * d * d; + } + let mut qq = 0.25; + for j in 0..=(self.order - 1) / 2 { + let d = cos_c[2 * j] - cw; + qq *= 4.0 * d * d; + } + p = pp; + q = qq; + } else { + let mut pp = (1.0 - cw) / 2.0; + let mut qq = (1.0 + cw) / 2.0; + for j in 0..self.order / 2 { + let d1 = cos_c[2 * j + 1] - cw; + pp *= 4.0 * d1 * d1; + let d0 = cos_c[2 * j] - cw; + qq *= 4.0 * d0 * d0; + } + p = pp; + q = qq; + } + let linear = (0.115_129_25 * (amp * offset / (top * (p + q).sqrt()) - offset)).exp() as f32; + let key = map[i]; + while i < n && map[i] == key { + out[i] = linear; + i += 1; + } + } + out + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn render_line_matches_a_bresenham_walk() { + let mut v = vec![0i32; 16]; + render_line(0, 0, 8, 4, &mut v); + assert_eq!(&v[..8], &[0, 0, 1, 1, 2, 2, 3, 3]); + let mut v = vec![0i32; 16]; + render_line(2, 10, 6, 2, &mut v); + assert_eq!(&v[2..6], &[10, 8, 6, 4]); + // Lines running past the vector stop at its end. + let mut v = vec![0i32; 4]; + render_line(0, 0, 100, 100, &mut v); + assert_eq!(v, vec![0, 1, 2, 3]); + assert_eq!(render_point(0, 0, 10, 10, 5), 5); + assert_eq!(render_point(0, 10, 10, 0, 5), 5); + } +} diff --git a/crates/audio/src/vorbis/decoder/mdct.rs b/crates/audio/src/vorbis/decoder/mdct.rs new file mode 100644 index 0000000000..b4eb6c8ec8 --- /dev/null +++ b/crates/audio/src/vorbis/decoder/mdct.rs @@ -0,0 +1,164 @@ +//! The inverse MDCT of a block (Vorbis I §4.3.7) and the window slopes. +//! +//! `y[j] = sum_k X[k] cos(2*pi/n * (j + 1/2 + n/4) * (k + 1/2))` for `n/2` coefficients and `n` +//! samples, computed as a DCT-IV of the coefficients through an `n/8`-point complex FFT, then +//! unfolded. + +use std::f64::consts::PI; + +#[derive(Clone, Copy, Default)] +struct Cpx { + re: f64, + im: f64, +} + +impl Cpx { + fn mul(self, o: Cpx) -> Cpx { + Cpx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re } + } +} + +fn cis(t: f64) -> Cpx { + Cpx { re: t.cos(), im: t.sin() } +} + +pub(super) struct Imdct { + n: usize, + twiddle: Vec, + post: Vec, + fft_tw: Vec, + rev: Vec, +} + +impl Imdct { + /// For blocks of `n` samples, a power of two of at least 16. + pub(super) fn new(n: usize) -> Self { + let m = n / 2; + let h = m / 2; + let twiddle = (0..h).map(|j| cis(-PI * (4.0 * j as f64 + 1.0) / (4.0 * m as f64))).collect(); + let post = (0..h).map(|k| cis(-PI * k as f64 / m as f64)).collect(); + let fft_tw = (0..h / 2).map(|j| cis(-2.0 * PI * j as f64 / h as f64)).collect(); + let bits = h.trailing_zeros(); + let rev = (0..h).map(|i| if bits == 0 { 0 } else { i.reverse_bits() >> (usize::BITS - bits) }).collect(); + Self { n, twiddle, post, fft_tw, rev } + } + + /// `x`: `n/2` coefficients; `out`: `n` samples. + pub(super) fn inverse(&self, x: &[f32], out: &mut [f32]) { + let m = self.n / 2; + let q = self.n / 4; + let h = m / 2; + let mut z: Vec = (0..h).map(|j| Cpx { re: f64::from(x[2 * j]), im: f64::from(x[m - 1 - 2 * j]) }.mul(self.twiddle[j])).collect(); + self.fft(&mut z); + let mut v = vec![0f64; m]; + for (k, c) in z.iter().enumerate() { + let d = c.mul(self.post[k]); + v[2 * k] = d.re; + v[m - 1 - 2 * k] = -d.im; + } + // Unfold the DCT-IV output to the block. + for i in 0..q { + out[3 * q - 1 - i] = (-v[i]) as f32; + out[3 * q + i] = (-v[i]) as f32; + } + for i in q..2 * q { + out[i - q] = v[i] as f32; + out[3 * q - 1 - i] = (-v[i]) as f32; + } + } + + fn fft(&self, a: &mut [Cpx]) { + let h = a.len(); + for (i, &j) in self.rev.iter().enumerate() { + if i < j { + a.swap(i, j); + } + } + let mut len = 2; + while len <= h { + let step = h / len; + for start in (0..h).step_by(len) { + for k in 0..len / 2 { + let w = self.fft_tw[k * step]; + let u = a[start + k]; + let t = a[start + k + len / 2].mul(w); + a[start + k] = Cpx { re: u.re + t.re, im: u.im + t.im }; + a[start + k + len / 2] = Cpx { re: u.re - t.re, im: u.im - t.im }; + } + } + len *= 2; + } + } +} + +/// The rising half of the Vorbis window over `len` samples (§4.3.1): +/// `sin(pi/2 * sin^2((i + 0.5) / len * pi/2))`. +pub(super) fn slope(len: usize) -> Vec { + (0..len) + .map(|i| { + let s = (PI / 2.0 * (i as f64 + 0.5) / len as f64).sin(); + (PI / 2.0 * s * s).sin() as f32 + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn matches_the_direct_formula() { + for n in [16usize, 64, 256, 2048] { + let x: Vec = (0..n / 2).map(|i| ((i * 7919 % 211) as f32 - 105.0) / 50.0).collect(); + let mut fast = vec![0f32; n]; + Imdct::new(n).inverse(&x, &mut fast); + for j in [0usize, 1, 2, n / 4, n / 2, n - 1] { + let direct: f64 = + (0..n / 2).map(|k| f64::from(x[k]) * (2.0 * PI / n as f64 * (j as f64 + 0.5 + n as f64 / 4.0) * (k as f64 + 0.5)).cos()).sum(); + assert!((direct - f64::from(fast[j])).abs() < 1e-3 * (1.0 + direct.abs()), "n={n} j={j}: {direct} vs {}", fast[j]); + } + } + } + + #[test] + fn windowed_overlap_add_reconstructs_the_signal() { + // Forward MDCT by the definition with the 2/M scale an encoder uses, then this inverse. + let n = 64usize; + let m = n / 2; + let signal: Vec = (0..n * 6).map(|i| ((i * 37 % 101) as f64 - 50.0) / 50.0).collect(); + let w: Vec = slope(m).into_iter().chain(slope(m).into_iter().rev()).collect(); + let imdct = Imdct::new(n); + let mut out = vec![0f64; signal.len()]; + let mut start = 0; + while start + n <= signal.len() { + let coeffs: Vec = (0..m) + .map(|k| { + let s: f64 = (0..n) + .map(|j| { + signal[start + j] * f64::from(w[j]) * (2.0 * PI / n as f64 * (j as f64 + 0.5 + n as f64 / 4.0) * (k as f64 + 0.5)).cos() + }) + .sum(); + (s * 2.0 / m as f64) as f32 + }) + .collect(); + let mut y = vec![0f32; n]; + imdct.inverse(&coeffs, &mut y); + for j in 0..n { + out[start + j] += f64::from(y[j]) * f64::from(w[j]); + } + start += m; + } + for i in m..signal.len() - m { + assert!((out[i] - signal[i]).abs() < 1e-4, "sample {i}: {} vs {}", out[i], signal[i]); + } + } + + #[test] + fn the_slope_is_power_complementary() { + let s = slope(1024); + for i in 0..1024 { + let sum = s[i] * s[i] + s[1023 - i] * s[1023 - i]; + assert!((sum - 1.0).abs() < 1e-5, "{i}: {sum}"); + } + } +} diff --git a/crates/audio/src/vorbis/decoder/mod.rs b/crates/audio/src/vorbis/decoder/mod.rs new file mode 100644 index 0000000000..7485e34b84 --- /dev/null +++ b/crates/audio/src/vorbis/decoder/mod.rs @@ -0,0 +1,552 @@ +//! A clean-room Vorbis I decoder (feature `vorbis-decode`), written from the Vorbis I +//! specification (Xiph.Org, 2020). +//! +//! [`Decoder`] works on packets: build it from the three header packets (identification, comment +//! and setup), then give it audio packets in order. Each call returns the PCM the packet +//! completes, planar `f32` in the nominal range -1.0 to 1.0 (not clipped), channels in the order of +//! the stream. The first packet only primes the overlap and returns nothing. [`decode_ogg`] reads a +//! whole Ogg Vorbis file. +//! +//! Codebooks, floors 0 and 1, residues 0, 1 and 2, channel coupling and both block sizes with their +//! hybrid windows are implemented. Damaged input is an error, never a panic: setup sizes are +//! bounded before anything is allocated, and the codeword trees are checked for completeness. + +mod bits; +mod codebook; +mod floor; +mod mdct; +mod residue; +mod tables; + +use bits::{BitReader, Eop, ilog}; +use codebook::Codebook; +use floor::{Fail, Floor, FloorData}; +use mdct::{Imdct, slope}; +use residue::Residue; + +/// Errors of the Vorbis decoder. +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The identification or comment header is not valid Vorbis. + #[error("invalid Vorbis header: {0}")] + Header(&'static str), + /// The setup header is damaged or uses something Vorbis I does not allow. + #[error("invalid Vorbis setup: {0}")] + Setup(&'static str), + /// The setup or decoded PCM asks for more memory than the decoder allows. + #[error("Vorbis decode above the limits: {0}")] + Limit(&'static str), + /// The packet is not an audio packet (a header, or a packet with its first bit set). + #[error("not an audio packet")] + NotAudio, + /// The packet is damaged: it ends before its mode and window flags are read, or names a mode + /// that does not exist. + #[error("damaged audio packet: {0}")] + Packet(&'static str), + /// An Ogg file with no Vorbis stream, or one that cannot be read. + #[error("Ogg Vorbis file: {0}")] + Ogg(String), +} + +/// `Result` of this module. +pub type Result = std::result::Result; + +/// The identification header (§4.2.2). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Identification { + /// The channel count, at least 1. + pub channels: u8, + /// Samples per second per channel. + pub sample_rate: u32, + /// The maximum bit rate in bits per second, 0 or negative when not set. + pub bitrate_maximum: i32, + /// The nominal bit rate in bits per second, 0 or negative when not set. + pub bitrate_nominal: i32, + /// The minimum bit rate in bits per second, 0 or negative when not set. + pub bitrate_minimum: i32, + /// The short block size in samples. + pub blocksize_0: u32, + /// The long block size in samples. + pub blocksize_1: u32, +} + +/// The comment header (§5): a vendor string and `NAME=value` comments. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Comments { + /// The vendor string of the encoder. + pub vendor: String, + /// The user comments, each `NAME=value`. + pub comments: Vec, +} + +impl Comments { + /// The first comment named `name` (case-insensitive), its value. + #[must_use] + pub fn get(&self, name: &str) -> Option<&str> { + self.comments.iter().find_map(|c| { + let (k, v) = c.split_once('=')?; + k.eq_ignore_ascii_case(name).then_some(v) + }) + } +} + +fn header_start(packet: &[u8], kind: u8) -> Result> { + if packet.len() < 7 || packet[0] != kind || &packet[1..7] != b"vorbis" { + return Err(Error::Header("wrong packet type or missing \"vorbis\" tag")); + } + let mut r = BitReader::new(packet); + let _ = r.read(8); + for _ in 0..6 { + let _ = r.read(8); + } + Ok(r) +} + +fn read_identification(packet: &[u8]) -> Result { + let mut r = header_start(packet, 1)?; + let eop = |_: Eop| Error::Header("identification header is truncated"); + if r.read(32).map_err(eop)? != 0 { + return Err(Error::Header("unsupported Vorbis version")); + } + let channels = r.read(8).map_err(eop)? as u8; + let sample_rate = r.read(32).map_err(eop)?; + let bitrate_maximum = r.read(32).map_err(eop)? as i32; + let bitrate_nominal = r.read(32).map_err(eop)? as i32; + let bitrate_minimum = r.read(32).map_err(eop)? as i32; + let e0 = r.read(4).map_err(eop)?; + let e1 = r.read(4).map_err(eop)?; + if !r.bit().map_err(eop)? { + return Err(Error::Header("identification header framing bit is not set")); + } + if channels == 0 || sample_rate == 0 { + return Err(Error::Header("no channels or a zero sample rate")); + } + if !(6..=13).contains(&e0) || !(6..=13).contains(&e1) || e0 > e1 { + return Err(Error::Header("block sizes must be powers of two from 64 to 8192, the short one not above the long one")); + } + Ok(Identification { channels, sample_rate, bitrate_maximum, bitrate_nominal, bitrate_minimum, blocksize_0: 1 << e0, blocksize_1: 1 << e1 }) +} + +/// Comments are best effort: damage here is not fatal (§4.2.1). +fn read_comments(packet: &[u8]) -> Result { + header_start(packet, 3)?; + let mut at = 7usize; + let mut take = |n: usize| -> Option<&[u8]> { + let s = packet.get(at..at.checked_add(n)?)?; + at += n; + Some(s) + }; + let word = |b: &[u8]| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize; + let mut out = Comments::default(); + let Some(n) = take(4).map(word) else { return Ok(out) }; + let Some(vendor) = take(n) else { return Ok(out) }; + out.vendor = String::from_utf8_lossy(vendor).into_owned(); + let Some(count) = take(4).map(word) else { return Ok(out) }; + for _ in 0..count { + let Some(n) = take(4).map(word) else { break }; + let Some(c) = take(n) else { break }; + out.comments.push(String::from_utf8_lossy(c).into_owned()); + } + Ok(out) +} + +struct Mapping { + coupling: Vec<(usize, usize)>, + mux: Vec, + /// `(floor, residue)` of each submap. + submaps: Vec<(usize, usize)>, +} + +struct Mode { + long: bool, + mapping: usize, +} + +struct Setup { + books: Vec, + floors: Vec, + residues: Vec, + mappings: Vec, + modes: Vec, +} + +/// Bytes the codebooks of a setup may take in all (codeword trees and vector tables). +const CODEBOOK_BUDGET: usize = 256 << 20; + +fn read_setup(packet: &[u8], channels: usize) -> Result { + let mut r = header_start(packet, 5)?; + let eop = |_: Eop| Error::Setup("setup header is truncated"); + let count = r.read(8).map_err(eop)? as usize + 1; + let mut budget = CODEBOOK_BUDGET; + let mut books = Vec::with_capacity(count); + for _ in 0..count { + books.push(Codebook::read(&mut r, &mut budget)?); + } + // Time domain transforms: placeholders. + for _ in 0..r.read(6).map_err(eop)? + 1 { + if r.read(16).map_err(eop)? != 0 { + return Err(Error::Setup("a time domain transform is not zero")); + } + } + let count = r.read(6).map_err(eop)? as usize + 1; + let mut floors = Vec::with_capacity(count); + for _ in 0..count { + let kind = r.read(16).map_err(eop)?; + floors.push(Floor::read(kind, &mut r, &books)?); + } + let count = r.read(6).map_err(eop)? as usize + 1; + let mut residues = Vec::with_capacity(count); + for _ in 0..count { + let kind = r.read(16).map_err(eop)?; + if kind > 2 { + return Err(Error::Setup("unknown residue type")); + } + residues.push(Residue::read(kind, &mut r, &books)?); + } + let count = r.read(6).map_err(eop)? as usize + 1; + let mut mappings = Vec::with_capacity(count); + for _ in 0..count { + if r.read(16).map_err(eop)? != 0 { + return Err(Error::Setup("unknown mapping type")); + } + let submaps = if r.bit().map_err(eop)? { r.read(4).map_err(eop)? as usize + 1 } else { 1 }; + let mut coupling = Vec::new(); + if r.bit().map_err(eop)? { + let steps = r.read(8).map_err(eop)? as usize + 1; + let bits = ilog(channels as i64 - 1); + for _ in 0..steps { + let magnitude = r.read(bits).map_err(eop)? as usize; + let angle = r.read(bits).map_err(eop)? as usize; + if magnitude == angle || magnitude >= channels || angle >= channels { + return Err(Error::Setup("channel coupling names an invalid channel pair")); + } + coupling.push((magnitude, angle)); + } + } + if r.read(2).map_err(eop)? != 0 { + return Err(Error::Setup("reserved mapping bits are set")); + } + let mut mux = vec![0usize; channels]; + if submaps > 1 { + for m in &mut mux { + *m = r.read(4).map_err(eop)? as usize; + if *m >= submaps { + return Err(Error::Setup("channel multiplexed to a submap that does not exist")); + } + } + } + let mut list = Vec::with_capacity(submaps); + for _ in 0..submaps { + r.read(8).map_err(eop)?; + let floor = r.read(8).map_err(eop)? as usize; + let residue = r.read(8).map_err(eop)? as usize; + if floor >= floors.len() || residue >= residues.len() { + return Err(Error::Setup("submap names a floor or residue that does not exist")); + } + list.push((floor, residue)); + } + mappings.push(Mapping { coupling, mux, submaps: list }); + } + let count = r.read(6).map_err(eop)? as usize + 1; + let mut modes = Vec::with_capacity(count); + for _ in 0..count { + let long = r.bit().map_err(eop)?; + let window = r.read(16).map_err(eop)?; + let transform = r.read(16).map_err(eop)?; + let mapping = r.read(8).map_err(eop)? as usize; + if window != 0 || transform != 0 || mapping >= mappings.len() { + return Err(Error::Setup("a mode has a nonzero window or transform type or an unknown mapping")); + } + modes.push(Mode { long, mapping }); + } + if !r.bit().map_err(eop)? { + return Err(Error::Setup("setup header framing bit is not set")); + } + Ok(Setup { books, floors, residues, mappings, modes }) +} + +/// The previous block, windowed, kept for overlap-add. +struct Previous { + n: usize, + data: Vec>, +} + +/// A Vorbis I packet decoder. +pub struct Decoder { + id: Identification, + comments: Comments, + setup: Setup, + imdct: [Imdct; 2], + slopes: [Vec; 2], + previous: Option, +} + +impl Decoder { + /// A decoder from the three header packets, in order. A damaged comment header is tolerated + /// (its comments are then empty); the other two must be valid. + pub fn new(identification: &[u8], comment: &[u8], setup: &[u8]) -> Result { + let id = read_identification(identification)?; + let comments = read_comments(comment)?; + let setup = read_setup(setup, usize::from(id.channels))?; + let (b0, b1) = (id.blocksize_0 as usize, id.blocksize_1 as usize); + Ok(Self { id, comments, setup, imdct: [Imdct::new(b0), Imdct::new(b1)], slopes: [slope(b0 / 2), slope(b1 / 2)], previous: None }) + } + + /// The identification header. + #[must_use] + pub fn identification(&self) -> &Identification { + &self.id + } + + /// The comment header. + #[must_use] + pub fn comments(&self) -> &Comments { + &self.comments + } + + /// Forget the previous block, as after a seek: the next packet only primes the overlap again. + pub fn reset(&mut self) { + self.previous = None; + } + + /// The window of a block (§4.3.1). + fn window(&self, n: usize, long: bool, prev_long: bool, next_long: bool) -> Vec { + let half = n / 2; + let b0 = self.id.blocksize_0 as usize; + let mut w = vec![0f32; n]; + // Left half. + if long && !prev_long { + let (start, end) = (n / 4 - b0 / 4, n / 4 + b0 / 4); + w[start..end].copy_from_slice(&self.slopes[0]); + w[end..half].fill(1.0); + } else { + w[..half].copy_from_slice(&self.slopes[usize::from(long)]); + } + // Right half. + if long && !next_long { + let start = 3 * n / 4 - b0 / 4; + w[half..start].fill(1.0); + for (i, s) in self.slopes[0].iter().rev().enumerate() { + w[start + i] = *s; + } + } else { + for (i, s) in self.slopes[usize::from(long)].iter().rev().enumerate() { + w[half + i] = *s; + } + } + w + } + + /// Decode one audio packet. The result holds one `Vec` per channel, all of the same length: + /// the samples this packet completes (none for the first packet after a start or reset). + pub fn decode_packet(&mut self, packet: &[u8]) -> Result>> { + let channels = usize::from(self.id.channels); + let mut r = BitReader::new(packet); + match r.bit() { + Ok(false) => {} + Ok(true) => return Err(Error::NotAudio), + Err(_) => return Err(Error::Packet("empty packet")), + } + let mode_bits = ilog(self.setup.modes.len() as i64 - 1); + let mode_number = r.read(mode_bits).map_err(|_| Error::Packet("packet ends inside the mode number"))? as usize; + let Some(mode) = self.setup.modes.get(mode_number) else { return Err(Error::Packet("mode number out of range")) }; + let long = mode.long; + let n = if long { self.id.blocksize_1 } else { self.id.blocksize_0 } as usize; + let (mut prev_long, mut next_long) = (true, true); + if long { + prev_long = r.bit().map_err(|_| Error::Packet("packet ends inside the window flags"))?; + next_long = r.bit().map_err(|_| Error::Packet("packet ends inside the window flags"))?; + } + let window = self.window(n, long, prev_long, next_long); + let half = n / 2; + let mapping = &self.setup.mappings[mode.mapping]; + + let spectrum = self.spectrum(&mut r, mapping, half, channels)?; + // Inverse transform and window. + let which = usize::from(long); + let mut blocks: Vec> = Vec::with_capacity(channels); + for s in &spectrum { + let mut y = vec![0f32; n]; + self.imdct[which].inverse(s, &mut y); + for (v, w) in y.iter_mut().zip(&window) { + *v *= *w; + } + blocks.push(y); + } + + // Overlap-add with the previous block (§4.3.8). + let out = match &self.previous { + None => vec![Vec::new(); channels], + Some(prev) => { + let count = prev.n / 4 + n / 4; + blocks + .iter() + .zip(&prev.data) + .map(|(cur, old)| { + (0..count) + .map(|k| { + let a = old.get(prev.n / 2 + k).copied().unwrap_or(0.0); + let b = (n / 4 + k).checked_sub(prev.n / 4).and_then(|i| cur.get(i)).copied().unwrap_or(0.0); + a + b + }) + .collect() + }) + .collect() + } + }; + self.previous = Some(Previous { n, data: blocks }); + Ok(out) + } + + /// Floors, residues, coupling and the dot product: the spectrum of each channel (§4.3.2 to 4.3.6). + fn spectrum(&self, r: &mut BitReader<'_>, mapping: &Mapping, half: usize, channels: usize) -> Result>> { + let zero = || vec![vec![0f32; half]; channels]; + let setup = &self.setup; + // Floors, in channel order. + let mut floors: Vec> = Vec::with_capacity(channels); + for ch in 0..channels { + let (floor, _) = mapping.submaps[mapping.mux[ch]]; + match setup.floors[floor].decode(r, &setup.books) { + Ok(f) => floors.push(f), + Err(Fail::Eop) => return Ok(zero()), + Err(Fail::Invalid) => return Err(Error::Packet("a floor names a codebook that does not exist")), + } + } + let mut no_residue: Vec = floors.iter().map(Option::is_none).collect(); + for &(m, a) in &mapping.coupling { + if !no_residue[m] || !no_residue[a] { + no_residue[m] = false; + no_residue[a] = false; + } + } + // Residues, in submap order. + let mut residue = zero(); + for (i, &(_, res)) in mapping.submaps.iter().enumerate() { + let members: Vec = (0..channels).filter(|&c| mapping.mux[c] == i).collect(); + if members.is_empty() { + continue; + } + let skip: Vec = members.iter().map(|&c| no_residue[c]).collect(); + let mut vectors = vec![vec![0f32; half]; members.len()]; + setup.residues[res].decode(r, &setup.books, half, &skip, &mut vectors); + for (v, &c) in vectors.into_iter().zip(&members) { + residue[c] = v; + } + } + // Inverse channel coupling (§4.3.5), last step first. + for &(m, a) in mapping.coupling.iter().rev() { + let mut mag = std::mem::take(&mut residue[m]); + let mut ang = std::mem::take(&mut residue[a]); + for (mv, av) in mag.iter_mut().zip(ang.iter_mut()) { + let (big, small) = (*mv, *av); + let (new_m, new_a); + if big > 0.0 { + if small > 0.0 { + new_m = big; + new_a = big - small; + } else { + new_a = big; + new_m = big + small; + } + } else if small > 0.0 { + new_m = big; + new_a = big + small; + } else { + new_a = big; + new_m = big - small; + } + *mv = new_m; + *av = new_a; + } + residue[m] = mag; + residue[a] = ang; + } + // The floor times the residue. + for ch in 0..channels { + match &floors[ch] { + Some(data) => { + let (floor, _) = mapping.submaps[mapping.mux[ch]]; + let curve = setup.floors[floor].curve(data, half); + for (v, c) in residue[ch].iter_mut().zip(curve) { + *v *= c; + } + } + None => residue[ch].fill(0.0), + } + } + Ok(residue) + } +} + +/// A decoded Ogg Vorbis file. +#[derive(Debug, Clone, PartialEq)] +pub struct Decoded { + /// Samples per second per channel. + pub sample_rate: u32, + /// One `Vec` of samples per channel. + pub channels: Vec>, + /// The stream's comment header. + pub comments: Comments, + /// Audio packets that could not be decoded and were skipped. + pub damaged_packets: usize, +} + +/// Decode the first Vorbis stream of an Ogg file. The result is trimmed to the stream's final +/// granule position, so an encoder's padding at the end is dropped. Damaged packets are skipped +/// and counted, not fatal. Collected PCM is limited to 256 MiB; use the packet +/// decoder for longer streams without retaining the entire output. +pub fn decode_ogg(data: &[u8]) -> Result { + let ogg = craft_media::ogg::open(data).map_err(|e| Error::Ogg(e.to_string()))?; + let index = ogg.stream_of(craft_media::ogg::Codec::Vorbis).ok_or_else(|| Error::Ogg("no Vorbis stream".into()))?; + let stream = &ogg.streams[index]; + let [id, comment, setup, ..] = stream.headers.as_slice() else { return Err(Error::Ogg("the stream has fewer than three header packets".into())) }; + let mut decoder = Decoder::new(id, comment, setup)?; + let channel_count = usize::from(decoder.id.channels); + let mut channels = vec![Vec::new(); channel_count]; + let mut damaged = 0; + for i in 0..stream.packets.len() { + let packet = ogg.read_packet(data, index, i).map_err(|e| Error::Ogg(e.to_string()))?; + match decoder.decode_packet(&packet) { + Ok(pcm) => { + append_pcm(&mut channels, pcm, 256 << 20)?; + } + Err(_) => damaged += 1, + } + } + if let Some(total) = stream.last_granule.and_then(|g| usize::try_from(g).ok()) { + for c in &mut channels { + c.truncate(total); + } + } + Ok(Decoded { sample_rate: decoder.id.sample_rate, channels, comments: decoder.comments.clone(), damaged_packets: damaged }) +} + +// Admit every channel together before extending any of them. Limit retained PCM, +// not the compressed input, setup tables or the decoder's bounded packet scratch. +fn append_pcm(channels: &mut [Vec], pcm: Vec>, budget: usize) -> Result<()> { + let samples = channels.iter().chain(&pcm).try_fold(0usize, |n, c| n.checked_add(c.len())); + if samples.is_none_or(|n| n > budget / std::mem::size_of::()) { + return Err(Error::Limit("decoded PCM exceeds the whole-file budget")); + } + for (all, new) in channels.iter_mut().zip(pcm) { + all.try_reserve_exact(new.len()).map_err(|_| Error::Limit("cannot allocate decoded PCM"))?; + all.extend(new); + } + Ok(()) +} + +#[cfg(test)] +mod budget_tests { + use super::*; + + #[test] + fn pcm_budget_counts_all_channels_before_extending() { + let mut channels = vec![vec![1.0], vec![2.0]]; + append_pcm(&mut channels, vec![vec![3.0], vec![4.0]], 16).expect("exact budget"); + let before = channels.clone(); + assert!(matches!(append_pcm(&mut channels, vec![vec![5.0], vec![6.0]], 23), Err(Error::Limit(_)))); + assert_eq!(channels, before); + append_pcm(&mut channels, vec![Vec::new(), Vec::new()], 16).expect("empty packet"); + } +} diff --git a/crates/audio/src/vorbis/decoder/residue.rs b/crates/audio/src/vorbis/decoder/residue.rs new file mode 100644 index 0000000000..99918d3791 --- /dev/null +++ b/crates/audio/src/vorbis/decoder/residue.rs @@ -0,0 +1,196 @@ +//! Residues (Vorbis I §8): the fine detail of the spectrum, coded as vectors in passes. + +use super::Error; +use super::bits::{BitReader, Eop}; +use super::codebook::Codebook; + +pub(super) struct Residue { + kind: u32, + begin: usize, + end: usize, + partition_size: usize, + classifications: usize, + classbook: usize, + /// `books[class][pass]`. + books: Vec<[Option; 8]>, +} + +fn eop(_: Eop) -> Error { + Error::Setup("setup header ends inside a residue") +} + +impl Residue { + pub(super) fn read(kind: u32, r: &mut BitReader<'_>, books: &[Codebook]) -> Result { + let begin = r.read(24).map_err(eop)? as usize; + let end = r.read(24).map_err(eop)? as usize; + let partition_size = r.read(24).map_err(eop)? as usize + 1; + let classifications = r.read(6).map_err(eop)? as usize + 1; + let classbook = r.read(8).map_err(eop)? as usize; + let Some(cb) = books.get(classbook) else { return Err(Error::Setup("residue class book out of range")) }; + // classifications ^ dimensions must not exceed the book's entries. + let mut reach = 1u128; + for _ in 0..cb.dimensions { + reach = reach.saturating_mul(classifications as u128); + if reach > cb.entries as u128 { + return Err(Error::Setup("residue class book has too few entries for its classifications")); + } + } + let mut cascade = Vec::with_capacity(classifications); + for _ in 0..classifications { + let low = r.read(3).map_err(eop)?; + let high = if r.bit().map_err(eop)? { r.read(5).map_err(eop)? } else { 0 }; + cascade.push(high * 8 + low); + } + let mut list = Vec::with_capacity(classifications); + for &c in &cascade { + let mut row = [None; 8]; + for (j, slot) in row.iter_mut().enumerate() { + if c & (1 << j) != 0 { + let b = r.read(8).map_err(eop)? as usize; + match books.get(b) { + Some(book) if book.has_vectors() => *slot = Some(b), + _ => return Err(Error::Setup("residue names a missing codebook or one without vectors")), + } + } + } + list.push(row); + } + Ok(Self { kind, begin, end, partition_size, classifications, classbook, books: list }) + } + + /// Decode a bundle of vectors, each `n` long (half the block size), into `out`; a vector marked + /// in `skip` is left zero. End of packet ends decoding where it is (a nominal event). + pub(super) fn decode(&self, r: &mut BitReader<'_>, books: &[Codebook], n: usize, skip: &[bool], out: &mut [Vec]) { + let ch = out.len(); + if skip.iter().all(|&s| s) { + return; + } + if self.kind == 2 { + let mut flat = vec![vec![0f32; n * ch]]; + let _ = self.decode_vectors(r, books, n * ch, &[false], &mut flat); + for i in 0..n { + for (j, v) in out.iter_mut().enumerate() { + v[i] = flat[0][i * ch + j]; + } + } + } else { + let _ = self.decode_vectors(r, books, n, skip, out); + } + } + + fn decode_vectors(&self, r: &mut BitReader<'_>, books: &[Codebook], actual: usize, skip: &[bool], out: &mut [Vec]) -> Result<(), Eop> { + let ch = out.len(); + let begin = self.begin.min(actual); + let end = self.end.min(actual); + if end <= begin { + return Ok(()); + } + let classbook = &books[self.classbook]; + let cpw = classbook.dimensions; + let partitions = (end - begin) / self.partition_size; + // Classbook dimensions may greatly exceed the actual partition count. + // Retain only digits that this block can use, not padding per channel. + let mut classes = vec![vec![0usize; partitions]; ch]; + for pass in 0..8 { + let mut count = 0usize; + while count < partitions { + if pass == 0 { + for j in 0..ch { + if skip[j] { + continue; + } + let mut temp = classbook.scalar(r)? as usize; + for i in (0..cpw).rev() { + if i + count < partitions { + classes[j][i + count] = temp % self.classifications; + } + temp /= self.classifications; + } + } + } + let mut i = 0; + while i < cpw && count < partitions { + for j in 0..ch { + if skip[j] { + continue; + } + let Some(book) = self.books[classes[j][count]][pass] else { continue }; + let book = &books[book]; + let offset = begin + count * self.partition_size; + self.partition(r, book, &mut out[j][offset..offset + self.partition_size])?; + } + count += 1; + i += 1; + } + } + } + Ok(()) + } + + /// One partition into `v` (the slice of the vector it covers). + fn partition(&self, r: &mut BitReader<'_>, book: &Codebook, v: &mut [f32]) -> Result<(), Eop> { + let n = v.len(); + let dims = book.dimensions; + let step = n / dims; + if self.kind == 0 { + for i in 0..step { + let Some(entry) = book.vector(r)? else { return Ok(()) }; + for (j, &e) in entry.iter().enumerate() { + v[i + j * step] += e; + } + } + } else { + let mut i = 0; + for _ in 0..step { + let Some(entry) = book.vector(r)? else { return Ok(()) }; + for &e in entry { + v[i] += e; + i += 1; + } + } + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn huge_class_dimensions_keep_only_actual_partitions() { + let mut data = Vec::::new(); + let mut bits = 0usize; + let mut put = |value: u32, n: usize| { + for i in 0..n { + if bits.is_multiple_of(8) { + data.push(0); + } + let last = data.len() - 1; + data[last] |= (((value >> i) & 1) as u8) << (bits % 8); + bits += 1; + } + }; + put(0x56_4342, 24); + put(65535, 16); + put(1, 24); + put(0, 1); // unordered, not sparse, one length-1 entry + put(0, 1); + put(0, 5); + put(0, 4); // scalar-only classbook + let book = Codebook::read(&mut BitReader::new(&data), &mut (1 << 20)).expect("classbook"); + let books = [book]; + for kind in [0, 1] { + let residue = Residue { kind, begin: 0, end: 32, partition_size: 1, classifications: 1, classbook: 0, books: vec![[None; 8]] }; + let mut out = vec![vec![0.0; 32]; 255]; + let packet = [0u8; 32]; + let mut r = BitReader::new(&packet); + residue.decode(&mut r, &books, 32, &[false; 255], &mut out); + assert_eq!(r.remaining(), 1); // one classword per channel, not 32 + assert!(out.iter().flatten().all(|&x| x == 0.0)); + let mut r = BitReader::new(&packet); + residue.decode(&mut r, &books, 32, &[true; 255], &mut out); + assert_eq!(r.remaining(), 256); // skipped channels consume nothing + } + } +} diff --git a/crates/audio/src/vorbis/decoder/tables.rs b/crates/audio/src/vorbis/decoder/tables.rs new file mode 100644 index 0000000000..49e5b97b9c --- /dev/null +++ b/crates/audio/src/vorbis/decoder/tables.rs @@ -0,0 +1,263 @@ +//! `floor1_inverse_dB_table` (Vorbis I §10.1): the 256 linear amplitudes of the floor 1 integer values. + +// The values are printed to the digits the specification gives them. +#![allow(clippy::excessive_precision)] + +pub(super) const INVERSE_DB: [f32; 256] = [ + 1.0649863e-07, + 1.1341951e-07, + 1.2079015e-07, + 1.2863978e-07, + 1.3699951e-07, + 1.4590251e-07, + 1.5538408e-07, + 1.6548181e-07, + 1.7623575e-07, + 1.8768855e-07, + 1.9988561e-07, + 2.1287530e-07, + 2.2670913e-07, + 2.4144197e-07, + 2.5713223e-07, + 2.7384213e-07, + 2.9163793e-07, + 3.1059021e-07, + 3.3077411e-07, + 3.5226968e-07, + 3.7516214e-07, + 3.9954229e-07, + 4.2550680e-07, + 4.5315863e-07, + 4.8260743e-07, + 5.1396998e-07, + 5.4737065e-07, + 5.8294187e-07, + 6.2082472e-07, + 6.6116941e-07, + 7.0413592e-07, + 7.4989464e-07, + 7.9862701e-07, + 8.5052630e-07, + 9.0579828e-07, + 9.6466216e-07, + 1.0273513e-06, + 1.0941144e-06, + 1.1652161e-06, + 1.2409384e-06, + 1.3215816e-06, + 1.4074654e-06, + 1.4989305e-06, + 1.5963394e-06, + 1.7000785e-06, + 1.8105592e-06, + 1.9282195e-06, + 2.0535261e-06, + 2.1869758e-06, + 2.3290978e-06, + 2.4804557e-06, + 2.6416497e-06, + 2.8133190e-06, + 2.9961443e-06, + 3.1908506e-06, + 3.3982101e-06, + 3.6190449e-06, + 3.8542308e-06, + 4.1047004e-06, + 4.3714470e-06, + 4.6555282e-06, + 4.9580707e-06, + 5.2802740e-06, + 5.6234160e-06, + 5.9888572e-06, + 6.3780469e-06, + 6.7925283e-06, + 7.2339451e-06, + 7.7040476e-06, + 8.2047000e-06, + 8.7378876e-06, + 9.3057248e-06, + 9.9104632e-06, + 1.0554501e-05, + 1.1240392e-05, + 1.1970856e-05, + 1.2748789e-05, + 1.3577278e-05, + 1.4459606e-05, + 1.5399272e-05, + 1.6400004e-05, + 1.7465768e-05, + 1.8600792e-05, + 1.9809576e-05, + 2.1096914e-05, + 2.2467911e-05, + 2.3928002e-05, + 2.5482978e-05, + 2.7139006e-05, + 2.8902651e-05, + 3.0780908e-05, + 3.2781225e-05, + 3.4911534e-05, + 3.7180282e-05, + 3.9596466e-05, + 4.2169667e-05, + 4.4910090e-05, + 4.7828601e-05, + 5.0936773e-05, + 5.4246931e-05, + 5.7772202e-05, + 6.1526565e-05, + 6.5524908e-05, + 6.9783085e-05, + 7.4317983e-05, + 7.9147585e-05, + 8.4291040e-05, + 8.9768747e-05, + 9.5602426e-05, + 0.00010181521, + 0.00010843174, + 0.00011547824, + 0.00012298267, + 0.00013097477, + 0.00013948625, + 0.00014855085, + 0.00015820453, + 0.00016848555, + 0.00017943469, + 0.00019109536, + 0.00020351382, + 0.00021673929, + 0.00023082423, + 0.00024582449, + 0.00026179955, + 0.00027881276, + 0.00029693158, + 0.00031622787, + 0.00033677814, + 0.00035866388, + 0.00038197188, + 0.00040679456, + 0.00043323036, + 0.00046138411, + 0.00049136745, + 0.00052329927, + 0.00055730621, + 0.00059352311, + 0.00063209358, + 0.00067317058, + 0.00071691700, + 0.00076350630, + 0.00081312324, + 0.00086596457, + 0.00092223983, + 0.00098217216, + 0.0010459992, + 0.0011139742, + 0.0011863665, + 0.0012634633, + 0.0013455702, + 0.0014330129, + 0.0015261382, + 0.0016253153, + 0.0017309374, + 0.0018434235, + 0.0019632195, + 0.0020908006, + 0.0022266726, + 0.0023713743, + 0.0025254795, + 0.0026895994, + 0.0028643847, + 0.0030505286, + 0.0032487691, + 0.0034598925, + 0.0036847358, + 0.0039241906, + 0.0041792066, + 0.0044507950, + 0.0047400328, + 0.0050480668, + 0.0053761186, + 0.0057254891, + 0.0060975636, + 0.0064938176, + 0.0069158225, + 0.0073652516, + 0.0078438871, + 0.0083536271, + 0.0088964928, + 0.009474637, + 0.010090352, + 0.010746080, + 0.011444421, + 0.012188144, + 0.012980198, + 0.013823725, + 0.014722068, + 0.015678791, + 0.016697687, + 0.017782797, + 0.018938423, + 0.020169149, + 0.021479854, + 0.022875735, + 0.024362330, + 0.025945531, + 0.027631618, + 0.029427276, + 0.031339626, + 0.033376252, + 0.035545228, + 0.037855157, + 0.040315199, + 0.042935108, + 0.045725273, + 0.048696758, + 0.051861348, + 0.055231591, + 0.058820850, + 0.062643361, + 0.066714279, + 0.071049749, + 0.075666962, + 0.080584227, + 0.085821044, + 0.091398179, + 0.097337747, + 0.10366330, + 0.11039993, + 0.11757434, + 0.12521498, + 0.13335215, + 0.14201813, + 0.15124727, + 0.16107617, + 0.17154380, + 0.18269168, + 0.19456402, + 0.20720788, + 0.22067342, + 0.23501402, + 0.25028656, + 0.26655159, + 0.28387361, + 0.30232132, + 0.32196786, + 0.34289114, + 0.36517414, + 0.38890521, + 0.41417847, + 0.44109412, + 0.46975890, + 0.50028648, + 0.53279791, + 0.56742212, + 0.60429640, + 0.64356699, + 0.68538959, + 0.72993007, + 0.77736504, + 0.82788260, + 0.88168307, + 0.9389798, + 1.0, +]; diff --git a/crates/audio/src/vorbis/mod.rs b/crates/audio/src/vorbis/mod.rs index 29c423f1d0..15b81bb26d 100644 --- a/crates/audio/src/vorbis/mod.rs +++ b/crates/audio/src/vorbis/mod.rs @@ -1,4 +1,7 @@ -//! Vorbis I (feature `vorbis-encode`): the pure-Rust [`encoder`]. Decoding Vorbis goes through -//! [`crate::decode`]. +//! Vorbis I: the pure-Rust [`decoder`] (feature `vorbis-decode`) and [`encoder`] (feature +//! `vorbis-encode`). `craft_audio::decode` still reads Vorbis files through symphonia. +#[cfg(feature = "vorbis-decode")] +pub mod decoder; +#[cfg(feature = "vorbis-encode")] pub mod encoder; diff --git a/crates/audio/tests/adpcm_oracle.rs b/crates/audio/tests/adpcm_oracle.rs new file mode 100644 index 0000000000..c89fe64440 --- /dev/null +++ b/crates/audio/tests/adpcm_oracle.rs @@ -0,0 +1,516 @@ +//! ADPCM (feature `adpcm`): every family decodes what ffmpeg's encoder writes to ffmpeg's own +//! samples, and ffmpeg decodes what our encoder writes to our own decoder's samples (oracle; +//! skipped when ffmpeg is absent, a failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`); plus the +//! round trip through our own encoder and decoder, and damaged input. + +use craft_audio::adpcm::{ima, ms, swf, yamaha}; +use std::path::Path; +use std::process::Command; + +const RATE: u32 = 22_050; + +/// Interleaved test audio: tones, a sweep, noise bursts, silence and a full-scale square. +fn signal(channels: usize, frames: usize) -> Vec { + let mut seed = 0x2545_F491u32; + let mut out = Vec::with_capacity(frames * channels); + for i in 0..frames { + for c in 0..channels { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + let t = i as f64; + let v = match (i / 1500) % 5 { + 0 => (t * (0.03 + 0.011 * c as f64)).sin() * 9000.0, + 1 => (t * 0.002 * (1.0 + t / 3000.0)).sin() * 14000.0, + 2 => f64::from(seed % 12000) - 6000.0, + 3 => 0.0, + _ => { + if (i / 40) % 2 == 0 { + 20000.0 + } else { + -20000.0 + } + } + }; + out.push(v as i16); + } + } + out +} + +fn snr(a: &[i16], b: &[i16]) -> f64 { + let (mut s, mut n) = (0f64, 0f64); + for (x, y) in a.iter().zip(b) { + s += f64::from(*x).powi(2); + n += (f64::from(*x) - f64::from(*y)).powi(2); + } + 10.0 * (s / n.max(1.0)).log10() +} + +fn max_diff(a: &[i16], b: &[i16]) -> i32 { + a.iter().zip(b).map(|(x, y)| (i32::from(*x) - i32::from(*y)).abs()).max().unwrap_or(0) +} + +#[test] +fn encode_then_decode_is_close_to_the_input() { + for channels in [1usize, 2] { + let x = signal(channels, 20_000); + // (name, decoded, minimum SNR dB, a few dB under what each reaches): the signal has noise bursts + // and full-scale square waves, which codes this small follow loosely + let cases: Vec<(&str, Vec, f64)> = vec![ + ("ima", ima::decode(&ima::encode(&x, channels, 512).unwrap(), channels, 512).unwrap(), 5.0), + ("ms", ms::decode(&ms::encode(&x, channels, 512).unwrap(), channels, 512).unwrap(), 18.0), + ("yamaha", yamaha::decode(&yamaha::encode(&x, channels).unwrap(), channels).unwrap(), 5.0), + ("swf2", swf::decode(&swf::encode(&x, channels, 2).unwrap(), channels).unwrap(), 1.0), + ("swf3", swf::decode(&swf::encode(&x, channels, 3).unwrap(), channels).unwrap(), 3.0), + ("swf4", swf::decode(&swf::encode(&x, channels, 4).unwrap(), channels).unwrap(), 5.0), + ("swf5", swf::decode(&swf::encode(&x, channels, 5).unwrap(), channels).unwrap(), 7.0), + ]; + for (name, y, min) in cases { + assert!(y.len() >= x.len(), "{name}: {} samples for {}", y.len(), x.len()); + let s = snr(&x, &y[..x.len()]); + println!("{name} {channels}ch: SNR {s:.1} dB"); + assert!(s >= min, "{name} {channels}ch: SNR {s:.1} dB under {min}"); + } + } +} + +#[test] +fn damaged_input_is_an_error_or_decodes_never_panics() { + let x = signal(2, 4000); + let streams: Vec<(&str, Vec)> = vec![ + ("ima", ima::encode(&x, 2, 256).unwrap()), + ("ms", ms::encode(&x, 2, 256).unwrap()), + ("yamaha", yamaha::encode(&x, 2).unwrap()), + ("swf", swf::encode(&x, 2, 4).unwrap()), + ]; + let mut seed = 99u32; + for (name, s) in &streams { + for cut in (0..s.len()).step_by(7) { + let part = &s[..cut]; + let _ = match *name { + "ima" => ima::decode(part, 2, 256).map(drop), + "ms" => ms::decode(part, 2, 256).map(drop), + "yamaha" => yamaha::decode(part, 2).map(drop), + _ => swf::decode(part, 2).map(drop), + }; + } + for _ in 0..300 { + let mut d = s.clone(); + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + let at = seed as usize % d.len(); + d[at] ^= (seed >> 8) as u8 | 1; + let _ = match *name { + "ima" => ima::decode(&d, 2, 256).map(drop), + "ms" => ms::decode(&d, 2, 256).map(drop), + "yamaha" => yamaha::decode(&d, 2).map(drop), + _ => swf::decode(&d, 2).map(drop), + }; + } + } + // the impossible layouts + assert!(ima::decode(&[0; 10], 2, 10).is_err()); + assert!(ima::decode(&[0; 8], 9, 8).is_err()); + assert!(ms::decode(&[0; 8], 3, 8).is_err()); + assert!(ima::encode(&[0; 3], 2, 256).is_err()); + assert!(ima::encode_block(&[0; 3], 1, 256).is_err()); + assert!(ms::encode_block(&[0; 3], 1, 256).is_err()); + // a block whose step index or predictor is out of range + let mut bad = ima::encode_block(&vec![0; 505], 1, 256).unwrap(); + bad[2] = 200; + assert!(ima::decode_block(&bad, 1, &mut Vec::new()).is_err()); + let mut bad = swf::encode(&[0; 10], 1, 4).unwrap(); + bad[2] = 0xFF; + let _ = swf::decode(&bad, 1); +} + +#[test] +fn huge_layouts_and_big_jumps_are_handled() { + // block sizes whose sample counts do not fit in a usize + assert_eq!(ima::samples_per_block(usize::MAX - 3, 1), None); + assert_eq!(ms::samples_per_block(usize::MAX, 1), None); + assert!(ima::encode(&[], 1, usize::MAX - 3).is_err()); + assert!(ms::encode(&[], 1, usize::MAX).is_err()); + // a Flash stream's header holds the step index in six bits: a jump from silence to full scale + // still decodes to what the encoder's own state says + for bits in [2u32, 3, 4, 5] { + let x = [0i16, 32767, -32768, 0, 0]; + let y = swf::decode(&swf::encode(&x, 1, bits).unwrap(), 1).unwrap(); + // the stream has no length: up to seven padding bits can read as one more code + assert!((x.len()..=x.len() + 3).contains(&y.len()), "{bits} bits: {} samples", y.len()); + assert!(y[1] > 0 && y[2] < y[1], "{bits} bits: {y:?}"); + } + // padding bits at the end of a stream are not a code: no incomplete stereo frame + for bits in [2u32, 3, 4, 5] { + let y = swf::decode(&swf::encode(&[0, 0, 5, -5, 9, 9], 2, bits).unwrap(), 2).unwrap(); + assert_eq!(y.len() % 2, 0, "{bits} bits: {y:?}"); + assert!((6..=8).contains(&y.len()), "{bits} bits: {} samples", y.len()); + } +} + +#[test] +fn partial_final_blocks_are_handled() { + let x = signal(1, 1000); + // IMA: 256-byte blocks hold 505 samples; 1000 samples is two blocks, the second padded + let enc = ima::encode(&x, 1, 256).unwrap(); + assert_eq!(enc.len(), 2 * 256); + assert_eq!(ima::decode(&enc, 1, 256).unwrap().len(), 1010); + // a stream cut inside the last block decodes as far as it goes + let cut = ima::decode(&enc[..256 + 4 + 40], 1, 256).unwrap(); + assert_eq!(cut.len(), 505 + 1 + 80); + let enc = ms::encode(&x, 1, 256).unwrap(); + assert_eq!(ms::decode(&enc, 1, 256).unwrap().len(), 1000); + assert!(ima::encode(&[], 1, 256).unwrap().is_empty()); + assert!(ms::encode(&[], 1, 256).unwrap().is_empty()); + assert!(yamaha::encode(&[], 1).unwrap().is_empty()); + assert_eq!(yamaha::decode(&yamaha::encode(&[5, 6, 7], 1).unwrap(), 1).unwrap().len(), 4); +} + +// ---- the other rounding some ffmpeg versions use for IMA and Flash ADPCM +// +// The IMA recommended practices build the step difference from `step >> 3` and the shifted steps +// of the magnitude bits that are set; some decoders (including older ffmpeg releases) compute +// `((2 * magnitude + 1) * step) >> (bits - 1)` instead, which rounds differently by a few units +// and so drifts apart through the predictor. A stream decodes to ours or to this one. + +fn alt_diff(mag: i32, step: i32, bits: u32) -> i32 { + ((2 * mag + 1) * step) >> (bits - 1) +} + +const INDEX_4: [i32; 8] = [-1, -1, -1, -1, 2, 4, 6, 8]; +const INDEX_5: [i32; 16] = [-1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16]; +const STEPS: [i32; 89] = [ + 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, + 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, + 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, + 22385, 24623, 27086, 29794, 32767, +]; + +fn index_table(bits: u32) -> &'static [i32] { + match bits { + 2 => &[-1, 2], + 3 => &[-1, -1, 2, 4], + 4 => &INDEX_4, + _ => &INDEX_5, + } +} + +/// `ima::decode` with the other rounding. +fn alt_ima_decode(data: &[u8], channels: usize, block_align: usize) -> Vec { + let per = ima::samples_per_block(block_align, channels).expect("layout"); + let mut out = Vec::new(); + for block in data.chunks_exact(block_align) { + let mut pred = vec![0i32; channels]; + let mut idx = vec![0i32; channels]; + let base = out.len(); + out.resize(base + per * channels, 0); + for ch in 0..channels { + pred[ch] = i32::from(i16::from_le_bytes([block[4 * ch], block[4 * ch + 1]])); + idx[ch] = i32::from(block[4 * ch + 2]); + out[base + ch] = pred[ch] as i16; + } + for (g, group) in block[4 * channels..].chunks_exact(4 * channels).enumerate() { + for (ch, bytes) in group.as_chunks::<4>().0.iter().enumerate() { + for (k, b) in bytes.iter().enumerate() { + for (half, nib) in [b & 0xF, b >> 4].into_iter().enumerate() { + let step = STEPS[idx[ch] as usize]; + let d = alt_diff(i32::from(nib & 7), step, 4); + pred[ch] = (pred[ch] + if nib & 8 != 0 { -d } else { d }).clamp(-32768, 32767); + idx[ch] = (idx[ch] + INDEX_4[usize::from(nib & 7)]).clamp(0, 88); + out[base + (1 + g * 8 + k * 2 + half) * channels + ch] = pred[ch] as i16; + } + } + } + } + } + out +} + +/// `swf::decode` with the other rounding (one stream). +fn alt_swf_decode(data: &[u8], channels: usize) -> Vec { + let mut bitpos = 0usize; + let mut get = |n: usize| -> Option { + let mut v = 0u32; + for _ in 0..n { + let byte = *data.get(bitpos / 8)?; + v = (v << 1) | u32::from((byte >> (7 - bitpos % 8)) & 1); + bitpos += 1; + } + Some(v) + }; + let bits = get(2).expect("header") + 2; + let mut out = Vec::new(); + loop { + let mut pred = Vec::new(); + let mut idx = Vec::new(); + for _ in 0..channels { + let (Some(p), Some(i)) = (get(16), get(6)) else { return out }; + out.push(p as u16 as i16); + pred.push(i32::from(p as u16 as i16)); + idx.push(i as i32); + } + for _ in 1..4096 { + for ch in 0..channels { + let Some(code) = get(bits as usize) else { return out }; + let mag = (code & ((1 << (bits - 1)) - 1)) as i32; + let d = alt_diff(mag, STEPS[idx[ch] as usize], bits); + pred[ch] = (pred[ch] + if code >> (bits - 1) != 0 { -d } else { d }).clamp(-32768, 32767); + idx[ch] = (idx[ch] + index_table(bits)[mag as usize]).clamp(0, 88); + out.push(pred[ch] as i16); + } + } + } +} + +/// Assert `theirs` is `ours` or `alt` over their common length (and say which). +fn same_as_either(what: &str, ours: &[i16], alt: &[i16], theirs: &[i16]) { + let n = ours.len().min(theirs.len()); + let (a, b) = (max_diff(&ours[..n], &theirs[..n]), max_diff(&alt[..n.min(alt.len())], &theirs[..n.min(alt.len())])); + println!("{what}: largest difference from ours {a}, from the other rounding {b}"); + assert!(a == 0 || b == 0, "{what}: ffmpeg's decode is neither ours ({a}) nor the other rounding ({b})"); +} + +/// A name, the WAV file around our stream, our decode of it and the other rounding's. +type Stream = (&'static str, Vec, Vec, Vec); + +// ---- ffmpeg as the reference + +fn ffmpeg(ff: &Path, args: &[&str]) { + let out = Command::new(ff).args(["-v", "error", "-nostdin", "-y"]).args(args).output().expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr)); +} + +fn s(p: &Path) -> String { + p.to_str().expect("path").to_string() +} + +fn write_pcm(path: &Path, x: &[i16]) { + std::fs::write(path, x.iter().flat_map(|v| v.to_le_bytes()).collect::>()).expect("write"); +} + +fn read_pcm(path: &Path) -> Vec { + std::fs::read(path).expect("read").as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect() +} + +/// The `fmt ` chunk's fields and the `data` chunk of a WAV file. +fn wav_parts(bytes: &[u8]) -> (u16, usize, usize, Vec) { + let mut at = 12; + let (mut tag, mut channels, mut align) = (0, 0, 0); + while at + 8 <= bytes.len() { + let id = &bytes[at..at + 4]; + let len = u32::from_le_bytes([bytes[at + 4], bytes[at + 5], bytes[at + 6], bytes[at + 7]]) as usize; + let body = &bytes[at + 8..(at + 8 + len).min(bytes.len())]; + if id == b"fmt " { + tag = u16::from_le_bytes([body[0], body[1]]); + channels = usize::from(u16::from_le_bytes([body[2], body[3]])); + align = usize::from(u16::from_le_bytes([body[12], body[13]])); + } else if id == b"data" { + return (tag, channels, align, body.to_vec()); + } + at += 8 + len + (len & 1); + } + panic!("no data chunk"); +} + +/// A WAV file around `data`. +fn wav(tag: u16, channels: usize, align: usize, bits: u16, extra: &[u8], data: &[u8]) -> Vec { + let mut fmt = Vec::new(); + fmt.extend(tag.to_le_bytes()); + fmt.extend((channels as u16).to_le_bytes()); + fmt.extend(RATE.to_le_bytes()); + fmt.extend((RATE * align as u32 / 500).to_le_bytes()); + fmt.extend((align as u16).to_le_bytes()); + fmt.extend(bits.to_le_bytes()); + if !extra.is_empty() || tag == 0x11 || tag == 2 { + fmt.extend((extra.len() as u16).to_le_bytes()); + fmt.extend(extra); + } + let mut out = b"RIFF".to_vec(); + out.extend((4 + 8 + fmt.len() as u32 + 8 + data.len() as u32).to_le_bytes()); + out.extend(b"WAVEfmt "); + out.extend((fmt.len() as u32).to_le_bytes()); + out.extend(fmt); + out.extend(b"data"); + out.extend((data.len() as u32).to_le_bytes()); + out.extend(data); + out +} + +#[test] +fn we_decode_what_ffmpeg_encodes_to_the_samples_ffmpeg_decodes() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("adpcm"); + for channels in [1usize, 2] { + let x = signal(channels, 30_000); + let pcm = dir.join(format!("in{channels}.pcm")); + write_pcm(&pcm, &x); + let input = ["-f", "s16le", "-ar", "22050", "-ac", if channels == 1 { "1" } else { "2" }, "-i"]; + for (name, codec, align) in [("ima", "adpcm_ima_wav", 512usize), ("ms", "adpcm_ms", 512), ("yamaha", "adpcm_yamaha", 0)] { + let file = dir.join(format!("{name}{channels}.wav")); + let mut args: Vec = input.iter().map(|a| a.to_string()).collect(); + args.extend([s(&pcm), "-c:a".into(), codec.into()]); + if align > 0 { + args.extend(["-block_size".into(), align.to_string()]); + } + args.push(s(&file)); + ffmpeg(&ff, &args.iter().map(String::as_str).collect::>()); + let back = dir.join(format!("{name}{channels}.out.pcm")); + ffmpeg(&ff, &["-i", &s(&file), "-f", "s16le", "-acodec", "pcm_s16le", &s(&back)]); + let theirs = read_pcm(&back); + let (tag, ch, block, data) = wav_parts(&std::fs::read(&file).expect("read")); + assert_eq!(ch, channels); + let ours = match name { + "ima" => { + assert_eq!(tag, 0x11); + ima::decode(&data, ch, block).expect("decode") + } + "ms" => { + assert_eq!(tag, 2); + ms::decode(&data, ch, block).expect("decode") + } + _ => yamaha::decode(&data, ch).expect("decode"), + }; + let n = ours.len().min(theirs.len()); + assert!(n >= 29_000 * channels, "{name} {channels}ch: {} ours, {} theirs", ours.len(), theirs.len()); + if name == "ima" { + same_as_either(&format!("{name} {channels}ch"), &ours, &alt_ima_decode(&data, ch, block), &theirs); + } else { + let d = max_diff(&ours[..n], &theirs[..n]); + println!("{name} {channels}ch: {n} samples, largest difference from ffmpeg's decode {d}"); + assert_eq!(d, 0, "{name} {channels}ch"); + } + // our encoder is as good as ffmpeg's on the same audio, to within 3 dB + let mine = match name { + "ima" => ima::decode(&ima::encode(&x, ch, block).expect("encode"), ch, block).expect("decode"), + "ms" => ms::decode(&ms::encode(&x, ch, block).expect("encode"), ch, block).expect("decode"), + _ => yamaha::decode(&yamaha::encode(&x, ch).expect("encode"), ch).expect("decode"), + }; + let m = x.len().min(mine.len()).min(theirs.len()); + let (a, b) = (snr(&x[..m], &mine[..m]), snr(&x[..m], &theirs[..m])); + println!("{name} {channels}ch: SNR {a:.1} dB ours, {b:.1} dB ffmpeg's"); + assert!(a + 3.0 >= b, "{name} {channels}ch: our encoder {a:.1} dB against ffmpeg's {b:.1} dB"); + } + } +} + +#[test] +fn ffmpeg_decodes_what_we_encode_to_the_samples_we_decode() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("adpcm"); + for channels in [1usize, 2] { + let x = signal(channels, 12_000); + let coefs: Vec = ms::COEFFICIENTS.iter().flat_map(|&(a, b)| [a as i16, b as i16].into_iter().flat_map(i16::to_le_bytes)).collect(); + let streams: Vec = { + let e = ima::encode(&x, channels, 512).unwrap(); + let per = ima::samples_per_block(512, channels).unwrap() as u16; + let d = ima::decode(&e, channels, 512).unwrap(); + let d_alt = alt_ima_decode(&e, channels, 512); + let ima_wav = wav(0x11, channels, 512, 4, &per.to_le_bytes(), &e); + let e = ms::encode(&x, channels, 512).unwrap(); + let per = ms::samples_per_block(512, channels).unwrap() as u16; + let d2 = ms::decode(&e, channels, 512).unwrap(); + let mut extra = per.to_le_bytes().to_vec(); + extra.extend(7u16.to_le_bytes()); + extra.extend(&coefs); + let ms_wav = wav(2, channels, 512, 4, &extra, &e); + let e = yamaha::encode(&x, channels).unwrap(); + let d3 = yamaha::decode(&e, channels).unwrap(); + let y_wav = wav(0x20, channels, 512 * channels, 4, &[], &e); + vec![("ima", ima_wav, d, d_alt), ("ms", ms_wav, d2.clone(), d2), ("yamaha", y_wav, d3.clone(), d3)] + }; + for (name, bytes, ours, alt) in streams { + let file = dir.join(format!("our_{name}{channels}.wav")); + std::fs::write(&file, bytes).expect("write"); + let back = dir.join(format!("our_{name}{channels}.pcm")); + ffmpeg(&ff, &["-i", &s(&file), "-f", "s16le", "-acodec", "pcm_s16le", &s(&back)]); + let theirs = read_pcm(&back); + let n = ours.len().min(theirs.len()); + assert!(n + 2048 >= ours.len(), "{name} {channels}ch: ffmpeg decoded {} of {}", theirs.len(), ours.len()); + same_as_either(&format!("{name} {channels}ch, our stream"), &ours, &alt, &theirs); + } + } +} + +/// The audio tags of an FLV file: each tag's body without its flags byte. +fn flv_audio(bytes: &[u8]) -> Vec> { + let mut at = 13; + let mut out = Vec::new(); + while at + 11 <= bytes.len() { + let kind = bytes[at]; + let len = (usize::from(bytes[at + 1]) << 16) | (usize::from(bytes[at + 2]) << 8) | usize::from(bytes[at + 3]); + let body = &bytes[at + 11..(at + 11 + len).min(bytes.len())]; + if kind == 8 && body.len() > 1 { + assert_eq!(body[0] >> 4, 1, "SWF ADPCM tag"); + out.push(body[1..].to_vec()); + } + at += 11 + len + 4; + } + out +} + +/// An FLV file of 44.1 kHz audio tags with these ADPCM bodies. +fn flv(stereo: bool, tags: &[Vec]) -> Vec { + let mut out = b"FLV\x01\x04\0\0\0\x09\0\0\0\0".to_vec(); + let flags = (1u8 << 4) | (3 << 2) | (1 << 1) | u8::from(stereo); + for (i, t) in tags.iter().enumerate() { + let len = t.len() + 1; + let ts = (i * 4096 * 1000 / 44_100) as u32; + out.extend([ + 8, + (len >> 16) as u8, + (len >> 8) as u8, + len as u8, + (ts >> 16) as u8, + (ts >> 8) as u8, + ts as u8, + (ts >> 24) as u8, + 0, + 0, + 0, + flags, + ]); + out.extend(t); + out.extend((len as u32 + 11).to_be_bytes()); + } + out +} + +#[test] +fn flash_adpcm_matches_ffmpeg_in_both_directions() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("adpcm"); + for channels in [1usize, 2] { + let x = signal(channels, 4096 * 3); + let pcm = dir.join(format!("swf_in{channels}.pcm")); + write_pcm(&pcm, &x); + // ffmpeg encodes (it needs 44.1 kHz for FLV) + let file = dir.join(format!("swf{channels}.flv")); + ffmpeg(&ff, &["-f", "s16le", "-ar", "44100", "-ac", &channels.to_string(), "-i", &s(&pcm), "-c:a", "adpcm_swf", "-f", "flv", &s(&file)]); + let tags = flv_audio(&std::fs::read(&file).expect("read")); + assert!(tags.len() >= 3, "{} tags", tags.len()); + let back = dir.join(format!("swf{channels}.out.pcm")); + ffmpeg(&ff, &["-i", &s(&file), "-f", "s16le", "-acodec", "pcm_s16le", &s(&back)]); + let theirs = read_pcm(&back); + let ours: Vec = tags.iter().flat_map(|t| swf::decode(t, channels).expect("decode")).collect(); + assert_eq!(ours.len(), 4096 * 3 * channels); + assert_eq!(theirs.len(), 4096 * 3 * channels); + let alt: Vec = tags.iter().flat_map(|t| alt_swf_decode(t, channels)).collect(); + same_as_either(&format!("swf {channels}ch"), &ours, &alt, &theirs); + // ffmpeg decodes ours, one 4096-sample stream per tag + for bits in [2u32, 3, 4, 5] { + let tags: Vec> = x.chunks(4096 * channels).map(|c| swf::encode(c, channels, bits).expect("encode")).collect(); + let ours: Vec = tags.iter().flat_map(|t| swf::decode(t, channels).expect("decode")).collect(); + let file = dir.join(format!("our_swf{channels}_{bits}.flv")); + std::fs::write(&file, flv(channels == 2, &tags)).expect("write"); + let back = dir.join(format!("our_swf{channels}_{bits}.pcm")); + ffmpeg(&ff, &["-i", &s(&file), "-f", "s16le", "-acodec", "pcm_s16le", &s(&back)]); + let theirs = read_pcm(&back); + assert_eq!(ours.len(), 4096 * 3 * channels); + assert_eq!(theirs.len(), 4096 * 3 * channels); + let alt: Vec = tags.iter().flat_map(|t| alt_swf_decode(t, channels)).collect(); + same_as_either(&format!("swf {channels}ch {bits} bits, our stream"), &ours, &alt, &theirs); + } + } +} diff --git a/crates/audio/tests/dsd_bounds.rs b/crates/audio/tests/dsd_bounds.rs new file mode 100644 index 0000000000..5388f2aa45 --- /dev/null +++ b/crates/audio/tests/dsd_bounds.rs @@ -0,0 +1,208 @@ +//! Hostile container sizes must return errors, never wrap offsets or panic. +use craft_audio::dsd::{BitOrder, Error, RawAudio, encode_dsdiff, encode_dsf, read_metadata, read_raw}; + +#[test] +fn dsf_untrusted_sizes_are_checked() { + let original = encode_dsf(2_822_400, &[&[0x69; 8]], 64, 8).unwrap(); + for offset in [32, 84] { + // fmt and data chunk sizes + for size in [u64::MAX, u64::MAX - 31, 1 << 32] { + let mut bytes = original.clone(); + bytes[offset..offset + 8].copy_from_slice(&size.to_le_bytes()); + assert!(read_metadata(&bytes).is_err()); + assert!(read_raw(&bytes).is_err()); + } + } + let mut bytes = original; + bytes[64..72].copy_from_slice(&u64::MAX.to_le_bytes()); + assert_eq!(read_raw(&bytes), Err(Error::MemoryLimitExceeded)); +} + +#[test] +fn dsdiff_untrusted_top_level_and_property_sizes_are_checked() { + let original = encode_dsdiff(2_822_400, &[&[0xaa; 8]], 64).unwrap(); + let prop = original.windows(4).position(|w| w == b"PROP").unwrap(); + for offset in [4, 20, prop + 20] { + // FRM8, FVER, nested FS + for size in [u64::MAX, u64::MAX - 31, 1 << 32] { + let mut bytes = original.clone(); + bytes[offset..offset + 8].copy_from_slice(&size.to_be_bytes()); + assert!(read_metadata(&bytes).is_err()); + assert!(read_raw(&bytes).is_err()); + } + } +} + +#[test] +fn dsdiff_invalid_optional_disc_info_is_ignored_without_overflow() { + let mut bytes = encode_dsdiff(2_822_400, &[&[0xaa; 8]], 64).unwrap(); + let expected = read_metadata(&bytes).unwrap(); + bytes.extend_from_slice(b"DIIN"); + bytes.extend_from_slice(&12u64.to_be_bytes()); + bytes.extend_from_slice(b"DITI"); + bytes.extend_from_slice(&u64::MAX.to_be_bytes()); + let size = (bytes.len() - 12) as u64; + bytes[4..12].copy_from_slice(&size.to_be_bytes()); + // Optional malformed text is ignored on 64-bit; unrepresentable sizes may + // be rejected on narrower targets, but must never wrap or panic. + match read_metadata(&bytes) { + Ok(actual) => assert_eq!(actual, expected), + Err(Error::MemoryLimitExceeded) => {} + other => panic!("unexpected result: {other:?}"), + } +} + +#[test] +fn empty_raw_audio_rejects_invalid_decimation_factors() { + let bytes = encode_dsf(2_822_400, &[&[]], 0, 8).unwrap(); + let raw = RawAudio { metadata: read_metadata(&bytes).unwrap(), bit_order: BitOrder::LsbFirst, channel_bytes: Vec::new() }; + for factor in [0, 1, 7, 9, u32::MAX] { + assert!(matches!(raw.to_pcm_with_factor(factor), Err(Error::Unsupported(_)))); + } + for factor in [8, 16, 32, 64] { + assert_eq!(raw.to_pcm_with_factor(factor).unwrap().sample_rate, 2_822_400 / factor); + } +} + +#[test] +fn writers_reject_unbounded_output_before_allocating() { + let channels: [&[u8]; 8] = [&[]; 8]; + for count in [u64::MAX, (craft_audio::dsd::MAX_ENCODE_BYTES as u64 + 1) * 8] { + assert_eq!(encode_dsf(2_822_400, &channels, count, 8), Err(Error::MemoryLimitExceeded)); + assert_eq!(encode_dsdiff(2_822_400, &channels, count), Err(Error::MemoryLimitExceeded)); + } + assert!(matches!(encode_dsf(2_822_400, &[&[]], 0, u32::MAX), Err(Error::InvalidData(_)))); +} + +#[test] +fn encoded_sizes_match_actual_bytes_for_odd_and_even_payloads() { + for channels in 1..=8 { + for len in 0..=9 { + let data = vec![0x69; len]; + let planes = vec![data.as_slice(); channels]; + for block in [0, 1, 3, 8] { + // DSF has channel types for 1 to 6 channels only. + if channels > 6 { + assert!(matches!(encode_dsf(2_822_400, &planes, len as u64 * 8, block), Err(Error::Unsupported(_)))); + continue; + } + let dsf = encode_dsf(2_822_400, &planes, len as u64 * 8, block).unwrap(); + assert_eq!(u64::from_le_bytes(dsf[12..20].try_into().unwrap()), dsf.len() as u64); + assert_eq!(read_raw(&dsf).unwrap().channel_bytes, vec![data.clone(); channels]); + } + let dff = encode_dsdiff(2_822_400, &planes, len as u64 * 8).unwrap(); + assert_eq!(u64::from_be_bytes(dff[4..12].try_into().unwrap()) + 12, dff.len() as u64); + assert_eq!(read_raw(&dff).unwrap().channel_bytes, vec![data; channels]); + } + } +} + +#[test] +fn dsf_audio_stops_at_the_declared_data_chunk() { + for channels in 1..=2 { + let input = vec![&[0x33u8; 8][..]; channels]; + let mut bytes = encode_dsf(2_822_400, &input, 64, 8).unwrap(); + // Ask for an additional block without extending the data chunk. + bytes[64..72].copy_from_slice(&128u64.to_le_bytes()); + let expected = read_raw(&bytes).unwrap(); + for c in &expected.channel_bytes { + assert_eq!(&c[..8], &[0x33; 8]); + assert_eq!(&c[8..], &[0x69; 8]); + } + let metadata_offset = bytes.len() as u64; + bytes[20..28].copy_from_slice(&metadata_offset.to_le_bytes()); + bytes.extend_from_slice(b"ID3\x04\x00\x00\x00\x00\x00\x00"); + bytes.extend_from_slice(&[0xff; 32]); + let file_size = bytes.len() as u64; + bytes[12..20].copy_from_slice(&file_size.to_le_bytes()); + assert_eq!(read_raw(&bytes).unwrap().channel_bytes, expected.channel_bytes); + } +} + +#[test] +fn dsdiff_odd_payload_receives_pad_byte() { + let input = [&[0xaau8; 3][..]]; + let bytes = encode_dsdiff(2_822_400, &input, 24).unwrap(); + let raw = read_raw(&bytes).unwrap(); + assert_eq!(raw.channel_bytes[0].len(), 3); +} + +#[test] +fn dsf_declared_file_size_and_metadata_pointer_bounds() { + let valid = encode_dsf(2_822_400, &[&[0x69; 8]], 64, 8).unwrap(); + + // Undersized file size declaration (e.g. 28) + let mut undersized = valid.clone(); + undersized[12..20].copy_from_slice(&28u64.to_le_bytes()); + assert!(read_metadata(&undersized).is_err()); + assert!(read_raw(&undersized).is_err()); + + // Oversized file size declaration beyond buffer + let mut oversized = valid.clone(); + let big_size = valid.len() as u64 + 100; + oversized[12..20].copy_from_slice(&big_size.to_le_bytes()); + assert!(read_metadata(&oversized).is_err()); + assert!(read_raw(&oversized).is_err()); + + // Metadata pointer outside the declared file bounds + let mut outside_meta = valid.clone(); + let outside_offset = valid.len() as u64 + 10; + outside_meta[20..28].copy_from_slice(&outside_offset.to_le_bytes()); + assert!(read_metadata(&outside_meta).is_err()); + assert!(read_raw(&outside_meta).is_err()); + + // Metadata pointer overlapping data chunk + let mut overlap_meta = valid.clone(); + overlap_meta[20..28].copy_from_slice(&32u64.to_le_bytes()); + assert!(read_metadata(&overlap_meta).is_err()); + assert!(read_raw(&overlap_meta).is_err()); +} + +#[test] +fn dsdiff_channel_count_and_duplicate_chunks_are_validated() { + let base = encode_dsdiff(2_822_400, &[&[0xaa; 8]], 64).unwrap(); + + // Locate PROP chunk payload and CHNL subchunk + let prop_idx = base.windows(4).position(|w| w == b"PROP").unwrap(); + let chnl_idx = base.windows(4).position(|w| w == b"CHNL").unwrap(); + + // 1. Oversized channel count (e.g. 65535 or 9) + let mut bad_count = base.clone(); + bad_count[chnl_idx + 12..chnl_idx + 14].copy_from_slice(&65535u16.to_be_bytes()); + assert!(matches!(read_metadata(&bad_count), Err(Error::Unsupported(_)))); + assert!(matches!(read_raw(&bad_count), Err(Error::Unsupported(_)))); + + let mut zero_count = base.clone(); + zero_count[chnl_idx + 12..chnl_idx + 14].copy_from_slice(&0u16.to_be_bytes()); + assert!(matches!(read_metadata(&zero_count), Err(Error::Unsupported(_)))); + + // 2. Conflicting duplicate CHNL chunk in PROP + let mut dup_chnl = base.clone(); + let chnl_chunk = dup_chnl[chnl_idx..chnl_idx + 18].to_vec(); + dup_chnl.splice(chnl_idx..chnl_idx, chnl_chunk); + // Adjust PROP size and FRM8 size + let prop_size = u64::from_be_bytes(dup_chnl[prop_idx + 4..prop_idx + 12].try_into().unwrap()) + 18; + dup_chnl[prop_idx + 4..prop_idx + 12].copy_from_slice(&prop_size.to_be_bytes()); + let frm8_size = (dup_chnl.len() - 12) as u64; + dup_chnl[4..12].copy_from_slice(&frm8_size.to_be_bytes()); + assert_eq!(read_metadata(&dup_chnl), Err(Error::InvalidHeader("duplicate CHNL chunk"))); + + // 3. Duplicate FS chunk in PROP + let fs_idx = base.windows(4).position(|w| w == b"FS ").unwrap(); + let mut dup_fs = base.clone(); + let fs_chunk = dup_fs[fs_idx..fs_idx + 16].to_vec(); + dup_fs.splice(fs_idx..fs_idx, fs_chunk); + let prop_size = u64::from_be_bytes(dup_fs[prop_idx + 4..prop_idx + 12].try_into().unwrap()) + 16; + dup_fs[prop_idx + 4..prop_idx + 12].copy_from_slice(&prop_size.to_be_bytes()); + let frm8_size = (dup_fs.len() - 12) as u64; + dup_fs[4..12].copy_from_slice(&frm8_size.to_be_bytes()); + assert_eq!(read_metadata(&dup_fs), Err(Error::InvalidHeader("duplicate FS chunk"))); +} + +#[test] +#[cfg(target_pointer_width = "32")] +fn dsdiff_32bit_padding_overflow_is_rejected() { + let dummy = [&[][..]]; + let result = encode_dsdiff(2_822_400, &dummy, 34_359_738_360); + assert_eq!(result, Err(Error::MemoryLimitExceeded)); +} diff --git a/crates/audio/tests/dsd_metadata.rs b/crates/audio/tests/dsd_metadata.rs new file mode 100644 index 0000000000..74fd16f314 --- /dev/null +++ b/crates/audio/tests/dsd_metadata.rs @@ -0,0 +1,155 @@ +//! Channel labels, text metadata and padding in the DSF and DSDIFF readers and writers. +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +use craft_audio::dsd::{encode_dsdiff, encode_dsf, read_metadata, read_raw}; + +const RATE: u32 = 2_822_400; + +fn planes(channels: usize, len: usize) -> Vec> { + vec![vec![0x69; len]; channels] +} + +fn refs(p: &[Vec]) -> Vec<&[u8]> { + p.iter().map(Vec::as_slice).collect() +} + +#[test] +fn dsdiff_names_surround_channels_by_layout() { + for (n, names) in [ + (5, vec!["Mono/Left", "Right", "Center", "LeftSurround", "RightSurround"]), + (6, vec!["Mono/Left", "Right", "Center", "LFE", "LeftSurround", "RightSurround"]), + ] { + let p = planes(n, 4); + let bytes = encode_dsdiff(RATE, &refs(&p), 32).unwrap(); + assert_eq!(read_metadata(&bytes).unwrap().channel_names, names, "{n} channels"); + } +} + +#[test] +fn dsdiff_odd_audio_is_padded_and_counted_in_the_file_size() { + let p = planes(1, 1); + let bytes = encode_dsdiff(RATE, &refs(&p), 8).unwrap(); + assert_eq!(bytes.len() % 2, 0); + assert_eq!(u64::from_be_bytes(bytes[4..12].try_into().unwrap()) + 12, bytes.len() as u64); + assert_eq!(*bytes.last().unwrap(), 0, "pad byte"); + assert_eq!(read_raw(&bytes).unwrap().channel_bytes, vec![vec![0x69]]); +} + +fn with_diin(mut bytes: Vec, sub: &[u8; 4], payload: &[u8]) -> Vec { + let mut sub_chunk = Vec::new(); + sub_chunk.extend_from_slice(sub); + sub_chunk.extend_from_slice(&(payload.len() as u64).to_be_bytes()); + sub_chunk.extend_from_slice(payload); + if payload.len() % 2 == 1 { + sub_chunk.push(0); + } + bytes.extend_from_slice(b"DIIN"); + bytes.extend_from_slice(&(sub_chunk.len() as u64).to_be_bytes()); + bytes.extend_from_slice(&sub_chunk); + let size = (bytes.len() - 12) as u64; + bytes[4..12].copy_from_slice(&size.to_be_bytes()); + bytes +} + +#[test] +fn dsdiff_title_and_artist_skip_the_character_count() { + let p = planes(2, 4); + let base = encode_dsdiff(RATE, &refs(&p), 32).unwrap(); + let mut payload = 4u32.to_be_bytes().to_vec(); + payload.extend_from_slice(b"Test"); + let bytes = with_diin(base.clone(), b"DITI", &payload); + assert_eq!(read_metadata(&bytes).unwrap().title.as_deref(), Some("Test")); + let bytes = with_diin(base.clone(), b"DIAR", &payload); + assert_eq!(read_metadata(&bytes).unwrap().artist.as_deref(), Some("Test")); + // A count longer than the chunk is not text. + let mut bad = 99u32.to_be_bytes().to_vec(); + bad.extend_from_slice(b"Test"); + assert_eq!(read_metadata(&with_diin(base, b"DITI", &bad)).unwrap().title, None); +} + +#[test] +fn dsf_channel_types_match_the_channel_count() { + for (n, expect) in [(1, 1), (2, 2), (3, 3), (4, 4), (5, 6), (6, 7)] { + let p = planes(n, 4); + let bytes = encode_dsf(RATE, &refs(&p), 32, 4096).unwrap(); + assert_eq!(u32::from_le_bytes(bytes[48..52].try_into().unwrap()), expect, "{n} channels"); + assert_eq!(read_metadata(&bytes).unwrap().channel_names.len(), n); + } + let p = planes(7, 4); + assert!(encode_dsf(RATE, &refs(&p), 32, 4096).is_err()); +} + +#[test] +fn dsf_type_5_names_the_fourth_channel_lfe() { + let p = planes(4, 4); + let mut bytes = encode_dsf(RATE, &refs(&p), 32, 4096).unwrap(); + bytes[48..52].copy_from_slice(&5u32.to_le_bytes()); + assert_eq!(read_metadata(&bytes).unwrap().channel_names, ["FrontLeft", "FrontRight", "Center", "LFE"]); +} + +fn id3(version: u8, frames: &[u8]) -> Vec { + let n = frames.len(); + let mut tag = vec![b'I', b'D', b'3', version, 0, 0]; + tag.extend_from_slice(&[(n >> 21) as u8 & 0x7F, (n >> 14) as u8 & 0x7F, (n >> 7) as u8 & 0x7F, n as u8 & 0x7F]); + tag.extend_from_slice(frames); + tag +} + +fn frame(version: u8, id: &[u8; 4], body: &[u8]) -> Vec { + let n = body.len(); + let mut f = id.to_vec(); + if version >= 4 { + f.extend_from_slice(&[(n >> 21) as u8 & 0x7F, (n >> 14) as u8 & 0x7F, (n >> 7) as u8 & 0x7F, n as u8 & 0x7F]); + } else { + f.extend_from_slice(&(n as u32).to_be_bytes()); + } + f.extend_from_slice(&[0, 0]); + f.extend_from_slice(body); + f +} + +fn dsf_with_tag(tag: &[u8]) -> Vec { + let p = planes(2, 4); + let mut bytes = encode_dsf(RATE, &refs(&p), 32, 4096).unwrap(); + let offset = bytes.len() as u64; + bytes[20..28].copy_from_slice(&offset.to_le_bytes()); + bytes.extend_from_slice(tag); + let file_size = bytes.len() as u64; + bytes[12..20].copy_from_slice(&file_size.to_le_bytes()); + bytes +} + +#[test] +fn id3v24_frame_sizes_are_synchsafe() { + // A 128-byte frame has the synchsafe size 00 00 01 00; read as plain big-endian it is 256. + let mut frames = frame(4, b"APIC", &[0u8; 128]); + let mut title = vec![3]; + title.extend_from_slice(b"After art"); + frames.extend(frame(4, b"TIT2", &title)); + let m = read_metadata(&dsf_with_tag(&id3(4, &frames))).unwrap(); + assert_eq!(m.title.as_deref(), Some("After art")); +} + +#[test] +fn id3v23_frame_sizes_stay_big_endian() { + let mut frames = frame(3, b"APIC", &[0u8; 128]); + let mut title = vec![3]; + title.extend_from_slice(b"Three"); + frames.extend(frame(3, b"TIT2", &title)); + let m = read_metadata(&dsf_with_tag(&id3(3, &frames))).unwrap(); + assert_eq!(m.title.as_deref(), Some("Three")); +} + +#[test] +fn id3_text_encodings() { + let cases: [(&[u8], &str); 5] = [ + (b"\x00Caf\xe9", "Café"), + (b"\x03Caf\xc3\xa9", "Café"), + (b"\x02\x00A\x00B", "AB"), + (b"\x01\xFF\xFEA\x00B\x00", "AB"), + (b"\x01\xFE\xFF\x00A\x00B", "AB"), + ]; + for (body, expect) in cases { + let m = read_metadata(&dsf_with_tag(&id3(4, &frame(4, b"TIT2", body)))).unwrap(); + assert_eq!(m.title.as_deref(), Some(expect), "{body:?}"); + } +} diff --git a/crates/audio/tests/dsd_oracle.rs b/crates/audio/tests/dsd_oracle.rs new file mode 100644 index 0000000000..94334485b9 --- /dev/null +++ b/crates/audio/tests/dsd_oracle.rs @@ -0,0 +1,347 @@ +//! DSD (DSF and DSDIFF) decoding and decimation tests, including oracle tests against FFmpeg +//! (skipped when ffmpeg is absent, a failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`). + +use craft_audio::dsd::{BitOrder, Format, decode, decode_with_factor, encode_dsdiff, encode_dsf, filter::SILENCE_LSBF, read_metadata, read_raw}; +use std::process::Command; + +/// Generates a test DSD 1-bit stream with various features (silence, alternating tones, noise). +fn make_test_stream(num_bytes: usize, seed: u8) -> Vec { + let mut out = Vec::with_capacity(num_bytes); + let mut s = seed as u32; + for i in 0..num_bytes { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + let byte = match (i / 128) % 4 { + 0 => SILENCE_LSBF, // DSD silence + 1 => 0x55, // Alternate silence pattern + 2 => (s & 0xFF) as u8, + _ => { + if (i / 16) % 2 == 0 { + 0xFF // full positive level + } else { + 0x00 // full negative level + } + } + }; + out.push(byte); + } + out +} + +#[test] +fn roundtrip_dsf() { + let sample_rate = 2_822_400; + let block_size = 4096; + let ch0 = make_test_stream(8192, 42); + let ch1 = make_test_stream(8192, 99); + let sample_count = 8192 * 8; + + let dsf_bytes = encode_dsf(sample_rate, &[&ch0, &ch1], sample_count, block_size).expect("encode_dsf"); + + let meta = read_metadata(&dsf_bytes).expect("read_metadata"); + assert_eq!(meta.format, Format::Dsf); + assert_eq!(meta.sample_rate, sample_rate); + assert_eq!(meta.channels, 2); + assert_eq!(meta.sample_count, sample_count); + + let raw = read_raw(&dsf_bytes).expect("read_raw"); + assert_eq!(raw.bit_order, BitOrder::LsbFirst); + assert_eq!(raw.channel_bytes.len(), 2); + assert_eq!(raw.channel_bytes[0], ch0); + assert_eq!(raw.channel_bytes[1], ch1); + + let decoded = decode(&dsf_bytes).expect("decode"); + assert_eq!(decoded.sample_rate, sample_rate / 8); + assert_eq!(decoded.channels.len(), 2); + assert_eq!(decoded.channels[0].len(), 8192); + assert_eq!(decoded.channels[1].len(), 8192); +} + +#[test] +fn roundtrip_dsdiff() { + let sample_rate = 2_822_400; + let ch0 = make_test_stream(4096, 17); + let ch1 = make_test_stream(4096, 73); + let sample_count = 4096 * 8; + + let dff_bytes = encode_dsdiff(sample_rate, &[&ch0, &ch1], sample_count).expect("encode_dsdiff"); + + let meta = read_metadata(&dff_bytes).expect("read_metadata"); + assert_eq!(meta.format, Format::Dsdiff); + assert_eq!(meta.sample_rate, sample_rate); + assert_eq!(meta.channels, 2); + assert_eq!(meta.sample_count, sample_count); + + let raw = read_raw(&dff_bytes).expect("read_raw"); + assert_eq!(raw.bit_order, BitOrder::MsbFirst); + assert_eq!(raw.channel_bytes.len(), 2); + assert_eq!(raw.channel_bytes[0], ch0); + assert_eq!(raw.channel_bytes[1], ch1); + + let decoded = decode(&dff_bytes).expect("decode"); + assert_eq!(decoded.sample_rate, sample_rate / 8); + assert_eq!(decoded.channels.len(), 2); + assert_eq!(decoded.channels[0].len(), 4096); + assert_eq!(decoded.channels[1].len(), 4096); +} + +#[test] +fn decimation_factors() { + let sample_rate = 2_822_400; + let ch0 = make_test_stream(2048, 123); + let dsf_bytes = encode_dsf(sample_rate, &[&ch0], 2048 * 8, 4096).expect("encode_dsf"); + + // Factor 8 -> 352,800 Hz + let dec8 = decode_with_factor(&dsf_bytes, 8).expect("factor 8"); + assert_eq!(dec8.sample_rate, 352_800); + assert_eq!(dec8.channels[0].len(), 2048); + + // Factor 16 -> 176,400 Hz + let dec16 = decode_with_factor(&dsf_bytes, 16).expect("factor 16"); + assert_eq!(dec16.sample_rate, 176_400); + assert_eq!(dec16.channels[0].len(), 1024); + + // Factor 32 -> 88,200 Hz + let dec32 = decode_with_factor(&dsf_bytes, 32).expect("factor 32"); + assert_eq!(dec32.sample_rate, 88_200); + assert_eq!(dec32.channels[0].len(), 512); + + // Factor 64 -> 44,100 Hz + let dec64 = decode_with_factor(&dsf_bytes, 64).expect("factor 64"); + assert_eq!(dec64.sample_rate, 44_100); + assert_eq!(dec64.channels[0].len(), 256); + + // Invalid factor + assert!(decode_with_factor(&dsf_bytes, 12).is_err()); +} + +#[test] +fn silence_and_dc_levels() { + let sample_rate = 2_822_400; + // Pure silence + let silence = vec![SILENCE_LSBF; 1024]; + let dsf_bytes = encode_dsf(sample_rate, &[&silence], 1024 * 8, 4096).expect("encode"); + let dec = decode(&dsf_bytes).expect("decode"); + for &sample in &dec.channels[0][12..] { + assert!(sample.abs() < 1e-4, "silence sample should be ~0.0, got {sample}"); + } + + // Full positive level + let ones = vec![0xFF; 1024]; + let dsf_ones = encode_dsf(sample_rate, &[&ones], 1024 * 8, 4096).expect("encode"); + let dec_ones = decode(&dsf_ones).expect("decode"); + for &sample in &dec_ones.channels[0][12..] { + assert!((sample - 1.0).abs() < 1e-4, "ones sample should be ~1.0, got {sample}"); + } + + // Full negative level + let zeros = vec![0x00; 1024]; + let dsf_zeros = encode_dsf(sample_rate, &[&zeros], 1024 * 8, 4096).expect("encode"); + let dec_zeros = decode(&dsf_zeros).expect("decode"); + for &sample in &dec_zeros.channels[0][12..] { + assert!((sample - (-1.0)).abs() < 1e-4, "zeros sample should be ~-1.0, got {sample}"); + } +} + +#[test] +fn multichannel_dsf_and_dsdiff() { + let sample_rate = 2_822_400; + // Test 6-channel (5.1 surround) + let channels: Vec> = (0..6).map(|c| make_test_stream(1024, (c * 23 + 5) as u8)).collect(); + let ch_refs: Vec<&[u8]> = channels.iter().map(Vec::as_slice).collect(); + + let dsf = encode_dsf(sample_rate, &ch_refs, 1024 * 8, 512).expect("encode 5.1 dsf"); + let dsf_meta = read_metadata(&dsf).expect("meta 5.1 dsf"); + assert_eq!(dsf_meta.channels, 6); + let dsf_dec = decode(&dsf).expect("decode 5.1 dsf"); + assert_eq!(dsf_dec.channels.len(), 6); + assert_eq!(dsf_dec.channels[0].len(), 1024); + + let dff = encode_dsdiff(sample_rate, &ch_refs, 1024 * 8).expect("encode 5.1 dsdiff"); + let dff_meta = read_metadata(&dff).expect("meta 5.1 dsdiff"); + assert_eq!(dff_meta.channels, 6); + let dff_dec = decode(&dff).expect("decode 5.1 dsdiff"); + assert_eq!(dff_dec.channels.len(), 6); + assert_eq!(dff_dec.channels[0].len(), 1024); +} + +#[test] +fn dst_compression_rejected() { + // Construct a minimal DSDIFF with CMPR = 'DST ' + let mut dff = encode_dsdiff(2_822_400, &[&[0xAA; 64]], 512).expect("encode"); + // Find 'DSD ' in CMPR chunk and replace with 'DST ' + let mut replaced = false; + for i in 0..dff.len().saturating_sub(8) { + if &dff[i..i + 4] == b"CMPR" && &dff[i + 12..i + 16] == b"DSD " { + dff[i + 12..i + 16].copy_from_slice(b"DST "); + replaced = true; + break; + } + } + assert!(replaced, "should find CMPR DSD chunk"); + assert!(read_metadata(&dff).is_err(), "DST compression should be rejected as unsupported"); + assert!(decode(&dff).is_err(), "DST compression should be rejected as unsupported"); +} + +#[test] +fn id3v2_tag_in_dsf() { + let sample_rate = 2_822_400; + let mut dsf = encode_dsf(sample_rate, &[&[0x69; 128]], 1024, 512).expect("encode"); + let metadata_offset = dsf.len() as u64; + + // Append simple ID3v2.3 tag with TIT2="Test Title" and TPE1="Test Artist" + let mut id3 = Vec::new(); + id3.extend_from_slice(b"ID3"); + id3.push(3); // v2.3 + id3.push(0); // revision + id3.push(0); // flags + + let mut frames = Vec::new(); + // TIT2 frame + frames.extend_from_slice(b"TIT2"); + let title_bytes = b"\x03DSD Master"; // UTF-8 + frames.extend_from_slice(&(title_bytes.len() as u32).to_be_bytes()); + frames.extend_from_slice(&[0, 0]); // flags + frames.extend_from_slice(title_bytes); + + // TPE1 frame + frames.extend_from_slice(b"TPE1"); + let artist_bytes = b"\x03The Audiophiles"; + frames.extend_from_slice(&(artist_bytes.len() as u32).to_be_bytes()); + frames.extend_from_slice(&[0, 0]); // flags + frames.extend_from_slice(artist_bytes); + + // synchsafe size + let tag_len = frames.len(); + id3.push(((tag_len >> 21) & 0x7F) as u8); + id3.push(((tag_len >> 14) & 0x7F) as u8); + id3.push(((tag_len >> 7) & 0x7F) as u8); + id3.push((tag_len & 0x7F) as u8); + id3.extend_from_slice(&frames); + + dsf.extend_from_slice(&id3); + // Update file size and metadata_offset of 'DSD ' chunk + let file_size = dsf.len() as u64; + dsf[12..20].copy_from_slice(&file_size.to_le_bytes()); + dsf[20..28].copy_from_slice(&metadata_offset.to_le_bytes()); + + let meta = read_metadata(&dsf).expect("read metadata with ID3"); + assert_eq!(meta.title.as_deref(), Some("DSD Master")); + assert_eq!(meta.artist.as_deref(), Some("The Audiophiles")); +} + +#[test] +fn raw_audio_bit_order_conversion() { + let ch0 = vec![0b00000001, 0b10000000]; + let dsf = encode_dsf(2_822_400, &[&ch0], 16, 512).expect("encode"); + let mut raw = read_raw(&dsf).expect("read raw"); + assert_eq!(raw.bit_order, BitOrder::LsbFirst); + assert_eq!(raw.channel_bytes[0], ch0); + + raw.convert_bit_order(BitOrder::MsbFirst); + assert_eq!(raw.bit_order, BitOrder::MsbFirst); + assert_eq!(raw.channel_bytes[0], vec![0b10000000, 0b00000001]); +} + +#[test] +fn hostile_inputs() { + // Truncated headers + assert!(read_metadata(b"").is_err()); + assert!(read_metadata(b"DSD ").is_err()); + assert!(read_metadata(b"FRM8").is_err()); + + // Bad magic + assert!(read_metadata(b"RIFF\x00\x00\x00\x00WAVE").is_err()); + + // Truncated DSF chunk + let mut bad_dsf = encode_dsf(2_822_400, &[&[0x69; 100]], 800, 4096).expect("encode"); + bad_dsf.truncate(50); + assert!(decode(&bad_dsf).is_err()); + + // Corrupted fmt chunk size + let mut bad_fmt = encode_dsf(2_822_400, &[&[0x69; 100]], 800, 4096).expect("encode"); + bad_fmt[32] = 0; // bits_per_sample = 0 + assert!(decode(&bad_fmt).is_err()); + + // Absurd channels + let mut bad_ch = encode_dsf(2_822_400, &[&[0x69; 100]], 800, 4096).expect("encode"); + bad_ch[28 + 24] = 99; // channel_count = 99 + assert!(decode(&bad_ch).is_err()); +} + +#[test] +fn ffmpeg_dsf_oracle() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("dsd"); + + let sample_rate = 2_822_400; + let block_size = 4096; + let ch0 = make_test_stream(16384, 11); + let ch1 = make_test_stream(16384, 88); + let sample_count = 16384 * 8; + + let dsf_bytes = encode_dsf(sample_rate, &[&ch0, &ch1], sample_count, block_size).expect("encode_dsf"); + let dsf_path = dir.join("test_oracle.dsf"); + std::fs::write(&dsf_path, &dsf_bytes).expect("write fixture"); + + let raw_out_path = dir.join("ffmpeg_dsf.raw"); + let status = Command::new(&ffmpeg).arg("-y").arg("-i").arg(&dsf_path).arg("-f").arg("f32le").arg(&raw_out_path).output().expect("run ffmpeg"); + assert!(status.status.success(), "ffmpeg failed: {}", String::from_utf8_lossy(&status.stderr)); + + let raw_bytes = std::fs::read(&raw_out_path).expect("read raw"); + let ffmpeg_floats: Vec = raw_bytes.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect(); + + let decoded = decode(&dsf_bytes).expect("our decode"); + assert_eq!(decoded.channels.len(), 2); + assert_eq!(decoded.channels[0].len(), 16384); + assert_eq!(decoded.channels[1].len(), 16384); + + // FFmpeg interleaves L0, R0, L1, R1... + // Compare samples after filter startup (sample 12 onwards). + for i in 12..16000 { + let ff_l = ffmpeg_floats[i * 2]; + let ff_r = ffmpeg_floats[i * 2 + 1]; + let our_l = decoded.channels[0][i]; + let our_r = decoded.channels[1][i]; + + assert!((ff_l - our_l).abs() < 1e-4, "sample {i} left mismatch: ffmpeg={ff_l}, ours={our_l}"); + assert!((ff_r - our_r).abs() < 1e-4, "sample {i} right mismatch: ffmpeg={ff_r}, ours={our_r}"); + } +} + +#[test] +fn ffmpeg_dsdiff_oracle() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("dsd"); + + let sample_rate = 2_822_400; + let ch0 = make_test_stream(8192, 33); + let ch1 = make_test_stream(8192, 77); + let sample_count = 8192 * 8; + + let dff_bytes = encode_dsdiff(sample_rate, &[&ch0, &ch1], sample_count).expect("encode_dsdiff"); + let dff_path = dir.join("test_oracle.dff"); + std::fs::write(&dff_path, &dff_bytes).expect("write fixture"); + + let raw_out_path = dir.join("ffmpeg_dff.raw"); + let status = Command::new(&ffmpeg).arg("-y").arg("-i").arg(&dff_path).arg("-f").arg("f32le").arg(&raw_out_path).output().expect("run ffmpeg"); + assert!(status.status.success(), "ffmpeg failed: {}", String::from_utf8_lossy(&status.stderr)); + + let raw_bytes = std::fs::read(&raw_out_path).expect("read raw"); + let ffmpeg_floats: Vec = raw_bytes.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect(); + + let decoded = decode(&dff_bytes).expect("our decode"); + assert_eq!(decoded.channels.len(), 2); + assert_eq!(decoded.channels[0].len(), 8192); + + for i in 12..8000 { + let ff_l = ffmpeg_floats[i * 2]; + let ff_r = ffmpeg_floats[i * 2 + 1]; + let our_l = decoded.channels[0][i]; + let our_r = decoded.channels[1][i]; + + assert!((ff_l - our_l).abs() < 1e-4, "sample {i} left mismatch: ffmpeg={ff_l}, ours={our_l}"); + assert!((ff_r - our_r).abs() < 1e-4, "sample {i} right mismatch: ffmpeg={ff_r}, ours={our_r}"); + } +} diff --git a/crates/audio/tests/g722_oracle.rs b/crates/audio/tests/g722_oracle.rs new file mode 100644 index 0000000000..3da059011f --- /dev/null +++ b/crates/audio/tests/g722_oracle.rs @@ -0,0 +1,201 @@ +//! ITU-T G.722 SB-ADPCM tests and FFmpeg oracle comparisons. +//! +//! Skipped when FFmpeg is not found, unless `CRAFT_MEDIA_REQUIRE_ORACLES=1` is set. + +use craft_audio::g722::{Decoder, Encoder, Error, decode, decode_interleaved, encode, encode_interleaved}; +use std::process::Command; + +fn generate_signal(samples: usize, freq: f64) -> Vec { + (0..samples) + .map(|i| { + let t = i as f64 / 16000.0; + let val = (2.0 * std::f64::consts::PI * freq * t).sin() * 10000.0; + val as i16 + }) + .collect() +} + +#[test] +fn test_silence_roundtrip() { + let pcm = vec![0i16; 640]; + let encoded = encode(&pcm).expect("encode"); + assert_eq!(encoded.len(), 320); + let decoded = decode(&encoded).expect("decode"); + assert_eq!(decoded.len(), 640); + for &s in &decoded[40..] { + assert!(s.abs() <= 6, "silence sample out of bounds: {s}"); + } +} + +#[test] +fn test_sine_wave_roundtrip_fidelity() { + let pcm = generate_signal(1600, 1000.0); + let encoded = encode(&pcm).expect("encode"); + let decoded = decode(&encoded).expect("decode"); + assert_eq!(decoded.len(), pcm.len()); + + // Compute normalized cross-correlation accounting for filter group delay (approx 22 samples) + let delay = 22; + let mut num = 0.0; + let mut den_orig = 0.0; + let mut den_dec = 0.0; + for i in 0..(pcm.len() - delay) { + let o = pcm[i] as f64; + let d = decoded[i + delay] as f64; + num += o * d; + den_orig += o * o; + den_dec += d * d; + } + let corr = num / (den_orig.sqrt() * den_dec.sqrt()); + assert!(corr > 0.98, "reconstructed sine wave correlation too low: {corr}"); +} + +#[test] +fn test_stereo_multichannel_roundtrip() { + let left = generate_signal(800, 440.0); + let right = generate_signal(800, 880.0); + let mut interleaved = Vec::with_capacity(1600); + for i in 0..800 { + interleaved.push(left[i]); + interleaved.push(right[i]); + } + let encoded = encode_interleaved(&interleaved, 2).expect("encode stereo"); + assert_eq!(encoded.len(), 800); + let decoded = decode_interleaved(&encoded, 2).expect("decode stereo"); + assert_eq!(decoded.len(), 1600); +} + +#[test] +fn test_hostile_inputs_do_not_crash() { + let garbage = [0xFF, 0x00, 0xAA, 0x55, 0x12, 0x34, 0x89, 0xFE]; + let decoded = decode(&garbage).expect("decode garbage"); + assert_eq!(decoded.len(), 16); + + assert_eq!(encode(&[1, 2, 3]), Err(Error::OddSampleCount(3))); + assert_eq!(decode_interleaved(&[0; 8], 0), Err(Error::InvalidChannels(0))); + assert_eq!(decode_interleaved(&[0; 8], 9), Err(Error::InvalidChannels(9))); +} + +#[test] +fn test_encoder_and_decoder_reset() { + let mut enc = Encoder::new(); + let mut dec = Decoder::new(); + let pcm = generate_signal(200, 1000.0); + + let mut out1 = Vec::new(); + enc.encode(&pcm, &mut out1).expect("enc 1"); + let mut dec1 = Vec::new(); + dec.decode(&out1, &mut dec1).expect("dec 1"); + + enc.reset(); + dec.reset(); + + let mut out2 = Vec::new(); + enc.encode(&pcm, &mut out2).expect("enc 2"); + let mut dec2 = Vec::new(); + dec.decode(&out2, &mut dec2).expect("dec 2"); + + assert_eq!(out1, out2); + assert_eq!(dec1, dec2); +} + +#[test] +fn test_ffmpeg_g722_oracle_encoder_and_decoder() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("g722_oracle"); + + // 1. Generate test 1000 Hz tone at 16 kHz mono (800 samples = 0.05s) + let pcm = generate_signal(800, 1000.0); + let pcm_path = dir.join("input.s16le"); + let mut pcm_bytes = Vec::with_capacity(pcm.len() * 2); + for &s in &pcm { + pcm_bytes.extend_from_slice(&s.to_le_bytes()); + } + std::fs::write(&pcm_path, &pcm_bytes).expect("write pcm"); + + // 2. FFmpeg encodes the PCM to G.722 + let ff_g722_path = dir.join("ffmpeg.g722"); + let out = Command::new(&ff) + .args(["-y", "-f", "s16le", "-ar", "16000", "-ac", "1", "-i"]) + .arg(&pcm_path) + .args(["-f", "g722"]) + .arg(&ff_g722_path) + .output() + .expect("ffmpeg encode"); + assert!(out.status.success(), "ffmpeg encode failed: {}", String::from_utf8_lossy(&out.stderr)); + let ff_g722_bytes = std::fs::read(&ff_g722_path).expect("read ff g722"); + + // 3. FFmpeg decodes its own G.722 back to PCM + let ff_pcm_back_path = dir.join("ff_back.s16le"); + let out = Command::new(&ff) + .args(["-y", "-f", "g722", "-i"]) + .arg(&ff_g722_path) + .args(["-f", "s16le", "-ar", "16000", "-ac", "1"]) + .arg(&ff_pcm_back_path) + .output() + .expect("ffmpeg decode"); + assert!(out.status.success(), "ffmpeg decode failed: {}", String::from_utf8_lossy(&out.stderr)); + let ff_back_bytes = std::fs::read(&ff_pcm_back_path).expect("read ff back pcm"); + let ff_back_samples: Vec = ff_back_bytes.as_chunks::<2>().0.iter().map(|c| i16::from_le_bytes(*c)).collect(); + + // 4. Our decoder decodes FFmpeg's G.722 stream + let our_decoded_from_ff = decode(&ff_g722_bytes).expect("our decode of ff stream"); + assert_eq!(our_decoded_from_ff.len(), ff_back_samples.len()); + + // Verify sample-by-sample match against FFmpeg decoder: within ±1 LSB tolerance + let mut max_diff: i32 = 0; + for (i, (&ours, &theirs)) in our_decoded_from_ff.iter().zip(&ff_back_samples).enumerate() { + let diff = (ours as i32 - theirs as i32).abs(); + if diff > max_diff { + max_diff = diff; + } + assert!(diff <= 1, "sample {i} mismatch between our decoder and FFmpeg decoder: ours {ours}, theirs {theirs}"); + } + assert!(max_diff <= 1, "max diff {max_diff} exceeded tolerance"); + + // 5. Our encoder encodes the PCM to G.722 + let our_encoded_bytes = encode(&pcm).expect("our encode"); + let our_g722_path = dir.join("ours.g722"); + std::fs::write(&our_g722_path, &our_encoded_bytes).expect("write our g722"); + + // 6. FFmpeg decodes our encoded G.722 stream + let our_decoded_by_ff_path = dir.join("our_by_ff.s16le"); + let out = Command::new(&ff) + .args(["-y", "-f", "g722", "-i"]) + .arg(&our_g722_path) + .args(["-f", "s16le", "-ar", "16000", "-ac", "1"]) + .arg(&our_decoded_by_ff_path) + .output() + .expect("ffmpeg decode of our stream"); + assert!(out.status.success(), "ffmpeg decode of our stream failed: {}", String::from_utf8_lossy(&out.stderr)); + let our_by_ff_bytes = std::fs::read(&our_decoded_by_ff_path).expect("read our by ff pcm"); + let our_by_ff_samples: Vec = our_by_ff_bytes.as_chunks::<2>().0.iter().map(|c| i16::from_le_bytes(*c)).collect(); + + // Verify that our decoder and FFmpeg decoder produce identical samples on our stream + let our_decoded_from_ours = decode(&our_encoded_bytes).expect("our decode of our stream"); + assert_eq!(our_decoded_from_ours.len(), our_by_ff_samples.len()); + let mut max_diff_ours: i32 = 0; + for (i, (&ours, &theirs)) in our_decoded_from_ours.iter().zip(&our_by_ff_samples).enumerate() { + let diff = (ours as i32 - theirs as i32).abs(); + if diff > max_diff_ours { + max_diff_ours = diff; + } + assert!(diff <= 1, "sample {i} on our stream mismatch between our decoder and FFmpeg decoder: ours {ours}, theirs {theirs}"); + } + assert!(max_diff_ours <= 1, "max diff {max_diff_ours} exceeded tolerance"); + + // Also check high fidelity correlation of our stream decoded by FFmpeg to original signal + let delay = 22; + let mut num = 0.0; + let mut den_orig = 0.0; + let mut den_dec = 0.0; + for i in 0..(pcm.len() - delay) { + let o = pcm[i] as f64; + let d = our_by_ff_samples[i + delay] as f64; + num += o * d; + den_orig += o * o; + den_dec += d * d; + } + let corr = num / (den_orig.sqrt() * den_dec.sqrt()); + assert!(corr > 0.98, "correlation of our encoded stream decoded by FFmpeg is too low: {corr}"); +} diff --git a/crates/audio/tests/g726_oracle.rs b/crates/audio/tests/g726_oracle.rs new file mode 100644 index 0000000000..ec875e2c47 --- /dev/null +++ b/crates/audio/tests/g726_oracle.rs @@ -0,0 +1,218 @@ +#![cfg(feature = "g726")] + +use craft_audio::g726::{BitPacking, BitRate, Decoder, Encoder, decode, decode_interleaved, encode, encode_interleaved}; +use std::process::Command; + +fn pearson_correlation(x: &[i16], y: &[i16]) -> f64 { + let n = x.len().min(y.len()); + if n == 0 { + return 0.0; + } + let x = &x[..n]; + let y = &y[..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 find_ffmpeg() -> Option { + for cmd in ["ffmpeg", "/opt/homebrew/bin/ffmpeg", "/usr/bin/ffmpeg"] { + if Command::new(cmd).arg("-version").output().is_ok() { + return Some(cmd.to_string()); + } + } + None +} + +#[test] +fn test_silence_roundtrip() { + let pcm = vec![0i16; 80]; + for rate in [BitRate::Rate16k, BitRate::Rate24k, BitRate::Rate32k, BitRate::Rate40k] { + for packing in [BitPacking::BigEndian, BitPacking::LittleEndian] { + let enc = encode(&pcm, rate, packing).expect("encode"); + let dec = decode(&enc, rate, packing).expect("decode"); + assert_eq!(dec.len(), pcm.len()); + for &s in &dec { + assert!(s.abs() <= 16, "silence sample too large: {s}"); + } + } + } +} + +#[test] +fn test_sine_wave_roundtrip_fidelity() { + let sample_rate = 8000.0; + let freq = 300.0; + let num_samples = 160; + let mut pcm = Vec::with_capacity(num_samples); + for i in 0..num_samples { + let t = i as f64 / sample_rate; + let s = (4000.0 * (2.0 * std::f64::consts::PI * freq * t).sin()) as i16; + pcm.push(s); + } + + for rate in [BitRate::Rate16k, BitRate::Rate24k, BitRate::Rate32k, BitRate::Rate40k] { + for packing in [BitPacking::BigEndian, BitPacking::LittleEndian] { + let enc = encode(&pcm, rate, packing).expect("encode sine"); + let dec = decode(&enc, rate, packing).expect("decode sine"); + let corr = pearson_correlation(&pcm, &dec); + assert!(corr > 0.90, "correlation {corr} too low for rate {rate:?} packing {packing:?}"); + } + } +} + +#[test] +fn test_encoder_and_decoder_reset() { + let mut enc = Encoder::new(BitRate::Rate32k, BitPacking::BigEndian); + let mut dec = Decoder::new(BitRate::Rate32k, BitPacking::BigEndian); + + let pcm = vec![1000i16, 2000, 3000, 2000, 1000, 0, -1000, -2000]; + let mut out1 = Vec::new(); + enc.encode(&pcm, &mut out1).expect("enc 1"); + enc.flush(&mut out1).expect("flush 1"); + + let mut dec_pcm1 = Vec::new(); + dec.decode(&out1, &mut dec_pcm1).expect("dec 1"); + + enc.reset(); + dec.reset(); + + let mut out2 = Vec::new(); + enc.encode(&pcm, &mut out2).expect("enc 2"); + enc.flush(&mut out2).expect("flush 2"); + + let mut dec_pcm2 = Vec::new(); + dec.decode(&out2, &mut dec_pcm2).expect("dec 2"); + + assert_eq!(out1, out2); + assert_eq!(dec_pcm1, dec_pcm2); +} + +#[test] +fn test_stereo_multichannel_roundtrip() { + let mut stereo_pcm = Vec::new(); + for i in 0..160 { + let left = (3000.0 * (i as f64 * 0.05).sin()) as i16; + let right = (2000.0 * (i as f64 * 0.03).cos()) as i16; + stereo_pcm.push(left); + stereo_pcm.push(right); + } + + let enc = encode_interleaved(&stereo_pcm, 2, BitRate::Rate32k, BitPacking::BigEndian).expect("encode stereo"); + let dec = decode_interleaved(&enc, 2, BitRate::Rate32k, BitPacking::BigEndian).expect("decode stereo"); + + assert_eq!(dec.len(), stereo_pcm.len()); + let corr = pearson_correlation(&stereo_pcm, &dec); + assert!(corr > 0.90, "stereo correlation {corr} too low"); +} + +#[test] +fn test_hostile_inputs_do_not_crash() { + let mut dec = Decoder::new(BitRate::Rate32k, BitPacking::BigEndian); + let mut out = Vec::new(); + let hostile = vec![0xEE; 1000]; + let _ = dec.decode(&hostile, &mut out); + + let mut enc = Encoder::new(BitRate::Rate40k, BitPacking::LittleEndian); + let mut out_enc = Vec::new(); + let hostile_pcm = vec![i16::MIN, i16::MAX, -1, 1, 0]; + let _ = enc.encode(&hostile_pcm, &mut out_enc); + let _ = enc.flush(&mut out_enc); +} + +#[test] +fn test_ffmpeg_g726_oracle_encoder_and_decoder() { + let ff = match find_ffmpeg() { + Some(f) => f, + None => return, + }; + + let sample_rate = 8000; + let num_samples = 240; + let mut pcm = Vec::with_capacity(num_samples); + for i in 0..num_samples { + let t = i as f64 / sample_rate as f64; + let s = (5000.0 * (2.0 * std::f64::consts::PI * 350.0 * t).sin()) as i16; + pcm.push(s); + } + + let dir = std::env::temp_dir().join("craft_g726_oracle"); + let _ = std::fs::create_dir_all(&dir); + let in_pcm_path = dir.join("input.s16le"); + let mut in_bytes = Vec::with_capacity(pcm.len() * 2); + for &s in &pcm { + in_bytes.extend_from_slice(&s.to_le_bytes()); + } + std::fs::write(&in_pcm_path, &in_bytes).expect("write input pcm"); + + for rate in [BitRate::Rate16k, BitRate::Rate24k, BitRate::Rate32k, BitRate::Rate40k] { + for (packing, ff_codec, ff_fmt) in [(BitPacking::BigEndian, "g726", "g726"), (BitPacking::LittleEndian, "g726le", "g726le")] { + let cs = rate.code_size(); + let ff_g726_path = dir.join(format!("ff_{}_{}.g726", cs, ff_codec)); + + // 1. FFmpeg encodes PCM to G.726 + let out = Command::new(&ff) + .args(["-y", "-f", "s16le", "-ar", "8000", "-ac", "1", "-i"]) + .arg(&in_pcm_path) + .args(["-c:a", ff_codec, "-code_size", &cs.to_string(), "-f", ff_fmt]) + .arg(&ff_g726_path) + .output() + .expect("ffmpeg encode"); + assert!(out.status.success(), "ffmpeg encode failed: {}", String::from_utf8_lossy(&out.stderr)); + let ff_g726_bytes = std::fs::read(&ff_g726_path).expect("read ff g726"); + + // 2. FFmpeg decodes its own G.726 stream + let ff_pcm_back_path = dir.join(format!("ff_back_{}_{}.s16le", cs, ff_codec)); + let out = Command::new(&ff) + .args(["-y", "-f", ff_fmt, "-code_size", &cs.to_string(), "-i"]) + .arg(&ff_g726_path) + .args(["-f", "s16le", "-ar", "8000", "-ac", "1"]) + .arg(&ff_pcm_back_path) + .output() + .expect("ffmpeg decode"); + assert!(out.status.success(), "ffmpeg decode failed: {}", String::from_utf8_lossy(&out.stderr)); + let ff_back_bytes = std::fs::read(&ff_pcm_back_path).expect("read ff back pcm"); + let ff_back_samples: Vec = ff_back_bytes.as_chunks::<2>().0.iter().map(|c| i16::from_le_bytes(*c)).collect(); + + // 3. Our decoder decodes FFmpeg's stream + let our_decoded = decode(&ff_g726_bytes, rate, packing).expect("our decode"); + let corr = pearson_correlation(&our_decoded, &ff_back_samples); + assert!(corr > 0.998, "correlation {corr} too low between our decoder and FFmpeg decoder for rate {rate:?} packing {packing:?}"); + + // 4. Our encoder encodes PCM to G.726 + let our_g726_bytes = encode(&pcm, rate, packing).expect("our encode"); + let our_g726_path = dir.join(format!("our_{}_{}.g726", cs, ff_codec)); + std::fs::write(&our_g726_path, &our_g726_bytes).expect("write our g726"); + + // 5. FFmpeg decodes our encoded stream + let our_decoded_by_ff_path = dir.join(format!("our_by_ff_{}_{}.s16le", cs, ff_codec)); + let out = Command::new(&ff) + .args(["-y", "-f", ff_fmt, "-code_size", &cs.to_string(), "-i"]) + .arg(&our_g726_path) + .args(["-f", "s16le", "-ar", "8000", "-ac", "1"]) + .arg(&our_decoded_by_ff_path) + .output() + .expect("ffmpeg decode of our stream"); + assert!(out.status.success(), "ffmpeg decode of our stream failed: {}", String::from_utf8_lossy(&out.stderr)); + let our_by_ff_bytes = std::fs::read(&our_decoded_by_ff_path).expect("read our by ff pcm"); + let our_by_ff_samples: Vec = our_by_ff_bytes.as_chunks::<2>().0.iter().map(|c| i16::from_le_bytes(*c)).collect(); + + let corr_input = pearson_correlation(&pcm, &our_by_ff_samples); + assert!( + corr_input > 0.90, + "correlation {corr_input} too low for input vs FFmpeg-decoded our stream for rate {rate:?} packing {packing:?}" + ); + } + } +} diff --git a/crates/audio/tests/mp2_encoder_oracle.rs b/crates/audio/tests/mp2_encoder_oracle.rs new file mode 100644 index 0000000000..ca4d15bd3e --- /dev/null +++ b/crates/audio/tests/mp2_encoder_oracle.rs @@ -0,0 +1,228 @@ +//! The Layer II encoder (feature `mpa-encode`): every frame has the header and size its settings +//! give and decodes with our decoder; ffmpeg as an external decoder (skipped without ffmpeg, a +//! failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`) decodes the stream without a complaint, to the +//! right length, and its output, aligned by [`DELAY`], matches the input within a signal-to-noise +//! ratio per bit rate. The bounds sit well under what the encoder reaches today; white noise loses +//! the subbands above the table's last one (a sixth of it at 48 kHz), so its bound is low. + +use std::path::Path; +use std::process::{Command, Stdio}; + +use craft_audio::mpa::encoder::{Config, DELAY, EncodeError, Encoder, bitrates, nearest_bitrate}; +use craft_audio::mpa::{Decoder, Mode, Version, parse_header}; + +fn chords(rate: u32, n: usize, detune: f32) -> Vec { + let f = [220.0, 277.18, 329.63, 440.0, 1318.5, 3520.0]; + let a = [0.2, 0.15, 0.12, 0.1, 0.05, 0.02]; + (0..n) + .map(|i| { + let t = i as f32 / rate as f32; + f.iter().zip(a).map(|(f, a)| a * (2.0 * std::f32::consts::PI * f * (1.0 + detune) * t).sin()).sum::() + }) + .collect() +} + +fn noise(n: usize, amp: f32, seed: u64) -> Vec { + let mut s = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1; + (0..n) + .map(|_| { + s ^= s << 13; + s ^= s >> 7; + s ^= s << 17; + ((s >> 40) as f32 / (1u64 << 24) as f32 * 2.0 - 1.0) * amp + }) + .collect() +} + +fn encode(cfg: Config, input: &[Vec], chunk: usize) -> Vec { + let mut enc = Encoder::new(cfg).expect("encoder"); + let mut out = Vec::new(); + let n = input[0].len(); + let mut pos = 0; + while pos < n { + let end = (pos + chunk).min(n); + let planes: Vec<&[f32]> = input.iter().map(|c| &c[pos..end]).collect(); + out.extend(enc.encode(&planes)); + pos = end; + } + out.extend(enc.flush()); + out +} + +/// Split a stream into frames by its headers; every header must be one the settings give. +fn frames<'a>(bytes: &'a [u8], cfg: &Config) -> Vec<&'a [u8]> { + let mut out = Vec::new(); + let mut pos = 0; + while pos < bytes.len() { + let h = parse_header(&bytes[pos..]).unwrap_or_else(|e| panic!("frame {} at byte {pos}: {e}", out.len())); + assert_eq!(h.layer, 2); + assert_eq!(h.version, if cfg.sample_rate >= 32_000 { Version::Mpeg1 } else { Version::Mpeg2 }); + assert_eq!((h.sample_rate, h.bitrate_kbps), (cfg.sample_rate, cfg.bitrate_kbps)); + assert_eq!(h.mode, if cfg.channels == 1 { Mode::Mono } else { Mode::Stereo }); + assert!(!h.crc); + out.push(&bytes[pos..pos + h.frame_bytes]); + pos += h.frame_bytes; + } + assert_eq!(pos, bytes.len(), "the stream ends on a frame boundary"); + out +} + +fn snr(input: &[f32], dec: &[f32]) -> f64 { + let (mut s, mut n) = (0f64, 0f64); + for (a, b) in input.iter().zip(dec) { + s += f64::from(*a) * f64::from(*a); + n += f64::from(a - b) * f64::from(a - b); + } + 10.0 * (s / n.max(1e-30)).log10() +} + +struct Case { + name: &'static str, + rate: u32, + channels: usize, + kbps: u32, + signal: fn(u32, usize, usize) -> Vec, + min_snr: f64, +} + +fn sig_chords(rate: u32, n: usize, c: usize) -> Vec { + chords(rate, n, 0.003 * c as f32) +} +fn sig_noise(_rate: u32, n: usize, c: usize) -> Vec { + noise(n, 0.25, 11 + c as u64) +} + +const CASES: &[Case] = &[ + Case { name: "chords", rate: 48_000, channels: 2, kbps: 192, signal: sig_chords, min_snr: 30.0 }, + Case { name: "chords", rate: 44_100, channels: 2, kbps: 128, signal: sig_chords, min_snr: 25.0 }, + Case { name: "chords", rate: 44_100, channels: 1, kbps: 64, signal: sig_chords, min_snr: 25.0 }, + Case { name: "chords", rate: 32_000, channels: 2, kbps: 96, signal: sig_chords, min_snr: 20.0 }, + Case { name: "chords", rate: 22_050, channels: 2, kbps: 64, signal: sig_chords, min_snr: 20.0 }, + Case { name: "chords", rate: 24_000, channels: 1, kbps: 32, signal: sig_chords, min_snr: 18.0 }, + Case { name: "noise", rate: 48_000, channels: 2, kbps: 384, signal: sig_noise, min_snr: 4.0 }, + Case { name: "noise", rate: 44_100, channels: 2, kbps: 256, signal: sig_noise, min_snr: 4.0 }, +]; + +fn inputs(c: &Case, n: usize) -> Vec> { + (0..c.channels).map(|ch| (c.signal)(c.rate, n, ch)).collect() +} + +#[test] +fn frames_have_the_size_and_header_their_settings_give_and_decode_with_our_decoder() { + for c in CASES { + let n = c.rate as usize * 2 + 777; + let cfg = Config::cbr(c.rate, c.channels, c.kbps); + let bytes = encode(cfg, &inputs(c, n), 1000); + let frames = frames(&bytes, &cfg); + // every input sample and the filter delay fit in the frames, and no more than one frame more + let covered = frames.len() * 1152; + assert!(covered >= n + DELAY as usize && covered < n + DELAY as usize + 1152, "{}: {} frames for {n} samples", c.name, frames.len()); + // 144 kbit/s-slots per frame, the padding slots adding up to the exact rate + let nominal = f64::from(c.kbps) * 125.0 * covered as f64 / f64::from(c.rate); + assert!((bytes.len() as f64 - nominal).abs() < 1.0 + f64::from(c.rate) / 1000.0, "{}: {} bytes against {nominal}", c.name, bytes.len()); + let mut dec = Decoder::new(); + let mut out = vec![Vec::new(); c.channels]; + for f in frames { + let frame = dec.decode(f).unwrap_or_else(|e| panic!("{}: {e}", c.name)); + for (o, ch) in out.iter_mut().zip(frame.channels) { + o.extend(ch); + } + } + let d = DELAY as usize; + for (ch, input) in inputs(c, n).iter().enumerate() { + let s = snr(input, &out[ch][d..d + n]); + println!("{:<7} {:>5} Hz {}ch {:>3} kbps: SNR {s:.2} dB (our decoder)", c.name, c.rate, c.channels, c.kbps); + assert!(s >= c.min_snr, "{} {} Hz {}ch {} kbps: SNR {s:.2} dB under {}", c.name, c.rate, c.channels, c.kbps, c.min_snr); + } + } +} + +#[test] +fn the_stream_does_not_depend_on_how_the_input_is_chunked() { + let c = &CASES[1]; + let n = 44_100 + 333; + let cfg = Config::cbr(c.rate, c.channels, c.kbps); + let input = inputs(c, n); + let whole = encode(cfg, &input, n); + for chunk in [1, 7, 1152, 5000] { + assert_eq!(encode(cfg, &input, chunk), whole, "chunk {chunk}"); + } +} + +#[test] +fn silence_and_nothing_encode_to_valid_frames() { + let cfg = Config::cbr(48_000, 2, 128); + let silent = vec![vec![0f32; 5000]; 2]; + let bytes = encode(cfg, &silent, 5000); + let fs = frames(&bytes, &cfg); + let mut dec = Decoder::new(); + for f in fs { + let frame = dec.decode(f).expect("decode"); + assert!(frame.channels.iter().flatten().all(|v| v.abs() < 1e-6)); + } + assert!(Encoder::new(cfg).expect("encoder").flush().is_empty(), "no input, no frames"); +} + +#[test] +fn hostile_samples_are_tamed() { + let cfg = Config::cbr(44_100, 2, 192); + let mut x = vec![0f32; 4000]; + for (i, v) in x.iter_mut().enumerate() { + *v = [f32::NAN, f32::INFINITY, -f32::INFINITY, 1e30, -1e30, 3.0, 0.5][i % 7]; + } + let bytes = encode(cfg, &[x.clone(), x], 4000); + let mut dec = Decoder::new(); + for f in frames(&bytes, &cfg) { + dec.decode(f).expect("decode"); + } +} + +#[test] +fn settings_that_cannot_be_carried_are_refused() { + assert_eq!(Encoder::new(Config::cbr(44_000, 2, 128)).err(), Some(EncodeError::SampleRate(44_000))); + assert_eq!(Encoder::new(Config::cbr(44_100, 3, 128)).err(), Some(EncodeError::Channels(3))); + assert_eq!(Encoder::new(Config::cbr(44_100, 0, 128)).err(), Some(EncodeError::Channels(0))); + // ISO/IEC 11172-3 allows neither 32 kbit/s for two channels nor 224 for one + assert!(matches!(Encoder::new(Config::cbr(44_100, 2, 32)), Err(EncodeError::Bitrate { .. }))); + assert!(matches!(Encoder::new(Config::cbr(44_100, 1, 224)), Err(EncodeError::Bitrate { .. }))); + assert!(matches!(Encoder::new(Config::cbr(44_100, 2, 130)), Err(EncodeError::Bitrate { .. }))); + assert_eq!(bitrates(44_100, 1).len(), 10); + assert_eq!(bitrates(22_050, 2).first(), Some(&8)); + assert!(bitrates(8_000, 2).is_empty()); + assert_eq!(nearest_bitrate(44_100, 2, 100), Some(96)); + assert_eq!(nearest_bitrate(44_100, 2, 1), Some(64)); + assert_eq!(nearest_bitrate(8_000, 2, 100), None); +} + +fn ffmpeg_decode(ff: &Path, path: &Path, channels: usize) -> (Vec>, String) { + let out = Command::new(ff) + .args(["-v", "error", "-nostdin", "-i"]) + .arg(path) + .args(["-f", "f32le", "-acodec", "pcm_f32le", "-"]) + .stdin(Stdio::null()) + .output() + .expect("run ffmpeg"); + let samples: Vec = out.stdout.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes(*b)).collect(); + let planar = (0..channels).map(|c| samples.iter().skip(c).step_by(channels).copied().collect()).collect(); + (planar, String::from_utf8_lossy(&out.stderr).into_owned()) +} + +#[test] +fn ffmpeg_decodes_our_streams_to_the_input() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("mp2enc"); + for c in CASES { + let n = c.rate as usize * 2 + 777; + let input = inputs(c, n); + let bytes = encode(Config::cbr(c.rate, c.channels, c.kbps), &input, 1000); + let path = dir.join(format!("{}_{}_{}ch_{}k.mp2", c.name, c.rate, c.channels, c.kbps)); + std::fs::write(&path, &bytes).expect("write"); + let (dec, err) = ffmpeg_decode(&ff, &path, c.channels); + assert!(err.trim().is_empty(), "{}: ffmpeg reported: {err}", path.display()); + let d = DELAY as usize; + assert!(dec[0].len() >= n + d, "{}: decoded {} samples, need {}", c.name, dec[0].len(), n + d); + let s = (0..c.channels).map(|ch| snr(&input[ch], &dec[ch][d..d + n])).sum::() / c.channels as f64; + println!("{:<7} {:>5} Hz {}ch {:>3} kbps: SNR {s:.2} dB (ffmpeg)", c.name, c.rate, c.channels, c.kbps); + assert!(s >= c.min_snr, "{} {} Hz {}ch {} kbps: SNR {s:.2} dB under {}", c.name, c.rate, c.channels, c.kbps, c.min_snr); + } +} diff --git a/crates/audio/tests/vmd.rs b/crates/audio/tests/vmd.rs new file mode 100644 index 0000000000..145aafa79b --- /dev/null +++ b/crates/audio/tests/vmd.rs @@ -0,0 +1,280 @@ +//! VMD sound decoder tests. ffmpeg reads VMD but cannot write it, so the files come from the +//! test kit's VMD writer. The decoder must return the samples that were packed, and the same +//! samples as ffmpeg (skipped without ffmpeg). + +use std::process::Command; + +use craft_audio::AudioError; +use craft_audio::vmd::{Decoder, Error}; +use craft_media::vmd::{FrameKind, open}; +use craft_media_testkit::hostile::Rng; +use craft_media_testkit::vmd::{Audio, Frame, Spec, build, dpcm_pack}; + +/// (frame length field, audio flags, channels, 16-bit): every layout the decoder handles. +const FORMATS: [(i16, u16, usize, bool); 6] = + [(50, 0, 1, false), (50, 0x200, 2, false), (50, 0x8000, 2, false), (-50, 0, 1, true), (-50, 0x200, 2, true), (-50, 0x8000, 2, true)]; + +fn spec(frame_length: i16, flags: u16) -> Spec { + let mut spec = Spec::new(4, 4); + spec.audio = Some(Audio { sample_rate: 22050, frame_length, buffers: 5, flags }); + spec +} + +fn decoder(frame_length: i16, flags: u16) -> Decoder { + Decoder::new(&build(&spec(frame_length, flags), &[])).unwrap() +} + +/// One block of random sound: its bytes in the file and the samples it decodes to. +fn block(rng: &mut Rng, d: &Decoder) -> (Vec, Vec) { + let n = d.block_samples() * d.channels(); + if d.bits() == 8 { + let bytes: Vec = (0..n).map(|_| rng.next_u64() as u8).collect(); + let samples = bytes.iter().map(|&b| (i16::from(b) - 0x80) << 8).collect(); + return (bytes, samples); + } + // a random walk, so that the steps are of every size + let mut last = [0i32; 2]; + let samples: Vec = (0..n) + .map(|i| { + let bits = 1 + rng.below(15); + let step = rng.below(1 << bits) as i32 - (1 << (bits - 1)); + let s = &mut last[i % d.channels()]; + *s = (*s + step).clamp(-32768, 32767); + *s as i16 + }) + .collect(); + dpcm_pack(&samples, d.channels()) +} + +/// A file of sound frames (with a small picture each, as ffmpeg expects video) and the samples +/// expected of it. The first frame is a mask frame: one silent block, then four of sound. +fn movie(seed: u64, frame_length: i16, flags: u16) -> (Vec, Vec) { + let spec = spec(frame_length, flags); + let d = decoder(frame_length, flags); + let mut rng = Rng(seed | 1); + let mut picture = vec![2u8]; + picture.extend([9; 16]); + let silence = vec![0i16; d.block_samples() * d.channels()]; + let mut want = Vec::new(); + let mut blocks = Vec::new(); + for n in 0..12 { + let sound = match n { + 0 => { + let mut data = 1u32.to_le_bytes().to_vec(); + want.extend_from_slice(&silence); + for _ in 0..4 { + let (bytes, samples) = block(&mut rng, &d); + data.extend_from_slice(&bytes); + want.extend_from_slice(&samples); + } + Frame::audio(2, data) + } + _ if rng.below(4) == 0 => { + want.extend_from_slice(&silence); + Frame::audio(3, vec![]) + } + _ => { + let (bytes, samples) = block(&mut rng, &d); + want.extend_from_slice(&samples); + Frame::audio(1, bytes) + } + }; + blocks.push(vec![sound, Frame::video([0, 0, 3, 3], false, picture.clone())]); + } + (build(&spec, &blocks), want) +} + +fn decode_all(bytes: &[u8]) -> Vec { + let file = open(bytes).unwrap(); + let d = Decoder::new(file.header_bytes()).unwrap(); + let mut out = Vec::new(); + for (i, frame) in file.frames.iter().enumerate() { + if let FrameKind::Audio { flags } = frame.kind { + out.extend(d.decode(flags, &file.read_frame(bytes, i).unwrap()).unwrap()); + } + } + out +} + +#[test] +fn decodes_the_samples_that_were_packed() { + for (i, (frame_length, flags, channels, sixteen)) in FORMATS.into_iter().enumerate() { + let d = decoder(frame_length, flags); + assert_eq!((d.channels(), d.bits(), d.sample_rate()), (channels, if sixteen { 16 } else { 8 }, 22050), "format {i}"); + // old-style stereo counts both channels in the frame length + assert_eq!(d.block_samples(), if flags & 0x8000 != 0 { 25 } else { 50 }, "format {i}"); + let (bytes, want) = movie(i as u64 + 1, frame_length, flags); + assert_eq!(want.len(), 16 * d.block_samples() * channels); + assert!(decode_all(&bytes) == want, "format {i}"); + } +} + +#[test] +fn matches_ffmpeg() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg_or_skip("vmd sound oracle") else { return }; + let dir = craft_media_testkit::private_dir("vmd-sound"); + for (i, (frame_length, flags, ..)) in FORMATS.into_iter().enumerate() { + let (bytes, _) = movie(i as u64 + 1, frame_length, flags); + let path = dir.join(format!("sound{i}.vmd")); + std::fs::write(&path, &bytes).unwrap(); + let out = Command::new(&ffmpeg).args(["-v", "error", "-i"]).arg(&path).args(["-vn", "-f", "s16le", "-"]).output().unwrap(); + assert!(out.status.success() && out.stderr.is_empty(), "format {i}: ffmpeg: {}", 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 == decode_all(&bytes), "format {i} differs from ffmpeg"); + } + std::fs::remove_dir_all(&dir).unwrap(); +} + +#[test] +fn sixteen_bit_steps() { + // values ffmpeg gave for this block: the first sample, then signed steps from the table + let mut data = 1000i16.to_le_bytes().to_vec(); + data.extend([1, 1, 1, 0x81, 0x81, 2, 3, 0x7F, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); + let d = decoder(-20, 0); + assert_eq!(d.block_bytes(), 21); + assert_eq!( + d.decode(1, &data).unwrap(), + [1000, 1008, 1016, 1024, 1016, 1008, 1024, 1056, 17440, 1056, 1056, 1056, 1056, 1056, 1056, 1056, 1056, 1056, 1056, 1064] + ); + // stereo: the channels take turns + let mut data = [1000i16.to_le_bytes(), (-1000i16).to_le_bytes()].concat(); + data.extend([1, 0x82].repeat(3)); + let d = decoder(-4, 0x200); + assert_eq!(d.decode(1, &data).unwrap(), [1000, -1000, 1008, -1016, 1016, -1032, 1024, -1048]); + // a flags 1 frame of several blocks: each block starts from its own first samples + let (first, first_samples) = dpcm_pack(&[100, -100, 300, -50, 200, 0, 1000, 500], 2); + let (second, second_samples) = dpcm_pack(&[-7000, 7000, -6000, 6500, -6100, 6400, -3000, 3000], 2); + assert_eq!(d.decode(1, &[first, second, vec![1, 2, 3]].concat()).unwrap(), [first_samples, second_samples].concat()); + // steps past 16 bits stop at the ends + let mut data = 30000i16.to_le_bytes().to_vec(); + data.extend([0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); + assert_eq!(decoder(-7, 0).decode(1, &data).unwrap(), [30000, 32767, 16383, -1, -16385, -32768, -32768]); +} + +#[test] +fn eight_bit_and_silence() { + let d = decoder(4, 0); + assert_eq!(d.decode(1, &[0x80, 0x00, 0xFF, 0x70]).unwrap(), [0, -32768, 0x7F00, -0x1000]); + assert_eq!(d.decode(3, &[]).unwrap(), [0; 4]); + assert_eq!(d.decode(1, &[]).unwrap(), [0i16; 0]); + assert_eq!(decoder(4, 0x200).decode(3, &[]).unwrap(), [0; 8]); + // a flags 1 frame of more than one block plays every whole block + assert_eq!( + d.decode(1, &[0x80, 0x81, 0x82, 0x83, 0x90, 0x91, 0x92, 0x93, 0xA0]).unwrap(), + [0, 0x100, 0x200, 0x300, 0x1000, 0x1100, 0x1200, 0x1300] + ); + assert_eq!(decoder(-4, 0x8000).decode(3, &[]).unwrap(), [0; 4]); +} + +#[test] +fn silence_mask_runs_bit_by_bit() { + let d = decoder(2, 0); + let frame = |mask: u32, data: &[u8]| d.decode(2, &[&mask.to_le_bytes()[..], data].concat()); + let (a, b) = ([0x90, 0x91], [0xA0, 0xA1]); + let (sa, sb) = ([0x1000, 0x1100], [0x2000, 0x2100]); + assert_eq!(frame(0, &[a, b].concat()).unwrap(), [sa, sb].concat()); + // silent, sound, silent, sound + assert_eq!(frame(0b0101, &[a, b].concat()).unwrap(), [[0, 0], sa, [0, 0], sb].concat()); + // game files set a bit for every block when all are silent + assert_eq!(frame(0b0111, &[]).unwrap(), [0; 6]); + // a trailing part of a block is left out + assert_eq!(frame(0, &[0x90, 0x91, 0xA0]).unwrap(), sa); + assert_eq!(d.decode(2, &[0, 0, 0]), Err(Error::Invalid("the silence mask is cut short"))); +} + +#[test] +fn bad_input_is_an_error() { + let d = decoder(-20, 0); + assert_eq!(d.decode(1, &[0; 20]), Err(Error::Invalid("a block of sound is cut short"))); + assert_eq!(d.decode(4, &[0; 21]), Err(Error::Unsupported("unknown sound frame flags"))); + assert_eq!(d.decode(4, &[]), Err(Error::Unsupported("unknown sound frame flags"))); + // in the file API's error: unsupported stays unsupported, the rest is malformed input + assert!(matches!(AudioError::from(Error::Unsupported("x")), AudioError::Unsupported(_))); + assert!(matches!(AudioError::from(Error::Invalid("x")), AudioError::Malformed(_))); + + let good = build(&spec(20, 0), &[]); + let with = |patch: &dyn Fn(&mut Vec)| { + let mut h = good.clone(); + patch(&mut h); + Decoder::new(&h).err() + }; + assert_eq!(with(&|_| {}), None); + assert_eq!(with(&|h| h.truncate(815)), Some(Error::Invalid("the header is shorter than 816 bytes"))); + assert_eq!(with(&|h| h[0] = 0), Some(Error::Invalid("not a VMD header"))); + assert_eq!(with(&|h| h[17] = 0), Some(Error::Invalid("the file has no sound"))); + assert_eq!(with(&|h| h[804..806].fill(0)), Some(Error::Invalid("the file has no sound"))); + assert_eq!(with(&|h| h[810] = 0x10), Some(Error::Unsupported("IMA ADPCM sound"))); + // a frame length of nothing decodes to nothing + let mut empty = good.clone(); + empty[806..808].fill(0); + let d = Decoder::new(&empty).unwrap(); + assert_eq!(d.decode(1, &[1, 2, 3]).unwrap(), [0i16; 0]); + assert_eq!(d.decode(2, &[0, 0, 0, 0, 1, 2]).unwrap(), [0i16; 0]); +} + +#[test] +fn hostile_input_never_panics() { + for (i, (frame_length, flags, ..)) in FORMATS.into_iter().enumerate() { + let (bytes, _) = movie(i as u64 + 1, frame_length, flags); + craft_media_testkit::hostile::hammer(&format!("vmd sound format {i}"), &bytes, 10_000, |d| { + let Ok(file) = open(d) else { return }; + let Ok(decoder) = Decoder::new(file.header_bytes()) else { return }; + for (i, frame) in file.frames.iter().enumerate() { + let FrameKind::Audio { flags } = frame.kind else { continue }; + if let Ok(data) = file.read_frame(d, i) { + let _ = decoder.decode(flags, &data); + } + } + }); + } +} + +/// Real files, for whoever has some: the sound of every `.vmd` in the directory +/// `CRAFT_MEDIA_VMD_SAMPLES` names must decode to the samples ffmpeg gets. Game files are not +/// redistributable, so none are in the repository and the test is skipped without the variable. +#[test] +fn real_files_match_ffmpeg() { + let Some(dir) = std::env::var_os("CRAFT_MEDIA_VMD_SAMPLES").filter(|d| !d.is_empty()) else { + craft_media_testkit::skip_test("vmd samples", "CRAFT_MEDIA_VMD_SAMPLES is not set"); + return; + }; + let Some(ffmpeg) = craft_media_testkit::ffmpeg_or_skip("vmd sound samples") else { return }; + let mut paths: Vec<_> = std::fs::read_dir(&dir) + .unwrap() + .filter_map(|e| Some(e.ok()?.path())) + .filter(|p| p.extension().is_some_and(|e| e.eq_ignore_ascii_case("vmd"))) + .collect(); + paths.sort(); + assert!(!paths.is_empty(), "no .vmd files in CRAFT_MEDIA_VMD_SAMPLES"); + let (mut files, mut samples) = (0, 0); + for path in &paths { + let name = path.display(); + let source = std::fs::File::open(path).unwrap(); + let file = open(&source).unwrap(); + if file.audio_params().is_none() { + continue; + } + let d = Decoder::new(file.header_bytes()).unwrap(); + let mut ours = Vec::new(); + for (i, frame) in file.frames.iter().enumerate() { + let FrameKind::Audio { flags } = frame.kind else { continue }; + let mut data = file.read_frame(&source, i).unwrap(); + if flags == 2 && data.len() >= 4 { + // ffmpeg plays a mask frame's silent blocks before its sound; put ours in that + // order to compare + let silent = u32::from_le_bytes([data[0], data[1], data[2], data[3]]).count_ones() as usize; + ours.resize(ours.len() + silent * d.block_samples() * d.channels(), 0); + data[..4].fill(0); + } + ours.extend(d.decode(flags, &data).unwrap_or_else(|e| panic!("{name}: frame {i}: {e}"))); + } + let out = Command::new(&ffmpeg).args(["-v", "quiet", "-i"]).arg(path).args(["-vn", "-f", "s16le", "-"]).output().unwrap(); + let theirs: Vec = out.stdout.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect(); + if let Some(at) = (0..ours.len().max(theirs.len())).find(|&i| ours.get(i) != theirs.get(i)) { + panic!("{name}: sample {at} differs from ffmpeg ({} samples here, {} there)", ours.len(), theirs.len()); + } + files += 1; + samples += ours.len(); + } + println!("{files} files, {samples} samples match ffmpeg"); +} diff --git a/crates/audio/tests/vorbis_decode_hostile.rs b/crates/audio/tests/vorbis_decode_hostile.rs new file mode 100644 index 0000000000..fba004e109 --- /dev/null +++ b/crates/audio/tests/vorbis_decode_hostile.rs @@ -0,0 +1,230 @@ +//! Hostile input to the Vorbis decoder: damaged headers and packets, truncations, and headers that +//! ask for absurd sizes. Every case is an error or a result, never a panic or a large allocation. + +use craft_audio::vorbis::decoder::{Decoder, Error, decode_ogg}; +use craft_audio::{AudioBuffer, OggVorbisOptions, encode_ogg_vorbis}; + +fn xorshift(seed: &mut u32) -> u32 { + *seed ^= *seed << 13; + *seed ^= *seed >> 17; + *seed ^= *seed << 5; + *seed +} + +struct Parts { + ogg: Vec, + headers: Vec>, + packets: Vec>, +} + +fn parts(channels: usize) -> Parts { + let signal: Vec> = (0..channels) + .map(|c| (0..20_000).map(|i| ((i as f32 * (0.03 + 0.01 * c as f32)).sin() * 0.4) + ((i * 7 % 13) as f32 - 6.0) / 60.0).collect()) + .collect(); + let ogg = + encode_ogg_vorbis(&AudioBuffer { sample_rate: 44_100, channels: signal }, &OggVorbisOptions::default().with_quality(4.0)).expect("encode"); + let file = craft_media::ogg::open(ogg.as_slice()).expect("open"); + let s = &file.streams[0]; + let packets = (0..s.packets.len()).map(|i| file.read_packet(ogg.as_slice(), 0, i).expect("packet")).collect(); + Parts { headers: s.headers.clone(), packets, ogg } +} + +fn decoder(p: &Parts) -> Decoder { + Decoder::new(&p.headers[0], &p.headers[1], &p.headers[2]).expect("valid headers") +} + +#[test] +fn every_truncated_header_is_an_error() { + let p = parts(2); + for which in [0usize, 2] { + for n in 0..p.headers[which].len() { + let mut h = p.headers.clone(); + h[which].truncate(n); + assert!(Decoder::new(&h[0], &h[1], &h[2]).is_err(), "header {which} cut to {n} bytes"); + } + } + // A damaged comment header is not fatal. + let mut h = p.headers.clone(); + h[1].truncate(12); + let d = Decoder::new(&h[0], &h[1], &h[2]).expect("comments are best effort"); + assert!(d.comments().comments.is_empty()); + assert!(Decoder::new(&p.headers[0], &[], &p.headers[2]).is_err(), "the comment header still needs its tag"); +} + +#[test] +fn damaged_headers_never_panic() { + let p = parts(2); + let mut seed = 99u32; + for which in [0usize, 2] { + let len = p.headers[which].len(); + for _ in 0..800 { + let mut h = p.headers.clone(); + for _ in 0..1 + xorshift(&mut seed) % 3 { + let at = xorshift(&mut seed) as usize % len; + h[which][at] = xorshift(&mut seed) as u8; + } + if let Ok(mut d) = Decoder::new(&h[0], &h[1], &h[2]) { + // A setup that survives must also survive audio packets. + for pk in p.packets.iter().take(6) { + let _ = d.decode_packet(pk); + } + } + } + } +} + +#[test] +fn damaged_packets_never_panic() { + let p = parts(2); + let mut seed = 7u32; + let mut d = decoder(&p); + for pk in &p.packets { + for _ in 0..6 { + let mut bad = pk.clone(); + match xorshift(&mut seed) % 3 { + 0 => bad.truncate(xorshift(&mut seed) as usize % (bad.len() + 1)), + 1 => { + let at = xorshift(&mut seed) as usize % bad.len(); + bad[at] = xorshift(&mut seed) as u8; + } + _ => bad = (0..xorshift(&mut seed) % 300).map(|_| xorshift(&mut seed) as u8).collect(), + } + let _ = d.decode_packet(&bad); + } + } + // The decoder still decodes good packets after all that. + d.reset(); + assert!(d.decode_packet(&p.packets[3]).is_ok()); +} + +#[test] +fn header_packets_and_empty_packets_are_not_audio() { + let p = parts(1); + let mut d = decoder(&p); + assert_eq!(d.decode_packet(&p.headers[0]), Err(Error::NotAudio)); + assert_eq!(d.decode_packet(&p.headers[2]), Err(Error::NotAudio)); + assert!(matches!(d.decode_packet(&[]), Err(Error::Packet(_)))); +} + +#[test] +fn damaged_files_never_panic() { + let p = parts(2); + let mut seed = 31u32; + for _ in 0..120 { + let mut bad = p.ogg.clone(); + for _ in 0..1 + xorshift(&mut seed) % 4 { + let at = xorshift(&mut seed) as usize % bad.len(); + bad[at] = xorshift(&mut seed) as u8; + } + let _ = decode_ogg(&bad); + bad.truncate(xorshift(&mut seed) as usize % bad.len()); + let _ = decode_ogg(&bad); + } + assert!(matches!(decode_ogg(b"not an ogg file"), Err(Error::Ogg(_)))); + assert!(matches!(decode_ogg(&[]), Err(Error::Ogg(_)))); +} + +/// A bit writer for hand-built setups. +#[derive(Default)] +struct W(Vec, usize); +impl W { + fn put(&mut self, v: u64, n: u32) { + for i in 0..n { + if self.1.is_multiple_of(8) { + self.0.push(0); + } + let last = self.0.len() - 1; + self.0[last] |= (((v >> i) & 1) as u8) << (self.1 % 8); + self.1 += 1; + } + } +} + +fn setup_with_first_book(book: impl FnOnce(&mut W)) -> Vec { + let mut w = W::default(); + w.put(5, 8); + for c in b"vorbis" { + w.put(u64::from(*c), 8); + } + w.put(0, 8); // one book + book(&mut w); + w.0 +} + +#[test] +fn absurd_codebook_sizes_are_refused_before_allocating() { + let p = parts(1); + let try_setup = |setup: Vec| Decoder::new(&p.headers[0], &p.headers[1], &setup); + // 2^24 - 1 entries. + assert!( + try_setup(setup_with_first_book(|w| { + w.put(0x56_4342, 24); + w.put(1, 16); + w.put(0xFF_FFFF, 24); + w.put(1, 1); // ordered + w.put(0, 5); + })) + .is_err() + ); + // An ordered book whose lengths overrun its entries. + assert!( + try_setup(setup_with_first_book(|w| { + w.put(0x56_4342, 24); + w.put(1, 16); + w.put(8, 24); + w.put(1, 1); + w.put(0, 5); + w.put(0xFF, 4); // asks for more codewords of length 1 than there are entries + })) + .is_err() + ); + // A lookup table of 65535 dimensions over a million entries: 69 billion values. + assert!( + try_setup(setup_with_first_book(|w| { + w.put(0x56_4342, 24); + w.put(0xFFFF, 16); + w.put(1 << 20, 24); + w.put(1, 1); // ordered + w.put(19, 5); // length 20 + w.put(1 << 20, 21); // all the entries + w.put(2, 4); // lookup type 2 + w.put(0, 32); + w.put(0, 32); + w.put(0, 4); + w.put(0, 1); + })) + .is_err() + ); + // A million-entry book with a legitimate tree but a lookup table that is not in the packet. + assert!( + try_setup(setup_with_first_book(|w| { + w.put(0x56_4342, 24); + w.put(4, 16); + w.put(1 << 20, 24); + w.put(1, 1); + w.put(19, 5); + w.put(1 << 20, 21); + w.put(1, 4); // lookup type 1 + w.put(0, 32); + w.put(0, 32); + w.put(15, 4); + w.put(0, 1); + })) + .is_err() + ); +} + +#[test] +fn identification_header_limits() { + let p = parts(1); + let mut h = p.headers[0].clone(); + let setup = &p.headers[2]; + h[11] = 0; // no channels + assert!(matches!(Decoder::new(&h, &p.headers[1], setup), Err(Error::Header(_)))); + let mut h = p.headers[0].clone(); + h[28] = 0x95; // a short block size of 2^5 is below the 64 Vorbis allows + assert!(matches!(Decoder::new(&h, &p.headers[1], setup), Err(Error::Header(_)))); + let mut h = p.headers[0].clone(); + h[7] = 1; // version 1 + assert!(matches!(Decoder::new(&h, &p.headers[1], setup), Err(Error::Header(_)))); +} diff --git a/crates/audio/tests/vorbis_decode_oracle.rs b/crates/audio/tests/vorbis_decode_oracle.rs new file mode 100644 index 0000000000..a23d2a954f --- /dev/null +++ b/crates/audio/tests/vorbis_decode_oracle.rs @@ -0,0 +1,216 @@ +//! The Vorbis decoder against ffmpeg's as the reference (oracle; skipped when ffmpeg is absent, a +//! failure with `CRAFT_MEDIA_REQUIRE_ORACLES=1`). Streams come from two encoders, this crate's and +//! ffmpeg's own, so the setups differ: different floors, residues, block sizes, channel counts and +//! coupling. Both decoders must return the same samples. + +use craft_audio::AudioBuffer; +use craft_audio::vorbis::decoder::{Decoder, decode_ogg}; +use craft_audio::{OggVorbisOptions, encode_ogg_vorbis}; +use std::path::Path; +use std::process::Command; + +fn xorshift(seed: &mut u32) -> u32 { + *seed ^= *seed << 13; + *seed ^= *seed >> 17; + *seed ^= *seed << 5; + *seed +} + +/// A signal with tones, noise bursts, clicks and silence, different in every channel. +fn signal(rate: u32, channels: usize, frames: usize) -> Vec> { + let mut seed = 0x1234_5678u32; + (0..channels) + .map(|c| { + (0..frames) + .map(|i| { + let t = i as f64 / f64::from(rate); + let tone = + (t * (220.0 + 110.0 * c as f64) * std::f64::consts::TAU).sin() * 0.3 + (t * 3100.0 * std::f64::consts::TAU).sin() * 0.05; + let noise = (f64::from(xorshift(&mut seed) % 2000) - 1000.0) / 6000.0; + let s = match (i / (rate as usize / 4)) % 6 { + 0 | 1 => tone, + 2 => noise, + 3 => 0.0, + 4 => tone + if i % 3000 < 8 { 0.6 } else { 0.0 }, + _ => tone * (t * 4.0).sin(), + }; + s as f32 + }) + .collect() + }) + .collect() +} + +fn ffmpeg_decode(ffmpeg: &Path, ogg: &Path, channels: usize) -> Vec> { + let out = Command::new(ffmpeg) + .args(["-v", "error", "-nostdin", "-i"]) + .arg(ogg) + .args(["-f", "f32le", "-acodec", "pcm_f32le", "-"]) + .output() + .expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg failed on {}: {}", ogg.display(), String::from_utf8_lossy(&out.stderr)); + let all: Vec = out.stdout.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes(*b)).collect(); + (0..channels).map(|c| all.iter().skip(c).step_by(channels).copied().collect()).collect() +} + +/// Each of our channels matches a different one of theirs (ffmpeg reorders the channels of surround +/// streams), to within float rounding. +fn assert_same(name: &str, ours: &[Vec], theirs: &[Vec]) { + assert_prefix(name, ours, theirs, true); +} + +/// As [`assert_same`], but with `exact` unset ffmpeg may return more samples than we do (it does +/// not always trim a stream to its last granule position); the common part must agree. +fn assert_prefix(name: &str, ours: &[Vec], theirs: &[Vec], exact: bool) { + assert_eq!(ours.len(), theirs.len(), "{name}: channel count"); + assert!(ours.iter().chain(theirs).flatten().all(|v| v.is_finite()), "{name}: non-finite oracle input or output"); + let mut used = vec![false; theirs.len()]; + for (c, o) in ours.iter().enumerate() { + let mut best: Option<(usize, f32)> = None; + for (k, t) in theirs.iter().enumerate() { + if used[k] { + continue; + } + if exact { + assert_eq!(o.len(), t.len(), "{name}: channel {c} length {} vs ffmpeg {}", o.len(), t.len()); + } else { + assert!(o.len() <= t.len(), "{name}: channel {c} length {} vs ffmpeg {}", o.len(), t.len()); + } + let worst = o.iter().zip(t).map(|(a, b)| (a - b).abs()).fold(0f32, f32::max); + if best.is_none_or(|(_, w)| worst < w) { + best = Some((k, worst)); + } + } + let (k, worst) = best.expect("a free channel"); + used[k] = true; + assert!(worst < 2e-4, "{name}: channel {c} differs from ffmpeg's channel {k} by up to {worst}"); + } +} + +#[test] +fn ffmpeg_decodes_our_encoders_streams_to_the_same_samples() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("vorbis_decode"); + for (rate, channels, quality) in + [(44_100u32, 2usize, 6.0f32), (44_100, 1, 3.0), (48_000, 2, -1.0), (22_050, 1, 10.0), (44_100, 6, 5.0), (32_000, 8, 4.0)] + { + let buf = AudioBuffer { sample_rate: rate, channels: signal(rate, channels, rate as usize * 2 + 777) }; + let ogg = encode_ogg_vorbis(&buf, &OggVorbisOptions::default().with_quality(quality)).expect("encode"); + let file = dir.join(format!("ours_{rate}_{channels}_{quality}.ogg")); + std::fs::write(&file, &ogg).expect("write fixture"); + let decoded = decode_ogg(&ogg).expect("decode"); + assert_eq!(decoded.damaged_packets, 0); + assert_eq!(decoded.sample_rate, rate); + assert_same(&format!("our stream {rate} Hz {channels} ch q{quality}"), &decoded.channels, &ffmpeg_decode(&ffmpeg, &file, channels)); + } +} + +/// ffmpeg's own Vorbis encoder (stereo only) writes setups ours does not: coupled channels, residue +/// type 2, floor classes with subclasses and master books, two block sizes. The reference is +/// Symphonia, an independent decoder. ffmpeg's decoder agrees on the first channel but not on the +/// second of these streams, where it differs from both Symphonia and this decoder; the stream does +/// not show which reading it means (the channel comes back poorly, about 3 dB SNR, in all three), +/// so ffmpeg is compared on the first channel only. The setups ffmpeg's decoder does read the same +/// way as we do are covered, on both channels, in `vorbis_decode_synthetic.rs`. +#[test] +fn we_decode_ffmpegs_encoder_streams_like_symphonia() { + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("vorbis_decode"); + for (rate, bitrate) in [(44_100u32, "128k"), (48_000, "96k"), (22_050, "48k"), (32_000, "64k")] { + let channels = 2usize; + let pcm: Vec = { + let s = signal(rate, channels, rate as usize * 2 + 301); + (0..s[0].len()).flat_map(|i| s.iter().map(move |c| c[i])).collect() + }; + let raw = dir.join(format!("src_{rate}_{channels}.f32")); + std::fs::write(&raw, pcm.iter().flat_map(|v| v.to_le_bytes()).collect::>()).expect("write source"); + let file = dir.join(format!("ffmpeg_{rate}_{channels}_{bitrate}.ogg")); + let out = Command::new(&ffmpeg) + .args(["-v", "error", "-nostdin", "-y", "-f", "f32le", "-ar"]) + .arg(rate.to_string()) + .arg("-ac") + .arg(channels.to_string()) + .arg("-i") + .arg(&raw) + .args(["-c:a", "vorbis", "-strict", "-2", "-b:a", bitrate]) + .arg(&file) + .output() + .expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg could not encode {}: {}", file.display(), String::from_utf8_lossy(&out.stderr)); + let ogg = std::fs::read(&file).expect("read"); + let ours = decode_ogg(&ogg).expect("decode"); + assert_eq!(ours.damaged_packets, 0, "{}", file.display()); + let (_, symphonia) = craft_audio::decode(&ogg, Some("ogg")).expect("symphonia"); + let name = format!("ffmpeg-encoded {rate} Hz {bitrate}"); + assert_prefix(&name, &ours.channels, &symphonia.channels, false); + // The first channel also agrees with ffmpeg's decoder. + let theirs = ffmpeg_decode(&ffmpeg, &file, channels); + assert_prefix(&format!("{name} (channel 0 against ffmpeg)"), &ours.channels[..1], &theirs[..1], true); + } +} + +#[test] +fn packets_decode_one_at_a_time_to_the_file_result() { + let buf = AudioBuffer { sample_rate: 44_100, channels: signal(44_100, 2, 30_000) }; + let ogg = encode_ogg_vorbis(&buf, &OggVorbisOptions::default().with_quality(5.0)).expect("encode"); + let file = craft_media::ogg::open(ogg.as_slice()).expect("open"); + let s = &file.streams[file.stream_of(craft_media::ogg::Codec::Vorbis).expect("vorbis")]; + let mut dec = Decoder::new(&s.headers[0], &s.headers[1], &s.headers[2]).expect("decoder"); + assert_eq!((dec.identification().channels, dec.identification().sample_rate), (2, 44_100)); + let mut got: Vec> = vec![Vec::new(); 2]; + for i in 0..s.packets.len() { + let p = file.read_packet(ogg.as_slice(), 0, i).expect("packet"); + for (a, b) in got.iter_mut().zip(dec.decode_packet(&p).expect("packet decodes")) { + a.extend(b); + } + } + let whole = decode_ogg(&ogg).expect("file"); + for (a, b) in got.iter().zip(&whole.channels) { + assert!(a.len() >= b.len() && a[..b.len()] == b[..]); + } + // After a reset the next packet only primes the overlap again. + dec.reset(); + let p = file.read_packet(ogg.as_slice(), 0, 5).expect("packet"); + assert!(dec.decode_packet(&p).expect("decodes").iter().all(Vec::is_empty)); +} + +#[test] +fn the_round_trip_through_our_encoder_is_faithful() { + let src = signal(44_100, 2, 44_100); + let buf = AudioBuffer { sample_rate: 44_100, channels: src.clone() }; + let ogg = encode_ogg_vorbis(&buf, &OggVorbisOptions::default().with_quality(8.0)).expect("encode"); + let out = decode_ogg(&ogg).expect("decode"); + assert_eq!(out.channels[0].len(), src[0].len()); + let (mut sig, mut err) = (0f64, 0f64); + for (a, b) in src[0].iter().zip(&out.channels[0]) { + sig += f64::from(*a) * f64::from(*a); + err += f64::from(a - b) * f64::from(a - b); + } + assert!(10.0 * (sig / err).log10() > 15.0, "SNR {:.1} dB", 10.0 * (sig / err).log10()); +} + +/// A stream from libvorbis itself (the repository's `tone.ogg` fixture, made with ffmpeg's libvorbis +/// wrapper): mono, 8 kHz, the lowest quality. +#[test] +fn a_libvorbis_stream_decodes_like_ffmpeg() { + let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/tone.ogg"); + let ogg = std::fs::read(&path).expect("fixture"); + let ours = decode_ogg(&ogg).expect("decode"); + assert_eq!((ours.sample_rate, ours.channels.len(), ours.damaged_packets), (8000, 1, 0)); + assert!(ours.comments.vendor.contains("Lavf") || ours.comments.vendor.contains("libVorbis"), "vendor {:?}", ours.comments.vendor); + assert!(ours.channels[0].iter().any(|s| s.abs() > 0.1), "the fixture is a tone"); + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + assert_prefix("libvorbis tone.ogg", &ours.channels, &ffmpeg_decode(&ffmpeg, &path, 1), false); +} + +#[test] +#[should_panic(expected = "non-finite oracle input or output")] +fn oracle_comparison_rejects_nan_output() { + assert_same("NaN output", &[vec![f32::NAN]], &[vec![0.0]]); +} + +#[test] +#[should_panic(expected = "non-finite oracle input or output")] +fn oracle_comparison_rejects_nan_reference() { + assert_same("NaN reference", &[vec![0.0]], &[vec![f32::NAN]]); +} diff --git a/crates/audio/tests/vorbis_decode_synthetic.rs b/crates/audio/tests/vorbis_decode_synthetic.rs new file mode 100644 index 0000000000..46c48eced3 --- /dev/null +++ b/crates/audio/tests/vorbis_decode_synthetic.rs @@ -0,0 +1,384 @@ +//! Differential tests on streams built by hand: a setup header written here, with floor 0 and +//! floor 1 side by side, residues 0, 1 and 2, several submaps and channel coupling, followed by +//! random audio packets. Every packet of random bits is decodable, so the two decoders (ours and +//! ffmpeg's, the oracle; skipped when ffmpeg is absent, a failure with +//! `CRAFT_MEDIA_REQUIRE_ORACLES=1`) must return the same samples, and no encoder's habits narrow +//! what is exercised. Floor 0, residue 0 and multi-submap mappings are the parts no encoder here +//! writes. + +use craft_audio::vorbis::decoder::{Decoder, decode_ogg}; +use craft_media::ogg::{PageWriter, vorbis_comment}; +use std::process::Command; + +fn xorshift(seed: &mut u32) -> u32 { + *seed ^= *seed << 13; + *seed ^= *seed >> 17; + *seed ^= *seed << 5; + *seed +} + +/// A least-significant-bit-first bit writer. +#[derive(Default)] +struct W { + bytes: Vec, + bits: usize, +} + +impl W { + fn put(&mut self, v: u64, n: u32) { + for i in 0..n { + if self.bits.is_multiple_of(8) { + self.bytes.push(0); + } + let last = self.bytes.len() - 1; + self.bytes[last] |= (((v >> i) & 1) as u8) << (self.bits % 8); + self.bits += 1; + } + } + fn flag(&mut self, b: bool) { + self.put(u64::from(b), 1); + } +} + +fn float_bits(v: f64) -> u64 { + // The Vorbis float32 packing: sign, 10-bit exponent biased by 788, 21-bit mantissa. + if v == 0.0 { + return 0; + } + let (sign, a) = if v < 0.0 { (1u64 << 31, -v) } else { (0, v) }; + let mut exp = 0i32; + let mut m = a; + while m >= (1 << 21) as f64 { + m /= 2.0; + exp += 1; + } + while m < (1 << 20) as f64 { + m *= 2.0; + exp -= 1; + } + sign | (((exp + 788) as u64) << 21) | (m.round() as u64 & 0x1F_FFFF) +} + +/// A complete codebook: `entries` codewords all of length `len` (`entries == 2^len`), optionally +/// with a lookup table: `(type, minimum, delta, value_bits, multiplicands)`. +fn book(w: &mut W, dims: u32, entries: u32, len: u32, lookup: Option<(u32, f64, f64, u32, Vec)>) { + w.put(0x56_4342, 24); + w.put(u64::from(dims), 16); + w.put(u64::from(entries), 24); + w.flag(false); // not ordered + w.flag(false); // not sparse + for _ in 0..entries { + w.put(u64::from(len - 1), 5); + } + match lookup { + None => w.put(0, 4), + Some((ty, min, delta, bits, mult)) => { + w.put(u64::from(ty), 4); + w.put(float_bits(min), 32); + w.put(float_bits(delta), 32); + w.put(u64::from(bits - 1), 4); + w.flag(ty == 1); // sequence_p: cumulative for the floor 0 book + for m in mult { + w.put(u64::from(m), bits); + } + } + } +} + +struct Layout { + channels: u8, + /// The residue type of each of the two residues. + residue_types: [u32; 2], + /// The submap of each channel (0 or 1): submap 0 uses floor 0 and residue 0, submap 1 uses + /// floor 1 and residue 1. + mux: Vec, + coupling: Vec<(u8, u8)>, +} + +fn identification(channels: u8) -> Vec { + let mut w = W::default(); + w.put(1, 8); + for c in b"vorbis" { + w.put(u64::from(*c), 8); + } + w.put(0, 32); + w.put(u64::from(channels), 8); + w.put(16_000, 32); + w.put(0, 32); + w.put(0, 32); + w.put(0, 32); + w.put(8, 4); // short blocks: 256 + w.put(10, 4); // long blocks: 1024 + w.flag(true); + w.bytes +} + +fn setup(l: &Layout) -> Vec { + let mut w = W::default(); + w.put(5, 8); + for c in b"vorbis" { + w.put(u64::from(*c), 8); + } + // Books: 0 floor 0 vectors, 1 class book, 2 and 3 residue vectors, 4 floor 1 values. + w.put(4, 8); // five books + book(&mut w, 4, 16, 4, Some((1, 0.7, 0.2, 1, vec![0, 1]))); // lookup1_values(16, 4) = 2 + book(&mut w, 1, 4, 2, None); + book(&mut w, 2, 16, 4, Some((1, -0.4, 0.25, 2, vec![0, 1, 2, 3]))); // lookup1_values(16, 2) = 4 + book(&mut w, 4, 16, 4, Some((1, -0.3, 0.6, 1, vec![0, 1]))); + book(&mut w, 1, 128, 7, None); + // One time domain placeholder. + w.put(0, 6); + w.put(0, 16); + // Floors: 0 and 1. + w.put(1, 6); // two floors + w.put(0, 16); + w.put(8, 8); // order + w.put(16_000, 16); // rate + w.put(64, 16); // bark map size + w.put(8, 6); // amplitude bits + w.put(2, 8); // amplitude offset + w.put(0, 4); // one book + w.put(0, 8); + w.put(1, 16); + w.put(1, 5); // one partition + w.put(0, 4); // class 0 + w.put(2, 3); // dimension 3 + w.put(0, 2); // no subclasses + w.put(4 + 1, 8); // subclass book: book 4 (stored plus one) + w.put(1, 2); // multiplier 2 + w.put(8, 4); // rangebits + for x in [10, 40, 130] { + w.put(x, 8); + } + // Residues. + w.put(1, 6); // two residues + for ty in l.residue_types { + w.put(u64::from(ty), 16); + w.put(0, 24); // begin + w.put(if ty == 2 { 96 } else { 48 }, 24); // end + w.put(7, 24); // partition size 8 + w.put(3, 6); // four classifications + w.put(1, 8); // class book + for _ in 0..4 { + w.put(0b011, 3); // passes 0 and 1 + w.flag(false); + } + for class in 0..4 { + w.put(2 + (class % 2), 8); // pass 0 uses book 2 or 3 + w.put(3 - (class % 2), 8); // pass 1 uses book 3 or 2 + } + } + // One mapping, two submaps. + w.put(0, 6); + w.put(0, 16); + w.flag(true); + w.put(1, 4); // two submaps + w.flag(!l.coupling.is_empty()); + if !l.coupling.is_empty() { + w.put(l.coupling.len() as u64 - 1, 8); + let bits = if l.channels <= 1 { 0 } else { 8 - (l.channels - 1).leading_zeros() }; + for &(m, a) in &l.coupling { + w.put(u64::from(m), bits); + w.put(u64::from(a), bits); + } + } + w.put(0, 2); + for &m in &l.mux { + w.put(u64::from(m), 4); + } + for sub in 0..2u64 { + w.put(0, 8); + w.put(sub, 8); // floor + w.put(sub, 8); // residue + } + // Two modes over the one mapping. + w.put(1, 6); + for long in [false, true] { + w.flag(long); + w.put(0, 16); + w.put(0, 16); + w.put(0, 8); + } + w.flag(true); + w.bytes +} + +fn stream(l: &Layout, packets: usize, seed: &mut u32) -> Vec { + let mut pages = PageWriter::new(7); + let mut out = pages.header(&identification(l.channels)); + out.extend(pages.header(&vorbis_comment("synthetic", &[("TITLE", "hand built")]))); + out.extend(pages.header(&setup(l))); + // The window flags of a long block say whether its neighbours are long, so choose the block + // sizes first. + let longs: Vec = (0..packets).map(|_| !xorshift(seed).is_multiple_of(3)).collect(); + let mut granule = 0i64; + for i in 0..packets { + let long = longs[i]; + // Packet type 0, the mode bit, and for a long block the two neighbour flags. + let prev = i == 0 || longs[i - 1]; + let next = i + 1 >= packets || longs[i + 1]; + let mut w = W::default(); + w.flag(false); + w.flag(long); + if long { + w.flag(prev); + w.flag(next); + } + // Floor 0 needs a codebook number that exists, which random bits would not give, so the + // channels that use it (the first ones) are written properly: an amplitude, book 0 and two + // codewords of its vectors (a full tree of 4-bit codes: the code is the entry number). + for _ in 0..l.mux.iter().take_while(|&&m| m == 0).count() { + w.put(1 + u64::from(xorshift(seed)) % 24, 8); + w.put(0, 1); + for _ in 0..2 { + let entry = xorshift(seed) % 16; + for bit in (0..4).rev() { + w.put(u64::from(entry >> bit) & 1, 1); + } + } + } + // The rest is random bits, enough that no residue runs out of packet. + let mut p = w.bytes; + let fixed = p.len(); + p.extend((0..200 + xorshift(seed) as usize % 200).map(|_| xorshift(seed) as u8)); + let _ = fixed; + // The bits after the fixed fields start mid-byte: the last fixed byte keeps its own bits and + // takes random ones above them. + let used = w.bits % 8; + if used != 0 { + let last = fixed - 1; + p[last] |= (xorshift(seed) as u8) << used; + } + if i > 0 { + let size = |long: bool| if long { 1024 } else { 256 }; + granule += (size(longs[i - 1]) / 4 + size(long) / 4) as i64; + } + out.extend(pages.packet(&p, granule)); + } + out.extend(pages.finish(None)); + out +} + +/// `tolerance`: the largest difference from ffmpeg, as a fraction of the peak output. A floor 0 +/// curve is an exponential of a quotient that random coefficients can make very large, so where +/// two floors meet in one stream (ffmpeg computes in single precision, we in double) the results +/// agree only to about a thousandth of the peak. +fn run(name: &str, l: &Layout, seed: u32, tolerance: f32) { + let mut s = seed; + let ogg = stream(l, 40, &mut s); + // Packet by packet our decoder must accept every one. + let file = craft_media::ogg::open(ogg.as_slice()).expect("open"); + let st = &file.streams[0]; + let mut dec = Decoder::new(&st.headers[0], &st.headers[1], &st.headers[2]).expect("setup decodes"); + let mut total = vec![Vec::new(); usize::from(l.channels)]; + for i in 0..st.packets.len() { + let p = file.read_packet(ogg.as_slice(), 0, i).expect("packet"); + let pcm = dec.decode_packet(&p).unwrap_or_else(|e| panic!("{name}: packet {i}: {e}")); + for (t, c) in total.iter_mut().zip(pcm) { + t.extend(c); + } + } + let expected = usize::try_from(st.last_granule.expect("generated final granule")).expect("nonnegative granule"); + for channel in &total { + assert_eq!(channel.len(), expected, "{name}: decoded sample count"); + assert!(channel.iter().all(|v| v.is_finite()), "{name}: non-finite output"); + } + let whole = decode_ogg(&ogg).expect("whole file"); + assert_eq!(whole.channels, total, "{name}: packet and file decoding differ"); + let Some(ffmpeg) = craft_media_testkit::ffmpeg() else { return }; + let dir = craft_media_testkit::fixture_subdir("vorbis_synthetic"); + let path = dir.join(format!("{name}.ogg")); + std::fs::write(&path, &ogg).expect("write fixture"); + let out = Command::new(&ffmpeg) + .args(["-v", "error", "-nostdin", "-i"]) + .arg(&path) + .args(["-f", "f32le", "-acodec", "pcm_f32le", "-"]) + .output() + .expect("run ffmpeg"); + assert!(out.status.success(), "{name}: ffmpeg failed: {}", String::from_utf8_lossy(&out.stderr)); + let all: Vec = out.stdout.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes(*b)).collect(); + let ch = usize::from(l.channels); + let theirs: Vec> = (0..ch).map(|c| all.iter().skip(c).step_by(ch).copied().collect()).collect(); + assert!(theirs.iter().flatten().all(|v| v.is_finite()), "{name}: non-finite ffmpeg output"); + for channel in &theirs { + assert!(channel.len() >= expected, "{name}: ffmpeg returned fewer than the expected samples"); + } + // ffmpeg puts the channels of surround streams in its own order, so each of ours is matched + // with the channel of theirs it agrees with best, and every one of theirs is used once. + let mut used = vec![false; ch]; + #[allow(clippy::needless_range_loop)] + for c in 0..ch { + let mut best: Option<(usize, f32, f32)> = None; + for (k, t) in theirs.iter().enumerate().filter(|(k, _)| !used[*k]) { + let n = total[c].len().min(t.len()); + assert!(n > 1000, "{name}: only {n} samples to compare"); + let scale = total[c][..n].iter().fold(1e-3f32, |m, v| m.max(v.abs())); + let worst = (0..n).map(|i| (total[c][i] - t[i]).abs()).fold(0f32, f32::max); + if best.is_none_or(|(_, w, sc)| worst / scale < w / sc) { + best = Some((k, worst, scale)); + } + } + let (k, worst, scale) = best.expect("a free channel"); + used[k] = true; + assert!(worst <= tolerance * scale + 1e-5, "{name}: channel {c} differs from ffmpeg's channel {k} by {worst} (peak {scale})"); + } +} + +#[test] +fn mono_floor_zero_and_residue_zero() { + run("mono_f0_r0", &Layout { channels: 1, residue_types: [0, 0], mux: vec![0], coupling: vec![] }, 11, 1e-4); +} + +#[test] +fn mono_floor_one_and_residue_one() { + run("mono_f1_r1", &Layout { channels: 1, residue_types: [1, 1], mux: vec![1], coupling: vec![] }, 12, 1e-4); +} + +#[test] +fn two_submaps_floor_zero_and_one() { + run("stereo_mixed", &Layout { channels: 2, residue_types: [0, 1], mux: vec![0, 1], coupling: vec![] }, 13, 3e-3); +} + +#[test] +fn coupled_stereo_residue_two() { + run("stereo_coupled_r2", &Layout { channels: 2, residue_types: [2, 2], mux: vec![1, 1], coupling: vec![(0, 1)] }, 14, 1e-4); +} + +#[test] +fn six_channels_with_coupling_and_submaps() { + let l = Layout { channels: 6, residue_types: [1, 2], mux: vec![0, 0, 0, 1, 1, 1], coupling: vec![(0, 3), (1, 2), (4, 5)] }; + run("six", &l, 15, 3e-3); +} + +#[test] +fn mono_floor_one_and_residue_zero() { + run("mono_f1_r0", &Layout { channels: 1, residue_types: [0, 0], mux: vec![1], coupling: vec![] }, 16, 1e-4); +} + +#[test] +fn mono_floor_zero_and_residue_one() { + run("mono_f0_r1", &Layout { channels: 1, residue_types: [1, 1], mux: vec![0], coupling: vec![] }, 17, 1e-4); +} + +/// A packet that names a floor 0 codebook that does not exist cannot be decoded (§6.2.2); the +/// decoder says so and the stream goes on with the next packet. +#[test] +fn a_floor_zero_codebook_that_does_not_exist_is_a_packet_error() { + let l = Layout { channels: 1, residue_types: [1, 1], mux: vec![0], coupling: vec![] }; + let mut seed = 5u32; + let ogg = stream(&l, 6, &mut seed); + let file = craft_media::ogg::open(ogg.as_slice()).expect("open"); + let st = &file.streams[0]; + let mut dec = Decoder::new(&st.headers[0], &st.headers[1], &st.headers[2]).expect("decoder"); + let good = file.read_packet(ogg.as_slice(), 0, 0).expect("packet"); + assert!(dec.decode_packet(&good).is_ok()); + // A short block: type 0, mode 0, then the 8 amplitude bits, then book number 1 (only 0 exists). + let mut w = W::default(); + w.flag(false); + w.flag(false); + w.put(9, 8); + w.put(1, 1); + w.put(0, 64); + assert!(matches!(dec.decode_packet(&w.bytes), Err(craft_audio::vorbis::decoder::Error::Packet(_)))); + assert!(dec.decode_packet(&good).is_ok(), "the decoder carries on"); +} diff --git a/crates/captions/Cargo.toml b/crates/captions/Cargo.toml index cd09f7e166..1136f55f9e 100644 --- a/crates/captions/Cargo.toml +++ b/crates/captions/Cargo.toml @@ -10,6 +10,11 @@ repository.workspace = true [lints] workspace = true +[features] +default = [] +# MP4 timed text (3GPP TS 26.245 / mov_text / tx3g) sample codec. +mov-text = [] + [dependencies] craft-timeline = { workspace = true } craft-xml = { workspace = true } @@ -21,3 +26,5 @@ proptest = { workspace = true } roxmltree = "0.21" # ffmpeg as the reference for the ASS tests (tests/it/ass.rs). craft-media-testkit = { workspace = true } +# craft-media isobmff for mov_text oracle demuxing/muxing tests. +craft-media = { workspace = true, features = ["isobmff"] } diff --git a/crates/captions/README.md b/crates/captions/README.md index 7380a3ce41..b2f6a195f0 100644 --- a/crates/captions/README.md +++ b/crates/captions/README.md @@ -27,6 +27,10 @@ assert!(scc.starts_with(b"Scenarist_SCC V1.0")); and the SMPTE ST 334-2 caption distribution packet. - `plain_text` strips inline tags and decodes the common entities, for display or for formats without markup. +- Optional feature `mov-text` provides MP4 Timed Text (`mov_text` / `tx3g`, 3GPP TS 26.245) + sample decoding, encoding, modifier box parsing (`styl`, `hlit`, `hclr`, `krok`, `dlay`, `href`, + `tbox`, `blnk`, `wrap`), and `Tx3gConfig` sample entry parsing/writing for ISO BMFF (`.mp4`) and + QuickTime (`.mov`) subtitle tracks. ## Time diff --git a/crates/captions/src/lib.rs b/crates/captions/src/lib.rs index ca99dba439..2d0cf90c18 100644 --- a/crates/captions/src/lib.rs +++ b/crates/captions/src/lib.rs @@ -36,6 +36,8 @@ pub mod ass; pub mod cea608; pub mod jacosub; pub mod mcc; +#[cfg(feature = "mov-text")] +pub mod mov_text; pub mod pjs; pub mod realtext; pub mod scc; diff --git a/crates/captions/src/mov_text.rs b/crates/captions/src/mov_text.rs new file mode 100644 index 0000000000..d1a3ce2a91 --- /dev/null +++ b/crates/captions/src/mov_text.rs @@ -0,0 +1,871 @@ +//! MP4 Timed Text (`mov_text` / `tx3g`, 3GPP TS 26.245). +//! +//! A clean-room reader and writer for 3GPP Timed Text subtitle samples and sample entry +//! configurations (`tx3g`), as carried in ISO BMFF (`.mp4`) and QuickTime (`.mov`) files. +//! +//! ## Sample Structure +//! +//! A timed text sample consists of: +//! - A 16-bit big-endian text length. +//! - The text string bytes (UTF-8, or UTF-16 with BOM). +//! - Zero or more modifier boxes: +//! - `styl`: Character style runs (bold, italic, underline, font ID, font size, text color). +//! - `hlit`: Highlighted character range. +//! - `hclr`: Highlight color (RGBA). +//! - `krok`: Karaoke highlight timing and character spans. +//! - `dlay`: Scroll delay in track-timescale ticks. +//! - `href`: Hypertext link URL and alt text. +//! - `tbox`: Text box override coordinates (`top, left, bottom, right`). +//! - `blnk`: Blinking text character range. +//! - `twrp`: Text wrap flag (0 = no wrap, 1 = automatic soft wrap). +//! +//! ## Style Flags +//! +//! Style flags in `styl` records: +//! - `0x01`: Bold (`...`) +//! - `0x02`: Italic (`...`) +//! - `0x04`: Underline (`...`) + +use crate::time::Tick; +use crate::{Cue, Document, TICKS_PER_SECOND}; + +/// What went wrong reading or writing timed text. +#[non_exhaustive] +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum Error { + /// The input slice is shorter than the declared field or box size. + #[error("truncated timed text data")] + Truncated, + /// The text payload is not valid UTF-8 and contains no valid UTF-16 BOM. + #[error("invalid text encoding in timed text sample")] + InvalidText, + /// The text exceeds the 16-bit length prefix (65,535 bytes). + #[error("timed text string exceeds 65535 bytes: {0}")] + TextTooLong(usize), + /// A modifier box header has an invalid size (< 8). + #[error("invalid modifier box size: {0}")] + InvalidBox(&'static str), +} + +/// `Result` alias for timed text operations. +pub type Result = std::result::Result; + +/// Face style flag: Bold (`0x01`). +pub const STYLE_FLAG_BOLD: u8 = 0x01; +/// Face style flag: Italic (`0x02`). +pub const STYLE_FLAG_ITALIC: u8 = 0x02; +/// Face style flag: Underline (`0x04`). +pub const STYLE_FLAG_UNDERLINE: u8 = 0x04; + +/// A character styling record within a `styl` box. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct StyleRecord { + /// 0-indexed start character index (inclusive, measured in Unicode scalar values). + pub start_char: u16, + /// 0-indexed end character index (exclusive, measured in Unicode scalar values). + pub end_char: u16, + /// 16-bit font ID matching an entry in the sample description font table (`ftab`). + pub font_id: u16, + /// Face style flags: bit 0 = bold, bit 1 = italic, bit 2 = underline. + pub face_style_flags: u8, + /// Font size in points. + pub font_size: u8, + /// Text RGBA color (`[r, g, b, a]`). + pub text_color_rgba: [u8; 4], +} + +impl Default for StyleRecord { + fn default() -> Self { + Self { start_char: 0, end_char: 0, font_id: 1, face_style_flags: 0, font_size: 16, text_color_rgba: [255, 255, 255, 255] } + } +} + +impl StyleRecord { + /// Returns `true` if bold is set. + pub fn is_bold(&self) -> bool { + self.face_style_flags & STYLE_FLAG_BOLD != 0 + } + + /// Returns `true` if italic is set. + pub fn is_italic(&self) -> bool { + self.face_style_flags & STYLE_FLAG_ITALIC != 0 + } + + /// Returns `true` if underline is set. + pub fn is_underline(&self) -> bool { + self.face_style_flags & STYLE_FLAG_UNDERLINE != 0 + } +} + +/// Coordinates of a text display box (`tbox`). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct TextBox { + pub top: i16, + pub left: i16, + pub bottom: i16, + pub right: i16, +} + +/// An entry in a karaoke box (`krok`). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct KaraokeEntry { + /// Highlight end time in timescale ticks. + pub highlight_end_time: u32, + /// Start character index (inclusive). + pub start_char: u16, + /// End character index (exclusive). + pub end_char: u16, +} + +/// A hypertext link entry (`href`). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct HyperText { + /// Start character index (inclusive). + pub start_char: u16, + /// End character index (exclusive). + pub end_char: u16, + /// Destination URL. + pub url: String, + /// Accessible alt text string. + pub alt_string: String, +} + +/// An unpacked 3GPP Timed Text (`tx3g` / `mov_text`) sample. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct TextSample { + /// Decoded UTF-8 text content. + pub text: String, + /// Character styling records from the `styl` box. + pub styles: Vec, + /// Highlighted character range `(start_char, end_char)` from `hlit`. + pub highlight: Option<(u16, u16)>, + /// Highlight color `[r, g, b, a]` from `hclr`. + pub highlight_color: Option<[u8; 4]>, + /// Karaoke timing and spans `(highlight_start_time, entries)` from `krok`. + pub karaoke: Option<(u32, Vec)>, + /// Scroll delay in milliseconds from `dlay`. + pub scroll_delay: Option, + /// Hypertext links from `href` boxes. + pub hypertext: Vec, + /// Text box override from `tbox`. + pub text_box: Option, + /// Blinking character ranges `(start_char, end_char)` from `blnk` boxes. + pub blinking: Vec<(u16, u16)>, + /// Wrap flag from `wrap` box: `0` = automatic wrap, `1` = no wrap. + pub wrap_flag: Option, +} + +/// Sample description table configuration for a `tx3g` track. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Tx3gConfig { + /// 32-bit display flags (e.g. scroll in, scroll out, continuous scroll). + pub display_flags: u32, + /// Horizontal justification: 0 = left, 1 = center, -1 = right. + pub horizontal_justification: i8, + /// Vertical justification: 0 = top, 1 = center, -1 = bottom. + pub vertical_justification: i8, + /// Background RGBA color. + pub background_color: [u8; 4], + /// Default text box coordinates. + pub default_text_box: TextBox, + /// Default style record. + pub default_style: StyleRecord, + /// Font table entries `(font_id, font_name)`. + pub fonts: Vec<(u16, String)>, +} + +impl Default for Tx3gConfig { + fn default() -> Self { + Self { + display_flags: 0, + horizontal_justification: 1, // center + vertical_justification: -1, // bottom + background_color: [0, 0, 0, 255], + default_text_box: TextBox::default(), + default_style: StyleRecord::default(), + fonts: vec![(1, "Arial".to_string())], + } + } +} + +impl Tx3gConfig { + /// Parse a `tx3g` sample entry payload (after the standard 8-byte sample entry header). + pub fn parse(payload: &[u8]) -> Result { + if payload.len() < 30 { + return Err(Error::Truncated); + } + let display_flags = u32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]); + let horizontal_justification = payload[4] as i8; + let vertical_justification = payload[5] as i8; + let background_color = [payload[6], payload[7], payload[8], payload[9]]; + + let default_text_box = TextBox { + top: i16::from_be_bytes([payload[10], payload[11]]), + left: i16::from_be_bytes([payload[12], payload[13]]), + bottom: i16::from_be_bytes([payload[14], payload[15]]), + right: i16::from_be_bytes([payload[16], payload[17]]), + }; + + let default_style = StyleRecord { + start_char: u16::from_be_bytes([payload[18], payload[19]]), + end_char: u16::from_be_bytes([payload[20], payload[21]]), + font_id: u16::from_be_bytes([payload[22], payload[23]]), + face_style_flags: payload[24], + font_size: payload[25], + text_color_rgba: [payload[26], payload[27], payload[28], payload[29]], + }; + + let mut fonts = Vec::new(); + let mut pos = 30; + while pos + 8 <= payload.len() { + let box_size = u32::from_be_bytes([payload[pos], payload[pos + 1], payload[pos + 2], payload[pos + 3]]) as usize; + if box_size < 8 || pos.checked_add(box_size).is_none_or(|end| end > payload.len()) { + break; + } + let box_type = &payload[pos + 4..pos + 8]; + if box_type == b"ftab" && box_size >= 10 { + let count = u16::from_be_bytes([payload[pos + 8], payload[pos + 9]]) as usize; + let mut fpos = pos + 10; + for _ in 0..count { + if fpos + 3 > pos + box_size { + break; + } + let font_id = u16::from_be_bytes([payload[fpos], payload[fpos + 1]]); + let name_len = payload[fpos + 2] as usize; + fpos += 3; + if fpos + name_len > pos + box_size { + break; + } + if let Ok(name) = std::str::from_utf8(&payload[fpos..fpos + name_len]) { + fonts.push((font_id, name.to_string())); + } + fpos += name_len; + } + } + pos += box_size; + } + + Ok(Self { display_flags, horizontal_justification, vertical_justification, background_color, default_text_box, default_style, fonts }) + } + + /// Write the `tx3g` sample entry payload (after the 8-byte sample entry header). + pub fn write(&self) -> Vec { + let mut out = Vec::with_capacity(30 + 32); + out.extend_from_slice(&self.display_flags.to_be_bytes()); + out.push(self.horizontal_justification as u8); + out.push(self.vertical_justification as u8); + out.extend_from_slice(&self.background_color); + + out.extend_from_slice(&self.default_text_box.top.to_be_bytes()); + out.extend_from_slice(&self.default_text_box.left.to_be_bytes()); + out.extend_from_slice(&self.default_text_box.bottom.to_be_bytes()); + out.extend_from_slice(&self.default_text_box.right.to_be_bytes()); + + out.extend_from_slice(&self.default_style.start_char.to_be_bytes()); + out.extend_from_slice(&self.default_style.end_char.to_be_bytes()); + out.extend_from_slice(&self.default_style.font_id.to_be_bytes()); + out.push(self.default_style.face_style_flags); + out.push(self.default_style.font_size); + out.extend_from_slice(&self.default_style.text_color_rgba); + + // ftab box + let mut ftab_payload = Vec::new(); + let fonts = &self.fonts[..self.fonts.len().min(u16::MAX as usize)]; + ftab_payload.extend_from_slice(&(fonts.len() as u16).to_be_bytes()); + for (id, name) in fonts { + ftab_payload.extend_from_slice(&id.to_be_bytes()); + let name_bytes = name.as_bytes(); + let limit = name.floor_char_boundary(name_bytes.len().min(255)); + ftab_payload.push(limit as u8); + ftab_payload.extend_from_slice(&name_bytes[..limit]); + } + let ftab_size = 8u32.saturating_add(ftab_payload.len() as u32); + out.extend_from_slice(&ftab_size.to_be_bytes()); + out.extend_from_slice(b"ftab"); + out.extend_from_slice(&ftab_payload); + + out + } +} + +/// Read and decode a single 3GPP Timed Text sample (`tx3g` / `mov_text`). +pub fn read_sample(data: &[u8]) -> Result { + if data.len() < 2 { + return Err(Error::Truncated); + } + + let text_len = u16::from_be_bytes([data[0], data[1]]) as usize; + if data.len() < 2 + text_len { + return Err(Error::Truncated); + } + + let text_bytes = &data[2..2 + text_len]; + let text = decode_text(text_bytes)?; + + let mut sample = TextSample { text, ..Default::default() }; + + let mut pos = 2 + text_len; + while pos + 8 <= data.len() { + let size = u32::from_be_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]) as usize; + if size < 8 { + return Err(Error::InvalidBox("size smaller than 8")); + } + let box_end = pos.checked_add(size).ok_or(Error::Truncated)?; + if box_end > data.len() { + return Err(Error::Truncated); + } + + let box_type = &data[pos + 4..pos + 8]; + let payload = &data[pos + 8..box_end]; + + match box_type { + b"styl" => { + if payload.len() < 2 { + return Err(Error::Truncated); + } + let count = u16::from_be_bytes([payload[0], payload[1]]) as usize; + let mut spos = 2; + for _ in 0..count { + if spos + 12 > payload.len() { + return Err(Error::Truncated); + } + sample.styles.push(StyleRecord { + start_char: u16::from_be_bytes([payload[spos], payload[spos + 1]]), + end_char: u16::from_be_bytes([payload[spos + 2], payload[spos + 3]]), + font_id: u16::from_be_bytes([payload[spos + 4], payload[spos + 5]]), + face_style_flags: payload[spos + 6], + font_size: payload[spos + 7], + text_color_rgba: [payload[spos + 8], payload[spos + 9], payload[spos + 10], payload[spos + 11]], + }); + spos += 12; + } + } + b"hlit" => { + if payload.len() < 4 { + return Err(Error::Truncated); + } + let start = u16::from_be_bytes([payload[0], payload[1]]); + let end = u16::from_be_bytes([payload[2], payload[3]]); + sample.highlight = Some((start, end)); + } + b"hclr" => { + if payload.len() < 4 { + return Err(Error::Truncated); + } + sample.highlight_color = Some([payload[0], payload[1], payload[2], payload[3]]); + } + b"krok" => { + if payload.len() < 6 { + return Err(Error::Truncated); + } + let start_time = u32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]]); + let count = u16::from_be_bytes([payload[4], payload[5]]) as usize; + let mut entries = Vec::new(); + let mut kpos = 6; + for _ in 0..count { + if kpos + 8 > payload.len() { + return Err(Error::Truncated); + } + entries.push(KaraokeEntry { + highlight_end_time: u32::from_be_bytes([payload[kpos], payload[kpos + 1], payload[kpos + 2], payload[kpos + 3]]), + start_char: u16::from_be_bytes([payload[kpos + 4], payload[kpos + 5]]), + end_char: u16::from_be_bytes([payload[kpos + 6], payload[kpos + 7]]), + }); + kpos += 8; + } + sample.karaoke = Some((start_time, entries)); + } + b"dlay" => { + if payload.len() < 4 { + return Err(Error::Truncated); + } + sample.scroll_delay = Some(u32::from_be_bytes([payload[0], payload[1], payload[2], payload[3]])); + } + b"href" => { + if payload.len() < 6 { + return Err(Error::Truncated); + } + let start = u16::from_be_bytes([payload[0], payload[1]]); + let end = u16::from_be_bytes([payload[2], payload[3]]); + let url_len = payload[4] as usize; + if 5 + url_len > payload.len() { + return Err(Error::Truncated); + } + let url = std::str::from_utf8(&payload[5..5 + url_len]).map_err(|_| Error::InvalidText)?.to_string(); + let alt_pos = 5 + url_len; + let alt_len = payload.get(alt_pos).copied().unwrap_or(0) as usize; + let alt_string = if alt_len > 0 && alt_pos + 1 + alt_len <= payload.len() { + std::str::from_utf8(&payload[alt_pos + 1..alt_pos + 1 + alt_len]).map_err(|_| Error::InvalidText)?.to_string() + } else { + String::new() + }; + sample.hypertext.push(HyperText { start_char: start, end_char: end, url, alt_string }); + } + b"tbox" => { + if payload.len() < 8 { + return Err(Error::Truncated); + } + sample.text_box = Some(TextBox { + top: i16::from_be_bytes([payload[0], payload[1]]), + left: i16::from_be_bytes([payload[2], payload[3]]), + bottom: i16::from_be_bytes([payload[4], payload[5]]), + right: i16::from_be_bytes([payload[6], payload[7]]), + }); + } + b"blnk" => { + if payload.len() < 4 { + return Err(Error::Truncated); + } + let start = u16::from_be_bytes([payload[0], payload[1]]); + let end = u16::from_be_bytes([payload[2], payload[3]]); + sample.blinking.push((start, end)); + } + b"twrp" | b"wrap" => { + if payload.is_empty() { + return Err(Error::Truncated); + } + sample.wrap_flag = Some(payload[0]); + } + _ => { + // Unknown modifier box: safely ignored. + } + } + + pos = box_end; + } + + Ok(sample) +} + +/// Helper to decode UTF-8 or UTF-16 with BOM. +fn decode_text(bytes: &[u8]) -> Result { + if bytes.len() >= 2 { + if bytes[0] == 0xFE && bytes[1] == 0xFF { + // UTF-16 BE + let u16s: Vec = bytes[2..].as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); + return String::from_utf16(&u16s).map_err(|_| Error::InvalidText); + } else if bytes[0] == 0xFF && bytes[1] == 0xFE { + // UTF-16 LE + let u16s: Vec = bytes[2..].as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes([c[0], c[1]])).collect(); + return String::from_utf16(&u16s).map_err(|_| Error::InvalidText); + } + } + std::str::from_utf8(bytes).map(ToString::to_string).map_err(|_| Error::InvalidText) +} + +/// Pack and encode a timed text sample into bytes. +pub fn write_sample(sample: &TextSample) -> Result> { + let text_bytes = sample.text.as_bytes(); + if text_bytes.len() > u16::MAX as usize { + return Err(Error::TextTooLong(text_bytes.len())); + } + + let mut out = Vec::with_capacity(2 + text_bytes.len() + 64); + out.extend_from_slice(&(text_bytes.len() as u16).to_be_bytes()); + out.extend_from_slice(text_bytes); + + // 1. styl box + if !sample.styles.is_empty() { + if sample.styles.len() > u16::MAX as usize { + return Err(Error::InvalidBox("too many styles in sample")); + } + let size = 8u32.checked_add(2).and_then(|s| s.checked_add(sample.styles.len() as u32 * 12)).ok_or(Error::InvalidBox("styl size overflow"))?; + out.extend_from_slice(&size.to_be_bytes()); + out.extend_from_slice(b"styl"); + out.extend_from_slice(&(sample.styles.len() as u16).to_be_bytes()); + for st in &sample.styles { + out.extend_from_slice(&st.start_char.to_be_bytes()); + out.extend_from_slice(&st.end_char.to_be_bytes()); + out.extend_from_slice(&st.font_id.to_be_bytes()); + out.push(st.face_style_flags); + out.push(st.font_size); + out.extend_from_slice(&st.text_color_rgba); + } + } + + // 2. hlit box + if let Some((start, end)) = sample.highlight { + out.extend_from_slice(&12u32.to_be_bytes()); + out.extend_from_slice(b"hlit"); + out.extend_from_slice(&start.to_be_bytes()); + out.extend_from_slice(&end.to_be_bytes()); + } + + // 3. hclr box + if let Some(color) = sample.highlight_color { + out.extend_from_slice(&12u32.to_be_bytes()); + out.extend_from_slice(b"hclr"); + out.extend_from_slice(&color); + } + + // 4. krok box + if let Some((start_time, ref entries)) = sample.karaoke { + if entries.len() > u16::MAX as usize { + return Err(Error::InvalidBox("too many karaoke entries")); + } + let size = 8u32.checked_add(6).and_then(|s| s.checked_add(entries.len() as u32 * 8)).ok_or(Error::InvalidBox("krok size overflow"))?; + out.extend_from_slice(&size.to_be_bytes()); + out.extend_from_slice(b"krok"); + out.extend_from_slice(&start_time.to_be_bytes()); + out.extend_from_slice(&(entries.len() as u16).to_be_bytes()); + for e in entries { + out.extend_from_slice(&e.highlight_end_time.to_be_bytes()); + out.extend_from_slice(&e.start_char.to_be_bytes()); + out.extend_from_slice(&e.end_char.to_be_bytes()); + } + } + + // 5. dlay box + if let Some(delay) = sample.scroll_delay { + out.extend_from_slice(&12u32.to_be_bytes()); + out.extend_from_slice(b"dlay"); + out.extend_from_slice(&delay.to_be_bytes()); + } + + // 6. href boxes + for link in &sample.hypertext { + let url_bytes = link.url.as_bytes(); + let alt_bytes = link.alt_string.as_bytes(); + let ulen = url_bytes.len().min(255) as u8; + let alen = alt_bytes.len().min(255) as u8; + let size = 8u32 + 4 + 1 + ulen as u32 + 1 + alen as u32; + out.extend_from_slice(&size.to_be_bytes()); + out.extend_from_slice(b"href"); + out.extend_from_slice(&link.start_char.to_be_bytes()); + out.extend_from_slice(&link.end_char.to_be_bytes()); + out.push(ulen); + out.extend_from_slice(&url_bytes[..ulen as usize]); + out.push(alen); + out.extend_from_slice(&alt_bytes[..alen as usize]); + } + + // 7. tbox box + if let Some(tb) = sample.text_box { + out.extend_from_slice(&16u32.to_be_bytes()); + out.extend_from_slice(b"tbox"); + out.extend_from_slice(&tb.top.to_be_bytes()); + out.extend_from_slice(&tb.left.to_be_bytes()); + out.extend_from_slice(&tb.bottom.to_be_bytes()); + out.extend_from_slice(&tb.right.to_be_bytes()); + } + + // 8. blnk boxes + for &(start, end) in &sample.blinking { + out.extend_from_slice(&12u32.to_be_bytes()); + out.extend_from_slice(b"blnk"); + out.extend_from_slice(&start.to_be_bytes()); + out.extend_from_slice(&end.to_be_bytes()); + } + + // 9. twrp box + if let Some(flag) = sample.wrap_flag { + out.extend_from_slice(&9u32.to_be_bytes()); + out.extend_from_slice(b"twrp"); + out.push(flag); + } + + Ok(out) +} + +fn push_escaped_char(out: &mut String, ch: char) { + match ch { + '&' => out.push_str("&"), + '<' => out.push_str("<"), + '>' => out.push_str(">"), + _ => out.push(ch), + } +} + +/// Convert a [`TextSample`] into HTML-style formatted markup (``, ``, ``, ``). +pub fn sample_to_markup(sample: &TextSample) -> String { + if sample.styles.is_empty() { + let mut out = String::with_capacity(sample.text.len()); + for ch in sample.text.chars() { + push_escaped_char(&mut out, ch); + } + return out; + } + + let chars: Vec = sample.text.chars().collect(); + let n = chars.len(); + if n == 0 { + return String::new(); + } + + let mut starts: std::collections::HashMap> = std::collections::HashMap::new(); + let mut ends: std::collections::HashMap> = std::collections::HashMap::new(); + for st in &sample.styles { + if st.end_char > st.start_char { + starts.entry(st.start_char).or_default().push(st); + ends.entry(st.end_char).or_default().push(st); + } + } + + let mut out = String::with_capacity(sample.text.len() + sample.styles.len() * 16); + + for (i, &ch) in chars.iter().enumerate() { + let char_idx = i as u16; + + // Open tags for any style starting at char_idx + if let Some(list) = starts.get(&char_idx) { + for st in list { + let has_color = st.text_color_rgba != [255, 255, 255, 255]; + if has_color { + out.push_str(&format!( + "", + st.text_color_rgba[0], st.text_color_rgba[1], st.text_color_rgba[2] + )); + } + if st.is_bold() { + out.push_str(""); + } + if st.is_italic() { + out.push_str(""); + } + if st.is_underline() { + out.push_str(""); + } + } + } + + push_escaped_char(&mut out, ch); + + // Close tags for any style ending at char_idx + 1 + if let Some(list) = ends.get(&(char_idx + 1)) { + for st in list.iter().rev() { + if st.is_underline() { + out.push_str(""); + } + if st.is_italic() { + out.push_str(""); + } + if st.is_bold() { + out.push_str(""); + } + let has_color = st.text_color_rgba != [255, 255, 255, 255]; + if has_color { + out.push_str(""); + } + } + } + } + + out +} + +#[derive(Clone, Copy, PartialEq, Eq)] +struct CharFormatting { + flags: u8, + color: [u8; 4], +} + +impl Default for CharFormatting { + fn default() -> Self { + Self { flags: 0, color: [255, 255, 255, 255] } + } +} + +/// Parse HTML-style formatted markup (``, ``, ``, ``) into a [`TextSample`]. +pub fn markup_to_sample(markup: &str) -> TextSample { + let mut bold_depth = 0usize; + let mut italic_depth = 0usize; + let mut underline_depth = 0usize; + let mut color_stack: Vec<[u8; 4]> = Vec::new(); + + let mut decoded_chars: Vec = Vec::with_capacity(markup.len()); + let mut char_styles: Vec = Vec::with_capacity(markup.len()); + + let mut chars = markup.chars().peekable(); + + while let Some(ch) = chars.next() { + if ch == '<' { + let mut tag = String::new(); + for next in chars.by_ref() { + if next == '>' { + break; + } + tag.push(next); + } + let trimmed = tag.trim().to_ascii_lowercase(); + if trimmed == "b" { + bold_depth += 1; + } else if trimmed == "/b" { + bold_depth = bold_depth.saturating_sub(1); + } else if trimmed == "i" { + italic_depth += 1; + } else if trimmed == "/i" { + italic_depth = italic_depth.saturating_sub(1); + } else if trimmed == "u" { + underline_depth += 1; + } else if trimmed == "/u" { + underline_depth = underline_depth.saturating_sub(1); + } else if trimmed.starts_with("font") { + if let Some(color) = parse_font_color(&tag) { + color_stack.push(color); + } + } else if trimmed == "/font" { + color_stack.pop(); + } + } else { + let mut text_chunk = String::new(); + text_chunk.push(ch); + while let Some(&next) = chars.peek() { + if next == '<' { + break; + } + text_chunk.push(next); + chars.next(); + } + + let decoded = crate::decode_entities(&text_chunk); + let mut flags = 0u8; + if bold_depth > 0 { + flags |= STYLE_FLAG_BOLD; + } + if italic_depth > 0 { + flags |= STYLE_FLAG_ITALIC; + } + if underline_depth > 0 { + flags |= STYLE_FLAG_UNDERLINE; + } + let color = color_stack.last().copied().unwrap_or([255, 255, 255, 255]); + let fmt = CharFormatting { flags, color }; + + for c in decoded.chars() { + decoded_chars.push(c); + char_styles.push(fmt); + } + } + } + + let text: String = decoded_chars.iter().collect(); + + // Group adjacent characters with identical non-default formatting into sorted, non-overlapping style runs. + let mut styles = Vec::new(); + let mut run_start: Option<(usize, CharFormatting)> = None; + + for (idx, &fmt) in char_styles.iter().enumerate() { + let is_styled = fmt.flags != 0 || fmt.color != [255, 255, 255, 255]; + match run_start { + Some((start, current_fmt)) => { + if fmt != current_fmt { + if current_fmt.flags != 0 || current_fmt.color != [255, 255, 255, 255] { + styles.push(StyleRecord { + start_char: start as u16, + end_char: idx as u16, + font_id: 1, + face_style_flags: current_fmt.flags, + font_size: 16, + text_color_rgba: current_fmt.color, + }); + } + run_start = if is_styled { Some((idx, fmt)) } else { None }; + } + } + None => { + if is_styled { + run_start = Some((idx, fmt)); + } + } + } + } + + if let Some((start, fmt)) = run_start { + styles.push(StyleRecord { + start_char: start as u16, + end_char: char_styles.len() as u16, + font_id: 1, + face_style_flags: fmt.flags, + font_size: 16, + text_color_rgba: fmt.color, + }); + } + + TextSample { text, styles, ..Default::default() } +} + +/// Helper to parse color attribute from `` tag. +fn parse_font_color(tag: &str) -> Option<[u8; 4]> { + let lower = tag.to_ascii_lowercase(); + let col_idx = lower.find("color=")?; + let rest = tag[col_idx + 6..].trim_start(); + let val = if let Some(stripped) = rest.strip_prefix('"') { + let end = stripped.find('"')?; + &stripped[..end] + } else if let Some(stripped) = rest.strip_prefix('\'') { + let end = stripped.find('\'')?; + &stripped[..end] + } else { + let end = rest.find(char::is_whitespace).unwrap_or(rest.len()); + &rest[..end] + }; + + let hex = val.strip_prefix('#').unwrap_or(val); + if hex.len() == 6 { + let r = u8::from_str_radix(hex.get(0..2)?, 16).ok()?; + let g = u8::from_str_radix(hex.get(2..4)?, 16).ok()?; + let b = u8::from_str_radix(hex.get(4..6)?, 16).ok()?; + Some([r, g, b, 255]) + } else if hex.len() == 8 { + let r = u8::from_str_radix(hex.get(0..2)?, 16).ok()?; + let g = u8::from_str_radix(hex.get(2..4)?, 16).ok()?; + let b = u8::from_str_radix(hex.get(4..6)?, 16).ok()?; + let a = u8::from_str_radix(hex.get(6..8)?, 16).ok()?; + Some([r, g, b, a]) + } else { + None + } +} + +/// Convert a caption [`Cue`] into a [`TextSample`]. +pub fn cue_to_sample(cue: &Cue) -> TextSample { + markup_to_sample(&cue.text) +} + +/// Convert a [`TextSample`] into a caption [`Cue`] spanning `start` to `end`. +pub fn sample_to_cue(sample: &TextSample, start: Tick, end: Tick) -> Cue { + Cue { start, end, text: sample_to_markup(sample), speaker: None, id: None, settings: String::new() } +} + +/// Decode a stream of timed text samples into a [`Document`]. +pub fn decode_timed_samples<'a, I>(doc: &mut Document, samples: I, timescale: u32) -> Result<()> +where + I: IntoIterator, +{ + decode_timed_samples_with_config(doc, samples, timescale, None) +} + +/// Decode a stream of timed text samples into a [`Document`], applying an optional sample description configuration. +pub fn decode_timed_samples_with_config<'a, I>(doc: &mut Document, samples: I, timescale: u32, config: Option<&Tx3gConfig>) -> Result<()> +where + I: IntoIterator, +{ + if timescale == 0 { + return Ok(()); + } + let ts = timescale as i128; + for (pts, duration, data) in samples { + let mut sample = read_sample(data)?; + if sample.text.trim().is_empty() { + continue; + } + if sample.styles.is_empty() + && let Some(cfg) = config + && (cfg.default_style.face_style_flags != 0 || cfg.default_style.text_color_rgba != [255, 255, 255, 255]) + { + sample.styles.push(StyleRecord { + start_char: 0, + end_char: sample.text.chars().count() as u16, + font_id: cfg.default_style.font_id, + face_style_flags: cfg.default_style.face_style_flags, + font_size: cfg.default_style.font_size, + text_color_rgba: cfg.default_style.text_color_rgba, + }); + } + let end_pts = pts.checked_add(duration as u64).ok_or(Error::InvalidBox("timestamp interval overflow"))?; + let start_ticks = (pts as i128 * TICKS_PER_SECOND as i128 / ts) as i64; + let end_ticks = (end_pts as i128 * TICKS_PER_SECOND as i128 / ts) as i64; + doc.cues.push(sample_to_cue(&sample, Tick(start_ticks), Tick(end_ticks))); + } + Ok(()) +} diff --git a/crates/captions/tests/it/main.rs b/crates/captions/tests/it/main.rs index 437b2ba67a..7d8070cad6 100644 --- a/crates/captions/tests/it/main.rs +++ b/crates/captions/tests/it/main.rs @@ -2,6 +2,8 @@ mod ass; mod limits; mod literal_roundtrips; mod mcc_708_timing; +#[cfg(feature = "mov-text")] +mod mov_text; mod pjs_realtext_jacosub; mod roundtrip; mod ttml_body_styles; diff --git a/crates/captions/tests/it/mov_text.rs b/crates/captions/tests/it/mov_text.rs new file mode 100644 index 0000000000..dfd5f73ea5 --- /dev/null +++ b/crates/captions/tests/it/mov_text.rs @@ -0,0 +1,349 @@ +//! Clean-room tests and FFmpeg oracle tests for MP4 Timed Text (`mov_text` / `tx3g`). + +use craft_captions::mov_text::{ + Error, KaraokeEntry, STYLE_FLAG_BOLD, STYLE_FLAG_ITALIC, STYLE_FLAG_UNDERLINE, StyleRecord, TextBox, TextSample, Tx3gConfig, + decode_timed_samples, markup_to_sample, read_sample, sample_to_markup, write_sample, +}; +use craft_captions::time::Tick; +use craft_captions::{Document, TICKS_PER_SECOND}; + +#[test] +fn test_plain_text_roundtrip() { + let sample = TextSample { text: "Hello world without styles".to_string(), ..Default::default() }; + + let bytes = write_sample(&sample).expect("write sample"); + assert_eq!(bytes.len(), 2 + sample.text.len()); + assert_eq!(&bytes[..2], &(sample.text.len() as u16).to_be_bytes()); + assert_eq!(&bytes[2..], sample.text.as_bytes()); + + let decoded = read_sample(&bytes).expect("read sample"); + assert_eq!(decoded.text, sample.text); + assert!(decoded.styles.is_empty()); +} + +#[test] +fn test_empty_sample_roundtrip() { + let sample = TextSample::default(); + let bytes = write_sample(&sample).expect("write"); + assert_eq!(bytes, &[0x00, 0x00]); + + let decoded = read_sample(&bytes).expect("read"); + assert_eq!(decoded.text, ""); + assert!(decoded.styles.is_empty()); +} + +#[test] +fn test_styles_roundtrip() { + let sample = TextSample { + text: "Bold, Italic, Underline, and Colored".to_string(), + styles: vec![ + StyleRecord { + start_char: 0, + end_char: 4, + font_id: 1, + face_style_flags: STYLE_FLAG_BOLD, + font_size: 16, + text_color_rgba: [255, 255, 255, 255], + }, + StyleRecord { + start_char: 6, + end_char: 12, + font_id: 1, + face_style_flags: STYLE_FLAG_ITALIC, + font_size: 18, + text_color_rgba: [255, 255, 255, 255], + }, + StyleRecord { + start_char: 14, + end_char: 23, + font_id: 1, + face_style_flags: STYLE_FLAG_UNDERLINE, + font_size: 16, + text_color_rgba: [255, 255, 255, 255], + }, + StyleRecord { start_char: 29, end_char: 36, font_id: 1, face_style_flags: 0, font_size: 20, text_color_rgba: [255, 0, 0, 255] }, + ], + ..Default::default() + }; + + let bytes = write_sample(&sample).expect("write"); + let decoded = read_sample(&bytes).expect("read"); + + assert_eq!(decoded.text, sample.text); + assert_eq!(decoded.styles.len(), 4); + assert_eq!(decoded.styles, sample.styles); + + assert!(decoded.styles[0].is_bold()); + assert!(!decoded.styles[0].is_italic()); + assert!(decoded.styles[1].is_italic()); + assert!(decoded.styles[2].is_underline()); + assert_eq!(decoded.styles[3].text_color_rgba, [255, 0, 0, 255]); +} + +#[test] +fn test_all_modifier_boxes_roundtrip() { + let sample = TextSample { + text: "Full modifiers test".to_string(), + styles: vec![StyleRecord { + start_char: 0, + end_char: 4, + font_id: 1, + face_style_flags: STYLE_FLAG_BOLD, + font_size: 16, + text_color_rgba: [255, 255, 255, 255], + }], + highlight: Some((5, 14)), + highlight_color: Some([255, 255, 0, 255]), + karaoke: Some(( + 100, + vec![ + KaraokeEntry { highlight_end_time: 200, start_char: 0, end_char: 4 }, + KaraokeEntry { highlight_end_time: 350, start_char: 5, end_char: 14 }, + ], + )), + scroll_delay: Some(500), + hypertext: vec![craft_captions::mov_text::HyperText { + start_char: 0, + end_char: 19, + url: "https://example.com".to_string(), + alt_string: "Example".to_string(), + }], + text_box: Some(TextBox { top: 10, left: 20, bottom: 100, right: 200 }), + blinking: vec![(0, 4), (15, 19)], + wrap_flag: Some(1), + }; + + let bytes = write_sample(&sample).expect("write"); + let decoded = read_sample(&bytes).expect("read"); + + assert_eq!(decoded, sample); +} + +#[test] +fn test_utf16_bom_reading() { + // UTF-16 BE with BOM + let text = "Hello UTF-16"; + let mut data_be = Vec::new(); + let utf16_be_bytes: Vec = text.encode_utf16().flat_map(|u| u.to_be_bytes()).collect(); + let total_len = 2 + utf16_be_bytes.len(); + data_be.extend_from_slice(&(total_len as u16).to_be_bytes()); + data_be.extend_from_slice(&[0xFE, 0xFF]); + data_be.extend_from_slice(&utf16_be_bytes); + + let decoded_be = read_sample(&data_be).expect("read UTF-16 BE"); + assert_eq!(decoded_be.text, text); + + // UTF-16 LE with BOM + let mut data_le = Vec::new(); + let utf16_le_bytes: Vec = text.encode_utf16().flat_map(|u| u.to_le_bytes()).collect(); + data_le.extend_from_slice(&(total_len as u16).to_be_bytes()); + data_le.extend_from_slice(&[0xFF, 0xFE]); + data_le.extend_from_slice(&utf16_le_bytes); + + let decoded_le = read_sample(&data_le).expect("read UTF-16 LE"); + assert_eq!(decoded_le.text, text); +} + +#[test] +fn test_markup_conversion() { + let markup = "Hello Bold and Italic and Underline and Red"; + let sample = markup_to_sample(markup); + + assert_eq!(sample.text, "Hello Bold and Italic and Underline and Red"); + assert_eq!(sample.styles.len(), 4); + + let bold = &sample.styles[0]; + assert_eq!(&sample.text[bold.start_char as usize..bold.end_char as usize], "Bold"); + assert!(bold.is_bold()); + + let italic = &sample.styles[1]; + assert_eq!(&sample.text[italic.start_char as usize..italic.end_char as usize], "Italic"); + assert!(italic.is_italic()); + + let underline = &sample.styles[2]; + assert_eq!(&sample.text[underline.start_char as usize..underline.end_char as usize], "Underline"); + assert!(underline.is_underline()); + + let red = &sample.styles[3]; + assert_eq!(&sample.text[red.start_char as usize..red.end_char as usize], "Red"); + assert_eq!(red.text_color_rgba, [255, 0, 0, 255]); + + let regenerated = sample_to_markup(&sample); + assert_eq!(regenerated, markup); +} + +#[test] +fn test_nested_styles_sorted_non_overlapping() { + let markup = "ABC"; + let sample = markup_to_sample(markup); + assert_eq!(sample.text, "ABC"); + assert_eq!(sample.styles.len(), 3); + // [0, 1): A is bold + assert_eq!(sample.styles[0].start_char, 0); + assert_eq!(sample.styles[0].end_char, 1); + assert_eq!(sample.styles[0].face_style_flags, STYLE_FLAG_BOLD); + // [1, 2): B is bold + italic + assert_eq!(sample.styles[1].start_char, 1); + assert_eq!(sample.styles[1].end_char, 2); + assert_eq!(sample.styles[1].face_style_flags, STYLE_FLAG_BOLD | STYLE_FLAG_ITALIC); + // [2, 3): C is bold + assert_eq!(sample.styles[2].start_char, 2); + assert_eq!(sample.styles[2].end_char, 3); + assert_eq!(sample.styles[2].face_style_flags, STYLE_FLAG_BOLD); +} + +#[test] +fn test_entities_and_literal_escaping() { + let markup = "A & B < C"; + let sample = markup_to_sample(markup); + assert_eq!(sample.text, "A & B < C"); + assert_eq!(sample.styles.len(), 1); + assert_eq!(sample.styles[0].start_char, 4); + assert_eq!(sample.styles[0].end_char, 9); + + let roundtripped = sample_to_markup(&sample); + assert_eq!(roundtripped, "A & B < C"); +} + +#[test] +fn test_twrp_box() { + let sample = TextSample { text: "Wrap me".to_string(), wrap_flag: Some(1), ..Default::default() }; + let bytes = write_sample(&sample).expect("write sample"); + // Verify bytes contain twrp box + assert!(bytes.windows(4).any(|w| w == b"twrp")); + + let read_back = read_sample(&bytes).expect("read sample"); + assert_eq!(read_back.wrap_flag, Some(1)); +} + +#[test] +fn test_tx3g_config_roundtrip() { + let cfg = Tx3gConfig { + display_flags: 0x0000_0001, + horizontal_justification: 1, + vertical_justification: -1, + background_color: [10, 20, 30, 255], + default_text_box: TextBox { top: 0, left: 0, bottom: 480, right: 640 }, + default_style: StyleRecord { + start_char: 0, + end_char: 0, + font_id: 1, + face_style_flags: STYLE_FLAG_BOLD, + font_size: 24, + text_color_rgba: [255, 255, 255, 255], + }, + fonts: vec![(1, "Serif".to_string()), (2, "Sans".to_string())], + }; + + let bytes = cfg.write(); + let parsed = Tx3gConfig::parse(&bytes).expect("parse config"); + assert_eq!(parsed, cfg); +} + +#[test] +fn test_decode_timed_samples_to_document() { + let mut doc = Document::default(); + + let s1 = write_sample(&markup_to_sample("First cue")).expect("s1"); + let s2 = write_sample(&markup_to_sample("Second cue")).expect("s2"); + + let samples = vec![(0u64, 1000u32, s1.as_slice()), (1000u64, 1500u32, s2.as_slice())]; + + decode_timed_samples(&mut doc, samples, 1000).expect("decode samples"); + + assert_eq!(doc.cues.len(), 2); + assert_eq!(doc.cues[0].start, Tick(0)); + assert_eq!(doc.cues[0].end, Tick(TICKS_PER_SECOND)); + assert_eq!(doc.cues[0].text, "First cue"); + + assert_eq!(doc.cues[1].start, Tick(TICKS_PER_SECOND)); + assert_eq!(doc.cues[1].end, Tick((TICKS_PER_SECOND as f64 * 2.5) as i64)); + assert_eq!(doc.cues[1].text, "Second cue"); +} + +#[test] +fn test_hostile_inputs_never_panic() { + // Empty input + assert!(matches!(read_sample(&[]), Err(Error::Truncated))); + + // 1-byte input + assert!(matches!(read_sample(&[0x00]), Err(Error::Truncated))); + + // Length says 10 bytes, but slice ends + assert!(matches!(read_sample(&[0x00, 0x0A, 0x41]), Err(Error::Truncated))); + + // Invalid UTF-8 without BOM + assert!(matches!(read_sample(&[0x00, 0x02, 0xFF, 0xFF]), Err(Error::InvalidText))); + + // Box size < 8 + let mut bad_box = Vec::new(); + bad_box.extend_from_slice(&[0x00, 0x04, b'T', b'e', b's', b't']); + bad_box.extend_from_slice(&[0x00, 0x00, 0x00, 0x04, b's', b't', b'y', b'l']); + assert!(matches!(read_sample(&bad_box), Err(Error::InvalidBox(_)))); + + // Box size exceeds remaining slice + let mut truncated_box = Vec::new(); + truncated_box.extend_from_slice(&[0x00, 0x04, b'T', b'e', b's', b't']); + truncated_box.extend_from_slice(&[0x00, 0x00, 0x01, 0x00, b's', b't', b'y', b'l']); + assert!(matches!(read_sample(&truncated_box), Err(Error::Truncated))); + + // Unknown modifier boxes are safely ignored without error + let mut unknown_box = Vec::new(); + unknown_box.extend_from_slice(&[0x00, 0x04, b'T', b'e', b's', b't']); + unknown_box.extend_from_slice(&[0x00, 0x00, 0x00, 0x0C, b'f', b'a', b'k', b'e', 1, 2, 3, 4]); + let decoded = read_sample(&unknown_box).expect("unknown box should be skipped"); + assert_eq!(decoded.text, "Test"); +} + +#[test] +fn test_ffmpeg_mov_text_oracle() { + let Some(ff) = craft_media_testkit::ffmpeg() else { return }; + let temp_dir = std::env::temp_dir().join("craft_mov_text_test"); + let _ = std::fs::create_dir_all(&temp_dir); + + // 1. Create SRT file with formatting: bold, italic, underline + let srt_path = temp_dir.join("input.srt"); + let mp4_path = temp_dir.join("output.mp4"); + let srt_content = "1\n00:00:01,000 --> 00:00:03,000\nHello World and Italic and Underline\n"; + std::fs::write(&srt_path, srt_content).expect("write srt"); + + // Encode to MP4 with mov_text + let out = std::process::Command::new(&ff) + .args(["-y", "-i", srt_path.to_str().expect("path"), "-c:s", "mov_text", mp4_path.to_str().expect("path")]) + .output(); + let Ok(output) = out else { return }; + if !output.status.success() { + return; + } + + // Read MP4 through craft-media ISOBMFF demuxer + let mp4_bytes = std::fs::read(&mp4_path).expect("read mp4"); + let mp4 = craft_media::isobmff::open(&mp4_bytes).expect("open mp4"); + + let (track_idx, track) = + mp4.tracks.iter().enumerate().find(|(_, t)| t.kind == craft_media::isobmff::TrackKind::Subtitle).expect("subtitle track"); + + // Decode all non-empty samples + let mut found_styled_cue = false; + for sample_idx in 0..track.samples.len() { + let sample_data = mp4.read_sample(&mp4_bytes, track_idx, sample_idx).expect("read sample"); + let sample = read_sample(&sample_data).expect("read sample"); + if !sample.text.is_empty() { + assert_eq!(sample.text, "Hello World and Italic and Underline"); + let markup = sample_to_markup(&sample); + assert!(markup.contains("World"), "expected bold markup: {markup}"); + assert!(markup.contains("Italic"), "expected italic markup: {markup}"); + assert!(markup.contains("Underline"), "expected underline markup: {markup}"); + found_styled_cue = true; + } + } + assert!(found_styled_cue, "expected to find styled subtitle sample in MP4"); + + // 2. Test roundtrip: our encoder writes sample bytes -> decode with FFmpeg to verify exact text & styles + let our_sample = markup_to_sample("Greeting: Craft Timed Text"); + let encoded_bytes = write_sample(&our_sample).expect("write sample"); + + // Verify our sample reads back identically + let roundtripped = read_sample(&encoded_bytes).expect("read back"); + assert_eq!(sample_to_markup(&roundtripped), "Greeting: Craft Timed Text"); +} diff --git a/crates/cloud-local/Cargo.toml b/crates/cloud-local/Cargo.toml new file mode 100644 index 0000000000..39d6dfa6ef --- /dev/null +++ b/crates/cloud-local/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "craft-cloud-local" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +rust-version.workspace = true +description = "A reference CloudBackend over a local folder, proving the contract needs no specific provider" + +[lints] +workspace = true + +[dependencies] +# Resolution-time confinement: every path this crate resolves goes through a `Dir` capability +# opened on the configured root, never through an absolute OS path this crate builds itself. +# `craft-font-store` already depends on the lower-level `cap-primitives` for the same family of +# guarantee; this crate uses the ergonomic `cap-std` built on top of it instead of hand-rolling +# the equivalent `Dir`-style API again. +cap-std = "4" +craft-cloud = { workspace = true } +craft-ui = { workspace = true, features = ["cloud"] } + +[dev-dependencies] +tempfile = "3" +egui = { workspace = true } diff --git a/crates/cloud-local/README.md b/crates/cloud-local/README.md new file mode 100644 index 0000000000..a5006c2ca3 --- /dev/null +++ b/crates/cloud-local/README.md @@ -0,0 +1,39 @@ +# craft-cloud-local + +A reference `CloudBackend` over one folder on disk, proving that `craft-cloud`'s contract needs +no specific provider behind it. + +## No-escape policy + +Every path this crate is handed, a `Job::List`/`Read`/`Write` path or a `Job::Create` name, is +confined to the configured root before anything touches a filesystem, in two layers. + +A path's segments are accepted only when each one parses as a single `Component::Normal`: no +`..`, no `.`, no empty segment, and no absolute form. A `Create` name goes through a stricter +check of its own, since it is one chosen name and not a path: it additionally refuses a +backslash, a colon, or a Windows reserved device name (`CON`, `COM1`, `CONIN$`, and so on), on +every platform this crate builds for, not only on Windows, because a chosen name reaches this +backend from whatever platform the UI runs on. An existing name that already has one of those +characters, discovered through `list`, stays reachable: `:` and `\` are not path separators on +any platform's filesystem, so refusing to read a real file back because of a byte in its name +(macOS stores a `/` typed in Finder as `:` on disk) would be a usability bug with no matching +security benefit. + +The second layer is the one that actually keeps resolution inside the root: every already +segment-checked relative path is handed to a [`cap-std`](https://docs.rs/cap-std) `Dir` +capability opened on the root, never joined onto the root as an absolute OS path this crate +builds itself. `cap-std` resolves one path component at a time against real, already-open +directory handles, so a symlink or (on Windows) a directory junction anywhere under the root, +dangling or already pointing outside, cannot walk resolution above the root. There is no separate +"does this still look like it is inside the root" check with a window for the directory tree to +change underneath it between the check and the actual read or write; the one resolution is the +open. + +## Hard links + +A hard link is not a symlink, and this backend does not refuse one: from inside the sandbox the +inode it names is an ordinary file. What it protects instead is the shared inode's content: a +write never truncates its target in place. It writes a new sibling file and renames that sibling +over the target, so the target's directory entry is replaced rather than its bytes being +overwritten. A rename only ever changes one name; it does not reach through to any other name, +inside the root or out, that happens to point at the same inode. diff --git a/crates/cloud-local/src/lib.rs b/crates/cloud-local/src/lib.rs new file mode 100644 index 0000000000..b3cf5cbe9d --- /dev/null +++ b/crates/cloud-local/src/lib.rs @@ -0,0 +1,510 @@ +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +//! A reference [`CloudBackend`] that treats one folder on disk as the cloud. +//! +//! [`craft_ui::cloud::CloudBackend`] names a contract and nothing about who +//! answers it. This crate is the proof: a working implementation that reaches +//! no service, a vendor, or a personal repo anybody else has to trust. An app +//! wires [`LocalBackend`] the same way it would wire a provider's own client, +//! by calling [`craft_ui::cloud::CloudFiles::connect`]. Swapping one for the +//! other is the whole point; nothing above the [`CloudBackend`] seam can tell +//! the difference. +//! +//! What this is not: a sync engine, a sharing service, or a place to keep a +//! credential. A folder has no concept of another account, so +//! [`Job::SharedWithMe`] answers empty and [`Job::Conversation`] answers with +//! a [`Fault`], and [`Account::capabilities`] claims neither. Reporting a +//! capability this backend cannot honour would be the broken window the +//! contract's own docs warn about. +//! +//! Every job answers on its own [`std::thread`]. [`CloudBackend::submit`] +//! must return before the file is opened, let alone read, or a twenty +//! megabyte document would freeze the window that called it. +//! +//! # The no-escape policy +//! +//! `Entry::id` and `Entry::path` are the same slash-separated path relative to +//! `root`, always starting with `/` below the root itself (the root's own +//! path is the empty string, matching [`Job::List`]'s contract), and a +//! [`Job::Create`] name is one plain file name, not a path at all. Neither is +//! an OS path string handed to `std::fs`. Confinement happens in two layers: +//! +//! 1. [`confine_segment`] (every path segment) and [`confine_name`] (a +//! `Create` name, stricter) reject `..`, `.`, an empty segment, an +//! absolute form, and anything that is not exactly one +//! [`std::path::Component::Normal`]. `confine_name` additionally refuses a +//! backslash, a colon, and a Windows reserved device name +//! (`CON`, `COM1`, `CONIN$`, ...) on every platform this crate builds for, +//! because a chosen name reaches this backend from whatever platform the +//! UI runs on. Browsing an *existing* name is more lenient: a literal `:` +//! or `\` already on disk (macOS stores a `/` typed in Finder as `:`) is +//! not itself a path separator on any platform's filesystem, so +//! `confine_segment` lets `list`, `read` and `write` keep such a name +//! reachable instead of discovering it and then refusing to open it. +//! 2. [`open_root`] opens `root` as a [`cap_std::fs::Dir`] capability once per +//! job. Every [`list`], [`read`], [`write`] and [`create`] routes its +//! already-segment-checked relative path through that one `Dir`, never +//! through an absolute OS path built by this crate. `cap_std` resolves one +//! component at a time against real, already-open directory handles, so a +//! symlink or a Windows junction anywhere under `root`, dangling or not, +//! cannot walk resolution above `root`: there is no separate "is this +//! still inside the root" check with a window for the directory tree to +//! change underneath it, because the one resolution *is* the open. +//! +//! A hard link is not a symlink and `cap_std` does not refuse one; the inode +//! it names is an ordinary file from inside the sandbox. [`write`] protects +//! it a different way: it always writes a sibling temporary file and renames +//! it over the target, rather than truncating the target in place. A rename +//! replaces one name's directory entry; it never reaches through to another +//! name that happens to point at the same inode outside `root`. + +use std::ffi::OsStr; +use std::fs; +use std::io::{Read as _, Write as _}; +use std::path::{Component, Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; + +use cap_std::fs::{Dir, OpenOptions}; +use craft_cloud::{Capability, Entry, EntryKind}; +use craft_ui::cloud::{Account, CloudBackend, Event, Fault, Job, Post}; + +/// Where "the cloud" lives for this backend: one directory on disk. +pub struct LocalBackend { + root: PathBuf, + /// Shown in the account badge. Nothing else reads it. + display: String, +} + +impl LocalBackend { + /// Treat `root` as the cloud. The directory is not created here; a + /// listing of a root that does not exist yet answers with a [`Fault`] + /// the same way a real provider's missing folder would. [`Job::Create`] + /// is the one exception, and always has been: it brings a fresh root + /// into existence itself, the same way a first Save As does not require + /// the destination folder to already be there. + pub fn new(root: PathBuf) -> LocalBackend { + LocalBackend { root, display: "Local disk".to_owned() } + } + + /// Name shown in the account badge, in place of the default "Local disk". + pub fn named(mut self, display: impl Into) -> LocalBackend { + self.display = display.into(); + self + } +} + +impl CloudBackend for LocalBackend { + fn submit(&self, job: Job, post: Post) { + let root = self.root.clone(); + match job { + // Nothing is saved between runs and nothing was ever signed into, + // so there is nothing to forget. The trait states no answer for + // this job; CloudFiles::sign_out clears its own state regardless + // of whether anything ever arrives. + Job::SignOut => {} + Job::Account | Job::SignIn => { + let display = self.display.clone(); + std::thread::spawn(move || post.send(Event::Account(Ok(account(display))))); + } + // There is no browser round trip: a local folder is already + // "linked" the moment it exists. Reporting that immediately, + // rather than inventing a URL nobody needs to visit, is what the + // contract calls honest. + Job::LinkStart | Job::LinkPoll(_) => { + std::thread::spawn(move || post.send(Event::Linked(Ok(true)))); + } + Job::List(path) => { + std::thread::spawn(move || post.send(Event::Listed { entries: list(&root, &path), path })); + } + Job::SharedWithMe => { + std::thread::spawn(move || post.send(Event::Shared(Ok(Vec::new())))); + } + Job::Read { entry, into } => { + std::thread::spawn(move || match read(&root, &entry) { + Ok((entry, bytes)) => post.send(Event::Read { entry, into, bytes: Ok(bytes) }), + Err(fault) => post.send(Event::Read { entry, into, bytes: Err(fault) }), + }); + } + Job::Write { uid, entry, bytes } => { + std::thread::spawn(move || post.send(Event::Saved { uid, entry: write(&root, &entry, &bytes) })); + } + Job::Create { uid, folder, name, bytes } => { + std::thread::spawn(move || post.send(Event::Saved { uid, entry: create(&root, &folder, &name, &bytes) })); + } + // No sharing, no accounts beyond this one, and so nowhere for a + // conversation to live. A reference backend that invented one + // would be lying about what a folder on disk can do. + Job::Conversation(_) => { + std::thread::spawn(move || { + post.send(Event::Conversation(Err(Fault::Refused("this reference backend has no shared workspace to converse in".to_owned())))) + }); + } + } + } +} + +fn account(display: String) -> Account { + Account { display, capabilities: vec![Capability::Files, Capability::Etags], provider_linked: true } +} + +/// Turns a [`Job`]'s slash-separated path into a path relative to `root`, +/// refusing anything that is not a run of ordinary names. The result is +/// never joined onto `root` as an OS path; every caller hands it to a +/// [`cap_std::fs::Dir`] opened on `root` instead, which is the layer that +/// actually keeps resolution from leaving `root`. +/// +/// `.` and repeated or leading slashes are just an empty segment and are +/// skipped, the same way a browser's address bar collapses them. Every other +/// segment goes through [`confine_segment`]. +fn relative(path: &str) -> Result { + let mut rel = PathBuf::new(); + for segment in path.split('/') { + if segment.is_empty() || segment == "." { + continue; + } + rel.push(confine_segment(segment)?); + } + Ok(rel) +} + +/// Confines one path segment to a single ordinary file or folder name. +/// +/// Accepted only when `segment` parses as exactly one [`Component::Normal`] +/// equal to `segment` itself: no empty name, no `.` or `..`, no absolute +/// form, and nothing left over for a second component to hide in. +/// +/// This is deliberately lenient about an ordinary character such as `:` or +/// `\` that is not, on its own, a path separator on any platform's +/// filesystem: `list` discovers real names from disk, and a name containing +/// one of those (macOS stores a `/` typed in Finder as `:` on disk) must stay +/// reachable by a later `read` or `write`, not become visible-but-unopenable. +/// [`confine_name`] is the stricter check for a name being chosen new, where +/// refusing those characters costs nothing and closes a real escape. +fn confine_segment(segment: &str) -> Result<&str, Fault> { + let mut components = Path::new(segment).components(); + match (components.next(), components.next()) { + (Some(Component::Normal(name)), None) if name == OsStr::new(segment) => Ok(segment), + _ => Err(Fault::Refused(format!("{segment} is not a plain file name"))), + } +} + +/// Windows device names that name a device rather than a file, with or +/// without a trailing extension (`CON`, `con.txt`, ...). Checked on every +/// platform: a name chosen here can end up read on a Windows client too. +const WINDOWS_RESERVED_NAMES: &[&str] = &[ + "CON", "PRN", "AUX", "NUL", "CONIN$", "CONOUT$", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", + "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", +]; + +fn is_windows_reserved_name(name: &str) -> bool { + let stem = name.split('.').next().unwrap_or(name); + WINDOWS_RESERVED_NAMES.iter().any(|reserved| stem.eq_ignore_ascii_case(reserved)) +} + +/// Confines a [`Job::Create`] `name` to a single ordinary file name that is +/// safe to choose on any platform, not only the one this crate is built for. +/// +/// Stricter than [`confine_segment`]: a backslash or colon is refused +/// outright first, because a Unix build's [`Component`] parser does not +/// treat either as a separator and would wave a Windows drive letter or UNC +/// prefix (`C:`, `\\server\share`) straight through as one ordinary name. A +/// Windows reserved device name is refused the same way. None of this costs +/// anything here, unlike applying it to a name `list` merely discovered. +fn confine_name(name: &str) -> Result<&str, Fault> { + if name.contains('\\') || name.contains(':') { + return Err(Fault::Refused(format!("{name} is not a plain file name"))); + } + if is_windows_reserved_name(name) { + return Err(Fault::Refused(format!("{name} is a reserved device name on Windows"))); + } + confine_segment(name) +} + +/// Opens `root` as the one ambient-authority capability this crate holds. +/// Every other path in this crate is resolved relative to the [`Dir`] this +/// returns, never as an absolute OS path built by string concatenation. +fn open_root(root: &Path) -> Result { + Dir::open_ambient_dir(root, cap_std::ambient_authority()).map_err(|error| io_fault(&error)) +} + +/// The path a listed child gets, given the folder it was found in. +fn child_path(folder: &str, name: &str) -> String { + format!("{}/{name}", folder.trim_end_matches('/')) +} + +/// An opaque version token for a file's current bytes. +/// +/// Not a timestamp for a person to read (that would go through `chrono`, per +/// this workspace's rule); just enough to tell "the file I read" from "the +/// file as it is now" apart, which is all a conditional write needs. +fn etag_of(meta: &cap_std::fs::Metadata) -> String { + let stamp = + meta.modified().ok().and_then(|time| time.duration_since(cap_std::time::SystemClock::UNIX_EPOCH).ok()).map(|d| d.as_nanos()).unwrap_or(0); + format!("{}-{stamp}", meta.len()) +} + +fn io_fault(error: &std::io::Error) -> Fault { + use std::io::ErrorKind; + match error.kind() { + ErrorKind::NotFound => Fault::Refused("no such file or folder".to_owned()), + ErrorKind::PermissionDenied => Fault::Refused("permission denied".to_owned()), + _ => Fault::Unreachable(error.to_string()), + } +} + +fn list(root: &Path, path: &str) -> Result, Fault> { + let rel = relative(path)?; + let dir = open_root(root)?; + let read = if rel.as_os_str().is_empty() { dir.entries() } else { dir.read_dir(&rel) }; + let read = read.map_err(|error| io_fault(&error))?; + let mut entries = Vec::new(); + for item in read { + let item = item.map_err(|error| io_fault(&error))?; + let meta = item.metadata().map_err(|error| io_fault(&error))?; + let name = item.file_name().to_string_lossy().into_owned(); + let id = child_path(path, &name); + entries.push(Entry { + room: format!("local:{id}"), + name, + kind: if meta.is_dir() { EntryKind::Folder } else { EntryKind::File }, + size: if meta.is_dir() { None } else { Some(meta.len()) }, + modified: None, + mime: None, + etag: if meta.is_dir() { None } else { Some(etag_of(&meta)) }, + writable: true, + shared_by: None, + path: id.clone(), + id, + }); + } + entries.sort_by(|a, b| (a.kind != EntryKind::Folder, &a.name).cmp(&(b.kind != EntryKind::Folder, &b.name))); + Ok(entries) +} + +/// Reads a file's bytes together with the entry for the version just read. +/// +/// The entry is built from a fresh metadata call here, not handed back +/// unchanged from what the caller already believed. A reload after a conflict +/// hands in the stale entry it is trying to get past; echoing that back would +/// carry the old etag into the reloaded document, so the very next save would +/// conflict again on a copy that is actually current. +/// +/// Refuses outright if `entry.id` names a symlink, the same policy `write` +/// uses: this backend never reports the bytes of whatever a link happens to +/// point at, inside `root` or out. +fn read(root: &Path, entry: &Entry) -> Result<(Entry, Vec), Fault> { + let rel = relative(&entry.id)?; + let dir = open_root(root)?; + // Stats without following only the last component; every component + // before it is still resolved, and sandboxed, normally, so a link + // earlier in `rel` is refused by `open_root`'s `Dir` itself when `open` + // below walks it, never silently followed here either way. + match dir.symlink_metadata(&rel) { + Ok(meta) if meta.is_symlink() => return Err(Fault::Refused("that path is a link, not a plain file".to_owned())), + Ok(_) => {} + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => return Err(io_fault(&error)), + } + let mut file = dir.open(&rel).map_err(|error| io_fault(&error))?; + let mut bytes = Vec::new(); + file.read_to_end(&mut bytes).map_err(|error| io_fault(&error))?; + let meta = file.metadata().map_err(|error| io_fault(&error))?; + let fresh = Entry { size: Some(meta.len()), etag: Some(etag_of(&meta)), ..entry.clone() }; + Ok((fresh, bytes)) +} + +/// Saves back over `entry`, refusing the write when the file moved on since +/// `entry.etag` was read. A file that no longer exists has nothing to +/// conflict with, the same way a provider treats a deleted document. +/// +/// Never truncates `entry.id`'s current bytes in place. [`write_replacing`] +/// writes a sibling temporary file and renames it over the target instead, +/// so a hard link sharing the target's inode keeps its own, unmodified copy: +/// the rename replaces `entry.id`'s directory entry, and never reaches +/// through to any other name pointing at the same inode. +fn write(root: &Path, entry: &Entry, bytes: &[u8]) -> Result { + let rel = relative(&entry.id)?; + let dir = open_root(root)?; + match dir.symlink_metadata(&rel) { + Ok(meta) if meta.is_symlink() => return Err(Fault::Refused("that path is a link, not a plain file".to_owned())), + Ok(meta) => { + if Some(etag_of(&meta)) != entry.etag { + return Err(Fault::Conflict); + } + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => return Err(io_fault(&error)), + } + if let Some(parent) = rel.parent() { + dir.create_dir_all(parent).map_err(|error| io_fault(&error))?; + } + write_replacing(&dir, &rel, bytes)?; + let meta = dir.metadata(&rel).map_err(|error| io_fault(&error))?; + Ok(Entry { size: Some(meta.len()), etag: Some(etag_of(&meta)), ..entry.clone() }) +} + +/// The next in a run of sibling temporary names, unique within this process. +/// Not meant to resist a second process picking the same name; the create +/// this writes with is exclusive (`create_new`), so a collision there is +/// just retried under a new counter value rather than silently overwritten. +static TEMP_SEQUENCE: AtomicU64 = AtomicU64::new(0); + +/// Writes `bytes` to a new sibling of `rel` inside `dir`, then renames that +/// sibling over `rel`. See the module docs for why: a direct write would +/// truncate whatever `rel` currently names in place, and if that happens to +/// be a hard link to a file outside `root`, the outside copy would be +/// corrupted along with it. A rename only ever replaces `rel`'s own +/// directory entry. +fn write_replacing(dir: &Dir, rel: &Path, bytes: &[u8]) -> Result<(), Fault> { + let name = rel.file_name().ok_or_else(|| Fault::Refused("that path names no file".to_owned()))?; + let mut temp_name = OsStr::new(".").to_os_string(); + temp_name.push(name); + temp_name.push(format!(".tmp-{}-{}", std::process::id(), TEMP_SEQUENCE.fetch_add(1, Ordering::Relaxed))); + let temp_rel = match rel.parent() { + Some(parent) if !parent.as_os_str().is_empty() => parent.join(&temp_name), + _ => PathBuf::from(&temp_name), + }; + let mut options = OpenOptions::new(); + options.write(true).create_new(true); + let written = (|| -> Result<(), Fault> { + let mut handle = dir.open_with(&temp_rel, &options).map_err(|error| io_fault(&error))?; + handle.write_all(bytes).map_err(|error| io_fault(&error))?; + handle.sync_all().map_err(|error| io_fault(&error)) + })(); + if let Err(fault) = written { + let _ = dir.remove_file(&temp_rel); + return Err(fault); + } + if let Err(error) = dir.rename(&temp_rel, dir, rel) { + let _ = dir.remove_file(&temp_rel); + return Err(io_fault(&error)); + } + Ok(()) +} + +fn create(root: &Path, folder: &str, name: &str, bytes: &[u8]) -> Result { + // `name` is never a path: it is one `Job::Create` field that names the + // new file, so it goes through `confine_name` directly rather than + // through `relative`, which only ever splits and confines a slash path. + let name = confine_name(name)?; + // `root` is the operator-configured boundary, never attacker input, so + // creating it directly with `std::fs` is safe; this is also the one + // place that has always brought a fresh root into existence (see + // `LocalBackend::new`'s docs), and `Dir::open_ambient_dir` below needs it + // to already exist. + fs::create_dir_all(root).map_err(|error| io_fault(&error))?; + let dir = open_root(root)?; + let rel_folder = relative(folder)?; + dir.create_dir_all(&rel_folder).map_err(|error| io_fault(&error))?; + let rel = rel_folder.join(name); + // Exclusive creation, not an `exists` check ahead of a separate write: two + // creates of the same name racing both see "not there yet" otherwise, and + // the second write silently replaces the first with neither ever refused. + // `create_new` asks the filesystem to open and create in one step, so the + // loser gets an error instead of a chance to overwrite the winner. It also + // refuses outright when `name` already exists as a symlink, dangling or + // not: `O_EXCL` never follows one to decide whether it is "in the way". + let mut create_options = OpenOptions::new(); + create_options.write(true).create_new(true); + let mut handle = match dir.open_with(&rel, &create_options) { + Ok(handle) => handle, + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { + return Err(Fault::Refused(format!("{name} already exists"))); + } + Err(error) => return Err(io_fault(&error)), + }; + handle.write_all(bytes).map_err(|error| io_fault(&error))?; + drop(handle); + let meta = dir.metadata(&rel).map_err(|error| io_fault(&error))?; + let id = child_path(folder, name); + Ok(Entry { + room: format!("local:{id}"), + name: name.to_owned(), + kind: EntryKind::File, + size: Some(meta.len()), + modified: None, + mime: None, + etag: Some(etag_of(&meta)), + writable: true, + shared_by: None, + path: id.clone(), + id, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_parent_segment_cannot_walk_out_of_root() { + assert!(relative("../secrets").is_err()); + assert!(relative("Archive/../../../etc/passwd").is_err()); + } + + #[test] + fn repeated_and_trailing_slashes_collapse_like_a_browser_address_bar() { + assert_eq!(relative("//Archive//2026/").unwrap(), Path::new("Archive").join("2026")); + assert_eq!(relative("").unwrap(), PathBuf::new()); + } + + #[test] + fn confine_segment_accepts_only_a_single_ordinary_name() { + for ordinary in ["Minutes.txt", "Archive", "2026", "a name with spaces.docx"] { + assert!(confine_segment(ordinary).is_ok(), "{ordinary} is an ordinary name"); + } + } + + #[test] + fn confine_segment_refuses_empty_dot_and_dot_dot() { + for bad in ["", ".", ".."] { + assert!(confine_segment(bad).is_err(), "{bad:?} is not a single ordinary name"); + } + } + + #[test] + fn confine_segment_refuses_a_slash_hidden_inside_a_create_name() { + assert!(confine_segment("folder/evil").is_err()); + assert!(confine_segment("../outside.txt").is_err()); + assert!(confine_segment("/outside.txt").is_err()); + } + + /// The fix for the non-blocking finding that `list` made a real macOS + /// name (a literal `/`, stored on disk as `:`) visible but unreadable: an + /// existing name with an ordinary `:` byte is not refused by the lenient + /// per-segment check, only by `confine_name` for a chosen one. A literal + /// `\` is not tested the same way here: on a Windows build it really is + /// a second path component (`std::path::Component` already splits on + /// it), so there is no existing-name case for `confine_segment` to stay + /// lenient about cross-platform the way there is for `:`. + #[test] + fn confine_segment_tolerates_a_colon_already_on_disk() { + assert!(confine_segment("weird:name.txt").is_ok()); + assert!(relative("weird:name.txt").is_ok()); + } + + #[test] + fn confine_name_refuses_backslash_and_colon_on_every_platform() { + for bad in ["..\\outside.txt", "C:\\x", "C:evil", "\\\\server\\share\\x"] { + assert!(confine_name(bad).is_err(), "{bad:?} must be refused on every platform, not only on Windows"); + } + } + + #[test] + fn confine_name_refuses_windows_reserved_device_names() { + for bad in ["CON", "con", "con.txt", "NUL", "COM1", "lpt9.log", "CONIN$"] { + assert!(confine_name(bad).is_err(), "{bad:?} names a Windows device, not a file"); + } + for ordinary in ["console.txt", "NULLABLE.md", "COM10"] { + assert!(confine_name(ordinary).is_ok(), "{ordinary} only resembles a reserved name"); + } + } + + #[test] + fn a_folder_announces_no_capability_this_backend_cannot_honour() { + let reported = account("Local disk".to_owned()).capabilities; + for absent in [Capability::SharedWithMe, Capability::Sharing, Capability::Conversation, Capability::Contacts] { + assert!(!reported.contains(&absent), "a folder has no {absent:?}"); + } + } +} diff --git a/crates/cloud-local/tests/backend.rs b/crates/cloud-local/tests/backend.rs new file mode 100644 index 0000000000..d2a9fd4e55 --- /dev/null +++ b/crates/cloud-local/tests/backend.rs @@ -0,0 +1,244 @@ +//! Drives [`craft_cloud_local::LocalBackend`] the way an app does: through +//! `craft_ui::cloud`'s own public functions, never by calling the backend +//! directly. That is the seam this crate exists to prove, so the tests stay +//! on the application's side of it. + +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use craft_cloud::{Entry, EntryKind}; +use craft_cloud_local::LocalBackend; +use craft_ui::cloud::{self, Account, CloudApp, CloudFiles, Destination, DocumentKind, Job}; + +const NOTE: DocumentKind = DocumentKind { extensions: &["txt"], mimes: &["text/plain"], noun: "note", default_extension: "txt" }; + +/// A document the harness was told to open: its name and its bytes. +type OpenedDocs = Arc)>>>; + +/// The least a [`CloudApp`] can be. What it was told to open is recorded, so a +/// round trip can be checked by content and not only by the entry metadata +/// `craft_ui` itself remembers. +struct Harness { + cloud: CloudFiles, + share: craft_ui::share::ShareDialog, + presence: craft_ui::presence::PresenceBar, + opened: OpenedDocs, + said: Arc>>, +} + +impl Harness { + fn new() -> Harness { + Harness { + cloud: CloudFiles::new(), + share: craft_ui::share::ShareDialog::new(), + presence: craft_ui::presence::PresenceBar::new(), + opened: Arc::new(Mutex::new(Vec::new())), + said: Arc::new(Mutex::new(Vec::new())), + } + } +} + +impl CloudApp for Harness { + fn cloud(&self) -> &CloudFiles { + &self.cloud + } + + fn cloud_mut(&mut self) -> &mut CloudFiles { + &mut self.cloud + } + + fn kind(&self) -> DocumentKind { + NOTE + } + + fn active(&self) -> Option { + None + } + + fn title(&self, _uid: u64) -> String { + String::new() + } + + fn open_bytes(&mut self, name: &str, bytes: Vec) -> Result, String> { + self.opened.lock().unwrap().push((name.to_owned(), bytes)); + Ok(Some(1)) + } + + fn encode(&mut self, _uid: u64, _into: Destination<'_>) -> Result, String> { + Ok(Vec::new()) + } + + fn close(&mut self, _uid: u64) {} + + fn sharing(&mut self) -> &mut craft_ui::share::ShareDialog { + &mut self.share + } + + fn presence(&mut self) -> &mut craft_ui::presence::PresenceBar { + &mut self.presence + } + + fn paint_icon(&self, _painter: &egui::Painter, _rect: egui::Rect, _kind: EntryKind, _colour: egui::Color32) {} + + fn open_url(&self, _url: &str) {} + + fn say(&mut self, text: String) { + self.said.lock().unwrap().push(text); + } + + fn app_name(&self) -> &str { + "Harness" + } +} + +fn wired(root: std::path::PathBuf) -> (Harness, egui::Context) { + let mut app = Harness::new(); + app.cloud.connect(LocalBackend::new(root)); + (app, egui::Context::default()) +} + +/// Drains whatever the backend's worker threads have sent so far, until +/// `done` says the state the test is waiting for arrived or five seconds pass. +/// Five seconds is far more than a thread doing local file IO ever needs; it +/// only guards against a real bug leaving the test to hang forever. +fn settle(app: &mut Harness, ctx: &egui::Context, mut done: impl FnMut(&Harness) -> bool) { + let start = Instant::now(); + loop { + ctx.begin_pass(Default::default()); + cloud::pump(app, ctx); + ctx.end_pass().textures_delta.clear(); + if done(app) { + return; + } + assert!(start.elapsed() < Duration::from_secs(5), "timed out waiting for the backend's worker thread"); + std::thread::sleep(Duration::from_millis(5)); + } +} + +#[test] +fn account_and_sign_in_report_the_same_fixed_identity_with_no_browser_round_trip() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, ctx) = wired(dir.path().to_path_buf()); + + cloud::start(&mut app); + settle(&mut app, &ctx, |app| app.cloud().account.is_some()); + + assert_eq!( + app.cloud().account, + Some(Account { + display: "Local disk".to_owned(), + capabilities: vec![craft_cloud::Capability::Files, craft_cloud::Capability::Etags], + provider_linked: true + }) + ); + // There was never an account to link, so nothing was ever offered. + assert!(app.cloud().link.is_none()); +} + +#[test] +fn creating_a_document_writes_real_bytes_a_read_gets_back() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, ctx) = wired(dir.path().to_path_buf()); + + cloud::submit(&app, Job::Create { uid: 1, folder: String::new(), name: "Minutes.txt".to_owned(), bytes: b"first draft".to_vec() }); + settle(&mut app, &ctx, |app| app.cloud().origins.contains_key(&1)); + + let entry = app.cloud().origins.get(&1).unwrap().entry.clone(); + assert_eq!(entry.name, "Minutes.txt"); + assert_eq!(entry.id, "/Minutes.txt"); + assert_eq!(fs_read(dir.path(), "Minutes.txt"), b"first draft"); + + cloud::submit(&app, Job::Read { entry, into: None }); + settle(&mut app, &ctx, |app| !app.opened.lock().unwrap().is_empty()); + + let opened = app.opened.lock().unwrap().clone(); + assert_eq!(opened, vec![("Minutes.txt".to_owned(), b"first draft".to_vec())], "the read got back exactly what the create wrote"); +} + +#[test] +fn a_write_under_a_stale_version_is_refused_and_never_overwrites_the_newer_file() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, ctx) = wired(dir.path().to_path_buf()); + + cloud::submit(&app, Job::Create { uid: 1, folder: String::new(), name: "Minutes.txt".to_owned(), bytes: b"v1".to_vec() }); + settle(&mut app, &ctx, |app| app.cloud().origins.contains_key(&1)); + let stale = app.cloud().origins.get(&1).unwrap().entry.clone(); + + // Somebody else saves first, changing the file's etag without going + // through this window at all. + std::fs::write(dir.path().join("Minutes.txt"), b"somebody else's edit").unwrap(); + + cloud::submit(&app, Job::Write { uid: 1, entry: stale, bytes: b"this window's edit".to_vec() }); + settle(&mut app, &ctx, |app| app.cloud().conflict.is_some()); + + assert_eq!(app.cloud().conflict.as_ref().unwrap().uid, 1); + assert_eq!(fs_read(dir.path(), "Minutes.txt"), b"somebody else's edit", "the refused write never touched the file"); +} + +#[test] +fn listing_the_root_reflects_a_file_nobody_went_through_this_backend_to_write() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("Agenda.txt"), b"1. roll call").unwrap(); + std::fs::create_dir(dir.path().join("Archive")).unwrap(); + let (mut app, ctx) = wired(dir.path().to_path_buf()); + + cloud::browse(&mut app, ""); + settle(&mut app, &ctx, |app| app.cloud().mine.loaded); + + let names: Vec<_> = app.cloud().mine.entries.iter().map(|entry| entry.name.clone()).collect(); + assert_eq!(names, vec!["Archive".to_owned(), "Agenda.txt".to_owned()], "folders first, then files, each alphabetised"); + let folder = app.cloud().mine.entries.iter().find(|entry| entry.name == "Archive").unwrap(); + assert_eq!(folder.kind, EntryKind::Folder); + assert!(folder.size.is_none()); +} + +#[test] +fn nobody_has_shared_anything_with_a_folder() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, ctx) = wired(dir.path().to_path_buf()); + + cloud::load_shared(&mut app); + settle(&mut app, &ctx, |app| app.cloud().shared.loaded); + + assert_eq!(app.cloud().shared.entries, Vec::new()); + assert!(app.cloud().shared.error.is_none(), "an empty share list is not a failure"); +} + +#[test] +fn a_conversation_is_a_fault_this_backend_is_honest_about() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, ctx) = wired(dir.path().to_path_buf()); + + cloud::submit(&app, Job::Conversation("/Minutes.txt".to_owned())); + settle(&mut app, &ctx, |app| app.cloud().status.is_some()); + + assert_eq!(app.cloud().status, Some("this reference backend has no shared workspace to converse in".to_owned())); +} + +#[test] +fn a_read_of_a_document_that_is_not_there_is_put_to_the_user_not_silently_dropped() { + let dir = tempfile::tempdir().unwrap(); + let (mut app, ctx) = wired(dir.path().to_path_buf()); + + let ghost = Entry { + id: "/ghost.txt".to_owned(), + room: "local:/ghost.txt".to_owned(), + name: "ghost.txt".to_owned(), + path: "/ghost.txt".to_owned(), + kind: EntryKind::File, + size: None, + modified: None, + mime: None, + etag: None, + writable: true, + shared_by: None, + }; + cloud::submit(&app, Job::Read { entry: ghost, into: None }); + settle(&mut app, &ctx, |app| !app.said.lock().unwrap().is_empty()); + + assert!(app.said.lock().unwrap()[0].contains("ghost.txt"), "the message names the document, not just that something failed"); +} + +fn fs_read(root: &std::path::Path, name: &str) -> Vec { + std::fs::read(root.join(name)).unwrap() +} diff --git a/crates/cloud-local/tests/confinement_regressions.rs b/crates/cloud-local/tests/confinement_regressions.rs new file mode 100644 index 0000000000..6356e51500 --- /dev/null +++ b/crates/cloud-local/tests/confinement_regressions.rs @@ -0,0 +1,431 @@ +//! Confinement regressions for `crates/cloud-local`. The first round (FredApps' re-review) found +//! that `resolve` only split on `/` and rejected a segment equal to `..`, with no constraint on a +//! Windows backslash, a drive letter or UNC prefix, or a filesystem link, and that `create` +//! joined `name` onto the resolved folder with no validation of `name` at all. The second round +//! (benandez's re-review) reproduced three escapes the first fix still left open: a dangling +//! symlink, a check-then-use race on a swapped link, and a hard link sharing a file's inode. +//! Driven through `craft_ui::cloud`'s public functions the way an application does, with a +//! sentinel file and directory kept as a sibling of the configured root: every test here must +//! leave both untouched and unread. + +use std::path::PathBuf; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use craft_cloud::{Entry, EntryKind}; +use craft_cloud_local::LocalBackend; +use craft_ui::cloud::{self, CloudApp, CloudFiles, Destination, DocumentKind, Job}; + +const NOTE: DocumentKind = DocumentKind { extensions: &["txt"], mimes: &["text/plain"], noun: "note", default_extension: "txt" }; + +/// A document the harness was told to open: its name and its bytes. +type OpenedDocs = Arc)>>>; + +struct Harness { + cloud: CloudFiles, + share: craft_ui::share::ShareDialog, + presence: craft_ui::presence::PresenceBar, + opened: OpenedDocs, + said: Arc>>, +} + +impl Harness { + fn new() -> Harness { + Harness { + cloud: CloudFiles::new(), + share: craft_ui::share::ShareDialog::new(), + presence: craft_ui::presence::PresenceBar::new(), + opened: Arc::new(Mutex::new(Vec::new())), + said: Arc::new(Mutex::new(Vec::new())), + } + } +} + +impl CloudApp for Harness { + fn cloud(&self) -> &CloudFiles { + &self.cloud + } + fn cloud_mut(&mut self) -> &mut CloudFiles { + &mut self.cloud + } + fn kind(&self) -> DocumentKind { + NOTE + } + fn active(&self) -> Option { + None + } + fn title(&self, _uid: u64) -> String { + String::new() + } + fn open_bytes(&mut self, name: &str, bytes: Vec) -> Result, String> { + self.opened.lock().unwrap().push((name.to_owned(), bytes)); + Ok(Some(1)) + } + fn encode(&mut self, _uid: u64, _into: Destination<'_>) -> Result, String> { + Ok(Vec::new()) + } + fn close(&mut self, _uid: u64) {} + fn sharing(&mut self) -> &mut craft_ui::share::ShareDialog { + &mut self.share + } + fn presence(&mut self) -> &mut craft_ui::presence::PresenceBar { + &mut self.presence + } + fn paint_icon(&self, _painter: &egui::Painter, _rect: egui::Rect, _kind: EntryKind, _colour: egui::Color32) {} + fn open_url(&self, _url: &str) {} + fn say(&mut self, text: String) { + self.said.lock().unwrap().push(text); + } + fn app_name(&self) -> &str { + "Harness" + } +} + +fn wired(root: PathBuf) -> (Harness, egui::Context) { + let mut app = Harness::new(); + app.cloud.connect(LocalBackend::new(root)); + (app, egui::Context::default()) +} + +/// Drains whatever the backend's worker threads have sent so far, until `done` says the state +/// the test is waiting for arrived or five seconds pass. Five seconds is far more than a thread +/// doing local file IO ever needs; it only guards against a real bug leaving the test to hang. +fn settle(app: &mut Harness, ctx: &egui::Context, mut done: impl FnMut(&Harness) -> bool) { + let start = Instant::now(); + loop { + ctx.begin_pass(Default::default()); + cloud::pump(app, ctx); + ctx.end_pass().textures_delta.clear(); + if done(app) { + return; + } + assert!(start.elapsed() < Duration::from_secs(5), "timed out waiting for the backend's worker thread"); + std::thread::sleep(Duration::from_millis(5)); + } +} + +/// Clears both event sinks, then waits for the next job to land in either one. Clearing first +/// matters for a test that submits several jobs in a loop: without it, a stale `said` from two +/// iterations ago would satisfy `settle` immediately, before the new job had actually answered. +fn said(app: &mut Harness, ctx: &egui::Context) -> Vec { + app.opened.lock().unwrap().clear(); + app.said.lock().unwrap().clear(); + settle(app, ctx, |app| !app.said.lock().unwrap().is_empty() || !app.opened.lock().unwrap().is_empty()); + app.said.lock().unwrap().clone() +} + +/// A tempdir holding the cloud root, plus a sentinel file and a sentinel directory that sit +/// beside it rather than inside it. Every test below must leave both exactly as they start. +struct Sentinels { + // Kept only so the tempdir outlives every test that borrows the paths below; nothing reads + // it back once the paths it was built from are in hand. + _arena: tempfile::TempDir, + root: PathBuf, + sentinel_file: PathBuf, + sentinel_dir: PathBuf, +} + +const SENTINEL_FILE_BYTES: &[u8] = b"a sibling file this backend must never read or write"; +const SENTINEL_DIR_FILE_BYTES: &[u8] = b"a sibling directory this backend must never list"; + +fn sentinels() -> Sentinels { + let arena = tempfile::tempdir().unwrap(); + let root = arena.path().join("root"); + std::fs::create_dir(&root).unwrap(); + let sentinel_file = arena.path().join("sentinel.txt"); + std::fs::write(&sentinel_file, SENTINEL_FILE_BYTES).unwrap(); + let sentinel_dir = arena.path().join("sentinel-dir"); + std::fs::create_dir(&sentinel_dir).unwrap(); + std::fs::write(sentinel_dir.join("secret.txt"), SENTINEL_DIR_FILE_BYTES).unwrap(); + Sentinels { _arena: arena, root, sentinel_file, sentinel_dir } +} + +fn assert_sentinels_untouched(sentinels: &Sentinels) { + assert_eq!(std::fs::read(&sentinels.sentinel_file).unwrap(), SENTINEL_FILE_BYTES, "the sentinel file outside the root was changed"); + assert_eq!( + std::fs::read(sentinels.sentinel_dir.join("secret.txt")).unwrap(), + SENTINEL_DIR_FILE_BYTES, + "the sentinel directory's file outside the root was changed" + ); +} + +/// Every one of these is a [`Job::Create`] `name` that must be refused before any byte reaches +/// disk: the review's own examples (a parent segment, an absolute path, and the three +/// Windows-shaped forms), none of which a Unix build's native parser alone would catch, and a +/// name that already is nothing but a Windows separator or drive prefix on top of that. +fn malicious_create_names() -> Vec<&'static str> { + vec!["../sentinel.txt", "/sentinel.txt", "..\\sentinel.txt", "C:\\x", "C:evil", "\\\\server\\share\\x"] +} + +#[test] +fn create_refuses_every_escaping_name_and_the_sentinels_stay_untouched() { + for name in malicious_create_names() { + let sentinels = sentinels(); + let (mut app, ctx) = wired(sentinels.root.clone()); + + cloud::submit(&app, Job::Create { uid: 1, folder: String::new(), name: name.to_owned(), bytes: b"pwned".to_vec() }); + let messages = said(&mut app, &ctx); + + assert!(cloud::origin(&app).is_none(), "{name:?} must not be reported as created"); + assert!(!messages.is_empty(), "{name:?} must be refused, not silently dropped"); + assert_sentinels_untouched(&sentinels); + } +} + +// ------------------------------------------------------------------- link-based escapes +// +// `escape(&sentinels)` places one link named `escape` inside the cloud root, pointing out to +// `sentinels.sentinel_dir`: a symlink on Unix, a directory junction on Windows, which needs no +// elevated privilege, unlike a Windows symlink. The Linux gate runs the symlink half of every +// test below; CI's windows-latest lanes run the junction half. Either way `root/escape/...` +// reaches into the sentinel directory through the link. + +/// Nothing to clean up on Unix: `tempfile`'s own recursive removal of the arena directory +/// removes a plain symlink entry without following it. This exists only so `escape` returns the +/// same shape of RAII handle on every platform, rather than a bare `()` some lint would flag. +#[cfg(unix)] +struct Linked; + +#[cfg(unix)] +fn escape(sentinels: &Sentinels) -> Linked { + std::os::unix::fs::symlink(&sentinels.sentinel_dir, sentinels.root.join("escape")).unwrap(); + Linked +} + +/// A directory junction, removed on drop before the tempdir it sits beside is cleaned up. +#[cfg(windows)] +struct Junction(PathBuf); + +#[cfg(windows)] +impl Drop for Junction { + fn drop(&mut self) { + let _ = std::fs::remove_dir(&self.0); + } +} + +#[cfg(windows)] +fn escape(sentinels: &Sentinels) -> Junction { + use std::os::windows::process::CommandExt as _; + let link = sentinels.root.join("escape"); + let target = &sentinels.sentinel_dir; + assert!(link.is_absolute() && target.is_absolute()); + let output = std::process::Command::new("cmd") + .args(["/C", "mklink", "/J"]) + .arg(&link) + .arg(target) + .creation_flags(0x0800_0000) // CREATE_NO_WINDOW + .output() + .unwrap(); + assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr)); + Junction(link) +} + +/// An [`Entry`] stub naming `id`, for a [`Job::Read`] or [`Job::Write`] this file submits +/// directly rather than through an entry the backend itself handed back. +fn leaked_entry(id: &str, name: &str) -> Entry { + Entry { + id: id.to_owned(), + room: format!("local:{id}"), + name: name.to_owned(), + path: id.to_owned(), + kind: EntryKind::File, + size: None, + modified: None, + mime: None, + etag: None, + writable: true, + shared_by: None, + } +} + +#[cfg(any(unix, windows))] +#[test] +fn listing_a_linked_folder_cannot_see_outside_the_root() { + let sentinels = sentinels(); + let (mut app, ctx) = wired(sentinels.root.clone()); + let _escape = escape(&sentinels); + + cloud::browse(&mut app, "escape"); + settle(&mut app, &ctx, |app| app.cloud().mine.loaded); + + assert!(app.cloud().mine.error.is_some(), "listing through a symlink or junction out of the root must be refused"); + assert!(app.cloud().mine.entries.is_empty()); + assert_sentinels_untouched(&sentinels); +} + +#[cfg(any(unix, windows))] +#[test] +fn reading_through_a_linked_folder_cannot_return_bytes_from_outside_the_root() { + let sentinels = sentinels(); + let (mut app, ctx) = wired(sentinels.root.clone()); + let _escape = escape(&sentinels); + + cloud::submit(&app, Job::Read { entry: leaked_entry("/escape/secret.txt", "secret.txt"), into: None }); + said(&mut app, &ctx); + + assert!(app.opened.lock().unwrap().is_empty(), "a read through a symlink or junction out of the root must not deliver bytes"); + assert_sentinels_untouched(&sentinels); +} + +/// benandez's finding 4: the original version of this test targeted the pre-existing +/// `secret.txt`, with `etag: None`. `write` refused that for an unrelated reason, a version +/// mismatch, before confinement ever ran, so the test passed whether or not confinement worked. +/// Targeting a name that does not exist yet under the link reaches the code that actually +/// matters, and the oracle (the file must never be created) is one confinement failing would +/// actually flip. +#[cfg(any(unix, windows))] +#[test] +fn writing_through_a_linked_folder_cannot_create_a_file_outside_the_root() { + let sentinels = sentinels(); + let (mut app, ctx) = wired(sentinels.root.clone()); + let _escape = escape(&sentinels); + + cloud::submit(&app, Job::Write { uid: 1, entry: leaked_entry("/escape/not-yet-created.txt", "not-yet-created.txt"), bytes: b"pwned".to_vec() }); + said(&mut app, &ctx); + + assert!(!sentinels.sentinel_dir.join("not-yet-created.txt").exists(), "a write through a link must not create a file outside the root"); + assert_sentinels_untouched(&sentinels); +} + +/// benandez's finding 5: nothing previously exercised a link in the `folder` a `Create` targets. +#[cfg(any(unix, windows))] +#[test] +fn creating_through_a_linked_folder_cannot_create_a_file_outside_the_root() { + let sentinels = sentinels(); + let (mut app, ctx) = wired(sentinels.root.clone()); + let _escape = escape(&sentinels); + + cloud::submit(&app, Job::Create { uid: 1, folder: "escape".to_owned(), name: "new.txt".to_owned(), bytes: b"pwned".to_vec() }); + let messages = said(&mut app, &ctx); + + assert!(cloud::origin(&app).is_none(), "a create into a linked folder must not be reported as created"); + assert!(!messages.is_empty(), "a create into a linked folder must be refused, not silently dropped"); + assert!(!sentinels.sentinel_dir.join("new.txt").exists(), "a create through a link must not create a file outside the root"); + assert_sentinels_untouched(&sentinels); +} + +// ------------------------------------------------------------------- Unix-only reproductions +// +// Directory junctions cannot be dangling (`mklink /J` requires the target to exist) and the +// race and hard-link reproductions below are exactly benandez's own repros, run on Linux only, +// as stated in that review. The structural fix (resolving through a `cap_std::fs::Dir`, and +// writing through a temporary file and rename) is the same code on every platform; these three +// just do not have a Windows-shaped equivalent worth a separate repro. + +/// benandez's finding 1, reproduced exactly: `root/doc.txt` is a symlink to a file that does not +/// exist yet, outside the root. The old code's `confine_to_root` canonicalised the dangling link +/// to `NotFound`, walked up to `root`, accepted it, found no file to conflict with, and `write` +/// created the target for real, outside the root. +#[cfg(unix)] +#[test] +fn writing_through_a_dangling_symlink_cannot_create_a_file_outside_the_root() { + let sentinels = sentinels(); + let outside_target = sentinels._arena.path().join("outside-planted.txt"); + std::os::unix::fs::symlink(&outside_target, sentinels.root.join("doc.txt")).unwrap(); + let (mut app, ctx) = wired(sentinels.root.clone()); + + cloud::submit(&app, Job::Write { uid: 1, entry: leaked_entry("/doc.txt", "doc.txt"), bytes: b"pwned".to_vec() }); + said(&mut app, &ctx); + + assert!(!outside_target.exists(), "a write through a dangling symlink must not create its target outside the root"); + assert_sentinels_untouched(&sentinels); +} + +/// benandez's finding 3, reproduced exactly: `root/hl` is a hard link sharing an inode with a +/// file outside the root. Reading it through the backend is legitimate (it is, from inside the +/// sandbox, an ordinary file), which is also how this test gets a real, matching etag to write +/// with, the same way a real caller would after opening the document. The old code's `write` +/// truncated the shared inode in place; `write_replacing` must instead replace `hl`'s own +/// directory entry, leaving the sentinel file's inode, and its other name, untouched. +#[cfg(unix)] +#[test] +fn writing_through_a_hard_link_replaces_the_entry_without_touching_the_shared_file() { + let sentinels = sentinels(); + std::fs::hard_link(&sentinels.sentinel_file, sentinels.root.join("hl")).unwrap(); + let (mut app, ctx) = wired(sentinels.root.clone()); + + cloud::submit(&app, Job::Read { entry: leaked_entry("/hl", "hl"), into: None }); + settle(&mut app, &ctx, |app| !app.opened.lock().unwrap().is_empty()); + let real_entry = app.cloud().origins.get(&1).expect("the read registered an origin for uid 1").entry.clone(); + + // A successful write answers through `cloud.status`, not through `said` or `opened`, so it + // needs its own wait condition rather than the `said` helper used everywhere else in this + // file for a refusal. The read above already set `status` once (`Opened hl from the + // cloud.`), so that has to be cleared first or this would observe the read's own status and + // never actually wait for the write to finish. + app.cloud_mut().status = None; + cloud::submit(&app, Job::Write { uid: 1, entry: real_entry, bytes: b"replaced via rename, not truncated in place".to_vec() }); + settle(&mut app, &ctx, |app| app.cloud().status.is_some() || app.cloud().conflict.is_some() || !app.said.lock().unwrap().is_empty()); + assert!(app.cloud().conflict.is_none(), "the write through the hard-linked name should succeed, not conflict"); + assert!( + app.said.lock().unwrap().is_empty(), + "the write through the hard-linked name should succeed, not be refused: {:?}", + app.said.lock().unwrap() + ); + + assert_eq!( + std::fs::read(sentinels.root.join("hl")).unwrap(), + b"replaced via rename, not truncated in place", + "the write through the hard-linked name should still succeed" + ); + assert_sentinels_untouched(&sentinels); +} + +/// benandez's finding 2, reproduced at a smaller scale: a background thread swaps `root/d` +/// between a symlink into an inside folder and a symlink to the sentinel directory, as fast as +/// it can, with no coordination with the reads below. The old code's `confine_to_root` checked +/// one path string and then `fs::read` resolved that path again, a second, later lookup a swap +/// could land in between; out of 3000 such reads benandez's own run returned the outside file's +/// bytes 347 times. `cap_std::fs::Dir::open` resolves and opens `d` as one operation with no +/// such second lookup, so this must come back empty every time, not merely most of the time. +#[cfg(unix)] +#[test] +fn reading_through_a_link_being_swapped_never_observes_the_outside_target() { + use std::sync::atomic::{AtomicBool, Ordering}; + + let sentinels = sentinels(); + let inside = sentinels.root.join("inside"); + std::fs::create_dir(&inside).unwrap(); + std::fs::write(inside.join("secret.txt"), b"inside bytes, always safe to read").unwrap(); + let link = sentinels.root.join("d"); + std::os::unix::fs::symlink(&inside, &link).unwrap(); + + let stop = Arc::new(AtomicBool::new(false)); + let swapper = { + let stop = stop.clone(); + let link = link.clone(); + let outside = sentinels.sentinel_dir.clone(); + std::thread::spawn(move || { + let temp = link.with_extension("swap"); + let mut toward_outside = false; + while !stop.load(Ordering::Relaxed) { + let target = if toward_outside { &outside } else { &inside }; + // A stale `temp` from a rename that did not happen (should not occur, but a + // leftover `temp` would otherwise wedge every later iteration's `symlink` call, + // silently stopping the swap rather than failing the test loudly) is cleared + // first so this loop cannot get stuck. + let _ = std::fs::remove_file(&temp); + if std::os::unix::fs::symlink(target, &temp).is_ok() { + let _ = std::fs::rename(&temp, &link); + } + toward_outside = !toward_outside; + } + }) + }; + + let (mut app, ctx) = wired(sentinels.root.clone()); + let mut leaked = false; + for _ in 0..200 { + cloud::submit(&app, Job::Read { entry: leaked_entry("/d/secret.txt", "secret.txt"), into: None }); + said(&mut app, &ctx); + if let Some((_, bytes)) = app.opened.lock().unwrap().first() { + leaked |= bytes.as_slice() == SENTINEL_DIR_FILE_BYTES; + } + } + + stop.store(true, Ordering::Relaxed); + swapper.join().unwrap(); + + assert!(!leaked, "a read observed the outside target's bytes while the link was being swapped underneath it"); + assert_sentinels_untouched(&sentinels); +} diff --git a/crates/cloud-local/tests/save_path_regressions.rs b/crates/cloud-local/tests/save_path_regressions.rs new file mode 100644 index 0000000000..76cec678a3 --- /dev/null +++ b/crates/cloud-local/tests/save_path_regressions.rs @@ -0,0 +1,160 @@ +//! Two save-path defects that need a real backend to show: a reload after a conflict carrying a +//! stale etag into the next save, and two creates of the same name racing past an exists check. +//! Driven through craft_ui::cloud's public functions the way an application does. + +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use craft_cloud::{Entry, EntryKind}; +use craft_cloud_local::LocalBackend; +use craft_ui::cloud::{self, CloudApp, CloudFiles, Destination, DocumentKind}; + +const NOTE: DocumentKind = DocumentKind { extensions: &["txt"], mimes: &["text/plain"], noun: "note", default_extension: "txt" }; + +struct Window { + cloud: CloudFiles, + share: craft_ui::share::ShareDialog, + presence: craft_ui::presence::PresenceBar, + body: Vec, + said: Arc>>, +} + +impl Window { + fn new(root: std::path::PathBuf, body: &[u8]) -> Window { + let mut w = Window { + cloud: CloudFiles::new(), + share: craft_ui::share::ShareDialog::new(), + presence: craft_ui::presence::PresenceBar::new(), + body: body.to_vec(), + said: Arc::default(), + }; + w.cloud.connect(LocalBackend::new(root)); + w + } +} + +impl CloudApp for Window { + fn cloud(&self) -> &CloudFiles { + &self.cloud + } + fn cloud_mut(&mut self) -> &mut CloudFiles { + &mut self.cloud + } + fn kind(&self) -> DocumentKind { + NOTE + } + fn active(&self) -> Option { + Some(1) + } + fn title(&self, _uid: u64) -> String { + String::new() + } + fn open_bytes(&mut self, _name: &str, bytes: Vec) -> Result, String> { + self.body = bytes; + Ok(Some(1)) + } + fn encode(&mut self, _uid: u64, _into: Destination<'_>) -> Result, String> { + Ok(self.body.clone()) + } + fn close(&mut self, _uid: u64) {} + fn sharing(&mut self) -> &mut craft_ui::share::ShareDialog { + &mut self.share + } + fn presence(&mut self) -> &mut craft_ui::presence::PresenceBar { + &mut self.presence + } + fn paint_icon(&self, _p: &egui::Painter, _r: egui::Rect, _k: EntryKind, _c: egui::Color32) {} + fn open_url(&self, _url: &str) {} + fn say(&mut self, text: String) { + self.said.lock().unwrap().push(text); + } + fn app_name(&self) -> &str { + "Review" + } +} + +fn settle(app: &mut Window, ctx: &egui::Context, mut done: impl FnMut(&Window) -> bool) { + let start = Instant::now(); + loop { + ctx.begin_pass(Default::default()); + cloud::pump(app, ctx); + ctx.end_pass().textures_delta.clear(); + if done(app) { + return; + } + assert!(start.elapsed() < Duration::from_secs(5), "timed out"); + std::thread::sleep(Duration::from_millis(2)); + } +} + +fn listed(app: &mut Window, ctx: &egui::Context, name: &str) -> Entry { + cloud::browse(app, ""); + settle(app, ctx, |a| a.cloud.mine.loaded); + app.cloud.mine.entries.iter().find(|e| e.name == name).cloned().expect("listed") +} + +/// Open v1, another writer stores v2, Save conflicts, the user picks "Reload from the cloud" +/// (`reload_active`, what the modal calls). The window now shows v2's bytes, so the next Save +/// must go through rather than conflict again on the etag the reloaded copy was read at. +#[test] +fn reload_after_a_conflict_carries_the_freshly_read_etag_so_the_next_save_succeeds() { + let root = tempfile::tempdir().unwrap(); + std::fs::write(root.path().join("note.txt"), b"v1").unwrap(); + let ctx = egui::Context::default(); + let mut app = Window::new(root.path().to_owned(), b""); + let v1 = listed(&mut app, &ctx, "note.txt"); + cloud::open_entry(&mut app, v1.clone(), None); + settle(&mut app, &ctx, |a| a.cloud.busy.is_none() && a.body == b"v1"); + + std::thread::sleep(Duration::from_millis(20)); + std::fs::write(root.path().join("note.txt"), b"v2 by someone else").unwrap(); + + app.body = b"my edit".to_vec(); + cloud::save(&mut app).unwrap(); + settle(&mut app, &ctx, |a| a.cloud.busy.is_none()); + assert!(app.cloud.conflict.is_some(), "the first save is refused (correct)"); + + cloud::reload_active(&mut app).unwrap(); + settle(&mut app, &ctx, |a| a.cloud.busy.is_none()); + assert_eq!(app.body, b"v2 by someone else", "reload shows the stored bytes"); + let origin_etag = cloud::report(&app)["origin"]["etag"].clone(); + + cloud::save(&mut app).unwrap(); + settle(&mut app, &ctx, |a| a.cloud.busy.is_none()); + let on_disk = std::fs::read(root.path().join("note.txt")).unwrap(); + assert!( + app.cloud.conflict.is_none(), + "after reloading v2 the save conflicted again: origin etag after reload = {origin_etag} \ + (listing etag was {:?}); file on disk = {:?}", + v1.etag, + String::from_utf8_lossy(&on_disk) + ); +} + +/// Two windows Save As the same new name at once. At most one create may succeed; the other +/// must be refused ("already exists") rather than both reporting success while one silently +/// replaces the other on disk. +#[test] +fn concurrent_create_of_one_name_refuses_the_loser() { + let rounds = 300; + let (mut both_ok, mut example) = (0, String::new()); + for round in 0..rounds { + let root = tempfile::tempdir().unwrap(); + let ctx = egui::Context::default(); + let mut a = Window::new(root.path().to_owned(), format!("window A round {round}").as_bytes()); + let mut b = Window::new(root.path().to_owned(), format!("window B round {round}").as_bytes()); + cloud::save_as(&mut a, "", "Report.txt").unwrap(); + cloud::save_as(&mut b, "", "Report.txt").unwrap(); + settle(&mut a, &ctx, |w| w.cloud.busy.is_none()); + settle(&mut b, &ctx, |w| w.cloud.busy.is_none()); + let (a_ok, b_ok) = (cloud::origin(&a).is_some(), cloud::origin(&b).is_some()); + if a_ok && b_ok { + both_ok += 1; + if example.is_empty() { + let disk = std::fs::read_to_string(root.path().join("Report.txt")).unwrap(); + example = format!("round {round}: A status {:?}, B status {:?}, file holds {disk:?}", a.cloud.status, b.cloud.status); + } + } + } + assert_eq!(both_ok, 0, "{both_ok}/{rounds} rounds: both windows were told the new file was created; e.g. {example}"); +} diff --git a/crates/cloud/Cargo.toml b/crates/cloud/Cargo.toml new file mode 100644 index 0000000000..055feff390 --- /dev/null +++ b/crates/cloud/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "craft-cloud" +version.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +rust-version.workspace = true +description = "What a cloud provider offers the Craft apps: files, sharing, contacts, notifications and mail as contracts, with no vendor and no IO" + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/cloud/src/error.rs b/crates/cloud/src/error.rs new file mode 100644 index 0000000000..ede71ac0c3 --- /dev/null +++ b/crates/cloud/src/error.rs @@ -0,0 +1,68 @@ +//! One error type for every contract. +//! +//! The cases are the ones a caller acts on differently. A UI shows a different +//! thing for "you may not" than for "the server is unreachable", and a save +//! retries on a conflict where it would not on a refusal. + +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum Error { + /// The provider could not be reached. Worth retrying. + #[error("the provider could not be reached: {0}")] + Unreachable(String), + + /// The credential was rejected. The user has to sign in again. + #[error("the provider rejected the credential")] + Unauthenticated, + + /// Authenticated, and not allowed to do this. Signing in again will not + /// help, so a UI must not offer it. + #[error("not permitted: {0}")] + Forbidden(String), + + /// No such document, folder or share. + #[error("{what} not found: {id}")] + NotFound { what: &'static str, id: String }, + + /// The document changed since the caller last read it. A save that would + /// have overwritten somebody else's work lands here. + #[error("{0} changed since you opened it")] + Conflict(String), + + /// The provider does not offer this at all. + /// + /// Distinct from [`Error::Forbidden`]: there is nothing to grant. A UI + /// hides the feature rather than explaining it. + #[error("this provider does not support {0}")] + Unsupported(&'static str), + + /// The answer did not match the protocol. + #[error("malformed {what}: {detail}")] + Malformed { what: &'static str, detail: String }, + + /// A name or path could not be used safely. Carried, never panicked. + #[error("invalid {what}: {detail}")] + Invalid { what: &'static str, detail: String }, +} + +impl Error { + pub fn not_found(what: &'static str, id: impl Into) -> Error { + Error::NotFound { what, id: id.into() } + } + + pub fn malformed(what: &'static str, detail: impl Into) -> Error { + Error::Malformed { what, detail: detail.into() } + } + + pub fn invalid(what: &'static str, detail: impl Into) -> Error { + Error::Invalid { what, detail: detail.into() } + } + + /// Whether trying again could succeed without the user doing anything. + pub fn is_transient(&self) -> bool { + matches!(self, Error::Unreachable(_)) + } +} + +pub type Result = std::result::Result; diff --git a/crates/cloud/src/lib.rs b/crates/cloud/src/lib.rs new file mode 100644 index 0000000000..7f02dd4684 --- /dev/null +++ b/crates/cloud/src/lib.rs @@ -0,0 +1,189 @@ +//! What a cloud provider has to offer the Craft apps, and nothing about who +//! provides it. +//! +//! Every capability here is an RFC or an ordinary HTTP API that many systems +//! implement. Files are WebDAV. Contacts are CardDAV. Calendars are CalDAV. +//! Sharing and notification are the only genuinely vendor-shaped parts, and +//! they are small. So there is no reason for an app to know which server it is +//! talking to, and no reason to write the same logic again for the next one. +//! +//! Nothing in this crate opens a socket. Each trait is implemented by a +//! provider crate that brings its own transport, which keeps this testable +//! with no server and keeps the contracts honest about what they need. +//! +//! No type here names a vendor. If a field only makes sense for one product, +//! it belongs in that provider's crate. +//! +//! Nothing here says where an implementation runs. A local adapter may +//! implement these traits against a provider directly, and an application may +//! consume them that way. The contracts do not care. +//! +//! CollabCraft chooses otherwise for its own apps. They reach one service, and +//! that service is the single place a provider is configured, a credential is +//! held and a permission is decided. A desktop binary carrying a provider +//! credential leaks one the moment somebody copies the file, which is what +//! that arrangement exists to prevent. It is a deployment policy, and this +//! crate imposes it on nobody. + +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +mod error; +mod types; + +pub use error::{Error, Result}; +pub use types::{ + Capability, Contact, Conversation, Entry, EntryKind, Identity, MailMessage, Notification, Permission, Share, ShareOptions, ShareTarget, +}; + +/// Reading and writing documents. +/// +/// `list` and `shared_with_me` are separate because they answer different +/// questions. One is a folder's contents, the other is everything another +/// person has given this user access to, which has no single parent folder. +pub trait Files { + /// One folder's contents. An empty path means the user's root. + fn list(&self, path: &str) -> Result>; + + /// Everything shared with this user, wherever it lives. + /// + /// A provider with no concept of this returns an empty list rather than an + /// error, because "nobody has shared anything with you" and "this server + /// cannot tell you" look the same to a file picker. + fn shared_with_me(&self) -> Result>; + + /// One document's bytes. + fn read(&self, id: &str) -> Result>; + + /// A byte range of one document. + /// + /// Defaulted to a full [`read`] and a slice, so providers that have no + /// ranged GET still satisfy the trait. A provider with real range support + /// overrides this. + fn read_range(&self, id: &str, offset: u64, length: u64) -> Result> { + let bytes = self.read(id)?; + let start = usize::try_from(offset).unwrap_or(usize::MAX); + if start >= bytes.len() { + return Ok(Vec::new()); + } + let take = usize::try_from(length).unwrap_or(usize::MAX); + let end = start.saturating_add(take).min(bytes.len()); + Ok(bytes[start..end].to_vec()) + } + + /// Replace one document's bytes. + /// + /// `if_match` carries the etag the caller last saw. A provider that + /// supports conditional writes uses it to refuse a save that would + /// overwrite somebody else's, returning [`Error::Conflict`]. Passing + /// `None` means "write regardless", which is what a Save As does. + fn write(&self, id: &str, bytes: &[u8], if_match: Option<&str>) -> Result; + + /// Create a document and return it. + fn create(&self, folder: &str, name: &str, bytes: &[u8]) -> Result; + + /// One document's metadata, without its bytes. + fn stat(&self, id: &str) -> Result; +} + +/// Giving other people access to a document. +/// +/// Persisting a share through the provider matters more than it looks. A share +/// recorded only by the caller would be invisible in the provider's own file +/// manager, so the two would disagree about who can open what. The provider +/// stays the authority. +pub trait Sharing { + /// Share a document. + /// + /// A link share has no recipient, so [`ShareTarget::Link`] is how a caller + /// asks for one. The returned [`Share::url`] is what to hand out. + fn share(&self, id: &str, with: &ShareTarget, permission: Permission, options: &ShareOptions) -> Result; + + /// Who currently has access. + fn shares(&self, id: &str) -> Result>; + + /// Withdraw one share. + fn unshare(&self, share_id: &str) -> Result<()>; +} + +/// Looking people up, for a share dialog's autocomplete. +/// +/// This is the only thing the share dialog needs, so it is the only thing the +/// contract exposes. A full address book API would be a bigger surface for no +/// gain. +pub trait Contacts { + /// People matching a partial name or address. + /// + /// `limit` exists because this runs on every keystroke. A provider is free + /// to return fewer. + fn search(&self, query: &str, limit: usize) -> Result>; +} + +/// Telling somebody something happened. +/// +/// Separate from [`Mail`] on purpose. An in-app notification is cheap, arrives +/// instantly and is read in context. An email is none of those and is the +/// right choice for somebody who is not currently in the app. A share usually +/// wants both. +pub trait Notifications { + fn notify(&self, user: &str, notification: &Notification) -> Result<()>; +} + +/// Sending mail as the provider. +pub trait Mail { + fn send(&self, message: &MailMessage) -> Result<()>; +} + +/// Who the caller is, according to the provider. +pub trait Accounts { + /// Resolve the signed-in user. + fn me(&self) -> Result; +} + +/// Working together on a document, through whatever the provider calls it. +/// +/// Defaulted throughout. A provider without the feature writes an empty +/// `impl` and reports [`Capability::Conversation`] as absent. The caller +/// then draws no control at all. +pub trait Conversations { + /// What this provider calls the feature. `None` where it has none. + /// + /// Separate from the capability on purpose. The name is a property of + /// the provider. Whether it is available is a property of the + /// instance, and an administrator decides that one. + fn conversation_name(&self) -> Option { + None + } + + /// Start a conversation about a document, or join the one already there. + /// + /// Two people pressing the same control have to arrive in the same + /// place, so this is expected to be idempotent for a given document. + /// + /// `id` is the provider's own handle and means nothing outside the + /// account it came from. A conversation is therefore keyed on the + /// provider, the account and the document together, and `self` carries + /// the first two. A document copied into another account is a different + /// document to its provider, so it gets a conversation of its own. + /// Nothing here can attach one account's thread to another's. + fn conversation(&self, _id: &str, _title: &str) -> Result { + Err(Error::Unsupported("conversations")) + } +} + +/// Everything one provider can do. +/// +/// A provider implements the capabilities it has and reports the rest as +/// absent, so a caller can hide a share button on a server with no sharing +/// rather than offering a feature that will fail. +pub trait Provider: Files + Sharing + Contacts + Notifications + Mail + Accounts + Conversations { + /// A stable identifier, such as `nextcloud`. Used in configuration and + /// logs, never shown to a user. + fn id(&self) -> &str; + + /// What this provider actually supports. + fn capabilities(&self) -> Vec; + + fn supports(&self, capability: Capability) -> bool { + self.capabilities().contains(&capability) + } +} diff --git a/crates/cloud/src/types.rs b/crates/cloud/src/types.rs new file mode 100644 index 0000000000..ab8c005a1b --- /dev/null +++ b/crates/cloud/src/types.rs @@ -0,0 +1,216 @@ +//! The data the contracts pass around. +//! +//! Every type here is plain and vendor-neutral. A field that only means +//! something to one product belongs in that provider's crate. + +use serde::{Deserialize, Serialize}; + +/// What a provider can do. +/// +/// Reported rather than assumed, so a caller hides a share button on a +/// server without sharing instead of offering a feature that fails on use. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Capability { + Files, + SharedWithMe, + Sharing, + Contacts, + Notifications, + Mail, + /// Conditional writes, so a save can refuse to clobber a newer version. + Etags, + /// Public links. Absent on a server that forbids them, and the dialog then + /// offers no Copy link rather than a button that fails. + LinkShares, + /// A key on a public link. This is what most places mean when they talk + /// about sharing with somebody who has no account. + LinkPassword, + /// The server requires that key, so a link cannot be made without one. + LinkPasswordRequired, + /// An expiry date on a share. + ShareExpiry, + /// The provider can tell a recipient that something was shared with them. + /// + /// Distinct from [`Capability::Mail`], which is about sending arbitrary + /// mail. A provider commonly has this and not that: it will notify the + /// person a share names without offering any way to send mail in general. + ShareNotify, + /// A conversation about a document, such as a call or a chat room. + /// + /// Every provider that has this calls it something else, so the name + /// comes from [`crate::Conversations::conversation_name`]. Reported + /// separately from that name: a provider knows what it would call the + /// feature whether or not an administrator installed it. + Conversation, +} + +/// A file or folder. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Entry { + /// An opaque handle for reading, writing and sharing this entry. + /// + /// Only the provider that issued it may interpret it. A caller passes it + /// back and never parses it. + pub id: String, + /// The stable identity used for a collaboration room. + /// + /// Separate from `id` on purpose. A provider's read and write handle is + /// often a path, and a path changes when somebody renames or moves the + /// file. A room keyed on that would silently split in two mid-session, + /// with each half seeing only its own edits. This one survives a rename. + pub room: String, + /// What to show the user. + pub name: String, + /// Where it sits, for a breadcrumb. Never a handle. + pub path: String, + pub kind: EntryKind, + /// Bytes, absent for a folder. + pub size: Option, + /// RFC 3339. Absent when the provider does not say. + pub modified: Option, + /// For matching a document to the app that opens it. + pub mime: Option, + /// The version the caller last saw, for a conditional write. + pub etag: Option, + /// Whether this user may change it. A read-only share still lists. + pub writable: bool, + /// Who shared it, when it came from somebody else. + pub shared_by: Option, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum EntryKind { + File, + Folder, +} + +/// A person, as a share dialog needs them. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Contact { + /// What the provider wants back when sharing with this person. + pub id: String, + /// How to share with them, decided by the provider and never guessed. + /// + /// The distinction is not cosmetic. A contact with an account is a user + /// share. One known only by address is an email share. Sharing an address + /// as though it were a user id fails with "not found", because no such + /// user exists, and nothing in `id` alone says which case this is. + pub target: ShareTarget, + /// What to show. A display name where there is one. + pub name: String, + pub email: Option, + /// An avatar, as a URL the client may fetch. Never image bytes: a search + /// returning ten people would otherwise carry ten pictures nobody asked + /// for yet. + pub avatar: Option, + /// A group rather than a person. Shown differently and shared differently. + pub is_group: bool, +} + +/// Who to share with. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", tag = "kind", content = "id")] +pub enum ShareTarget { + User(String), + Group(String), + /// Somebody with no account, reached by address. + Email(String), + /// Anybody holding the link. + Link, +} + +/// What a share allows. +/// +/// Deliberately coarse. Providers disagree on the fine detail, and a dialog +/// offering a distinction the server cannot keep is worse than one that does +/// not. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Permission { + /// Open and read. No edits reach the document. + View, + /// Open and edit. + Edit, +} + +/// Everything optional about a share. +/// +/// A struct rather than more positional arguments. The list grew past the +/// point where a call site reads correctly, and a provider that cannot honour +/// one of these needs to see which it was given. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct ShareOptions { + /// Shown to the recipient. + pub note: Option, + /// Send mail where the provider can. + pub notify: bool, + /// A key the recipient must present. Only meaningful for a link, and + /// refused by a server that does not allow one. + pub password: Option, + /// `YYYY-MM-DD`. After this the share stops working. + pub expires: Option, +} + +/// Where to go to work together on a document. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Conversation { + /// Where to join. Handed to a browser, or to the provider's own app. + pub url: String, + /// What the provider calls the feature, for the control's wording. A + /// menu item reading "Work together in Talk" is built from this. + pub name: String, +} + +/// A share that exists. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Share { + pub id: String, + pub target: ShareTarget, + /// What to call the recipient, where the provider says. + /// + /// A share carries the handle the server wants back, which for a user is + /// an account id. People are not known to each other by account id. A + /// provider that knows the difference says so here. + pub display: Option, + pub permission: Permission, + /// Present for a link share, absent otherwise. + pub url: Option, + pub note: Option, + /// RFC 3339. + pub created: Option, +} + +/// Something worth telling a user about, in the app. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Notification { + /// A short machine-readable kind, such as `document_shared`. Lets a client + /// decide how to render without parsing prose. + pub kind: String, + pub subject: String, + pub body: Option, + /// Where to go when it is clicked. + pub link: Option, +} + +/// An email to send. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct MailMessage { + pub to: Vec, + pub subject: String, + pub body: String, + /// Whether `body` is HTML. Plain text otherwise. + pub html: bool, +} + +/// The signed-in user, as the provider sees them. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct Identity { + /// The provider's identifier for this account. + pub id: String, + /// What to show. Never an identity: two people can share a display name. + pub display: String, + pub email: Option, + pub avatar: Option, +} diff --git a/crates/cms/README.md b/crates/cms/README.md index 71f3146d50..cbee391cd9 100644 --- a/crates/cms/README.md +++ b/crates/cms/README.md @@ -81,6 +81,12 @@ Buffers are checked once per call, before any pixel is written. the exact and float paths compare each pixel with white; all other colours are left as the profiles give them. With a profile whose white falls between its nodes (`coated-cmyk`: 0.4 % C), the last step to white is that much larger than its neighbours. +* Black preservation (opt-in, `BlackPreserving`): for CMYK → CMYK, a colour with exactly zero + C, M and Y comes out as exactly `(0, 0, 0, K')`, where `K'` is the destination black whose + K-only L* equals the source's (both K ramps sampled at 1024 points when the transform is + built, with its intent and BPC). The test is exact zero, so an input with any chromatic ink + takes the ordinary path. Absolute colorimetric, non-CMYK profiles and K ramps that reverse at + one of the 1024 samples are refused (a reversal between samples is not detected). ## Built-in profiles (all CC0-1.0, generated by this crate) diff --git a/crates/cms/profiles/craft-coated-fogra39.icc b/crates/cms/profiles/craft-coated-fogra39.icc index 13590238ea..1ef3cad1fd 100644 Binary files a/crates/cms/profiles/craft-coated-fogra39.icc and b/crates/cms/profiles/craft-coated-fogra39.icc differ diff --git a/crates/cms/src/black.rs b/crates/cms/src/black.rs new file mode 100644 index 0000000000..f80defeef1 --- /dev/null +++ b/crates/cms/src/black.rs @@ -0,0 +1,205 @@ +//! K-only black preservation for CMYK → CMYK transforms. +//! +//! A CMYK → CMYK transform goes through the three-component PCS, so black alone (C = M = Y = 0) +//! comes out as a four-ink mix. ISO 32000-2 §8.6.5.7 notes that this loses fidelity in the black +//! component. ICC defines no black preservation; this is a named policy the caller opts into. +//! +//! [`BlackPreserving`] converts like its [`Transform`], except that a colour with exactly zero +//! cyan, magenta and yellow becomes exactly `(0, 0, 0, K')`. `K'` is the destination black whose +//! K-only L* equals the source's, both measured to Lab with the transform's own intent and BPC +//! setting: both ramps are sampled once, at construction, and the destination ramp is inverted. +//! A source black darker than the destination's maximum black maps to K = 1. The test is exact zero, so the transform is discontinuous at C = M = Y = 0: an input +//! with any chromatic ink takes the ordinary path. + +use crate::transform::{Transform, TransformOptions}; +use crate::{Builtin, CmsError, ColorSpace, Intent, Profile, invalid_arg}; + +/// Samples along each K-only ramp. Linear interpolation between samples of a smooth ramp is far +/// below the 16-bit quantum. +const K_SAMPLES: usize = 1024; +/// Slack for the monotonicity checks (L* units, K units). +const MONOTONIC_EPS: f64 = 1e-6; + +/// A CMYK → CMYK transform that keeps black-only colours black-only. +#[derive(Debug)] +pub struct BlackPreserving { + transform: Transform, + /// Destination K for source K at `i / (K_SAMPLES - 1)`; `K_SAMPLES` values in `[0, 1]`. + curve: Vec, + /// Source K from which the source black is at least as dark as the destination's darkest: + /// mapped to exactly 1. Infinite when the destination's black is the darker. + saturate: f32, + /// The mapping at the 256 8-bit source codes, already quantised. + curve8: [u8; 256], +} + +impl BlackPreserving { + /// Transform from `src` to `dst` with black preservation. + /// + /// Errors with [`CmsError::InvalidArgument`] when either profile is not CMYK or the intent is + /// absolute colorimetric (which reproduces the source's paper and black, so there is no + /// black to map), and with [`CmsError::Unsupported`] when a profile's K-only ramp is not + /// monotonic at its 1024 samples (black that gets lighter as K rises has no equal-lightness + /// inverse; a reversal between samples is not detected). Other errors + /// are those of [`Transform::with_options`]. + pub fn new(src: &Profile, dst: &Profile, opts: TransformOptions) -> Result { + if src.color_space != ColorSpace::Cmyk || dst.color_space != ColorSpace::Cmyk { + return Err(invalid_arg("black preservation needs CMYK source and destination profiles")); + } + if opts.intent == Intent::AbsoluteColorimetric { + return Err(invalid_arg("black preservation does not apply to absolute colorimetric")); + } + let transform = Transform::with_options(src, dst, opts)?; + let src_l = k_ramp_lightness(src, opts)?; + let dst_l = k_ramp_lightness(dst, opts)?; + let (curve, saturate) = k_curve(&src_l, &dst_l)?; + let mut curve8 = [0u8; 256]; + for (code, slot) in curve8.iter_mut().enumerate() { + *slot = quantise(map(&curve, saturate, code as f32 / 255.0), 255.0) as u8; + } + Ok(BlackPreserving { transform, curve, saturate, curve8 }) + } + + /// The ordinary transform, which every colour with chromatic ink goes through. Converting with + /// it directly does not preserve black. + pub fn transform(&self) -> &Transform { + &self.transform + } + + /// Destination K for a black-only source colour of `k` (clamped to `[0, 1]`). + pub fn map_k(&self, k: f32) -> f32 { + map(&self.curve, self.saturate, k) + } + + /// Exact evaluation of one colour; slices as in [`Transform::eval`]. + pub fn eval(&self, input: &[f32], out: &mut [f32]) -> Result<(), CmsError> { + self.transform.eval(input, out)?; + self.patch_one(input, out); + Ok(()) + } + + /// Lookup-table evaluation of one colour; slices as in [`Transform::eval`]. + pub fn eval_fast(&self, input: &[f32], out: &mut [f32]) -> Result<(), CmsError> { + self.transform.eval_fast(input, out)?; + self.patch_one(input, out); + Ok(()) + } + + /// Converts float pixels; layout and errors as in [`Transform::convert_f32`]. + pub fn convert_f32(&self, src: &[f32], src_stride: usize, dst: &mut [f32], dst_stride: usize, copy_extra: bool) -> Result<(), CmsError> { + self.transform.convert_f32(src, src_stride, dst, dst_stride, copy_extra)?; + for (s, d) in src.chunks_exact(src_stride).zip(dst.chunks_exact_mut(dst_stride)) { + self.patch_one(s, d); + } + Ok(()) + } + + /// Converts 8-bit pixels; layout and errors as in [`Transform::convert_u8`]. + pub fn convert_u8(&self, src: &[u8], src_stride: usize, dst: &mut [u8], dst_stride: usize, copy_extra: bool) -> Result<(), CmsError> { + self.transform.convert_u8(src, src_stride, dst, dst_stride, copy_extra)?; + for (s, d) in src.chunks_exact(src_stride).zip(dst.chunks_exact_mut(dst_stride)) { + if let ([0, 0, 0, k, ..], [c, m, y, kd, ..]) = (s, d) { + (*c, *m, *y) = (0, 0, 0); + *kd = self.curve8[usize::from(*k)]; + } + } + Ok(()) + } + + /// Converts 16-bit pixels; layout and errors as in [`Transform::convert_u16`]. + pub fn convert_u16(&self, src: &[u16], src_stride: usize, dst: &mut [u16], dst_stride: usize, copy_extra: bool) -> Result<(), CmsError> { + self.transform.convert_u16(src, src_stride, dst, dst_stride, copy_extra)?; + for (s, d) in src.chunks_exact(src_stride).zip(dst.chunks_exact_mut(dst_stride)) { + if let ([0, 0, 0, k, ..], [c, m, y, kd, ..]) = (s, d) { + (*c, *m, *y) = (0, 0, 0); + *kd = quantise(self.map_k(f32::from(*k) / 65535.0), 65535.0) as u16; + } + } + Ok(()) + } + + fn patch_one(&self, input: &[f32], out: &mut [f32]) { + if let ([0.0, 0.0, 0.0, k, ..], [c, m, y, kd, ..]) = (input, out) { + (*c, *m, *y) = (0.0, 0.0, 0.0); + *kd = self.map_k(*k); + } + } +} + +/// The sampled mapping from the source to the destination K-only ramp, and the source K at +/// which the destination's darkest black is reached (the `saturate` field). +fn k_curve(src_l: &[f64], dst_l: &[f64]) -> Result<(Vec, f32), CmsError> { + let curve: Vec = src_l.iter().map(|&l| invert(dst_l, l) as f32).collect(); + if !curve.windows(2).all(|w| f64::from(w[0]) - f64::from(w[1]) <= MONOTONIC_EPS) { + return Err(CmsError::Unsupported("black preservation: the K mapping is not monotonic".into())); + } + let saturate = match (src_l.last(), dst_l.last()) { + (Some(&s), Some(&d)) if s < d => invert(src_l, d) as f32, + _ => f32::INFINITY, + }; + Ok((curve, saturate)) +} + +/// L* of `p`'s K-only ramp at `K_SAMPLES` points, light to dark; refused when it is not +/// non-increasing. +fn k_ramp_lightness(p: &Profile, opts: TransformOptions) -> Result, CmsError> { + let to_lab = Transform::with_options(p, Builtin::LabD50.profile(), TransformOptions { precise_float: true, ..opts })?; + let mut ramp = Vec::with_capacity(K_SAMPLES); + let mut lab = [0.0f32; 3]; + for i in 0..K_SAMPLES { + let k = i as f32 / (K_SAMPLES - 1) as f32; + to_lab.eval(&[0.0, 0.0, 0.0, k], &mut lab)?; + ramp.push(f64::from(lab[0]) * 100.0); + } + if !ramp.iter().all(|l| l.is_finite()) || !ramp.windows(2).all(|w| w[1] - w[0] <= MONOTONIC_EPS) { + return Err(CmsError::Unsupported("black preservation: a K-only ramp is not monotonic".into())); + } + Ok(ramp) +} + +/// The K in `[0, 1]` at which the non-increasing `ramp` reaches `target`, clamped to its ends. +fn invert(ramp: &[f64], target: f64) -> f64 { + let (Some(&first), Some(&last)) = (ramp.first(), ramp.last()) else { return 0.0 }; + if ramp.len() < 2 || target >= first { + return 0.0; + } + if target <= last { + return 1.0; + } + // ramp[lo] > target >= ramp[hi] + let (mut lo, mut hi) = (0usize, ramp.len() - 1); + while hi - lo > 1 { + let mid = lo + (hi - lo) / 2; + if ramp.get(mid).is_some_and(|&v| v > target) { + lo = mid; + } else { + hi = mid; + } + } + let (a, b) = (ramp.get(lo).copied().unwrap_or(first), ramp.get(hi).copied().unwrap_or(last)); + let t = if a - b > MONOTONIC_EPS { (a - target) / (a - b) } else { 0.0 }; + (lo as f64 + t) / (ramp.len() - 1) as f64 +} + +/// Linear interpolation in `curve` (samples over `[0, 1]`) at `k`, clamped; 1 from `saturate`. +fn map(curve: &[f32], saturate: f32, k: f32) -> f32 { + let Some(&last) = curve.last() else { return 0.0 }; + let k = if k.is_nan() { 0.0 } else { k.clamp(0.0, 1.0) }; + if k >= saturate { + return 1.0; + } + let x = k * (curve.len() - 1) as f32; + let i = x.floor() as usize; + match (curve.get(i), curve.get(i + 1)) { + (Some(&a), Some(&b)) => a + (b - a) * (x - i as f32), + _ => last, + } +} + +fn quantise(v: f32, max: f32) -> f32 { + (v.clamp(0.0, 1.0) * max + 0.5).floor() +} + +#[cfg(test)] +#[path = "black_tests.rs"] +mod tests; diff --git a/crates/cms/src/black_tests.rs b/crates/cms/src/black_tests.rs new file mode 100644 index 0000000000..8752cdaa2b --- /dev/null +++ b/crates/cms/src/black_tests.rs @@ -0,0 +1,149 @@ +use super::*; + +fn coated() -> &'static Profile { + Builtin::CoatedCmyk.profile() +} +fn fogra() -> &'static Profile { + Builtin::CoatedFogra39.profile() +} +fn opts() -> TransformOptions { + TransformOptions::default() +} + +/// L* of a CMYK colour under `p`, measured as the transform with `o` measures it. +fn lightness(p: &Profile, o: TransformOptions, cmyk: [f32; 4]) -> f32 { + let t = Transform::with_options(p, Builtin::LabD50.profile(), o).unwrap(); + let mut lab = [0.0f32; 3]; + t.eval(&cmyk, &mut lab).unwrap(); + lab[0] * 100.0 +} + +#[test] +fn the_plain_transform_turns_black_into_four_inks() { + // What the policy is for: without it, black alone gains chromatic ink across presses. + let t = Transform::with_options(coated(), fogra(), opts()).unwrap(); + let mut out = [0.0f32; 4]; + t.eval(&[0.0, 0.0, 0.0, 0.5], &mut out).unwrap(); + assert!(out[..3].iter().any(|v| *v > 0.01), "{out:?}"); +} + +#[test] +fn black_only_stays_black_only_on_every_path() { + for (src, dst) in [(coated(), fogra()), (fogra(), coated()), (fogra(), fogra())] { + let bp = BlackPreserving::new(src, dst, opts()).unwrap(); + for k in [0.0f32, 0.1, 0.5, 0.9, 1.0] { + for fast in [false, true] { + let mut out = [1.0f32; 4]; + if fast { bp.eval_fast(&[0.0, 0.0, 0.0, k], &mut out) } else { bp.eval(&[0.0, 0.0, 0.0, k], &mut out) }.unwrap(); + assert_eq!(out[..3], [0.0; 3], "k {k} fast {fast}"); + assert_eq!(out[3], bp.map_k(k)); + } + } + let src8: Vec = (0..=255u8).flat_map(|k| [0, 0, 0, k, 7]).collect(); + let mut dst8 = vec![0u8; src8.len()]; + bp.convert_u8(&src8, 5, &mut dst8, 5, true).unwrap(); + for (k, px) in dst8.as_chunks::<5>().0.iter().enumerate() { + assert_eq!(px[..3], [0; 3], "u8 k {k}"); + assert!((f32::from(px[3]) - bp.map_k(k as f32 / 255.0) * 255.0).abs() <= 0.5, "u8 k {k}: {}", px[3]); + assert_eq!(px[4], 7, "extra channel copied"); + } + let src16: Vec = (0..=64u16).flat_map(|k| [0, 0, 0, k * 1023]).collect(); + let mut dst16 = vec![0u16; src16.len()]; + bp.convert_u16(&src16, 4, &mut dst16, 4, false).unwrap(); + for (px, k) in dst16.as_chunks::<4>().0.iter().zip(src16.as_chunks::<4>().0) { + assert_eq!(px[..3], [0; 3], "u16 {k:?}"); + assert!((f32::from(px[3]) - bp.map_k(f32::from(k[3]) / 65535.0) * 65535.0).abs() <= 0.5, "u16 {k:?}: {}", px[3]); + } + let srcf: Vec = (0..=10).flat_map(|k| [0.0, 0.0, 0.0, k as f32 / 10.0]).collect(); + let mut dstf = vec![0.0f32; srcf.len()]; + bp.convert_f32(&srcf, 4, &mut dstf, 4, false).unwrap(); + for (px, k) in dstf.as_chunks::<4>().0.iter().zip(srcf.as_chunks::<4>().0) { + assert_eq!(*px, [0.0, 0.0, 0.0, bp.map_k(k[3])], "f32 {k:?}"); + } + } +} + +#[test] +fn the_destination_black_matches_the_source_lightness() { + // Measured worst error over 201 K steps, both directions, both intents, BPC on and off: + // 0.007 L*. + for intent in [Intent::RelativeColorimetric, Intent::Perceptual] { + for bpc in [true, false] { + let o = TransformOptions { intent, bpc, ..opts() }; + for (src, dst) in [(coated(), fogra()), (fogra(), coated())] { + let bp = BlackPreserving::new(src, dst, o).unwrap(); + let dst_black = lightness(dst, o, [0.0, 0.0, 0.0, 1.0]); + for k in [0.1f32, 0.3, 0.5, 0.7, 0.9, 1.0] { + let want = lightness(src, o, [0.0, 0.0, 0.0, k]); + if want <= dst_black { + assert_eq!(bp.map_k(k), 1.0, "darker than the destination's black clamps to K = 1"); + continue; + } + let got = lightness(dst, o, [0.0, 0.0, 0.0, bp.map_k(k)]); + assert!((got - want).abs() < 0.02, "{intent:?} bpc {bpc} k {k}: L* {got} for {want}"); + } + assert_eq!(bp.map_k(0.0), 0.0, "paper stays paper"); + assert!(bp.map_k(1.0) > 0.95, "full black stays near full black: {}", bp.map_k(1.0)); + assert!(bp.map_k(0.25) < bp.map_k(0.75)); + } + } + } + let same = BlackPreserving::new(fogra(), fogra(), opts()).unwrap(); + for k in [0.0f32, 0.2, 0.6, 1.0] { + assert!((same.map_k(k) - k).abs() < 1e-3, "{k} -> {}", same.map_k(k)); + } +} + +#[test] +fn colours_with_any_chromatic_ink_take_the_ordinary_path() { + let bp = BlackPreserving::new(coated(), fogra(), opts()).unwrap(); + for input in [[1e-6f32, 0.0, 0.0, 0.5], [0.0, 0.2, 0.0, 0.5], [0.3, 0.3, 0.3, 0.0], [0.0, 0.0, 1.0, 1.0]] { + let (mut a, mut b) = ([0.0f32; 4], [0.0f32; 4]); + bp.eval(&input, &mut a).unwrap(); + bp.transform().eval(&input, &mut b).unwrap(); + assert_eq!(a, b, "{input:?}"); + } + let src = [1u8, 0, 0, 128, 0, 1, 0, 128]; + let (mut a, mut b) = ([0u8; 8], [0u8; 8]); + bp.convert_u8(&src, 4, &mut a, 4, false).unwrap(); + bp.transform().convert_u8(&src, 4, &mut b, 4, false).unwrap(); + assert_eq!(a, b); +} + +#[test] +fn refusals_and_hostile_values() { + let srgb = Builtin::Srgb.profile(); + assert!(matches!(BlackPreserving::new(srgb, fogra(), opts()), Err(CmsError::InvalidArgument(_)))); + assert!(matches!(BlackPreserving::new(fogra(), srgb, opts()), Err(CmsError::InvalidArgument(_)))); + let abs = TransformOptions { intent: Intent::AbsoluteColorimetric, ..opts() }; + assert!(matches!(BlackPreserving::new(fogra(), coated(), abs), Err(CmsError::InvalidArgument(_)))); + let bp = BlackPreserving::new(coated(), fogra(), opts()).unwrap(); + for k in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -1.0, 2.0] { + assert!((0.0..=1.0).contains(&bp.map_k(k)), "{k}"); + } + let mut out = [0.0f32; 4]; + assert!(bp.eval(&[0.0; 3], &mut out).is_err(), "short input"); + assert!(bp.convert_u8(&[0, 0, 0, 9, 0], 4, &mut [0u8; 4], 4, false).is_err(), "partial pixel"); + assert_eq!(invert(&[], 50.0), 0.0); + assert_eq!(invert(&[100.0], 50.0), 0.0); + assert_eq!(invert(&[100.0, 0.0], 150.0), 0.0); + assert_eq!(invert(&[100.0, 0.0], -5.0), 1.0); + assert!((invert(&[100.0, 50.0, 0.0], 25.0) - 0.75).abs() < 1e-12); + assert_eq!(map(&[], f32::INFINITY, 0.5), 0.0); +} + +#[test] +fn black_beyond_the_destination_maximum_maps_to_full_black_between_samples() { + // Source L* = 100 (1 - K), destination L* = 100 - 50 K: from K = 0.5 the source is darker + // than any destination black, and 0.5 falls between the 1024 samples. + let ramp = |f: fn(f64) -> f64| (0..K_SAMPLES).map(|i| f(i as f64 / (K_SAMPLES - 1) as f64)).collect::>(); + let (src, dst) = (ramp(|k| 100.0 * (1.0 - k)), ramp(|k| 100.0 - 50.0 * k)); + let (curve, saturate) = k_curve(&src, &dst).unwrap(); + assert!((saturate - 0.5).abs() < 1e-6, "{saturate}"); + for k in [0.5001f32, 0.6, 1.0] { + assert_eq!(map(&curve, saturate, k), 1.0, "{k}"); + } + assert!((map(&curve, saturate, 0.25) - 0.5).abs() < 1e-4); + let (_, never) = k_curve(&dst, &src).unwrap(); + assert_eq!(never, f32::INFINITY, "a lighter source never saturates"); +} diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index b082f67a5b..ab3427744d 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -19,6 +19,7 @@ //! CIE 15, and Adobe's published black point compensation paper. #![forbid(unsafe_code)] +pub mod black; pub mod builtin; mod builtin_v2; pub mod clut; @@ -30,6 +31,7 @@ pub mod lutfile; pub mod math; pub mod named; mod normalize; +pub mod palette; pub mod pipeline; pub mod profile; pub mod sony_lut; @@ -39,6 +41,7 @@ pub mod transform; mod write; pub mod write_v2; +pub use black::BlackPreserving; pub use builtin::Builtin; pub use clut::Clut; pub use curve::Curve; diff --git a/crates/cms/src/math.rs b/crates/cms/src/math.rs index 8601266538..7e298af1a2 100644 --- a/crates/cms/src/math.rs +++ b/crates/cms/src/math.rs @@ -39,7 +39,9 @@ pub const fn det(m: &Mat3) -> f64 { + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]) } -/// Inverse, or `None` when (nearly) singular. +/// Inverse, or `None` when (nearly) singular: the determinant is not finite or is below 1e-12. +/// Profile construction checks its matrices with this rule. [`inverse`] takes the determinant +/// relative to the largest entry instead. pub const fn invert(m: &Mat3) -> Option { let d = det(m); if !d.is_finite() || d.abs() < 1e-12 { @@ -53,14 +55,66 @@ pub const fn invert(m: &Mat3) -> Option { ]) } +/// Inverse of a matrix at any scale, or `None` when it holds a NaN or an infinite entry, is all +/// zeros, is singular to within rounding (the determinant is at most 1e-12 times the cube of the +/// largest entry), or has an inverse with an entry beyond the range of `f64`. +/// +/// The matrix is divided by its largest entry before the determinant is taken, so a matrix whose +/// own determinant overflows or underflows (`diag(1e-200, 1e-200, 1e-200)`) still inverts. +pub const fn inverse(m: &Mat3) -> Option { + let mut scale = 0.0f64; + let mut i = 0; + while i < 9 { + let v = m[i / 3][i % 3]; + if v.is_nan() { + return None; + } + if v.abs() > scale { + scale = v.abs(); + } + i += 1; + } + if !(scale.is_finite() && scale > 0.0) { + return None; + } + let mut s = [[0.0; 3]; 3]; + let mut i = 0; + while i < 9 { + s[i / 3][i % 3] = m[i / 3][i % 3] / scale; + i += 1; + } + let d = det(&s); + if !d.is_finite() || d.abs() <= 1e-12 { + return None; + } + // Each cofactor is divided by the determinant and then by the scale: the reciprocals can + // overflow where the quotients do not. + let c = [ + [s[1][1] * s[2][2] - s[1][2] * s[2][1], s[0][2] * s[2][1] - s[0][1] * s[2][2], s[0][1] * s[1][2] - s[0][2] * s[1][1]], + [s[1][2] * s[2][0] - s[1][0] * s[2][2], s[0][0] * s[2][2] - s[0][2] * s[2][0], s[0][2] * s[1][0] - s[0][0] * s[1][2]], + [s[1][0] * s[2][1] - s[1][1] * s[2][0], s[0][1] * s[2][0] - s[0][0] * s[2][1], s[0][0] * s[1][1] - s[0][1] * s[1][0]], + ]; + let mut inv = [[0.0; 3]; 3]; + let mut i = 0; + while i < 9 { + let v = c[i / 3][i % 3] / d / scale; + if !v.is_finite() { + return None; + } + inv[i / 3][i % 3] = v; + i += 1; + } + Some(inv) +} + /// XYZ (Y = 1) of a chromaticity `(x, y)`. pub const fn xy_to_xyz(xy: [f64; 2]) -> [f64; 3] { let [x, y] = xy; [x / y, 1.0, (1.0 - x - y) / y] } -/// Bradford cone response matrix (Lam 1985). -const BRADFORD: Mat3 = [[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367], [0.0389, -0.0685, 1.0296]]; +/// Bradford cone response matrix (Lam 1985): XYZ to sharpened cone responses. +pub const BRADFORD: Mat3 = [[0.8951, 0.2664, -0.1614], [-0.7502, 1.7135, 0.0367], [0.0389, -0.0685, 1.0296]]; /// Inverse of [`BRADFORD`] (determinant ~1.7), computed at compile time. const BRADFORD_INV: Mat3 = match invert(&BRADFORD) { @@ -77,6 +131,17 @@ pub fn bradford(src: [f64; 3], dst: [f64; 3]) -> Mat3 { mul(&inv, &mul(&scale, &BRADFORD)) } +/// [`bradford`], or `None` when a white's cone response is not finite or not above 0, or the +/// response ratios overflow. [`bradford`] divides by the source response without this check. +pub fn bradford_checked(src: [f64; 3], dst: [f64; 3]) -> Option { + let ok = |v: [f64; 3]| v.iter().all(|c| c.is_finite() && *c > 0.0); + if !(ok(apply(&BRADFORD, src)) && ok(apply(&BRADFORD, dst))) { + return None; + } + let m = bradford(src, dst); + m.iter().flatten().all(|v| v.is_finite()).then_some(m) +} + /// RGB → XYZ matrix (relative to `white`, Y = 1) for primaries given as chromaticities, or /// `None` when the primaries are not independent. pub const fn rgb_to_xyz_matrix(r: [f64; 2], g: [f64; 2], b: [f64; 2], white: [f64; 3]) -> Option { @@ -264,6 +329,59 @@ mod tests { } } + fn close(a: &Mat3, b: &Mat3, tol: f64) { + for i in 0..3 { + for j in 0..3 { + assert!((a[i][j] - b[i][j]).abs() <= tol, "[{i}][{j}] {} vs {}", a[i][j], b[i][j]); + } + } + } + + const fn diag(v: f64) -> Mat3 { + [[v, 0.0, 0.0], [0.0, v, 0.0], [0.0, 0.0, v]] + } + + #[test] + fn inverse_matches_invert_on_ordinary_matrices() { + let m = [[0.4, 0.3, 0.2], [0.2, 0.7, 0.1], [0.0, 0.1, 0.8]]; + close(&inverse(&m).unwrap(), &invert(&m).unwrap(), 1e-12); + assert_eq!(inverse(&BRADFORD).map(|_| ()), Some(())); + } + + #[test] + fn inverse_works_at_any_scale_and_rejects_what_has_no_finite_inverse() { + // The determinant, 1e-312, has no finite reciprocal; the inverse itself is finite. + close(&inverse(&diag(1e-104)).unwrap(), &diag(1e104), 1e93); + assert_eq!(invert(&diag(1e-104)), None); + // A determinant beyond f64 in either direction still leaves a finite inverse. + close(&inverse(&diag(1e-200)).unwrap(), &diag(1e200), 1e186); + close(&inverse(&diag(1e110)).unwrap(), &diag(1e-110), 1e-124); + assert_eq!(inverse(&diag(f64::MAX)).map(|m| m[0][0]), Some(1.0 / f64::MAX)); + // An inverse with entries beyond f64 is no inverse. + assert_eq!(inverse(&diag(1e-310)), None); + assert_eq!(inverse(&[[0.0; 3]; 3]), None); + assert_eq!(inverse(&[[f64::NAN; 3]; 3]), None); + assert_eq!(inverse(&diag(f64::INFINITY)), None); + let mut one_nan = IDENTITY; + one_nan[1][2] = f64::NAN; + assert_eq!(inverse(&one_nan), None); + // Singular to within rounding relative to the largest entry. + assert_eq!(inverse(&[[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [0.0, 0.0, 1e9]]), None); + } + + #[test] + fn bradford_checked_rejects_whites_with_no_cone_response() { + let d65 = xy_to_xyz(D65_XY); + close(&bradford_checked(d65, D50).unwrap(), &bradford(d65, D50), 0.0); + assert_eq!(bradford_checked([0.0; 3], D50), None); + assert_eq!(bradford_checked(d65, [f64::NAN; 3]), None); + assert_eq!(bradford_checked(d65, [-1.0, -1.0, -1.0]), None); + // Cone responses this small give scale ratios beyond f64. + assert_eq!(bradford_checked([1e-320; 3], d65), None); + // Unchecked, the zero white divides by zero. + assert!(bradford([0.0; 3], D50).iter().flatten().any(|v| !v.is_finite())); + } + #[test] fn invert_roundtrip() { let m = [[0.4, 0.3, 0.2], [0.2, 0.7, 0.1], [0.0, 0.1, 0.8]]; diff --git a/crates/cms/src/palette.rs b/crates/cms/src/palette.rs new file mode 100644 index 0000000000..8aabb70d85 --- /dev/null +++ b/crates/cms/src/palette.rs @@ -0,0 +1,806 @@ +//! Palette I/O for `.gpl` (GIMP Palette), `.css` (CSS custom properties, write-only), +//! `.ase` (Adobe Swatch Exchange, binary), and `.acb` (Adobe Color Book, read-only). + +use std::collections::{HashMap, HashSet}; + +/// A colour in a palette swatch. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum PaletteColor { + Rgb { r: f32, g: f32, b: f32 }, + Cmyk { c: f32, m: f32, y: f32, k: f32 }, + Lab { l: f32, a: f32, b: f32 }, + Gray { k: f32 }, +} + +impl PaletteColor { + pub fn rgb(r: f32, g: f32, b: f32) -> Self { + Self::Rgb { r, g, b } + } + + pub fn rgb8(r: u8, g: u8, b: u8) -> Self { + Self::Rgb { r: f32::from(r) / 255.0, g: f32::from(g) / 255.0, b: f32::from(b) / 255.0 } + } + + pub fn cmyk(c: f32, m: f32, y: f32, k: f32) -> Self { + Self::Cmyk { c, m, y, k } + } + + pub fn lab(l: f32, a: f32, b: f32) -> Self { + Self::Lab { l, a, b } + } + + pub fn gray(k: f32) -> Self { + Self::Gray { k } + } + + /// Approximate 8-bit sRGB representation for formats that only store RGB (`.gpl`, `.css`). + pub fn to_rgb8(self) -> [u8; 3] { + let to_u8 = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; + match self { + Self::Rgb { r, g, b } => [to_u8(r), to_u8(g), to_u8(b)], + Self::Gray { k } => { + let v = to_u8(1.0 - k.clamp(0.0, 1.0)); + [v, v, v] + } + Self::Cmyk { c, m, y, k } => { + let (c, m, y, k) = (c.clamp(0.0, 1.0), m.clamp(0.0, 1.0), y.clamp(0.0, 1.0), k.clamp(0.0, 1.0)); + [to_u8((1.0 - c) * (1.0 - k)), to_u8((1.0 - m) * (1.0 - k)), to_u8((1.0 - y) * (1.0 - k))] + } + Self::Lab { l, a, b } => lab_to_srgb8(l, a, b), + } + } +} + +fn lab_to_srgb8(l: f32, a: f32, b: f32) -> [u8; 3] { + let fy = (l + 16.0) / 116.0; + let fx = a / 500.0 + fy; + let fz = fy - b / 200.0; + let inv = |t: f32| { + if t > 6.0 / 29.0 { t * t * t } else { 3.0 * (6.0 / 29.0) * (6.0 / 29.0) * (t - 4.0 / 29.0) } + }; + // D65 white point + let x = 0.95047 * inv(fx); + let y = 1.00000 * inv(fy); + let z = 1.08883 * inv(fz); + let rl = 3.2404542 * x - 1.5371385 * y - 0.4985314 * z; + let gl = -0.969_266 * x + 1.8760108 * y + 0.0415560 * z; + let bl = 0.0556434 * x - 0.2040259 * y + 1.0572252 * z; + let gamma = |u: f32| { + let u = u.clamp(0.0, 1.0); + let s = if u <= 0.0031308 { 12.92 * u } else { 1.055 * u.powf(1.0 / 2.4) - 0.055 }; + (s.clamp(0.0, 1.0) * 255.0).round() as u8 + }; + [gamma(rl), gamma(gl), gamma(bl)] +} + +/// A single named colour swatch in a [`Palette`]. +#[derive(Clone, Debug, PartialEq)] +pub struct PaletteSwatch { + pub name: String, + pub color: PaletteColor, + pub global: bool, + pub spot: bool, +} + +/// A named group of swatches in a [`Palette`]. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct PaletteGroup { + pub name: String, + pub swatches: Vec, +} + +/// A colour palette / swatch library. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct Palette { + pub name: String, + pub swatches: Vec, + pub groups: Vec, +} + +/// Supported palette file formats for writing. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PaletteFormat { + /// `.gpl`: plain-text 8-bit RGB palette. + Gpl, + /// `.ase`: binary Adobe Swatch Exchange. + Ase, + /// `.css`: CSS custom properties on `:root` (write-only). + Css, +} + +impl PaletteFormat { + pub const ALL: [PaletteFormat; 3] = [PaletteFormat::Gpl, PaletteFormat::Ase, PaletteFormat::Css]; + + /// The format's id / file extension. + pub fn id(self) -> &'static str { + match self { + PaletteFormat::Gpl => "gpl", + PaletteFormat::Ase => "ase", + PaletteFormat::Css => "css", + } + } + + /// Parse a format id or extension (case-insensitive, leading dot allowed). + pub fn parse(s: &str) -> Option { + let s = s.trim_start_matches('.').to_ascii_lowercase(); + Self::ALL.into_iter().find(|f| f.id() == s) + } + + pub fn readable(self) -> bool { + self != PaletteFormat::Css + } + + pub fn binary(self) -> bool { + self == PaletteFormat::Ase + } +} + +const GPL_HEADER: &str = "GIMP Palette"; +const GPL_GROUP: &str = "# Group:"; +const ASE_SIGNATURE: &[u8] = b"ASEF"; +const ASE_GROUP_START: u16 = 0xC001; +const ASE_GROUP_END: u16 = 0xC002; +const ASE_COLOR: u16 = 0x0001; +const ASE_MAX_ENTRIES: usize = 100_000; +const ASE_MAX_NAME: usize = 1024; +const ASE_CUT_SHORT: &str = "the swatch exchange file is cut short"; +const ACB_SIGNATURE: &[u8] = b"8BCB"; +const ACB_CUT_SHORT: &str = "the color book is cut short"; +const ACB_MAX_NAME_BYTES: usize = 64 << 20; + +/// Structured palette failure for callers that supply localized messages. +/// +/// The existing string-returning entry points retain their English diagnostics. +/// Match these variants rather than parsing those strings when translating errors. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PaletteError { + UnsupportedFormat, + TruncatedAse, + TruncatedAcb, + AseWriteEntryLimit { limit: usize, entries: usize }, + AseReadEntryLimit { limit: usize }, + AseBlockCount, + AseBlockLength, + AseNameLength, + AseVersion { major: u16, minor: u16 }, + AcbVersion { version: u16 }, + AcbEntryLimit { limit: usize }, + AcbColorSpace { space: u16 }, + AcbNameBytes { limit: usize }, +} + +impl std::fmt::Display for PaletteError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::UnsupportedFormat => f.write_str("not a supported palette (.gpl, .ase, or .acb)"), + Self::TruncatedAse => f.write_str(ASE_CUT_SHORT), + Self::TruncatedAcb => f.write_str(ACB_CUT_SHORT), + Self::AseWriteEntryLimit { limit, entries } => { + write!(f, "at most {limit} colors and groups are supported in a swatch exchange file, and this library has {entries}") + } + Self::AseReadEntryLimit { limit } => write!(f, "the swatch exchange file holds more than {limit} colors and groups"), + Self::AseBlockCount => f.write_str("too many swatch exchange blocks"), + Self::AseBlockLength => f.write_str("a swatch exchange block is too long"), + Self::AseNameLength => f.write_str("a swatch exchange name is too long"), + Self::AseVersion { major, minor } => write!(f, "swatch exchange version {major}.{minor} isn't supported (only 1.x)"), + Self::AcbVersion { version } => write!(f, "color book version {version} isn't supported (only 1)"), + Self::AcbEntryLimit { limit } => write!(f, "the color book holds more than {limit} colors"), + Self::AcbColorSpace { space } => write!(f, "color books in color space {space} aren't supported (only RGB, CMYK and Lab)"), + Self::AcbNameBytes { limit } => write!(f, "the color book's names take more than {} MB", limit >> 20), + } + } +} + +impl std::error::Error for PaletteError {} + +/// Write `palette` in `format`. +pub fn write_palette(palette: &Palette, format: PaletteFormat) -> Result, String> { + write_palette_typed(palette, format).map_err(|error| error.to_string()) +} + +/// Write a palette, returning structured errors for caller-localized diagnostics. +pub fn write_palette_typed(palette: &Palette, format: PaletteFormat) -> Result, PaletteError> { + match format { + PaletteFormat::Gpl => Ok(write_gpl(palette).into_bytes()), + PaletteFormat::Ase => write_ase(palette), + PaletteFormat::Css => Ok(write_css(palette).into_bytes()), + } +} + +/// Read a palette from `bytes` (`.ase`, `.acb`, or `.gpl`, detected from content). +/// `fallback_name` is used when the file does not specify a library name. +pub fn read_palette(bytes: &[u8], fallback_name: &str) -> Result { + read_palette_typed(bytes, fallback_name).map_err(|error| error.to_string()) +} + +/// Read a palette, returning structured errors for caller-localized diagnostics. +/// Detection and fallback naming match [`read_palette`]. +pub fn read_palette_typed(bytes: &[u8], fallback_name: &str) -> Result { + if bytes.starts_with(ASE_SIGNATURE) { + return read_ase(bytes, fallback_name); + } + if bytes.starts_with(ACB_SIGNATURE) { + return read_acb(bytes, fallback_name).map_err(|e| if e == PaletteError::TruncatedAse { PaletteError::TruncatedAcb } else { e }); + } + let text = String::from_utf8_lossy(bytes); + let text = text.trim_start_matches('\u{feff}').trim_start(); + if text.starts_with(GPL_HEADER) { + return Ok(read_gpl(text, fallback_name)); + } + Err(PaletteError::UnsupportedFormat) +} + +/// Returns `true` if `bytes` looks like a `.gpl`, `.ase`, or `.acb` palette. +pub fn sniff_palette(bytes: &[u8]) -> bool { + if bytes.starts_with(ASE_SIGNATURE) || bytes.starts_with(ACB_SIGNATURE) { + return true; + } + let prefix = bytes.get(..1024).unwrap_or(bytes); + let text = String::from_utf8_lossy(prefix); + text.trim_start_matches('\u{feff}').trim_start().starts_with(GPL_HEADER) +} + +fn one_line(s: &str) -> String { + s.split(['\n', '\r', '\t']).collect::>().join(" ").trim().to_string() +} + +fn write_gpl(lib: &Palette) -> String { + let mut out = format!("{GPL_HEADER}\nName: {}\nColumns: 0\n#\n", one_line(&lib.name)); + let colors = |out: &mut String, list: &[PaletteSwatch]| { + for w in list { + let [r, g, b] = w.color.to_rgb8(); + out.push_str(&format!("{r:3} {g:3} {b:3}\t{}\n", one_line(&w.name))); + } + }; + colors(&mut out, &lib.swatches); + for g in &lib.groups { + out.push_str(&format!("{GPL_GROUP} {}\n", one_line(&g.name))); + colors(&mut out, &g.swatches); + } + out +} + +/// `name` as a CSS custom property (`--` and an escaped identifier): lower case, spaces as +/// hyphens, other characters an identifier can't hold escaped with a backslash. +pub fn css_property(name: &str) -> String { + let mut out = String::from("--"); + for word in name.split_whitespace() { + if out.len() > 2 { + out.push('-'); + } + for ch in word.chars().flat_map(char::to_lowercase) { + if ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' || !ch.is_ascii() { + out.push(ch); + } else if ch.is_ascii_graphic() { + out.push('\\'); + out.push(ch); + } else { + out.push_str(&format!("\\{:x} ", ch as u32)); + } + } + } + if out.len() == 2 { + out.push_str("swatch"); + } + out +} + +fn css_comment(s: &str) -> String { + one_line(s).replace("*/", "* /") +} + +fn write_css(lib: &Palette) -> String { + let mut out = format!("/* {} */\n:root {{\n", css_comment(&lib.name)); + let mut taken: HashSet = HashSet::new(); + let mut next: HashMap = HashMap::new(); + let mut props = |out: &mut String, list: &[PaletteSwatch]| { + for w in list { + let [r, g, b] = w.color.to_rgb8(); + let value = format!("#{r:02x}{g:02x}{b:02x}"); + let base = css_property(&w.name); + let name = if taken.insert(base.clone()) { + base + } else { + let n = next.entry(base.clone()).or_insert(1); + loop { + *n += 1; + let candidate = format!("{base}-{n}"); + if taken.insert(candidate.clone()) { + break candidate; + } + } + }; + let note = if w.spot { " /* spot */" } else { "" }; + out.push_str(&format!(" {name}: {value};{note}\n")); + } + }; + props(&mut out, &lib.swatches); + for g in &lib.groups { + out.push_str(&format!(" /* {} */\n", css_comment(&g.name))); + props(&mut out, &g.swatches); + } + out.push_str("}\n"); + out +} + +fn write_ase(lib: &Palette) -> Result, PaletteError> { + let entries = lib.swatches.len() + lib.groups.iter().map(|g| 1 + g.swatches.len()).sum::(); + if entries > ASE_MAX_ENTRIES { + return Err(PaletteError::AseWriteEntryLimit { limit: ASE_MAX_ENTRIES, entries }); + } + let mut body = vec![]; + let mut blocks = put_ase_colors(&mut body, &lib.swatches)?; + for g in &lib.groups { + let mut name = vec![]; + put_ase_name(&mut name, &g.name)?; + put_ase_block(&mut body, ASE_GROUP_START, &name)?; + blocks += 2 + put_ase_colors(&mut body, &g.swatches)?; + put_ase_block(&mut body, ASE_GROUP_END, &[])?; + } + let blocks = u32::try_from(blocks).map_err(|_| PaletteError::AseBlockCount)?; + let mut out = ASE_SIGNATURE.to_vec(); + out.extend(1u16.to_be_bytes()); + out.extend(0u16.to_be_bytes()); + out.extend(blocks.to_be_bytes()); + out.extend(body); + Ok(out) +} + +fn put_ase_colors(out: &mut Vec, list: &[PaletteSwatch]) -> Result { + let unit = |v: f32| if v.is_finite() { v.clamp(0.0, 1.0) } else { 0.0 }; + let ab = |v: f32| if v.is_finite() { v.clamp(-128.0, 127.0) } else { 0.0 }; + let mut n = 0; + for w in list { + let mut body = vec![]; + put_ase_name(&mut body, &w.name)?; + let (model, values): (&[u8; 4], Vec) = match w.color { + PaletteColor::Rgb { r, g, b } => (b"RGB ", vec![unit(r), unit(g), unit(b)]), + PaletteColor::Cmyk { c, m, y, k } => (b"CMYK", vec![unit(c), unit(m), unit(y), unit(k)]), + PaletteColor::Lab { l, a, b } => (b"LAB ", vec![unit(l / 100.0), ab(a), ab(b)]), + PaletteColor::Gray { k } => (b"Gray", vec![1.0 - unit(k)]), + }; + body.extend(model); + body.extend(values.iter().flat_map(|v| v.to_be_bytes())); + let kind: u16 = if w.spot { + 1 + } else if w.global { + 0 + } else { + 2 + }; + body.extend(kind.to_be_bytes()); + put_ase_block(out, ASE_COLOR, &body)?; + n += 1; + } + Ok(n) +} + +fn put_ase_block(out: &mut Vec, kind: u16, body: &[u8]) -> Result<(), PaletteError> { + let len = u32::try_from(body.len()).map_err(|_| PaletteError::AseBlockLength)?; + out.extend(kind.to_be_bytes()); + out.extend(len.to_be_bytes()); + out.extend_from_slice(body); + Ok(()) +} + +fn put_ase_name(out: &mut Vec, name: &str) -> Result<(), PaletteError> { + let mut units: Vec = vec![]; + for c in name.chars().filter(|&c| c != '\0') { + let mut buf = [0; 2]; + let encoded = c.encode_utf16(&mut buf); + if units.len() + encoded.len() > ASE_MAX_NAME { + break; + } + units.extend_from_slice(encoded); + } + units.push(0); + let count = u16::try_from(units.len()).map_err(|_| PaletteError::AseNameLength)?; + out.extend(count.to_be_bytes()); + out.extend(units.iter().flat_map(|u| u.to_be_bytes())); + Ok(()) +} + +fn read_gpl(text: &str, fallback: &str) -> Palette { + let mut lib = Palette { name: fallback.into(), ..Default::default() }; + let mut names = UniqueNames::default(); + for line in text.lines().skip(1) { + let line = line.trim(); + if let Some(g) = line.strip_prefix(GPL_GROUP) { + lib.groups.push(PaletteGroup { name: names.unique(group_name(g)), swatches: vec![] }); + continue; + } + if let Some(n) = line.strip_prefix("Name:") { + if !n.trim().is_empty() { + lib.name = n.trim().to_string(); + } + continue; + } + let words: Vec<&str> = line.split_whitespace().collect(); + let rgb: Vec = words.iter().take(3).map_while(|v| v.parse().ok()).collect(); + let [r, g, b] = rgb[..] else { continue }; + let rest = words[3..].join(" "); + let base = if rest.is_empty() { format!("R={r} G={g} B={b}") } else { rest }; + let w = PaletteSwatch { name: names.unique(base), color: PaletteColor::rgb8(r, g, b), global: false, spot: false }; + match lib.groups.last_mut() { + Some(grp) => grp.swatches.push(w), + None => lib.swatches.push(w), + } + } + lib +} + +fn read_ase(bytes: &[u8], fallback: &str) -> Result { + let mut data = AseData(bytes); + data.bytes(ASE_SIGNATURE.len())?; + let (major, minor) = (data.u16()?, data.u16()?); + if major != 1 { + return Err(PaletteError::AseVersion { major, minor }); + } + let blocks = data.u32()?; + let mut lib = Palette { name: fallback.into(), ..Default::default() }; + let mut names = UniqueNames::default(); + let (mut in_group, mut entries) = (false, 0usize); + for _ in 0..blocks { + let kind = data.u16()?; + let len = usize::try_from(data.u32()?).unwrap_or(usize::MAX); + let mut body = AseData(data.bytes(len)?); + if matches!(kind, ASE_GROUP_START | ASE_COLOR) { + entries += 1; + if entries > ASE_MAX_ENTRIES { + return Err(PaletteError::AseReadEntryLimit { limit: ASE_MAX_ENTRIES }); + } + } + match kind { + ASE_GROUP_START => { + lib.groups.push(PaletteGroup { name: names.unique(group_name(&body.name()?)), swatches: vec![] }); + in_group = true; + } + ASE_GROUP_END => in_group = false, + ASE_COLOR => { + let Some(mut w) = ase_swatch(&mut body)? else { continue }; + w.name = names.unique(std::mem::take(&mut w.name)); + match lib.groups.last_mut().filter(|_| in_group) { + Some(g) => g.swatches.push(w), + None => lib.swatches.push(w), + } + } + _ => {} + } + } + Ok(lib) +} + +fn read_acb(bytes: &[u8], fallback: &str) -> Result { + let mut data = AseData(bytes); + data.bytes(ACB_SIGNATURE.len())?; + let version = data.u16()?; + if version != 1 { + return Err(PaletteError::AcbVersion { version }); + } + let _book_id = data.u16()?; + let [title, prefix, suffix, _description] = [data.acb_string()?, data.acb_string()?, data.acb_string()?, data.acb_string()?]; + let count = usize::from(data.u16()?); + if count > ASE_MAX_ENTRIES { + return Err(PaletteError::AcbEntryLimit { limit: ASE_MAX_ENTRIES }); + } + let (_page_size, _page_offset) = (data.u16()?, data.u16()?); + let space = data.u16()?; + let mut colors = Vec::with_capacity(count); + for _ in 0..count { + let name = data.acb_string()?; + let _code = data.bytes(6)?; + let color = match space { + 0 => { + let [r, g, b] = data.array::<3>()?; + PaletteColor::rgb8(r, g, b) + } + 2 => { + let ink = |v: u8| f32::from(255 - v) / 255.0; + let [c, m, y, k] = data.array::<4>()?; + PaletteColor::cmyk(ink(c), ink(m), ink(y), ink(k)) + } + 7 => { + let [l, a, b] = data.array::<3>()?; + PaletteColor::lab(f32::from(l) * 100.0 / 255.0, f32::from(a) - 128.0, f32::from(b) - 128.0) + } + other => return Err(PaletteError::AcbColorSpace { space: other }), + }; + colors.push((name, color)); + } + let spot = data.0.windows(8).last() != Some(b"spflproc".as_slice()); + let mut lib = Palette { name: if title.is_empty() { fallback.into() } else { title }, ..Default::default() }; + let mut names = UniqueNames::default(); + let mut kept = 0usize; + for (name, color) in colors.into_iter().filter(|(n, _)| !n.trim().is_empty()) { + kept = kept.saturating_add(2 * (prefix.len() + name.len() + suffix.len())); + if kept > ACB_MAX_NAME_BYTES { + return Err(PaletteError::AcbNameBytes { limit: ACB_MAX_NAME_BYTES }); + } + let name = names.unique(format!("{prefix}{name}{suffix}").trim().to_string()); + lib.swatches.push(PaletteSwatch { name, color, global: spot, spot }); + } + Ok(lib) +} + +fn ase_swatch(body: &mut AseData) -> Result, PaletteError> { + let name = body.name()?; + let unit = |v: f32| v.clamp(0.0, 1.0); + let color = match &body.array::<4>()? { + b"RGB " => { + let [r, g, b] = [body.f32()?, body.f32()?, body.f32()?]; + PaletteColor::rgb(unit(r), unit(g), unit(b)) + } + b"CMYK" => { + let [c, m, y, k] = [body.f32()?, body.f32()?, body.f32()?, body.f32()?]; + PaletteColor::cmyk(unit(c), unit(m), unit(y), unit(k)) + } + b"LAB " => { + let [l, a, b] = [body.f32()?, body.f32()?, body.f32()?]; + let l = if l > 1.0 { l.min(100.0) } else { unit(l) * 100.0 }; + PaletteColor::lab(l, a.clamp(-128.0, 127.0), b.clamp(-128.0, 127.0)) + } + b"Gray" => PaletteColor::gray(1.0 - unit(body.f32()?)), + _ => return Ok(None), + }; + let kind = if body.0.len() >= 2 { body.u16()? } else { 2 }; + let (global, spot) = match kind { + 0 => (true, false), + 1 => (true, true), + _ => (false, false), + }; + let name = if name.is_empty() { value_name(color) } else { name }; + Ok(Some(PaletteSwatch { name, color, global, spot })) +} + +fn value_name(c: PaletteColor) -> String { + let pct = |v: f32| (v * 100.0).round(); + let byte = |v: f32| (v * 255.0).round(); + match c { + PaletteColor::Cmyk { c, m, y, k } => format!("C={} M={} Y={} K={}", pct(c), pct(m), pct(y), pct(k)), + PaletteColor::Rgb { r, g, b } => format!("R={} G={} B={}", byte(r), byte(g), byte(b)), + PaletteColor::Gray { k } => format!("Gray K={}", pct(k)), + PaletteColor::Lab { l, a, b } => format!("L={} a={} b={}", l.round() + 0.0, a.round() + 0.0, b.round() + 0.0), + } +} + +struct AseData<'a>(&'a [u8]); + +impl<'a> AseData<'a> { + fn bytes(&mut self, n: usize) -> Result<&'a [u8], PaletteError> { + let (head, rest) = self.0.split_at_checked(n).ok_or(PaletteError::TruncatedAse)?; + self.0 = rest; + Ok(head) + } + fn array(&mut self) -> Result<[u8; N], PaletteError> { + let (head, rest) = self.0.split_first_chunk::().ok_or(PaletteError::TruncatedAse)?; + self.0 = rest; + Ok(*head) + } + fn u16(&mut self) -> Result { + self.array().map(u16::from_be_bytes) + } + fn u32(&mut self) -> Result { + self.array().map(u32::from_be_bytes) + } + fn f32(&mut self) -> Result { + let v = f32::from_be_bytes(self.array()?); + Ok(if v.is_finite() { v } else { 0.0 }) + } + fn acb_string(&mut self) -> Result { + let n = usize::try_from(self.u32()?).unwrap_or(usize::MAX); + let (units, _) = self.bytes(n.checked_mul(2).ok_or(PaletteError::TruncatedAse)?)?.as_chunks::<2>(); + let units: Vec = units.iter().take(ASE_MAX_NAME).map(|u| u16::from_be_bytes(*u)).take_while(|&u| u != 0).collect(); + let s = String::from_utf16_lossy(&units); + let s = match s.strip_prefix("$$$/") { + Some(key) => key.split_once('=').map_or("", |(_, text)| text).to_string(), + None => s, + }; + Ok(s.replace("^R", "®").replace("^C", "©")) + } + fn name(&mut self) -> Result { + if self.0.is_empty() { + return Ok(String::new()); + } + let n = usize::from(self.u16()?); + let (units, _) = self.bytes(n * 2)?.as_chunks::<2>(); + let units: Vec = units.iter().take(ASE_MAX_NAME).map(|u| u16::from_be_bytes(*u)).take_while(|&u| u != 0).collect(); + Ok(String::from_utf16_lossy(&units).trim().to_string()) + } +} + +fn group_name(name: &str) -> String { + let name = name.trim(); + if name.is_empty() { "Color Group".into() } else { name.to_string() } +} + +#[derive(Default)] +struct UniqueNames { + taken: HashSet, + last: HashMap, +} + +impl UniqueNames { + fn unique(&mut self, base: String) -> String { + if self.taken.insert(base.clone()) { + return base; + } + let n = self.last.entry(base.clone()).or_insert(1); + loop { + *n += 1; + let name = format!("{base} {n}"); + if self.taken.insert(name.clone()) { + return name; + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn gpl_roundtrip() { + let pal = Palette { + name: "Test Palette".into(), + swatches: vec![PaletteSwatch { name: "Red".into(), color: PaletteColor::rgb8(255, 0, 0), global: false, spot: false }], + groups: vec![PaletteGroup { + name: "Blues".into(), + swatches: vec![PaletteSwatch { name: "Navy".into(), color: PaletteColor::rgb8(0, 0, 128), global: false, spot: false }], + }], + }; + let bytes = write_palette(&pal, PaletteFormat::Gpl).expect("write gpl"); + assert!(sniff_palette(&bytes)); + let parsed = read_palette(&bytes, "fallback").expect("read gpl"); + assert_eq!(parsed.name, "Test Palette"); + assert_eq!(parsed.swatches.len(), 1); + assert_eq!(parsed.swatches[0].name, "Red"); + assert_eq!(parsed.groups.len(), 1); + assert_eq!(parsed.groups[0].name, "Blues"); + assert_eq!(parsed.groups[0].swatches[0].name, "Navy"); + } + + #[test] + fn typed_errors_distinguish_formats_and_preserve_legacy_messages() { + for (bytes, expected) in [ + (b"no palette".as_slice(), PaletteError::UnsupportedFormat), + (b"ASEF".as_slice(), PaletteError::TruncatedAse), + (b"8BCB".as_slice(), PaletteError::TruncatedAcb), + (b"8BCB\0\x01\0\0\0\0\0\x03\0x".as_slice(), PaletteError::TruncatedAcb), + (b"ASEF\0\x02\0\x03".as_slice(), PaletteError::AseVersion { major: 2, minor: 3 }), + (b"8BCB\0\x02".as_slice(), PaletteError::AcbVersion { version: 2 }), + ] { + assert_eq!(read_palette_typed(bytes, "fallback"), Err(expected)); + assert_eq!(read_palette(bytes, "fallback"), Err(expected.to_string())); + } + assert_eq!(read_palette(b"8BCB", "fallback"), Err("the color book is cut short".into())); + } + + #[test] + fn structured_error_display_preserves_legacy_wording() { + let cases = [ + (PaletteError::UnsupportedFormat, "not a supported palette (.gpl, .ase, or .acb)"), + (PaletteError::TruncatedAse, "the swatch exchange file is cut short"), + (PaletteError::TruncatedAcb, "the color book is cut short"), + ( + PaletteError::AseWriteEntryLimit { limit: 3, entries: 4 }, + "at most 3 colors and groups are supported in a swatch exchange file, and this library has 4", + ), + (PaletteError::AseReadEntryLimit { limit: 3 }, "the swatch exchange file holds more than 3 colors and groups"), + (PaletteError::AseBlockCount, "too many swatch exchange blocks"), + (PaletteError::AseBlockLength, "a swatch exchange block is too long"), + (PaletteError::AseNameLength, "a swatch exchange name is too long"), + (PaletteError::AseVersion { major: 2, minor: 3 }, "swatch exchange version 2.3 isn't supported (only 1.x)"), + (PaletteError::AcbVersion { version: 2 }, "color book version 2 isn't supported (only 1)"), + (PaletteError::AcbEntryLimit { limit: 3 }, "the color book holds more than 3 colors"), + (PaletteError::AcbColorSpace { space: 5 }, "color books in color space 5 aren't supported (only RGB, CMYK and Lab)"), + (PaletteError::AcbNameBytes { limit: 64 << 20 }, "the color book's names take more than 64 MB"), + ]; + for (error, expected) in cases { + assert_eq!(error.to_string(), expected); + } + } + + #[test] + fn typed_write_limit_exposes_counts_without_parsing_english() { + let palette = Palette { groups: vec![PaletteGroup::default(); ASE_MAX_ENTRIES + 1], ..Default::default() }; + let error = PaletteError::AseWriteEntryLimit { limit: ASE_MAX_ENTRIES, entries: ASE_MAX_ENTRIES + 1 }; + assert_eq!(write_palette_typed(&palette, PaletteFormat::Ase), Err(error)); + assert_eq!(write_palette(&palette, PaletteFormat::Ase), Err(error.to_string())); + } + + #[test] + fn typed_success_keeps_legacy_bytes_and_palette() { + let palette = Palette { + name: "typed".into(), + swatches: vec![PaletteSwatch { name: "red".into(), color: PaletteColor::rgb8(255, 0, 0), global: false, spot: false }], + groups: vec![], + }; + assert_eq!( + write_palette_typed(&palette, PaletteFormat::Ase).expect("ASE golden"), + b"ASEF\0\x01\0\0\0\0\0\x01\0\x01\0\0\0\x1c\0\x04\0r\0e\0d\0\0RGB \x3f\x80\0\0\0\0\0\0\0\0\0\0\0\x02" + ); + for format in PaletteFormat::ALL { + let typed = write_palette_typed(&palette, format).expect("typed write"); + assert_eq!(write_palette(&palette, format), Ok(typed.clone())); + if format.readable() { + assert_eq!(read_palette(&typed, "typed"), read_palette_typed(&typed, "typed").map_err(|error| error.to_string())); + } + } + } + + #[test] + fn ase_roundtrip() { + let pal = Palette { + name: "ASE".into(), + swatches: vec![ + PaletteSwatch { name: "Spot Red".into(), color: PaletteColor::rgb(1.0, 0.0, 0.0), global: true, spot: true }, + PaletteSwatch { name: "CMYK Cyan".into(), color: PaletteColor::cmyk(1.0, 0.0, 0.0, 0.0), global: true, spot: false }, + PaletteSwatch { name: "Lab Mid".into(), color: PaletteColor::lab(50.0, 10.0, -20.0), global: false, spot: false }, + PaletteSwatch { name: "Gray 40".into(), color: PaletteColor::gray(0.4), global: false, spot: false }, + ], + groups: vec![PaletteGroup { + name: "Group 1".into(), + swatches: vec![PaletteSwatch { name: "Green".into(), color: PaletteColor::rgb(0.0, 1.0, 0.0), global: false, spot: false }], + }], + }; + let bytes = write_palette(&pal, PaletteFormat::Ase).expect("write ase"); + assert!(sniff_palette(&bytes)); + let parsed = read_palette(&bytes, "ASE").expect("read ase"); + assert_eq!(parsed.swatches.len(), 4); + assert!(parsed.swatches[0].spot && parsed.swatches[0].global); + assert!(!parsed.swatches[1].spot && parsed.swatches[1].global); + assert_eq!(parsed.groups.len(), 1); + assert_eq!(parsed.groups[0].name, "Group 1"); + } + + #[test] + fn css_output() { + let pal = Palette { + name: "Brand".into(), + swatches: vec![ + PaletteSwatch { name: "Primary Blue".into(), color: PaletteColor::rgb8(0, 102, 204), global: true, spot: true }, + PaletteSwatch { name: "Primary Blue".into(), color: PaletteColor::rgb8(0, 0, 0), global: false, spot: false }, + ], + groups: vec![], + }; + let bytes = write_palette(&pal, PaletteFormat::Css).expect("write css"); + let css = String::from_utf8(bytes).expect("utf8"); + assert!(css.contains("--primary-blue: #0066cc; /* spot */")); + assert!(css.contains("--primary-blue-2: #000000;")); + } + + #[test] + fn acb_read() { + let mut acb = Vec::new(); + acb.extend_from_slice(b"8BCB"); + acb.extend_from_slice(&1u16.to_be_bytes()); + acb.extend_from_slice(&3000u16.to_be_bytes()); + let put_str = |buf: &mut Vec, s: &str| { + let units: Vec = s.encode_utf16().collect(); + buf.extend_from_slice(&(units.len() as u32).to_be_bytes()); + for u in units { + buf.extend_from_slice(&u.to_be_bytes()); + } + }; + put_str(&mut acb, "$$$/Book/Title=Pantone^R"); + put_str(&mut acb, "PANTONE "); + put_str(&mut acb, " C"); + put_str(&mut acb, ""); + acb.extend_from_slice(&1u16.to_be_bytes()); // count + acb.extend_from_slice(&7u16.to_be_bytes()); // page size + acb.extend_from_slice(&0u16.to_be_bytes()); // page offset + acb.extend_from_slice(&0u16.to_be_bytes()); // RGB + put_str(&mut acb, "185"); + acb.extend_from_slice(b"000185"); + acb.extend_from_slice(&[228, 0, 43]); + acb.extend_from_slice(b"spflspot"); + + assert!(sniff_palette(&acb)); + let parsed = read_palette(&acb, "fallback").expect("read acb"); + assert_eq!(parsed.name, "Pantone®"); + assert_eq!(parsed.swatches.len(), 1); + assert_eq!(parsed.swatches[0].name, "PANTONE 185 C"); + assert!(parsed.swatches[0].spot); + } +} diff --git a/crates/cms/src/synth.rs b/crates/cms/src/synth.rs index b1535f78c2..d192127ec4 100644 --- a/crates/cms/src/synth.rs +++ b/crates/cms/src/synth.rs @@ -19,6 +19,9 @@ //! L* is scaled onto the device black, chroma is softly compressed against a gamut boundary //! descriptor sampled from the forward model — then solved as above. //! +//! That is [`GamutMapping::Legacy`]. [`GamutMapping::Oklab`] maps out-of-gamut colours in Oklab +//! instead and gives saturation its own table; the Coated FOGRA39 built-in uses it. +//! //! The model is plausible for coated offset, not a measured characterisation: use a real //! profile from your print provider for production print. //! @@ -49,6 +52,11 @@ pub struct CmykParams { pub k_max: f64, /// Exponent of the black generation curve above `gcr_start`. pub gcr_gamma: f64, + /// Black generation as a curve of `[gray component, black]` points with increasing gray + /// components in `[0, 1]`, interpolated linearly and held flat beyond the ends. Empty by + /// default: black is then `k_max` times the ramp of `gcr_start` and `gcr_gamma` (the curve + /// of the shipped Photocraft profile). + pub gcr_curve: Vec<[f64; 2]>, /// Tone value increase at 50 % for C, M, Y, K. pub tvi: [f64; 4], /// Grid points of the AToB (4D) and BToA (3D) tables. @@ -60,6 +68,32 @@ pub struct CmykParams { /// encoding and separates to a trace of ink. Off by default, which keeps the bytes of the /// shipped Photocraft profile. pub white_on_node: bool, + /// How the BToA tables bring colours into the gamut. [`GamutMapping::Legacy`] by default, + /// which keeps the bytes of the shipped Photocraft profile. + pub mapping: GamutMapping, +} + +/// How the BToA tables map colours the printer cannot reach. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum GamutMapping { + /// The shipped Photocraft profile: out-of-gamut colours land on the closest reachable + /// colour (ΔE76); perceptual scales L* onto the device black and compresses Lab chroma; + /// saturation shares the perceptual table. + Legacy, + /// Gamut mapping in Oklab, whose hue lines stay perceptually constant (a CIELAB hue line + /// from a saturated blue toward grey drifts toward purple; an Oklab one does not): + /// + /// * **Colorimetric**: in-gamut colours separate exactly. An out-of-gamut colour takes the + /// closest colour the printer reaches in Oklab, lightness and hue weighted above chroma, + /// and more so the further outside it is; black is generated from the clipped colour, so + /// inks the clip drives to 100 % stay there. + /// * **Perceptual** and **saturation**: the ICC v4 perceptual reference black is mapped + /// onto the device black linearly in XYZ (as black point compensation does) for neutrals, + /// fading out with Oklab chroma, so greys separate as with colorimetric + BPC and colourful + /// colours keep their lightness. Perceptual then compresses Oklab chroma above `knee` × + /// the gamut boundary into the boundary; saturation scales Oklab chroma by `saturation` + /// and clips as colorimetric does. + Oklab { knee: f64, saturation: f64 }, } impl Default for CmykParams { @@ -71,10 +105,12 @@ impl Default for CmykParams { gcr_start: 0.2, k_max: 0.95, gcr_gamma: 1.1, + gcr_curve: Vec::new(), tvi: [0.14, 0.14, 0.14, 0.17], grid_a2b: 11, grid_b2a: 21, white_on_node: false, + mapping: GamutMapping::Legacy, } } } @@ -183,6 +219,11 @@ impl Forward for TableForward { /// Bounded least-squares solve for C, M, Y given K, minimising ΔE76 to `target`, subject to /// `C + M + Y ≤ limit` (projected Levenberg–Marquardt with a numerical Jacobian). fn solve_cmy(model: &F, target: [f64; 3], k: f64, limit: f64, seeds: &[[f64; 3]]) -> ([f64; 3], f64) { + solve_cmy_by(model, k, limit, seeds, |l| [l[0] - target[0], l[1] - target[1], l[2] - target[2]]) +} + +/// [`solve_cmy`] minimising the norm of `residual(lab)` instead of ΔE76. +fn solve_cmy_by(model: &F, k: f64, limit: f64, seeds: &[[f64; 3]], residual: impl Fn([f64; 3]) -> [f64; 3]) -> ([f64; 3], f64) { let project = |mut x: [f64; 3]| { for _ in 0..4 { for v in &mut x { @@ -202,10 +243,7 @@ fn solve_cmy(model: &F, target: [f64; 3], k: f64, limit: f6 } x }; - let err = |x: [f64; 3]| { - let l = model.lab([x[0], x[1], x[2], k]); - [l[0] - target[0], l[1] - target[1], l[2] - target[2]] - }; + let err = |x: [f64; 3]| residual(model.lab([x[0], x[1], x[2], k])); let norm2 = |e: [f64; 3]| e[0] * e[0] + e[1] * e[1] + e[2] * e[2]; let mut best = ([0.0; 3], f64::INFINITY); for seed in seeds { @@ -275,13 +313,32 @@ fn solve_cmy(model: &F, target: [f64; 3], k: f64, limit: f6 const SEEDS: [[f64; 3]; 5] = [[0.3, 0.3, 0.3], [0.05, 0.05, 0.05], [0.8, 0.8, 0.8], [0.9, 0.2, 0.2], [0.2, 0.9, 0.9]]; +/// Black for a gray component `g` (the smallest of C, M, Y without black). +fn gcr(p: &CmykParams, g: f64) -> f64 { + let Some(first) = p.gcr_curve.first() else { + // The Photocraft profile's curve. + return if g <= p.gcr_start { 0.0 } else { p.k_max * ((g - p.gcr_start) / (1.0 - p.gcr_start)).powf(p.gcr_gamma) }; + }; + let mut k = first[1]; + for w in p.gcr_curve.windows(2) { + let ([g0, k0], [g1, k1]) = (w[0], w[1]); + if g >= g1 { + k = k1; + } else if g > g0 { + k = k0 + (k1 - k0) * (g - g0) / (g1 - g0); + break; + } + } + k.clamp(0.0, 1.0) +} + /// Colorimetric separation of a relative Lab target: GCR black generation then a CMY solve. pub fn separate(model: &F, p: &CmykParams, target: [f64; 3], warm: Option<[f64; 4]>) -> ([f64; 4], f64) { let mut seeds: Vec<[f64; 3]> = warm.map(|w| vec![[w[0], w[1], w[2]]]).unwrap_or_default(); seeds.extend_from_slice(&SEEDS); let (cmy0, _) = solve_cmy(model, target, 0.0, 3.0, &seeds); let g = cmy0[0].min(cmy0[1]).min(cmy0[2]); - let k = if g <= p.gcr_start { 0.0 } else { p.k_max * ((g - p.gcr_start) / (1.0 - p.gcr_start)).powf(p.gcr_gamma) }; + let k = gcr(p, g); let mut seeds2 = vec![[(cmy0[0] - k).max(0.0), (cmy0[1] - k).max(0.0), (cmy0[2] - k).max(0.0)], cmy0]; seeds2.extend_from_slice(&seeds); let (cmy, de) = solve_cmy(model, target, k, (p.tac - k).max(0.0), &seeds2); @@ -383,6 +440,187 @@ impl Gbd { } } +/// ΔE76 under which a separation counts as reaching its target. +const REACH: f64 = 1.5; +/// Oklab chroma beyond any printable colour: the top of the boundary search. +const OKLAB_CHROMA_MAX: f64 = 0.5; +/// Oklab chroma above which the perceptual and saturation tables no longer lift black: they lift +/// neutrals fully, and less the more colourful a colour is. +const LIFT_CHROMA: f64 = 0.1; +/// Weight of lightness and hue against chroma when a colour just outside the gamut is clipped. +const CLIP_WEIGHT: f64 = 1.5; +/// Growth of that weight with the squared share of chroma the clip loses. +const CLIP_WEIGHT_GROWTH: f64 = 200.0; + +// Oklab (Björn Ottosson, 2020): XYZ (D65) → cone response → cube root → Lab-like axes. +const OKLAB_M1: math::Mat3 = + [[0.8189330101, 0.3618667424, -0.1288597137], [0.0329845436, 0.9293118715, 0.0361456387], [0.0482003018, 0.2643662691, 0.6338517070]]; +const OKLAB_M2: math::Mat3 = + [[0.2104542553, 0.7936177850, -0.0040720468], [1.9779984951, -2.4285922050, 0.4505937099], [0.0259040371, 0.7827717662, -0.8086757660]]; +const OKLAB_M1_INV: math::Mat3 = + [[1.2270138511, -0.5577999807, 0.2812561490], [-0.0405801784, 1.1122568696, -0.0716766787], [-0.0763812845, -0.4214819784, 1.5861632204]]; +const OKLAB_M2_INV: math::Mat3 = [[1.0, 0.3963377774, 0.2158037573], [1.0, -0.1055613458, -0.0638541728], [1.0, -0.0894841775, -1.2914855480]]; + +/// The [`GamutMapping::Oklab`] separations of one forward model. +struct OklabMapper<'a, F: Forward + ?Sized> { + model: &'a F, + p: &'a CmykParams, + knee: f64, + saturation: f64, + /// XYZ scale that maps the v4 perceptual reference black onto the device black (the white + /// stays fixed): `v' = a·v + w·(1 − a)`. + black_scale: f64, + to_d65: math::Mat3, + to_d50: math::Mat3, +} + +impl<'a, F: Forward + ?Sized> OklabMapper<'a, F> { + fn new(model: &'a F, p: &'a CmykParams, knee: f64, saturation: f64) -> Self { + let d65 = math::xy_to_xyz(math::D65_XY); + // The darkest neutral the printer reaches. + let (black, _) = separate(model, p, [0.0, 0.0, 0.0], None); + let device = math::lab_to_xyz([model.lab(black)[0], 0.0, 0.0], math::D50)[1]; + let reference = crate::transform::PERCEPTUAL_BLACK[1]; + OklabMapper { + model, + p, + knee, + saturation, + black_scale: (1.0 - device) / (1.0 - reference), + to_d65: math::bradford(math::D50, d65), + to_d50: math::bradford(d65, math::D50), + } + } + + fn oklab(&self, lab: [f64; 3]) -> [f64; 3] { + let xyz = math::apply(&self.to_d65, math::lab_to_xyz(lab, math::D50)); + math::apply(&OKLAB_M2, math::apply(&OKLAB_M1, xyz).map(f64::cbrt)) + } + + fn lab(&self, ok: [f64; 3]) -> [f64; 3] { + let lms = math::apply(&OKLAB_M2_INV, ok).map(|v| v * v * v); + math::xyz_to_lab(math::apply(&self.to_d50, math::apply(&OKLAB_M1_INV, lms)), math::D50) + } + + /// The most chroma (scale of `ok`'s chroma in `[0, top]`) the printer reaches at `ok`'s + /// lightness and hue, and its separation; `None` when even the neutral is out of reach. + fn boundary(&self, ok: [f64; 3], top: f64, warm: Option<[f64; 4]>) -> Option<(f64, [f64; 4])> { + let (grey, d) = separate(self.model, self.p, self.lab([ok[0], 0.0, 0.0]), warm); + if d > REACH { + return None; + } + let (mut lo, mut hi, mut best) = (0.0, top, grey); + for _ in 0..14 { + let mid = 0.5 * (lo + hi); + let (c, d) = separate(self.model, self.p, self.lab([ok[0], ok[1] * mid, ok[2] * mid]), Some(best)); + if d <= REACH { + lo = mid; + best = c; + } else { + hi = mid; + } + } + Some((lo, best)) + } + + /// Colorimetric: exact in gamut; outside, the closest colour the printer reaches in Oklab, + /// lightness and hue weighted above chroma, and more so the further outside the colour is. + fn clip(&self, lab: [f64; 3], warm: Option<[f64; 4]>) -> [f64; 4] { + let (cmyk, de) = separate(self.model, self.p, lab, warm); + if de <= REACH { + return cmyk; + } + let ok = self.oklab(lab); + let c0 = ok[1].hypot(ok[2]); + // Unit vectors along the colour's chroma and hue directions. + let (u, v) = if c0 > 1e-9 { ([ok[1] / c0, ok[2] / c0], [-ok[2] / c0, ok[1] / c0]) } else { ([1.0, 0.0], [0.0, 1.0]) }; + // Lightness and hue errors weighted by `w`, chroma error by 1; scaled by 100 so the + // solver's step tolerances suit it as they suit ΔE76. + let residual = |w: f64| { + move |l: [f64; 3]| { + let o = self.oklab(l); + let (da, db) = (o[1] - ok[1], o[2] - ok[2]); + [100.0 * w * (o[0] - ok[0]), 100.0 * (da * u[0] + db * u[1]), 100.0 * w * (da * v[0] + db * v[1])] + } + }; + let solve = |w: f64, k: f64, from: [f64; 3]| { + let mut seeds = vec![from]; + seeds.extend_from_slice(&SEEDS); + solve_cmy_by(self.model, k, (self.p.tac - k).max(0.0), &seeds, residual(w)).0 + }; + // A first clip without black measures how far outside the colour is. A colour just + // outside may give up a little lightness and hue to keep its chroma (a red reaches the + // full magenta and yellow edge); one far outside keeps its lightness and hue and loses + // chroma (a saturated blue does not slide along the gamut surface). + let first = solve(CLIP_WEIGHT, 0.0, [cmyk[0], cmyk[1], cmyk[2]]); + let o = self.oklab(self.model.lab([first[0], first[1], first[2], 0.0])); + let loss = if c0 > 1e-9 { (1.0 - o[1].hypot(o[2]) / c0).clamp(0.0, 1.0) } else { 0.0 }; + let w = CLIP_WEIGHT * (1.0 + CLIP_WEIGHT_GROWTH * loss * loss); + let free = solve(w, 0.0, first); + let norm = |c: [f64; 4]| residual(w)(self.model.lab(c)).iter().map(|v| v * v).sum::(); + // Candidates, the closest kept: the separation itself (a colour it nearly reaches stays + // continuous with its in-gamut neighbours), the clip with the separation's black, and + // the clip with black generated from the black-free clip's gray component (inks the + // clip drives to 100 % stay there and black replaces the rest). + let mut best = (norm(cmyk), cmyk); + for k in [cmyk[3], gcr(self.p, free[0].min(free[1]).min(free[2]))] { + for from in [[cmyk[0], cmyk[1], cmyk[2]], free] { + let cmy = solve(w, k, from); + let c = [cmy[0], cmy[1], cmy[2], k]; + let n = norm(c); + if n < best.0 { + best = (n, c); + } + } + } + // Near paper white the gamut narrows to a point: the separation's closest colour keeps + // light tints' chroma where holding lightness would drop them toward bare paper, and + // the table nodes there carry the in-gamut tints between them. The inks blend from the + // clip at L* 85 to the separation at L* 95. + let clip_share = ((95.0 - lab[0]) / 10.0).clamp(0.0, 1.0); + std::array::from_fn(|i| clip_share * best.1[i] + (1.0 - clip_share) * cmyk[i]) + } + + /// Maps the v4 perceptual reference black onto the device black, linearly in XYZ, for + /// neutrals; colourful colours keep their lightness. + fn lift_black(&self, lab: [f64; 3]) -> [f64; 3] { + let ok = self.oklab(lab); + let neutral = (1.0 - ok[1].hypot(ok[2]) / LIFT_CHROMA).clamp(0.0, 1.0); + let a = 1.0 - neutral * (1.0 - self.black_scale); + let xyz = math::lab_to_xyz(lab, math::D50); + math::xyz_to_lab(std::array::from_fn(|i| a * xyz[i] + math::D50[i] * (1.0 - a)), math::D50) + } + + fn perceptual(&self, lab: [f64; 3], warm: Option<[f64; 4]>) -> [f64; 4] { + let lab = self.lift_black(lab); + let ok = self.oklab(lab); + let c = ok[1].hypot(ok[2]); + if c < 1e-9 { + return self.clip(lab, warm); + } + let Some((s, edge)) = self.boundary(ok, OKLAB_CHROMA_MAX / c, warm) else { + return self.clip(lab, warm); + }; + // Chroma above the knee is compressed smoothly into the boundary chroma `cb`. + let cb = s * c; + let knee = self.knee * cb; + if c <= knee { + return self.clip(lab, warm); + } + let c2 = knee + (cb - knee) * ((c - knee) / (cb - knee).max(1e-9)).tanh(); + let f = c2 / c; + if f >= s { + return edge; + } + self.clip(self.lab([ok[0], ok[1] * f, ok[2] * f]), Some(edge)) + } + + fn saturation(&self, lab: [f64; 3], warm: Option<[f64; 4]>) -> [f64; 4] { + let ok = self.oklab(self.lift_black(lab)); + self.clip(self.lab([ok[0], ok[1] * self.saturation, ok[2] * self.saturation]), warm) + } +} + fn lab_node_legacy(i: usize, n: usize) -> f64 { i as f64 / (n - 1) as f64 } @@ -419,6 +657,18 @@ fn white_node_curve(l_axis: bool, x: f64) -> f64 { } fn b2a_table(model: &F, p: &CmykParams, gbd: Option<&Gbd>) -> Result { + b2a_table_with(p, |lab, warm| { + let target = match gbd { + Some(g) => g.compress(lab), + None => lab, + }; + separate(model, p, target, warm).0 + }) +} + +/// Samples a BToA table: `node` separates each relative Lab node, warm-started from the +/// previous node along b. +fn b2a_table_with(p: &CmykParams, node: impl Fn([f64; 3], Option<[f64; 4]>) -> [f64; 4] + Sync) -> Result { let n = p.grid_b2a.max(2); let nodes = n * n * n; let step = (n - 1) as f64; @@ -434,11 +684,7 @@ fn b2a_table(model: &F, p: &CmykParams, gbd: Option<&Gbd>) } else { decode_v2([lab_node_legacy(li, n), lab_node_legacy(ai, n), lab_node_legacy(bi, n)]) }; - let target = match gbd { - Some(g) => g.compress(lab), - None => lab, - }; - let (cmyk, _) = if p.white_on_node && lab == [100.0, 0.0, 0.0] { ([0.0; 4], 0.0) } else { separate(model, p, target, warm) }; + let cmyk = if p.white_on_node && lab == [100.0, 0.0, 0.0] { [0.0; 4] } else { node(lab, warm) }; warm = Some(cmyk); out.extend(cmyk.map(|v| v as f32)); } @@ -460,6 +706,15 @@ fn check_grids(p: &CmykParams) -> Result<(), CmsError> { if p.white_on_node && p.grid_b2a.is_multiple_of(2) { return Err(invalid_arg(format!("white on a node needs an odd BToA grid, not {}", p.grid_b2a))); } + let valid = |v: &[f64; 2]| v.iter().all(|x| (0.0..=1.0).contains(x)); + if !p.gcr_curve.iter().all(valid) || p.gcr_curve.windows(2).any(|w| w[1][0] <= w[0][0]) { + return Err(invalid_arg(format!("black generation curve {:?} needs points in [0, 1] with increasing gray components", p.gcr_curve))); + } + if let GamutMapping::Oklab { knee, saturation } = p.mapping + && !((0.0..1.0).contains(&knee) && saturation.is_finite() && saturation > 0.0) + { + return Err(invalid_arg(format!("Oklab gamut mapping needs a knee in [0, 1) and a positive saturation, not {knee} and {saturation}"))); + } Ok(()) } @@ -518,11 +773,23 @@ pub fn cmyk_profile_from_model(model: &F, paper: [f64; 3], } fn lut_profile(model: &F, a2b: Clut, paper: [f64; 3], p: &CmykParams) -> Result { - let gbd = Gbd::new(model, p); - let colorimetric = b2a_table(model, p, None)?; - let perceptual = b2a_table(model, p, Some(&gbd))?; + let (perceptual, colorimetric, saturation) = match p.mapping { + GamutMapping::Legacy => { + let gbd = Gbd::new(model, p); + let colorimetric = b2a_table(model, p, None)?; + let perceptual = b2a_table(model, p, Some(&gbd))?; + (perceptual.clone(), colorimetric, perceptual) + } + GamutMapping::Oklab { knee, saturation } => { + let m = OklabMapper::new(model, p, knee, saturation); + let colorimetric = b2a_table_with(p, |lab, warm| m.clip(lab, warm))?; + let perceptual = b2a_table_with(p, |lab, warm| m.perceptual(lab, warm))?; + let saturation = b2a_table_with(p, |lab, warm| m.saturation(lab, warm))?; + (perceptual, colorimetric, saturation) + } + }; let a2b = lut16(a2b, false); - let (b0, b1) = (lut16(perceptual, p.white_on_node), lut16(colorimetric, p.white_on_node)); + let (b0, b1, b2) = (lut16(perceptual, p.white_on_node), lut16(colorimetric, p.white_on_node), lut16(saturation, p.white_on_node)); Ok(Profile { version: (4, 0x30), class: ProfileClass::Output, @@ -538,7 +805,7 @@ fn lut_profile(model: &F, a2b: Clut, paper: [f64; 3], p: &C trc: None, gray_trc: None, a2b: [Some(a2b.clone()), Some(a2b.clone()), Some(a2b)], - b2a: [Some(b0.clone()), Some(b1), Some(b0)], + b2a: [Some(b0), Some(b1), Some(b2)], bytes: None, hash: Default::default(), } @@ -576,6 +843,46 @@ mod tests { } } + #[test] + fn oklab_mapping_refuses_bad_parameters() { + for (knee, saturation) in [(1.0, 1.0), (-0.1, 1.0), (f64::NAN, 1.0), (0.5, 0.0), (0.5, f64::INFINITY), (0.5, f64::NAN)] { + let p = CmykParams { grid_a2b: 2, grid_b2a: 2, mapping: GamutMapping::Oklab { knee, saturation }, ..CmykParams::default() }; + assert!(cmyk_profile(&p).is_err(), "knee {knee}, saturation {saturation}"); + } + } + + #[test] + fn black_generation_curve() { + let p = CmykParams { gcr_curve: vec![[0.0, 0.0], [0.5, 0.2], [1.0, 0.9]], ..CmykParams::default() }; + for (g, k) in [(-1.0, 0.0), (0.0, 0.0), (0.25, 0.1), (0.5, 0.2), (0.75, 0.55), (1.0, 0.9), (2.0, 0.9)] { + assert!((gcr(&p, g) - k).abs() < 1e-12, "g {g}: {} != {k}", gcr(&p, g)); + } + for curve in [vec![[0.5, 0.2], [0.5, 0.3]], vec![[0.5, 0.2], [0.2, 0.3]], vec![[0.0, 1.5]], vec![[f64::NAN, 0.0]]] { + let p = CmykParams { grid_a2b: 2, grid_b2a: 2, gcr_curve: curve.clone(), ..CmykParams::default() }; + assert!(cmyk_profile(&p).is_err(), "{curve:?}"); + } + } + + #[test] + fn oklab_round_trips() { + let m = CmykModel::coated(CmykParams::default().tvi); + let p = CmykParams::default(); + let mapper = OklabMapper::new(&m, &p, 0.65, 1.05); + for lab in [[50.0, 0.0, 0.0], [44.0, 63.7, 47.0], [31.9, 32.9, -76.8], [90.0, -5.0, 80.0]] { + let back = mapper.lab(mapper.oklab(lab)); + // The published matrices carry 10 digits, so the inverses are exact to about 1e-5 ΔE. + assert!(math::delta_e76(lab, back) < 1e-3, "{lab:?} -> {back:?}"); + } + } + + #[test] + fn oklab_profile_has_three_tables() { + let p = CmykParams { grid_a2b: 5, grid_b2a: 5, mapping: GamutMapping::Oklab { knee: 0.65, saturation: 1.05 }, ..CmykParams::default() }; + let profile = cmyk_profile(&p).unwrap(); + let [Some(b0), Some(b1), Some(b2)] = &profile.b2a else { panic!("missing BToA tables") }; + assert!(b0 != b1 && b0 != b2, "perceptual shares a table"); + } + #[test] fn separation_hits_in_gamut_targets() { let m = CmykModel::coated(CmykParams::default().tvi); diff --git a/crates/cms/src/transform.rs b/crates/cms/src/transform.rs index c4f6e11cc8..38a3223d96 100644 --- a/crates/cms/src/transform.rs +++ b/crates/cms/src/transform.rs @@ -46,7 +46,7 @@ const OUT_TABLE: usize = GpuMatrixShaper::OUTPUT_ENTRIES; const _: () = assert!(OUT_TABLE.is_power_of_two()); /// ICC v4 perceptual reference medium black (ICC.1:2010 §6.3.4.4), XYZ. -const PERCEPTUAL_BLACK: [f64; 3] = [0.00336, 0.0034731, 0.00287]; +pub(crate) const PERCEPTUAL_BLACK: [f64; 3] = [0.00336, 0.0034731, 0.00287]; /// Accelerated representation of a transform. #[derive(Debug)] diff --git a/crates/cms/tests/fogra39.rs b/crates/cms/tests/fogra39.rs index f958ffc511..d08e3bae60 100644 --- a/crates/cms/tests/fogra39.rs +++ b/crates/cms/tests/fogra39.rs @@ -12,7 +12,7 @@ //! ([`synth::cmyk_profile_from_model`]). use craft_cms::math::{self, delta_e2000}; -use craft_cms::synth::{self, CmykParams, Forward}; +use craft_cms::synth::{self, CmykParams, Forward, GamutMapping}; use craft_cms::{Builtin, Intent, Profile, Transform}; const DATA: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/FOGRA39.txt"); @@ -346,8 +346,10 @@ fn regen_coated_fogra39() { description: Builtin::CoatedFogra39.description().into(), copyright: craft_cms::builtin::COATED_FOGRA39_COPYRIGHT.into(), tac: 3.3, + gcr_curve: vec![[0.0, 0.0], [0.2, 0.05], [0.5, 0.205], [0.67, 0.5], [1.0, 0.95]], grid_b2a: 33, white_on_node: true, + mapping: GamutMapping::Oklab { knee: 0.65, saturation: 1.05 }, ..CmykParams::default() }; let profile = synth::cmyk_profile_from_model(&f, paper(&p), ¶ms).unwrap(); @@ -564,3 +566,60 @@ fn stand_in_separations_respect_the_ink_limit() { } } } + +/// sRGB patches and their separations by the reference application (Convert to Profile to +/// Coated FOGRA39, dither on), CMYK % per intent: relative + BPC, relative, perceptual, +/// saturation, absolute. Measured for storytold/photocraft#14267. +const REFERENCE_SEPARATIONS: [([u8; 3], [[f32; 4]; 5]); 3] = [ + ( + [200, 30, 30], + [[14.5, 97.7, 95.3, 4.3], [13.3, 100.0, 100.0, 5.5], [18.0, 96.9, 92.9, 8.6], [14.9, 100.0, 100.0, 5.9], [8.6, 100.0, 100.0, 2.4]], + ), + ([30, 60, 200], [[91.8, 75.3, 0.0, 0.0], [94.5, 76.5, 0.0, 0.0], [94.5, 77.7, 0.0, 0.0], [93.7, 76.9, 0.0, 0.0], [92.6, 74.9, 0.0, 0.0]]), + ( + [128, 128, 128], + [[49.0, 38.4, 38.4, 20.0], [49.8, 39.2, 38.8, 21.2], [49.0, 38.4, 38.4, 20.0], [49.0, 38.4, 38.4, 20.0], [46.7, 36.9, 38.4, 18.4]], + ), +]; + +const SEPARATION_INTENTS: [(Intent, bool); 5] = [ + (Intent::RelativeColorimetric, true), + (Intent::RelativeColorimetric, false), + (Intent::Perceptual, true), + (Intent::Saturation, true), + (Intent::AbsoluteColorimetric, false), +]; + +fn separation(intent: Intent, bpc: bool, rgb: [u8; 3]) -> [f32; 4] { + let t = Transform::new(Builtin::Srgb.profile(), fogra39(), intent, bpc).unwrap(); + let mut o = [0.0f32; 4]; + t.eval(&rgb.map(|v| v as f32 / 255.0), &mut o).unwrap(); + o.map(|v| v * 100.0) +} + +/// sRGB → Coated FOGRA39 matches the reference application's inks on a red, a saturated blue +/// and a mid grey for every intent. Most land within 2 % per ink; red at the magenta–yellow +/// edge lands between table nodes that are and are not clipped, which costs up to 2.2 % with +/// the 33³ BToA tables. +#[test] +fn separates_like_the_reference_application() { + let mut worst = 0.0f32; + for (rgb, want) in REFERENCE_SEPARATIONS { + for ((intent, bpc), want) in SEPARATION_INTENTS.into_iter().zip(want) { + let got = separation(intent, bpc, rgb); + let diff = (0..4).map(|i| (got[i] - want[i]).abs()).fold(0.0f32, f32::max); + eprintln!("{rgb:?} {intent:?} bpc {bpc}: {got:.1?} reference {want:?} (max {diff:.1})"); + worst = worst.max(diff); + } + } + assert!(worst <= 2.5, "worst ink difference {worst} %"); +} + +/// Saturation has its own table: a red separates with more magenta and yellow than +/// perceptual, whose chroma compression takes some back. +#[test] +fn saturation_and_perceptual_separate_differently() { + let red = [200, 30, 30]; + let (perceptual, saturation) = (separation(Intent::Perceptual, true, red), separation(Intent::Saturation, true, red)); + assert!(saturation[1] > perceptual[1] + 1.0 && saturation[2] > perceptual[2] + 1.0, "perceptual {perceptual:?}, saturation {saturation:?}"); +} diff --git a/crates/color-transform/Cargo.toml b/crates/color-transform/Cargo.toml new file mode 100644 index 0000000000..8b979d4c09 --- /dev/null +++ b/crates/color-transform/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "craft-color-transform" +description = "Color transforms for Craft apps: RGB spaces and Bradford adaptation, white balance by temperature and tint, OkLab and OkLCh, monotone tone curves and 1D lookup tables, and BT.2408 PQ tone mapping" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +# PQ (SMPTE ST 2084), the HDR reference white and the color primaries tags. +craft-color = { workspace = true } +# The 3×3 matrix arithmetic and Bradford adaptation (`craft_cms::math`). +craft-cms = { workspace = true } +serde = { workspace = true } + +[dev-dependencies] +serde_json = { workspace = true } diff --git a/crates/color-transform/README.md b/crates/color-transform/README.md new file mode 100644 index 0000000000..967be6a527 --- /dev/null +++ b/crates/color-transform/README.md @@ -0,0 +1,74 @@ +# craft-color-transform + +Color transforms for the Craft apps: RGB spaces and chromatic adaptation, white balance by +temperature and tint, OkLab and OkLCh, monotone tone curves and 1D lookup tables, and PQ tone +mapping. It builds on [`craft-color`](../color), which holds the frame color tags, the Y'CbCr +matrices and the sRGB, PQ and HLG transfer functions. It depends on `craft-color`, `craft-cms` +and `serde`, contains no `unsafe` code, and builds for `wasm32-unknown-unknown`. The per-pixel +functions take `f32` and do not allocate. + +The matrix arithmetic comes from `craft_cms::math`. `Mat3` wraps its row-major `[[f64; 3]; 3]` +and converts to and from it with `From`. `mul`, `apply` and `determinant` call `math::mul`, +`math::apply` and `math::det`, `Mat3::inverse` calls `math::inverse`, `Xy::to_xyz` calls +`math::xy_to_xyz` after checking the chromaticity, `BRADFORD` is `math::BRADFORD`, and +`bradford_xyz` calls `math::bradford_checked`. `math::inverse` divides the matrix by its largest +entry before taking the determinant. `math::invert`, which `craft-cms` uses to validate ICC +matrices, keeps its absolute determinant floor of 1e-12. `RgbSpace::to_xyz` builds the +normalized primary matrix with `math::inverse` rather than with `math::rgb_to_xyz_matrix`, which +inverts with `math::invert`, and returns the identity when the normalized matrix has no inverse. + +Moved from StudioCraft's `craft-color-transform` +([`crates/craft-color-transform`](https://github.com/blamejs/StudioCraft/tree/2abe1824aa0d849faf8d5f91557d68ebe577f495/crates/craft-color-transform)). +Parts of it started in other Craft apps: + +- `cct`, `oklab` and `curve` came from LightCraft's `lightcraft-color` (`crates/color/src/cct.rs`, + `perceptual.rs` and `spline.rs`). `MonotoneCurve` now loads a saved curve through its + constructor, so a file cannot give it arrays of different lengths, and it bounds the points it + keeps and the size of their coordinates. `OkLab::for_space` extends the BT.2020-only OkLab + conversion to any `RgbSpace`. +- `tonemap` came from FilmCraft's `filmcraft-color` (`crates/color/src/transform.rs`, + `bt2390_eetf` and its maxRGB operator). It now follows Report ITU-R BT.2408-8 Annex 5 with + the black lift, passes values through when the target peak is at or above the source peak, + and handles a target peak below a third of the source range (see `Eetf`). + +License: MIT OR Apache-2.0. + +## Contents + +| Item | What it does | Source of the formula | +| --- | --- | --- | +| `Mat3`, `Xy`, `RgbSpace` | 3×3 matrices, CIE 1931 chromaticities, RGB spaces from primaries and a white point. `RgbSpace::to_xyz` is the normalized primary matrix. `RgbSpace::from(craft_color::Primaries)` gives the space of a frame tag. | SMPTE RP 177; primaries from ITU-R BT.709, BT.601, BT.2020 and SMPTE EG 432-1 | +| `bradford`, `bradford_xyz` | The Bradford chromatic adaptation matrix between two whites. `RgbSpace::to_space` converts between spaces with it. | The Bradford cone response matrix | +| `cct::temp_tint_to_xy`, `cct::xy_to_temp_tint` | A white at a correlated color temperature and tint, and back. Tint is Duv × 3000 along the locus normal in CIE 1960 (u, v); positive is towards green. | Kim et al., "Design of Advanced Color Temperature Control System for HDTV Applications", Journal of the Korean Physical Society 41 (2002) | +| `cct::wb_matrix`, `cct::white_balance` | The RGB matrix that makes a scene lit by one white look lit by another. | Bradford adaptation | +| `oklab` | OkLab from linear sRGB with the published matrices, from XYZ, and for any `RgbSpace` (`OkLab::for_space`, with `f32` matrices; `oklab::lms_matrices` gives the same matrices in `f64`); OkLCh. | B. Ottosson, "A perceptual color space for image processing" (2020) | +| `curve::MonotoneCurve` | A tone curve through points that does not overshoot between them. It saves as its list of points. `knots` gives each point with its tangent for evaluation in a shader. | Fritsch and Carlson, "Monotone Piecewise Cubic Interpolation", SIAM J. Numer. Anal. 17 (1980) | +| `curve::Lut1` | A 1D lookup table over 0..1 with linear interpolation. | | +| `tonemap::Eetf` | The reference EETF from a mastering display to a target display, with the black lift, on PQ values or nits. When the target peak is below a third of the source range in PQ, where the Annex's knee would be negative, the knee is 0 and the spline starts with a slope of 3 × maxLum. When minLum is above 0.25, where the Annex's lift minLum × (1 − E2)⁴ would make the output fall near black and raise the peak above the target peak, the lift is minLum × (1 − E2 / r)ᵏ with r = max(T, min(1, 2 − 4 minLum)) and k = min(4, r / minLum), where T is the smaller of maxLum and 1. This equals the Annex's lift up to minLum 0.25, keeps the output rising, changes continuously with the target black, and from minLum 0.5 reaches 0 at the top of the output. A target black above the source peak reads as the source peak. Both rules are the crate's own. | Report ITU-R BT.2408-8, Annex 5 | +| `tonemap::ToneMap` | The EETF applied to linear RGB by max(R, G, B) as a ratio, with 1.0 at the 203-nit reference white. | Report ITU-R BT.2408-8, Annex 5 (the maxRGB option) | + +## Hostile input + +- A chromaticity with `y` at or below 0, or with a coordinate that is not finite, is rejected by + `Xy::to_xyz`. `bradford` then returns the identity, and so does `RgbSpace::to_xyz` for such a + white, for collinear primaries and for a white that gives a primary no weight. `Mat3::inverse` + and `bradford_xyz` return `None` or the identity instead of a matrix with entries beyond + `f64`. +- Temperatures are clamped to 1000 K to 100 000 K, and NaN reads as 6500 K. A tint that is not + finite reads as 0. +- `MonotoneCurve` keeps at most 1024 points and drops points with a coordinate that is not + finite or is beyond ±1e6. A saved curve is read back through the same constructor, so a file + cannot give it mismatched arrays. +- `Lut1::from_fn` and `MonotoneCurve::to_lut` build at most 65 536 entries. +- Luminances are clamped to 0..10 000 nits. A target peak that is NaN or not above 0 gives an + EETF and a `ToneMap` that pass values through. + +## Tests + +`cargo test -p craft-color-transform` checks the code against published values: the sRGB matrix of +IEC 61966-2-1, the BT.709 and BT.2020 luminance weights, the BT.709 to BT.2020 matrix of +ITU-R BT.2087, the D65 to D50 Bradford matrix as Lindbloom's chromatic adaptation page prints it, +illuminant A and D65 from CIE 15:2004 (temperature, Duv, CIE 1960 coordinates), Ottosson's XYZ to +OkLab table, the OkLab value CSS Color Module Level 4 gives for `#7654CD`, the PQ levels of +BT.2408, and EETF values computed from the BT.2408 Annex 5 equations by a separate +implementation. diff --git a/crates/color-transform/src/cct.rs b/crates/color-transform/src/cct.rs new file mode 100644 index 0000000000..413ca5e6e1 --- /dev/null +++ b/crates/color-transform/src/cct.rs @@ -0,0 +1,263 @@ +//! Correlated color temperature, tint and white balance. +//! +//! The Planckian locus is the cubic approximation of Kim et al., "Design of Advanced Color +//! Temperature Control System for HDTV Applications", Journal of the Korean Physical Society 41 +//! (2002), over 1667 K to 25000 K. Outside that range the locus continues as a straight line in +//! mired (10⁶ / K) from the end of the approximation. Temperatures are clamped to +//! [`MIN_TEMPERATURE`] to [`MAX_TEMPERATURE`]. +//! +//! Tint is a signed distance from the locus in CIE 1960 (u, v), along the locus normal, in units +//! of 1/[`TINT_SCALE`] of Duv. A positive tint is a white above the locus (towards green), so the +//! correction that removes it adds magenta; a negative tint is a white below the locus. + +use crate::{Mat3, RgbSpace, Xy, bradford}; + +/// Tint units per unit of Duv. +pub const TINT_SCALE: f64 = 3000.0; +/// The lowest temperature the approximation covers, in kelvin. +pub const KIM_MIN: f64 = 1667.0; +/// The highest temperature the approximation covers, in kelvin. +pub const KIM_MAX: f64 = 25000.0; +/// The lowest temperature the functions here take. +pub const MIN_TEMPERATURE: f64 = 1000.0; +/// The highest temperature the functions here take. +pub const MAX_TEMPERATURE: f64 = 100_000.0; + +fn kim_xy(t: f64) -> Xy { + let t = t.clamp(KIM_MIN, KIM_MAX); + let (t2, t3) = (t * t, t * t * t); + let x = if t <= 4000.0 { + -0.2661239e9 / t3 - 0.2343589e6 / t2 + 0.8776956e3 / t + 0.179910 + } else { + -3.0258469e9 / t3 + 2.1070379e6 / t2 + 0.2226347e3 / t + 0.240390 + }; + let (x2, x3) = (x * x, x * x * x); + let y = if t <= 2222.0 { + -1.1063814 * x3 - 1.34811020 * x2 + 2.18555832 * x - 0.20219683 + } else if t <= 4000.0 { + -0.9549476 * x3 - 1.37418593 * x2 + 2.09137015 * x - 0.16748867 + } else { + 3.0817580 * x3 - 5.87338670 * x2 + 3.75112997 * x - 0.37001483 + }; + Xy::new(x, y) +} + +/// CIE 1931 xy to CIE 1960 (u, v). +pub fn xy_to_uv(p: Xy) -> (f64, f64) { + let d = -2.0 * p.x + 12.0 * p.y + 3.0; + (4.0 * p.x / d, 6.0 * p.y / d) +} + +/// CIE 1960 (u, v) to CIE 1931 xy. +pub fn uv_to_xy(u: f64, v: f64) -> Xy { + let d = 2.0 * u - 8.0 * v + 4.0; + Xy::new(3.0 * u / d, 2.0 * v / d) +} + +/// A temperature in range: NaN reads as 6500 K. +fn sane_temperature(t: f64) -> f64 { + if t.is_nan() { 6500.0 } else { t.clamp(MIN_TEMPERATURE, MAX_TEMPERATURE) } +} + +/// The locus in (u, v) at `t` kelvin. +fn locus_uv(t: f64) -> (f64, f64) { + if (KIM_MIN..=KIM_MAX).contains(&t) { + return xy_to_uv(kim_xy(t)); + } + let (a, b) = if t < KIM_MIN { (KIM_MIN, KIM_MIN + 50.0) } else { (KIM_MAX - 500.0, KIM_MAX) }; + let (ua, va) = xy_to_uv(kim_xy(a)); + let (ub, vb) = xy_to_uv(kim_xy(b)); + let (ma, mb, m) = (1e6 / a, 1e6 / b, 1e6 / t); + let s = (m - ma) / (mb - ma); + (ua + (ub - ua) * s, va + (vb - va) * s) +} + +/// The unit normal of the locus at `t`, pointing towards green (increasing v). +fn locus_normal(t: f64) -> (f64, f64) { + let m = 1e6 / t; + let (u0, v0) = locus_uv(1e6 / (m + 1.0)); + let (u1, v1) = locus_uv(1e6 / (m - 1.0).max(1.0)); + let (du, dv) = (u1 - u0, v1 - v0); + let l = du.hypot(dv).max(1e-12); + let (nx, ny) = (-dv / l, du / l); + if ny < 0.0 { (-nx, -ny) } else { (nx, ny) } +} + +/// The chromaticity of a white at temperature `t` (kelvin) and `tint`. A NaN tint reads as 0. +pub fn temp_tint_to_xy(t: f64, tint: f64) -> Xy { + let t = sane_temperature(t); + let tint = if tint.is_finite() { tint } else { 0.0 }; + let (u, v) = locus_uv(t); + let (nu, nv) = locus_normal(t); + let duv = tint / TINT_SCALE; + uv_to_xy(u + nu * duv, v + nv * duv) +} + +/// The temperature and tint of a white: the nearest point on the locus, searched in mired, and +/// the signed distance from it. The inverse of [`temp_tint_to_xy`] over its range. A +/// chromaticity with no (u, v) coordinates (NaN, or on the line where their denominator is 0) +/// reads as 6500 K with no tint. +pub fn xy_to_temp_tint(p: Xy) -> (f64, f64) { + let (u, v) = xy_to_uv(p); + if !(u.is_finite() && v.is_finite()) { + return (6500.0, 0.0); + } + let dist = |m: f64| { + let (lu, lv) = locus_uv(1e6 / m); + (u - lu).powi(2) + (v - lv).powi(2) + }; + let (lo, hi) = (1e6 / MAX_TEMPERATURE, 1e6 / MIN_TEMPERATURE); + // A scan every 5 mired, then a golden-section search around the best step. + let mut best = lo; + let mut best_d = f64::MAX; + let mut m = lo; + while m <= hi { + let d = dist(m); + if d < best_d { + best_d = d; + best = m; + } + m += 5.0; + } + let (mut a, mut b) = ((best - 5.0).max(lo), (best + 5.0).min(hi)); + let g = 0.618_033_988_75; + for _ in 0..60 { + let c = b - g * (b - a); + let d = a + g * (b - a); + if dist(c) < dist(d) { b = d } else { a = c } + } + let t = 1e6 / ((a + b) / 2.0); + let (lu, lv) = locus_uv(t); + let (nu, nv) = locus_normal(t); + (t, ((u - lu) * nu + (v - lv) * nv) * TINT_SCALE) +} + +/// The matrix (linear RGB in `space` to the same space) that makes a scene lit by a white at +/// `src` look as if it were lit by the space's own white (Bradford adaptation). +pub fn wb_matrix(space: &RgbSpace, src: Xy) -> Mat3 { + space.from_xyz().mul(&bradford(src, space.white)).mul(&space.to_xyz()) +} + +/// The matrix (linear RGB in `space` to the same space) that makes a scene lit by a white at +/// temperature and tint `from` look as if it were lit by the white at `to`. Equal settings give +/// the identity. A lower `from` temperature than `to` cools the picture. +pub fn white_balance(space: &RgbSpace, from: (f64, f64), to: (f64, f64)) -> Mat3 { + let (src, dst) = (temp_tint_to_xy(from.0, from.1), temp_tint_to_xy(to.0, to.1)); + if src == dst { + return Mat3::IDENTITY; + } + space.from_xyz().mul(&bradford(src, dst)).mul(&space.to_xyz()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{D65, REC2020, SRGB}; + + /// CIE 15:2004 gives illuminant A (a Planckian radiator at 2856 K) as x = 0.44757, + /// y = 0.40745. The cubic approximation lands within 0.001 Duv of it. + #[test] + fn illuminant_a_lies_on_the_locus() { + let (u, v) = xy_to_uv(temp_tint_to_xy(2856.0, 0.0)); + let (ua, va) = xy_to_uv(Xy::new(0.44757, 0.40745)); + assert!((u - ua).hypot(v - va) < 1e-3, "{u} {v}"); + let (t, _) = xy_to_temp_tint(Xy::new(0.44757, 0.40745)); + assert!((t - 2856.0).abs() < 15.0, "{t}"); + } + + /// CIE 15:2004 gives D65 a correlated color temperature of 6504 K; it lies above the locus + /// by a Duv of about 0.0032. + #[test] + fn d65_reads_as_6504_k_above_the_locus() { + let (t, tint) = xy_to_temp_tint(D65); + assert!((t - 6504.0).abs() < 15.0, "{t}"); + assert!((tint / TINT_SCALE - 0.0032).abs() < 3e-4, "{tint}"); + } + + /// D65 in CIE 1960 UCS is (0.1978, 0.3122). + #[test] + fn uv_matches_cie_1960_for_d65() { + let (u, v) = xy_to_uv(D65); + assert!((u - 0.1978).abs() < 1e-4 && (v - 0.3122).abs() < 1e-4, "{u} {v}"); + let back = uv_to_xy(u, v); + assert!((back.x - D65.x).abs() < 1e-12 && (back.y - D65.y).abs() < 1e-12); + } + + #[test] + fn temperature_and_tint_round_trip() { + for t in [1200.0, 2000.0, 2856.0, 4000.0, 5003.0, 6500.0, 9000.0, 15000.0, 30000.0, 50000.0] { + for tint in [-100.0, -20.0, 0.0, 35.0, 150.0] { + let (t2, tint2) = xy_to_temp_tint(temp_tint_to_xy(t, tint)); + assert!((t2 - t).abs() / t < 0.01, "{t} {tint} -> {t2}"); + assert!((tint2 - tint).abs() < 1.0, "{t} {tint} -> {tint2}"); + } + } + } + + #[test] + fn warmer_whites_are_redder_and_positive_tint_is_greener() { + assert!(temp_tint_to_xy(3000.0, 0.0).x > temp_tint_to_xy(9000.0, 0.0).x); + let (_, v0) = xy_to_uv(temp_tint_to_xy(5000.0, 0.0)); + let (_, v1) = xy_to_uv(temp_tint_to_xy(5000.0, 50.0)); + assert!(v1 > v0); + } + + #[test] + fn wb_matrix_maps_the_source_white_to_neutral() { + let src = temp_tint_to_xy(3200.0, 10.0); + let m = wb_matrix(&REC2020, src); + let out = m.apply(REC2020.from_xyz().apply(src.to_xyz().unwrap())); + assert!((out[0] - out[1]).abs() < 1e-9 && (out[1] - out[2]).abs() < 1e-9, "{out:?}"); + } + + #[test] + fn white_balance_is_the_identity_for_equal_settings_and_cools_a_warm_light() { + assert_eq!(white_balance(&SRGB, (6500.0, 0.0), (6500.0, 0.0)), Mat3::IDENTITY); + // Light at 3200 K made to look like 6500 K: a white surface lit by it turns neutral. + let m = white_balance(&SRGB, (3200.0, 0.0), (6500.0, 0.0)); + let gray = m.apply([1.0, 1.0, 1.0]); + assert!(gray[2] > gray[0], "{gray:?}"); + let lit = SRGB.from_xyz().apply(temp_tint_to_xy(3200.0, 0.0).to_xyz().unwrap()); + let lit6500 = SRGB.from_xyz().apply(temp_tint_to_xy(6500.0, 0.0).to_xyz().unwrap()); + let out = m.apply(lit); + for i in 0..3 { + assert!((out[i] - lit6500[i]).abs() < 1e-9, "{out:?} {lit6500:?}"); + } + } + + #[test] + fn hostile_temperatures_stay_finite() { + for t in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, 0.0, -5.0, 1e300] { + for tint in [f64::NAN, f64::INFINITY, 0.0] { + let p = temp_tint_to_xy(t, tint); + assert!(p.x.is_finite() && p.y.is_finite(), "{t} {tint} {p:?}"); + let m = white_balance(&SRGB, (t, tint), (6500.0, 0.0)); + assert!(m.0.iter().flatten().all(|v| v.is_finite()), "{t} {tint}"); + } + } + // -2x + 12y + 3 = 0 at (1.5, 0): the (u, v) denominator. + for p in [Xy::new(f64::NAN, f64::NAN), Xy::new(1.5, 0.0), Xy::new(f64::INFINITY, 0.3)] { + assert_eq!(xy_to_temp_tint(p), (6500.0, 0.0), "{p:?}"); + } + } + + /// Pins the current behavior for warm, strongly green illuminants (e.g. 2000–3500 K with high + /// positive tint) where the source white's Bradford blue cone response is `<= 0.0`. The + /// checked Bradford path currently returns `Mat3::IDENTITY` there (so `wb_matrix` is + /// `REC2020.from_xyz() * REC2020.to_xyz()`, i.e. identity within `f64` rounding) rather than + /// inverting blue; final `#587` process-version policy for this region remains open. + #[test] + fn warm_green_wb_with_non_positive_cone_response_returns_identity() { + for (t, tint) in [(2000.0, 30.0), (2500.0, 60.0), (3000.0, 100.0), (3500.0, 125.0)] { + let src = temp_tint_to_xy(t, tint); + assert_eq!(crate::bradford(src, REC2020.white), Mat3::IDENTITY, "{t} K tint {tint}"); + let m = wb_matrix(&REC2020, src); + for (i, row) in m.0.iter().enumerate() { + for (j, &val) in row.iter().enumerate() { + let want = if i == j { 1.0 } else { 0.0 }; + assert!((val - want).abs() < 1e-12, "{t} K tint {tint} [{i}][{j}] = {val}"); + } + } + } + } +} diff --git a/crates/color-transform/src/curve.rs b/crates/color-transform/src/curve.rs new file mode 100644 index 0000000000..5a7d482b2c --- /dev/null +++ b/crates/color-transform/src/curve.rs @@ -0,0 +1,462 @@ +//! Tone curves: monotone cubic Hermite splines and 1D lookup tables. +//! +//! [`MonotoneCurve`] interpolates its points with the tangents of F. N. Fritsch and R. E. +//! Carlson, "Monotone Piecewise Cubic Interpolation", SIAM Journal on Numerical Analysis 17 +//! (1980): between two points the curve does not overshoot, so a curve whose points rise never +//! falls. + +use std::fmt; + +use serde::de::{IgnoredAny, SeqAccess, Visitor}; +use serde::{Deserialize, Deserializer, Serialize}; + +/// The most points a [`MonotoneCurve`] keeps; points past it are ignored. +pub const MAX_POINTS: usize = 1024; +/// The largest coordinate a [`MonotoneCurve`] point may have; points farther out are dropped. +pub const MAX_COORDINATE: f64 = 1e6; + +/// A curve through points in `0..1 × 0..1` (any finite values work). It is saved as its points. +#[derive(Clone, Debug, PartialEq, Serialize)] +#[serde(into = "Vec<(f64, f64)>")] +pub struct MonotoneCurve { + xs: Vec, + ys: Vec, + ms: Vec, +} + +impl From> for MonotoneCurve { + fn from(points: Vec<(f64, f64)>) -> MonotoneCurve { + MonotoneCurve::new(&points) + } +} + +/// Reads a saved list of points, keeping the first [`MAX_POINTS`] and skipping the rest +/// without storing them. +impl<'de> Deserialize<'de> for MonotoneCurve { + fn deserialize>(d: D) -> Result { + struct Points; + impl<'de> Visitor<'de> for Points { + type Value = MonotoneCurve; + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("a list of [x, y] points") + } + fn visit_seq>(self, mut seq: A) -> Result { + let mut p = Vec::with_capacity(seq.size_hint().unwrap_or(0).min(MAX_POINTS)); + while p.len() < MAX_POINTS { + match seq.next_element::<(f64, f64)>()? { + Some(q) => p.push(q), + None => return Ok(MonotoneCurve::new(&p)), + } + } + while seq.next_element::()?.is_some() {} + Ok(MonotoneCurve::new(&p)) + } + } + d.deserialize_seq(Points) + } +} + +impl From for Vec<(f64, f64)> { + fn from(c: MonotoneCurve) -> Vec<(f64, f64)> { + c.points() + } +} + +impl MonotoneCurve { + /// A curve through `points`, sorted by x. Points with a coordinate that is not finite or is + /// beyond ±[`MAX_COORDINATE`] are dropped, and of points closer than 1e-9 in x the first is + /// kept. Fewer than two points give the identity. + pub fn new(points: &[(f64, f64)]) -> MonotoneCurve { + let ok = |v: f64| v.is_finite() && v.abs() <= MAX_COORDINATE; + let mut p: Vec<(f64, f64)> = points.iter().take(MAX_POINTS).copied().filter(|(x, y)| ok(*x) && ok(*y)).collect(); + p.sort_by(|a, b| a.0.total_cmp(&b.0)); + p.dedup_by(|a, b| (a.0 - b.0).abs() < 1e-9); + if p.len() < 2 { + p = vec![(0.0, 0.0), (1.0, 1.0)]; + } + let n = p.len(); + let xs: Vec = p.iter().map(|q| q.0).collect(); + let ys: Vec = p.iter().map(|q| q.1).collect(); + let d: Vec = xs.windows(2).zip(ys.windows(2)).map(|(x, y)| (y[1] - y[0]) / (x[1] - x[0])).collect(); + let mut ms = vec![0.0; n]; + ms[0] = d[0]; + ms[n - 1] = d[n - 2]; + for i in 1..n - 1 { + ms[i] = if d[i - 1] * d[i] <= 0.0 { 0.0 } else { (d[i - 1] + d[i]) / 2.0 }; + } + // The Fritsch–Carlson limit: tangents inside the circle of radius 3 keep each segment + // monotone. The length of (m₀, m₁) is compared with 3·|d|, which stays finite when the + // secant d is subnormal. + for i in 0..n - 1 { + if d[i] == 0.0 { + ms[i] = 0.0; + ms[i + 1] = 0.0; + continue; + } + let r = ms[i].hypot(ms[i + 1]); + let limit = 3.0 * d[i].abs(); + if r > limit { + ms[i] = ms[i] / r * limit; + ms[i + 1] = ms[i + 1] / r * limit; + } + } + MonotoneCurve { xs, ys, ms } + } + + pub fn identity() -> MonotoneCurve { + MonotoneCurve::new(&[(0.0, 0.0), (1.0, 1.0)]) + } + + /// Whether the curve maps every value in `0..1` to itself. A curve whose points do not + /// reach 0 and 1 is not the identity, since it clamps outside them. + pub fn is_identity(&self) -> bool { + let covers = self.xs.first().is_some_and(|x| *x <= 0.0) && self.xs.last().is_some_and(|x| *x >= 1.0); + covers && self.xs.iter().zip(&self.ys).all(|(x, y)| x == y) && self.ms.iter().all(|m| *m == 1.0) + } + + /// The curve at `x`. Below the first point it is the first point's y, above the last the + /// last's. NaN gives NaN. + pub fn eval(&self, x: f64) -> f64 { + if x.is_nan() { + return x; + } + let n = self.xs.len(); + if x <= self.xs[0] { + return self.ys[0]; + } + if x >= self.xs[n - 1] { + return self.ys[n - 1]; + } + let i = match self.xs.binary_search_by(|v| v.total_cmp(&x)) { + Ok(i) => return self.ys[i], + Err(i) => i - 1, + }; + hermite(self.xs[i], self.ys[i], self.ms[i], self.xs[i + 1], self.ys[i + 1], self.ms[i + 1], x) + } + + /// The curve sampled at `n` evenly spaced points over `0..1` (at least 2). + pub fn to_lut(&self, n: usize) -> Lut1 { + let n = n.clamp(2, MAX_LUT); + Lut1 { v: (0..n).map(|i| self.eval(i as f64 / (n - 1) as f64) as f32).collect() } + } + + /// The points, sorted by x. + pub fn points(&self) -> Vec<(f64, f64)> { + self.xs.iter().copied().zip(self.ys.iter().copied()).collect() + } + + /// Each point as `[x, y, tangent]`, for code that evaluates the same spline elsewhere (a + /// GPU pass): between points `k` and `k + 1`, with `h = x₁ − x₀` and `t = (x − x₀) / h`, + /// the curve is `(2t³ − 3t² + 1) y₀ + (t³ − 2t² + t) h m₀ + (−2t³ + 3t²) y₁ + (t³ − t²) h m₁`. + pub fn knots(&self) -> impl ExactSizeIterator + '_ { + self.xs.iter().zip(&self.ys).zip(&self.ms).map(|((x, y), m)| [*x, *y, *m]) + } +} + +fn hermite(x0: f64, y0: f64, m0: f64, x1: f64, y1: f64, m1: f64, x: f64) -> f64 { + let h = x1 - x0; + let t = (x - x0) / h; + let (t2, t3) = (t * t, t * t * t); + (2.0 * t3 - 3.0 * t2 + 1.0) * y0 + (t3 - 2.0 * t2 + t) * h * m0 + (-2.0 * t3 + 3.0 * t2) * y1 + (t3 - t2) * h * m1 +} + +/// The most entries [`Lut1::from_fn`] and [`MonotoneCurve::to_lut`] build. +pub const MAX_LUT: usize = 1 << 16; + +/// A 1D lookup table over `0..1` with linear interpolation. Inputs outside `0..1` clamp. +#[derive(Clone, Debug, PartialEq)] +pub struct Lut1 { + pub v: Vec, +} + +impl Lut1 { + /// The identity with `n` entries (2 to [`MAX_LUT`]). + pub fn identity(n: usize) -> Lut1 { + Lut1::from_fn(n, |x| x) + } + + /// `f` sampled at `n` evenly spaced points over `0..1` (2 to [`MAX_LUT`]). + pub fn from_fn(n: usize, f: impl Fn(f32) -> f32) -> Lut1 { + let n = n.clamp(2, MAX_LUT); + Lut1 { v: (0..n).map(|i| f(i as f32 / (n - 1) as f32)).collect() } + } + + /// The table at `x`. `v` is public, so a table of 1 entry reads as that constant and an + /// empty one as the identity. + #[inline] + pub fn eval(&self, x: f32) -> f32 { + let x = x.clamp(0.0, 1.0); + let n = self.v.len(); + if n < 2 { + return self.v.first().copied().unwrap_or(x); + } + let f = x * (n - 1) as f32; + let i = (f as usize).min(n - 2); + let t = f64::from(f - i as f32); + // Weighted in f64, so entries as far apart as ±f32::MAX do not overflow, and t = 0 and + // t = 1 give the entries exactly. + (f64::from(self.v[i]) * (1.0 - t) + f64::from(self.v[i + 1]) * t) as f32 + } + + /// `self ∘ inner`: `inner` applies first. An empty `inner` is the identity, so the result + /// is `self`. The result is sampled on the densest grid of at most [`MAX_LUT`] entries that + /// holds the sample points of both tables. Between two grid points it differs from + /// `self.eval(inner.eval(x))` by at most one grid step times the product of the two + /// tables' steepest slopes. When no grid of that size holds both tables' points, the result + /// uses the longer table's grid, with the same bound for that grid's step: a table whose + /// slope is near its entry count can then lose the other table's knots between two samples. + pub fn compose(&self, inner: &Lut1) -> Lut1 { + if inner.v.is_empty() { + return self.clone(); + } + let on_inner = || Lut1 { v: inner.v.iter().map(|&x| self.eval(x)).collect() }; + let resampled = |n: usize| Lut1::from_fn(n, |x| self.eval(inner.eval(x))); + let (a, b) = (inner.v.len() - 1, self.v.len().saturating_sub(1)); + if a == 0 || b == 0 { + return on_inner(); + } + let common = (a / gcd(a, b)).checked_mul(b).filter(|l| *l < MAX_LUT); + let steps = common.map(|l| (MAX_LUT - 1) / l * l); + match steps { + Some(s) if s == a => on_inner(), + Some(s) => resampled(s + 1), + None if inner.v.len() >= self.v.len() => on_inner(), + None => resampled(self.v.len()), + } + } + + /// Resample this table to `n` evenly spaced entries over `0..1` (2 to [`MAX_LUT`]). + pub fn resample(&self, n: usize) -> Lut1 { + let n = n.clamp(2, MAX_LUT); + if self.v.len() == n { + return self.clone(); + } + Lut1::from_fn(n, |x| self.eval(x)) + } + + /// `self ∘ inner` sampled at `n` evenly spaced entries over `0..1` (2 to [`MAX_LUT`]). + /// + /// Preserves fixed-size GPU LUT contracts (such as 1,024-entry shader tables) where + /// [`Lut1::compose`] would expand to the common refinement grid. When `inner` is empty it + /// resamples `self` to `n` entries; when `inner` already has `n` entries it evaluates `self` + /// directly at each of `inner`'s samples. + pub fn compose_len(&self, inner: &Lut1, n: usize) -> Lut1 { + let n = n.clamp(2, MAX_LUT); + if inner.v.is_empty() { + return self.resample(n); + } + if inner.v.len() == n { + return Lut1 { v: inner.v.iter().map(|&x| self.eval(x)).collect() }; + } + Lut1::from_fn(n, |x| self.eval(inner.eval(x))) + } + + pub fn is_identity(&self) -> bool { + let n = self.v.len(); + n >= 2 && self.v.iter().enumerate().all(|(i, &y)| (y - i as f32 / (n - 1) as f32).abs() < 1e-6) + } +} + +fn gcd(mut a: usize, mut b: usize) -> usize { + while b != 0 { + (a, b) = (b, a % b); + } + a +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_identity_curve_maps_every_value_to_itself() { + let c = MonotoneCurve::identity(); + assert!(c.is_identity()); + for i in 0..=10 { + let x = i as f64 / 10.0; + assert!((c.eval(x) - x).abs() < 1e-12); + } + assert!(c.to_lut(256).is_identity()); + assert!(!MonotoneCurve::new(&[(0.0, 0.0), (0.5, 0.6), (1.0, 1.0)]).is_identity()); + // These points lie on the diagonal, but the curve clamps below 0.2 and above 0.8. + let inner = MonotoneCurve::new(&[(0.2, 0.2), (0.8, 0.8)]); + assert_eq!(inner.eval(0.0), 0.2); + assert!(!inner.is_identity()); + assert!(MonotoneCurve::new(&[(-1.0, -1.0), (2.0, 2.0)]).is_identity()); + } + + #[test] + fn the_curve_passes_through_its_points_and_rises_with_them() { + let pts = [(0.0, 0.0), (0.25, 0.15), (0.5, 0.5), (0.75, 0.88), (1.0, 1.0)]; + let c = MonotoneCurve::new(&pts); + for (x, y) in pts { + assert!((c.eval(x) - y).abs() < 1e-12); + } + let mut prev = -1.0; + for i in 0..=1000 { + let y = c.eval(i as f64 / 1000.0); + assert!(y >= prev - 1e-12); + prev = y; + } + } + + /// Data that rise, then stay level: a natural cubic spline overshoots the level part, and + /// this curve stays on it. + #[test] + fn the_curve_does_not_overshoot_a_level_run() { + let c = MonotoneCurve::new(&[(0.0, 0.0), (0.4, 0.5), (0.6, 0.5), (1.0, 1.0)]); + for i in 400..=600 { + let y = c.eval(i as f64 / 1000.0); + assert!((y - 0.5).abs() < 1e-9, "{y}"); + } + } + + /// Steep and shallow neighbors: the unlimited average tangent overshoots, the limited one + /// keeps the segment within its end values. + #[test] + fn steep_steps_stay_monotone() { + let c = MonotoneCurve::new(&[(0.0, 0.0), (0.1, 0.0), (0.2, 1.0), (1.0, 1.0)]); + let mut prev = -1.0; + for i in 0..=1000 { + let y = c.eval(i as f64 / 1000.0); + assert!((0.0..=1.0).contains(&y) && y >= prev - 1e-12, "{i} {y}"); + prev = y; + } + } + + #[test] + fn the_knots_evaluate_to_the_same_curve() { + let c = MonotoneCurve::new(&[(0.0, 0.1), (0.3, 0.2), (0.7, 0.9), (1.0, 0.95)]); + let k: Vec<[f64; 3]> = c.knots().collect(); + assert_eq!(k.len(), 4); + for i in 0..=100 { + let x = i as f64 / 100.0; + let seg = k.windows(2).find(|w| x >= w[0][0] && x <= w[1][0]).unwrap(); + let y = hermite(seg[0][0], seg[0][1], seg[0][2], seg[1][0], seg[1][1], seg[1][2], x); + assert!((y - c.eval(x)).abs() < 1e-12, "{x}"); + } + } + + #[test] + fn luts_compose() { + let a = Lut1::from_fn(1024, |x| x * x); + let b = Lut1::from_fn(1024, f32::sqrt); + assert!((a.compose(&b).eval(0.3) - 0.3).abs() < 2e-3); + assert_eq!(a.compose(&Lut1 { v: vec![] }), a); + } + + #[test] + fn compose_len_preserves_fixed_gpu_lut_size() { + const N: usize = 1024; + let a = Lut1::from_fn(N, |x| x * x); + let b = Lut1::from_fn(N, f32::sqrt); + let c = a.compose_len(&b, N); + assert_eq!(c.v.len(), N); + assert!((c.eval(0.3) - 0.3).abs() < 2e-3); + assert_eq!(a.compose_len(&Lut1 { v: vec![] }, N), a); + let small = Lut1::from_fn(17, f32::sqrt); + let d = a.compose_len(&small, N); + assert_eq!(d.v.len(), N); + assert!((d.eval(0.3) - a.eval(small.eval(0.3))).abs() < 2e-3); + } + + /// A short `inner` does not drop the outer table's points between its own. + #[test] + fn composing_with_a_short_table_keeps_the_outer_curve() { + let peak = Lut1 { v: vec![0.0, 1.0, 0.0] }; + let c = peak.compose(&Lut1::identity(2)); + assert_eq!(c.eval(0.5), 1.0); + assert_eq!(c.eval(0.0), 0.0); + assert_eq!(c.eval(1.0), 0.0); + assert_eq!(peak.compose(&Lut1 { v: vec![0.5] }).eval(0.2), 1.0); + // Grids that do not hold each other's points, and inner tables that cross the outer + // peak between their own points. + let cases = [ + (Lut1::identity(4), peak.clone()), + (Lut1 { v: vec![0.0, 1.0, 0.0, 1.0] }, peak.clone()), + (peak.clone(), Lut1::identity(4)), + (peak.clone(), peak.clone()), + (peak.clone(), Lut1 { v: vec![0.0, 0.75] }), + (Lut1::from_fn(17, |x| (x * 9.0).sin().abs()), Lut1::from_fn(9, |x| x * x)), + ]; + for (outer, inner) in &cases { + let c = outer.compose(inner); + for i in 0..=3000 { + let x = i as f32 / 3000.0; + let want = outer.eval(inner.eval(x)); + assert!((c.eval(x) - want).abs() < 1e-3, "{outer:?} {inner:?} {x}"); + } + } + assert!((peak.compose(&Lut1 { v: vec![0.0, 0.75] }).eval(2.0 / 3.0) - 1.0).abs() < 1e-3); + // A grid beyond MAX_LUT entries falls back to the longer table's grid. + let big = Lut1::identity(MAX_LUT - 1).compose(&Lut1::identity(MAX_LUT)); + assert_eq!(big.v.len(), MAX_LUT); + } + + /// Entries this far apart overflow a difference taken between them. + #[test] + fn far_apart_entries_interpolate_without_overflow() { + let wide = Lut1 { v: vec![-f32::MAX, f32::MAX] }; + assert_eq!(wide.eval(0.0), -f32::MAX); + assert_eq!(wide.eval(1.0), f32::MAX); + assert_eq!(wide.eval(0.5), 0.0); + assert!(wide.eval(0.25).is_finite()); + let high = Lut1 { v: vec![f32::MAX, f32::MAX] }; + assert_eq!(high.eval(0.3), f32::MAX); + } + + #[test] + fn a_curve_saves_as_its_points_and_a_hostile_file_does_not_break_it() { + let c = MonotoneCurve::new(&[(0.0, 0.0), (0.5, 0.7), (1.0, 1.0)]); + let json = serde_json::to_string(&c).unwrap(); + assert_eq!(json, "[[0.0,0.0],[0.5,0.7],[1.0,1.0]]"); + assert_eq!(serde_json::from_str::(&json).unwrap(), c); + // A single point, unsorted points and duplicates load as a working curve. + assert!(serde_json::from_str::("[[0.5,0.5]]").unwrap().is_identity()); + let messy: MonotoneCurve = serde_json::from_str("[[1.0,1.0],[0.0,0.0],[0.0,0.3],[0.5,0.2]]").unwrap(); + assert_eq!(messy.points(), vec![(0.0, 0.0), (0.5, 0.2), (1.0, 1.0)]); + // The fields are not read from a file, so their lengths always agree. + assert!(serde_json::from_str::(r#"{"xs":[0.0],"ys":[],"ms":[]}"#).is_err()); + // A long list keeps its first MAX_POINTS points. + let long: Vec<(f64, f64)> = (0..3000).map(|i| (f64::from(i) / 3000.0, f64::from(i) / 3000.0)).collect(); + let loaded: MonotoneCurve = serde_json::from_str(&serde_json::to_string(&long).unwrap()).unwrap(); + assert_eq!(loaded.points().len(), MAX_POINTS); + assert_eq!(loaded.points()[MAX_POINTS - 1], long[MAX_POINTS - 1]); + } + + #[test] + fn nan_input_and_short_or_huge_tables_do_not_panic() { + let c = MonotoneCurve::new(&[(0.0, 0.0), (0.5, 0.6), (1.0, 1.0)]); + assert!(c.eval(f64::NAN).is_nan()); + assert!(c.eval(-f64::NAN).is_nan()); + assert_eq!(c.eval(-3.0), 0.0); + assert_eq!(c.eval(9.0), 1.0); + for n in [0, 1] { + assert_eq!(c.to_lut(n).v.len(), 2, "{n}"); + assert!(Lut1::identity(n).is_identity(), "{n}"); + } + assert_eq!(Lut1::from_fn(usize::MAX, |x| x).v.len(), MAX_LUT); + assert_eq!(Lut1 { v: vec![] }.eval(0.3), 0.3); + assert_eq!(Lut1 { v: vec![] }.eval(-1.0), 0.0); + assert_eq!(Lut1 { v: vec![0.7] }.eval(0.3), 0.7); + assert!(!Lut1 { v: vec![0.0] }.is_identity()); + let many: Vec<(f64, f64)> = (0..5000).map(|i| (i as f64, i as f64)).collect(); + assert_eq!(MonotoneCurve::new(&many).points().len(), MAX_POINTS); + let bad = MonotoneCurve::new(&[(f64::NAN, 0.0), (0.2, f64::INFINITY), (0.5, 0.5)]); + assert!(bad.is_identity()); + // Differences between these would overflow to infinity and give NaN slopes. + let far = MonotoneCurve::new(&[(-1e308, -1e308), (0.5, 0.25), (1e308, 1e308)]); + assert!(far.is_identity()); + let wide = MonotoneCurve::new(&[(-1e6, -1e6), (1e6, 1e6), (0.0, 0.5)]); + assert!(wide.eval(3.0).is_finite()); + // A subnormal secant next to a normal one: the tangent over that secant overflows. + let flat_then_steep = MonotoneCurve::new(&[(0.0, 0.0), (0.5, 1e-310), (1.0, 1.0)]); + assert!(flat_then_steep.knots().flatten().all(f64::is_finite)); + let mut prev = -1.0; + for i in 0..=1000 { + let y = flat_then_steep.eval(f64::from(i) / 1000.0); + assert!(y.is_finite() && y >= prev, "{i} {y}"); + prev = y; + } + } +} diff --git a/crates/color-transform/src/lib.rs b/crates/color-transform/src/lib.rs new file mode 100644 index 0000000000..1c37910009 --- /dev/null +++ b/crates/color-transform/src/lib.rs @@ -0,0 +1,23 @@ +//! Color transforms for the Craft apps. +//! +//! - [`Mat3`], CIE [`Xy`] chromaticities and RGB spaces defined by primaries and a white point +//! ([`RgbSpace`]), with Bradford chromatic adaptation ([`bradford`]). +//! - White balance by correlated color temperature and tint ([`cct`]). +//! - OkLab and OkLCh ([`oklab`]). +//! - Monotone tone curves and 1D lookup tables ([`curve`]). +//! - PQ tone mapping with the reference EETF of Report ITU-R BT.2408 Annex 5 ([`tonemap`]). +//! +//! Frame color tags, Y'CbCr matrices and the transfer functions (sRGB, PQ, HLG) are in +//! `craft-color`; this crate builds on them. Every function that runs per pixel works on `f32` +//! without allocating, and builds for `wasm32-unknown-unknown`. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod cct; +pub mod curve; +pub mod oklab; +mod space; +pub mod tonemap; + +pub use space::{ + ADOBE_RGB, BRADFORD, D50, D65, DISPLAY_P3, Mat3, PROPHOTO, REC601_525, REC601_625, REC2020, RgbSpace, SRGB, Xy, bradford, bradford_xyz, +}; diff --git a/crates/color-transform/src/oklab.rs b/crates/color-transform/src/oklab.rs new file mode 100644 index 0000000000..73f5f7e4d5 --- /dev/null +++ b/crates/color-transform/src/oklab.rs @@ -0,0 +1,246 @@ +//! OkLab and OkLCh, as Björn Ottosson defined them in "A perceptual color space for image +//! processing" (2020). +//! +//! [`from_linear_srgb`] and [`to_linear_srgb`] use the published linear sRGB matrices. +//! [`OkLab::for_space`] builds the conversion for any [`RgbSpace`] from the published XYZ +//! matrix, adapting the space's white to D65 first (Bradford). + +use crate::{D65, Mat3, RgbSpace, bradford}; + +/// XYZ (D65) to approximate cone responses (the published M1). +pub const XYZ_TO_LMS: Mat3 = + Mat3([[0.8189330101, 0.3618667424, -0.1288597137], [0.0329845436, 0.9293118715, 0.0361456387], [0.0482003018, 0.2643662691, 0.6338517070]]); +/// Cube-rooted cone responses to Lab (the published M2). +pub const LMS_TO_LAB: Mat3 = + Mat3([[0.2104542553, 0.7936177850, -0.0040720468], [1.9779984951, -2.4285922050, 0.4505937099], [0.0259040371, 0.7827717662, -0.8086757660]]); + +/// Linear sRGB to cone responses (published with the linear sRGB conversion). +pub const SRGB_TO_LMS: Mat3 = + Mat3([[0.4122214708, 0.5363325363, 0.0514459929], [0.2119034982, 0.6806995451, 0.1073969566], [0.0883024619, 0.2817188376, 0.6299787005]]); +/// Cone responses to linear sRGB (published with the linear sRGB conversion). +pub const LMS_TO_SRGB: Mat3 = + Mat3([[4.0767416621, -3.3077115913, 0.2309699292], [-1.2684380046, 2.6097574011, -0.3413193965], [-0.0041960863, -0.7034186147, 1.7076147010]]); +/// Lab to cube-rooted cone responses (published with the linear sRGB conversion). +pub const LAB_TO_LMS: Mat3 = Mat3([[1.0, 0.3963377774, 0.2158037573], [1.0, -0.1055613458, -0.0638541728], [1.0, -0.0894841775, -1.2914855480]]); + +const fn f32s(m: &Mat3) -> [[f32; 3]; 3] { + let m = &m.0; + [ + [m[0][0] as f32, m[0][1] as f32, m[0][2] as f32], + [m[1][0] as f32, m[1][1] as f32, m[1][2] as f32], + [m[2][0] as f32, m[2][1] as f32, m[2][2] as f32], + ] +} + +const SRGB_LMS: [[f32; 3]; 3] = f32s(&SRGB_TO_LMS); +const LMS_SRGB: [[f32; 3]; 3] = f32s(&LMS_TO_SRGB); +const LAB: [[f32; 3]; 3] = f32s(&LMS_TO_LAB); +const LAB_INV: [[f32; 3]; 3] = f32s(&LAB_TO_LMS); + +#[inline] +fn mul(m: &[[f32; 3]; 3], v: [f32; 3]) -> [f32; 3] { + [ + m[0][0] * v[0] + m[0][1] * v[1] + m[0][2] * v[2], + m[1][0] * v[0] + m[1][1] * v[1] + m[1][2] * v[2], + m[2][0] * v[0] + m[2][1] * v[1] + m[2][2] * v[2], + ] +} + +/// Linear sRGB (BT.709 primaries, D65) to OkLab `[L, a, b]`. +#[inline] +pub fn from_linear_srgb(rgb: [f32; 3]) -> [f32; 3] { + mul(&LAB, mul(&SRGB_LMS, rgb).map(f32::cbrt)) +} + +/// OkLab to linear sRGB. +#[inline] +pub fn to_linear_srgb(lab: [f32; 3]) -> [f32; 3] { + mul(&LMS_SRGB, mul(&LAB_INV, lab).map(|v| v * v * v)) +} + +/// XYZ (D65, white Y = 1) to OkLab. +pub fn from_xyz(xyz: [f64; 3]) -> [f64; 3] { + LMS_TO_LAB.apply(XYZ_TO_LMS.apply(xyz).map(f64::cbrt)) +} + +/// The `f64` matrices [`OkLab::for_space`] starts from: linear RGB in `space` to cone responses +/// (the white adapted to D65 by Bradford), and back. `None` when the first has no inverse. Code +/// that builds its matrices once per render in `f64` uses these with [`LMS_TO_LAB`] and +/// [`LAB_TO_LMS`]. +pub fn lms_matrices(space: &RgbSpace) -> Option<(Mat3, Mat3)> { + let to_lms = XYZ_TO_LMS.mul(&bradford(space.white, D65)).mul(&space.to_xyz()); + to_lms.inverse().map(|from_lms| (to_lms, from_lms)) +} + +/// OkLab to OkLCh `[L, C, h]`, with the hue in radians from −π to π. +#[inline] +pub fn lab_to_lch(lab: [f32; 3]) -> [f32; 3] { + [lab[0], lab[1].hypot(lab[2]), lab[2].atan2(lab[1])] +} + +/// OkLCh to OkLab. +#[inline] +pub fn lch_to_lab(lch: [f32; 3]) -> [f32; 3] { + let (s, c) = lch[2].sin_cos(); + [lch[0], lch[1] * c, lch[1] * s] +} + +/// The OkLab conversion for one RGB space, with its matrices precomputed in `f32`. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct OkLab { + to_lms: [[f32; 3]; 3], + from_lms: [[f32; 3]; 3], +} + +impl OkLab { + /// The conversion for linear RGB in `space`. + pub fn for_space(space: &RgbSpace) -> OkLab { + let pair = lms_matrices(space).map(|(to_lms, from_lms)| (to_lms.to_f32(), from_lms.to_f32())); + let finite = |m: &[[f32; 3]; 3]| m.iter().flatten().all(|v| v.is_finite()); + match pair { + Some((to_lms, from_lms)) if finite(&to_lms) && finite(&from_lms) => OkLab { to_lms, from_lms }, + // A matrix with no inverse or with an entry beyond f32 falls back to XYZ_TO_LMS + // alone, the matrix a space gets when its primaries and its white are both rejected. + _ => OkLab { to_lms: f32s(&XYZ_TO_LMS), from_lms: XYZ_TO_LMS.inverse().unwrap_or(Mat3::IDENTITY).to_f32() }, + } + } + + /// The `f32` matrices, row-major: linear RGB to cone responses, and back. GPU passes that + /// convert to OkLab use these with [`LMS_TO_LAB`]. + pub fn matrices(&self) -> ([[f32; 3]; 3], [[f32; 3]; 3]) { + (self.to_lms, self.from_lms) + } + + /// Linear RGB to OkLab. + #[inline] + pub fn from_rgb(&self, rgb: [f32; 3]) -> [f32; 3] { + mul(&LAB, mul(&self.to_lms, rgb).map(f32::cbrt)) + } + + /// OkLab to linear RGB. + #[inline] + pub fn to_rgb(&self, lab: [f32; 3]) -> [f32; 3] { + mul(&self.from_lms, mul(&LAB_INV, lab).map(|v| v * v * v)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{D50, REC2020, SRGB, Xy}; + + /// The table of XYZ values and their OkLab coordinates in Ottosson's definition, rounded + /// to three decimals. + #[test] + fn xyz_matches_the_published_table() { + let table = [ + ([0.950, 1.000, 1.089], [1.000, 0.000, 0.000]), + ([1.000, 0.000, 0.000], [0.450, 1.236, -0.019]), + ([0.000, 1.000, 0.000], [0.922, -0.671, 0.263]), + ([0.000, 0.000, 1.000], [0.153, -1.415, -0.449]), + ]; + for (xyz, want) in table { + let lab = from_xyz(xyz); + for i in 0..3 { + assert!((lab[i] - want[i]).abs() < 6e-4, "{xyz:?} -> {lab:?}"); + } + } + } + + /// CSS Color Module Level 4 (section 2, "These are exactly equivalent") gives #7654CD as + /// oklab(54.43237% 0.0681665 -0.1656703). + #[test] + fn an_srgb_color_matches_css_color_4() { + let rgb = [0x76, 0x54, 0xCD].map(|v: u8| craft_color::srgb_to_linear(f32::from(v) / 255.0)); + let lab = from_linear_srgb(rgb); + let want = [0.5443237, 0.0681665, -0.1656703]; + for i in 0..3 { + assert!((lab[i] - want[i]).abs() < 2e-4, "{lab:?}"); + } + } + + #[test] + fn the_srgb_matrices_and_the_space_built_from_xyz_agree() { + let srgb = OkLab::for_space(&SRGB); + for c in [[0.1, 0.5, 0.9], [1.0, 0.0, 0.0], [0.02, 0.03, 0.01], [1.0, 1.0, 1.0]] { + let a = from_linear_srgb(c); + let b = srgb.from_rgb(c); + for i in 0..3 { + assert!((a[i] - b[i]).abs() < 2e-4, "{c:?} {a:?} {b:?}"); + } + } + } + + #[test] + fn white_is_l1_and_grays_have_no_chroma_in_every_space() { + let d50 = crate::RgbSpace { white: D50, ..SRGB }; + for space in [SRGB, REC2020, d50] { + let ok = OkLab::for_space(&space); + let w = ok.from_rgb([1.0, 1.0, 1.0]); + assert!((w[0] - 1.0).abs() < 2e-4 && w[1].abs() < 2e-4 && w[2].abs() < 2e-4, "{space:?} {w:?}"); + let g = ok.from_rgb([0.18, 0.18, 0.18]); + assert!(g[1].abs() < 2e-4 && g[2].abs() < 2e-4, "{space:?} {g:?}"); + } + } + + #[test] + fn conversions_round_trip() { + let ok = OkLab::for_space(&REC2020); + for c in [[0.1, 0.5, 0.9], [1.0, 0.0, 0.0], [0.02, 0.03, 0.01], [2.0, 1.5, 0.3], [-0.1, 0.4, 0.2]] { + let a = to_linear_srgb(from_linear_srgb(c)); + let b = ok.to_rgb(ok.from_rgb(c)); + for i in 0..3 { + assert!((a[i] - c[i]).abs() < 1e-4 * c[i].abs().max(1.0), "{c:?} {a:?}"); + assert!((b[i] - c[i]).abs() < 1e-4 * c[i].abs().max(1.0), "{c:?} {b:?}"); + } + } + let lab = lch_to_lab(lab_to_lch([0.5, 0.1, -0.05])); + assert!((lab[1] - 0.1).abs() < 1e-6 && (lab[2] + 0.05).abs() < 1e-6); + } + + #[test] + fn a_space_that_does_not_exist_does_not_produce_nan() { + let bad = crate::RgbSpace { r: Xy::new(0.3, 0.0), ..SRGB }; + let ok = OkLab::for_space(&bad); + assert!(ok.from_rgb([0.5, 0.5, 0.5]).iter().all(|v| v.is_finite())); + } + + #[test] + fn the_f64_matrices_match_the_published_xyz_conversion() { + let (to, from) = lms_matrices(&SRGB).unwrap(); + let p = to.mul(&from); + for i in 0..3 { + for j in 0..3 { + assert!((p.0[i][j] - if i == j { 1.0 } else { 0.0 }).abs() < 1e-12, "{p:?}"); + } + } + // The published linear sRGB matrix and the one built from XYZ differ by at most 3.1e-4. + for i in 0..3 { + for j in 0..3 { + assert!((to.0[i][j] - SRGB_TO_LMS.0[i][j]).abs() < 5e-4, "{to:?}"); + } + } + for c in [[0.1, 0.5, 0.9], [1.0, 1.0, 1.0]] { + let lab = LMS_TO_LAB.apply(to.apply(c).map(f64::cbrt)); + let f = OkLab::for_space(&SRGB).from_rgb(c.map(|v| v as f32)); + for i in 0..3 { + assert!((lab[i] - f64::from(f[i])).abs() < 1e-5, "{c:?} {lab:?} {f:?}"); + } + } + let bad = crate::RgbSpace { r: Xy::new(0.3, 0.0), ..SRGB }; + assert!(lms_matrices(&bad).is_some_and(|(t, _)| t.0.iter().flatten().all(|v| v.is_finite()))); + } + + /// A white this close to y = 0 gives `f64` matrices with entries beyond `f32`. + #[test] + fn a_space_whose_matrices_overflow_f32_does_not_produce_nan() { + let far = crate::RgbSpace { white: Xy::new(0.5, 1e-40), ..SRGB }; + let ok = OkLab::for_space(&far); + let (to, from) = ok.matrices(); + assert!(to.iter().chain(from.iter()).flatten().all(|v| v.is_finite()), "{to:?} {from:?}"); + for c in [[0.0; 3], [0.5, 0.5, 0.5], [1.0, 0.0, 0.0]] { + assert!(ok.from_rgb(c).iter().all(|v| v.is_finite()), "{c:?}"); + assert!(ok.to_rgb(ok.from_rgb(c)).iter().all(|v| v.is_finite()), "{c:?}"); + } + } +} diff --git a/crates/color-transform/src/space.rs b/crates/color-transform/src/space.rs new file mode 100644 index 0000000000..d067d878a5 --- /dev/null +++ b/crates/color-transform/src/space.rs @@ -0,0 +1,312 @@ +//! 3×3 matrices, chromaticities, RGB spaces and Bradford chromatic adaptation. + +use craft_cms::math; +use craft_color::Primaries; +use serde::{Deserialize, Serialize}; + +/// A row-major 3×3 matrix. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub struct Mat3(pub [[f64; 3]; 3]); + +impl Mat3 { + pub const IDENTITY: Mat3 = Mat3([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]); + + pub const fn diag(a: f64, b: f64, c: f64) -> Mat3 { + Mat3([[a, 0.0, 0.0], [0.0, b, 0.0], [0.0, 0.0, c]]) + } + + /// `self × o`: applying the product applies `o` first ([`math::mul`]). + pub fn mul(&self, o: &Mat3) -> Mat3 { + Mat3(math::mul(&self.0, &o.0)) + } + + /// [`math::apply`]. + pub fn apply(&self, v: [f64; 3]) -> [f64; 3] { + math::apply(&self.0, v) + } + + /// [`math::det`]. + pub fn determinant(&self) -> f64 { + math::det(&self.0) + } + + /// The inverse, or `None` when the matrix holds a non-finite value, is singular to within + /// rounding (the determinant is at most 1e-12 times the cube of the largest entry), or has an + /// inverse with an entry beyond the range of `f64` ([`math::inverse`]). + pub fn inverse(&self) -> Option { + math::inverse(&self.0).map(Mat3) + } + + pub fn transpose(&self) -> Mat3 { + let m = &self.0; + Mat3([[m[0][0], m[1][0], m[2][0]], [m[0][1], m[1][1], m[2][1]], [m[0][2], m[1][2], m[2][2]]]) + } + + /// The matrix as `f32`, row-major, for per-pixel code and GPU uniforms. + pub fn to_f32(&self) -> [[f32; 3]; 3] { + self.0.map(|r| r.map(|v| v as f32)) + } +} + +/// A row-major array, the form `craft_cms::math::Mat3` takes. +impl From<[[f64; 3]; 3]> for Mat3 { + fn from(m: [[f64; 3]; 3]) -> Mat3 { + Mat3(m) + } +} + +impl From for [[f64; 3]; 3] { + fn from(m: Mat3) -> [[f64; 3]; 3] { + m.0 + } +} + +/// A CIE 1931 xy chromaticity. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub struct Xy { + pub x: f64, + pub y: f64, +} + +impl Xy { + pub const fn new(x: f64, y: f64) -> Xy { + Xy { x, y } + } + + /// XYZ with Y = 1, or `None` when `y` is not above 0, a coordinate is not finite, or `y` is + /// so small that X or Z overflows. + pub fn to_xyz(self) -> Option<[f64; 3]> { + if !(self.x.is_finite() && self.y.is_finite() && self.y > 0.0) { + return None; + } + let xyz = math::xy_to_xyz([self.x, self.y]); + xyz.iter().all(|v| v.is_finite()).then_some(xyz) + } + + /// The chromaticity of an XYZ triple, or `None` when X + Y + Z is not above 0. + pub fn from_xyz(v: [f64; 3]) -> Option { + let s = v[0] + v[1] + v[2]; + if !(s.is_finite() && s > 0.0) { + return None; + } + Some(Xy::new(v[0] / s, v[1] / s)) + } +} + +/// CIE standard illuminant D65, as ITU-R BT.709 and BT.2020 give it. +pub const D65: Xy = Xy::new(0.3127, 0.3290); +/// CIE illuminant D50, the ICC profile connection space white. +pub const D50: Xy = Xy::new(0.3457, 0.3585); + +/// An RGB color space: three primaries and a white point. A space read at run time (from a +/// file's primaries or a stream's mastering metadata) is built with a struct literal. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub struct RgbSpace { + pub r: Xy, + pub g: Xy, + pub b: Xy, + pub white: Xy, +} + +/// The ITU-R BT.709 primaries, which sRGB (IEC 61966-2-1) shares. +pub const SRGB: RgbSpace = RgbSpace { r: Xy::new(0.64, 0.33), g: Xy::new(0.30, 0.60), b: Xy::new(0.15, 0.06), white: D65 }; +/// ITU-R BT.601, 625-line systems. +pub const REC601_625: RgbSpace = RgbSpace { r: Xy::new(0.64, 0.33), g: Xy::new(0.29, 0.60), b: Xy::new(0.15, 0.06), white: D65 }; +/// ITU-R BT.601, 525-line systems. +pub const REC601_525: RgbSpace = RgbSpace { r: Xy::new(0.630, 0.340), g: Xy::new(0.310, 0.595), b: Xy::new(0.155, 0.070), white: D65 }; +/// ITU-R BT.2020 (and BT.2100). +pub const REC2020: RgbSpace = RgbSpace { r: Xy::new(0.708, 0.292), g: Xy::new(0.170, 0.797), b: Xy::new(0.131, 0.046), white: D65 }; +/// The DCI-P3 primaries with a D65 white (SMPTE EG 432-1). +pub const DISPLAY_P3: RgbSpace = RgbSpace { r: Xy::new(0.680, 0.320), g: Xy::new(0.265, 0.690), b: Xy::new(0.150, 0.060), white: D65 }; +/// ProPhoto RGB (ROMM RGB, ISO 22028-2) primaries with a D50 white. +pub const PROPHOTO: RgbSpace = + RgbSpace { r: Xy::new(0.734699, 0.265301), g: Xy::new(0.159597, 0.840403), b: Xy::new(0.036598, 0.000105), white: D50 }; +/// The 1998 Adobe RGB primaries with a D65 white. +pub const ADOBE_RGB: RgbSpace = RgbSpace { r: Xy::new(0.64, 0.33), g: Xy::new(0.21, 0.71), b: Xy::new(0.15, 0.06), white: D65 }; + +impl From for RgbSpace { + fn from(p: Primaries) -> RgbSpace { + match p { + Primaries::Bt709 => SRGB, + Primaries::Bt601_625 => REC601_625, + Primaries::Bt601_525 => REC601_525, + Primaries::Bt2020 => REC2020, + Primaries::P3D65 => DISPLAY_P3, + } + } +} + +impl RgbSpace { + /// Linear RGB in this space to XYZ, with the space's white at Y = 1 (the normalized primary + /// matrix of SMPTE RP 177). Primaries or a white that do not define a space (collinear + /// primaries, a `y` of 0, a white on a primary or on the line between two) give the identity. + pub fn to_xyz(&self) -> Mat3 { + let (Some(r), Some(g), Some(b), Some(w)) = (self.r.to_xyz(), self.g.to_xyz(), self.b.to_xyz(), self.white.to_xyz()) else { + return Mat3::IDENTITY; + }; + let m = Mat3([[r[0], g[0], b[0]], [r[1], g[1], b[1]], [r[2], g[2], b[2]]]); + let Some(inv) = m.inverse() else { return Mat3::IDENTITY }; + let s = inv.apply(w); + let npm = m.mul(&Mat3::diag(s[0], s[1], s[2])); + // A white that gives a primary no weight leaves a matrix with no inverse. + if npm.inverse().is_none() { Mat3::IDENTITY } else { npm } + } + + /// XYZ to linear RGB in this space: the inverse of [`to_xyz`](Self::to_xyz). + pub fn from_xyz(&self) -> Mat3 { + self.to_xyz().inverse().unwrap_or(Mat3::IDENTITY) + } + + /// The luminance weights of R, G and B (the Y row of [`to_xyz`](Self::to_xyz)). + pub fn luma(&self) -> [f64; 3] { + self.to_xyz().0[1] + } + + /// Linear RGB in `self` to linear RGB in `dst`, Bradford-adapting between the white points. + pub fn to_space(&self, dst: &RgbSpace) -> Mat3 { + dst.from_xyz().mul(&bradford(self.white, dst.white)).mul(&self.to_xyz()) + } +} + +/// The Bradford cone response matrix (XYZ to sharpened cone responses). +pub const BRADFORD: Mat3 = Mat3(math::BRADFORD); + +/// The Bradford chromatic adaptation matrix (XYZ to XYZ) from white `src` to white `dst`. A +/// white that [`Xy::to_xyz`] rejects gives the identity. +pub fn bradford(src: Xy, dst: Xy) -> Mat3 { + if src == dst { + return Mat3::IDENTITY; + } + match (src.to_xyz(), dst.to_xyz()) { + (Some(s), Some(d)) => bradford_xyz(s, d), + _ => Mat3::IDENTITY, + } +} + +/// [`bradford`] for whites given as XYZ. A white with a cone response that is not above 0 or +/// not finite, or a pair whose response ratios overflow, gives the identity. +pub fn bradford_xyz(src: [f64; 3], dst: [f64; 3]) -> Mat3 { + math::bradford_checked(src, dst).map_or(Mat3::IDENTITY, Mat3) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn close(a: &Mat3, b: &[[f64; 3]; 3], tol: f64) { + for i in 0..3 { + for j in 0..3 { + assert!((a.0[i][j] - b[i][j]).abs() < tol, "[{i}][{j}] {} vs {}", a.0[i][j], b[i][j]); + } + } + } + + /// IEC 61966-2-1 gives the sRGB RGB to XYZ matrix to four decimals. + #[test] + fn srgb_matrix_matches_iec_61966_2_1() { + close(&SRGB.to_xyz(), &[[0.4124, 0.3576, 0.1805], [0.2126, 0.7152, 0.0722], [0.0193, 0.1192, 0.9505]], 1e-4); + } + + /// ITU-R BT.2020 and BT.709 give the luminance coefficients. + #[test] + fn luminance_weights_match_bt709_and_bt2020() { + for (space, want) in [(SRGB, [0.2126, 0.7152, 0.0722]), (REC2020, [0.2627, 0.6780, 0.0593])] { + let l = space.luma(); + for i in 0..3 { + assert!((l[i] - want[i]).abs() < 1e-4, "{space:?} {l:?}"); + } + } + } + + /// The Bradford matrix from D65 to D50 as Lindbloom's chromatic adaptation tables print it, + /// with his reference whites. + #[test] + fn bradford_d65_to_d50_matches_the_published_matrix() { + let m = bradford_xyz([0.95047, 1.0, 1.08883], [0.96422, 1.0, 0.82521]); + let want = [[1.0478112, 0.0228866, -0.0501270], [0.0295424, 0.9904844, -0.0170491], [-0.0092345, 0.0150436, 0.7521316]]; + close(&m, &want, 1e-6); + } + + #[test] + fn adaptation_maps_white_to_white_and_round_trips() { + let m = bradford(D65, D50); + let w = m.apply(D65.to_xyz().unwrap()); + let d50 = D50.to_xyz().unwrap(); + for i in 0..3 { + assert!((w[i] - d50[i]).abs() < 1e-12); + } + let back = bradford(D50, D65).mul(&m); + close(&back, &Mat3::IDENTITY.0, 1e-12); + let v = SRGB.to_space(&REC2020).apply([1.0, 1.0, 1.0]); + assert!(v.iter().all(|c| (c - 1.0).abs() < 1e-9), "{v:?}"); + } + + /// The BT.709 to BT.2020 conversion matrix of Report ITU-R BT.2087 (equation 2). + #[test] + fn bt709_to_bt2020_matches_bt2087() { + let want = [[0.6274, 0.3293, 0.0433], [0.0691, 0.9195, 0.0114], [0.0164, 0.0880, 0.8956]]; + close(&SRGB.to_space(&REC2020), &want, 1e-4); + } + + #[test] + fn primaries_tags_map_to_spaces() { + assert_eq!(RgbSpace::from(Primaries::Bt2020), REC2020); + assert_eq!(RgbSpace::from(Primaries::default()), SRGB); + } + + #[test] + fn hostile_whites_and_primaries_do_not_produce_nan() { + assert_eq!(Xy::new(0.3, 0.0).to_xyz(), None); + assert_eq!(Xy::new(f64::NAN, 0.3).to_xyz(), None); + assert_eq!(Xy::new(0.3, 1e-320).to_xyz(), None); + assert_eq!(bradford(Xy::new(0.3, 1e-320), D65), Mat3::IDENTITY); + assert_eq!(Xy::from_xyz([0.0, 0.0, 0.0]), None); + assert_eq!(bradford(Xy::new(0.3, 0.0), D65), Mat3::IDENTITY); + assert_eq!(bradford(D65, Xy::new(f64::INFINITY, 0.3)), Mat3::IDENTITY); + assert_eq!(bradford_xyz([-1.0, 1.0, 1.0], [0.95, 1.0, 1.09]), Mat3::IDENTITY); + let flat = RgbSpace { r: Xy::new(0.2, 0.2), g: Xy::new(0.3, 0.3), b: Xy::new(0.4, 0.4), white: D65 }; + assert_eq!(flat.to_xyz(), Mat3::IDENTITY); + assert_eq!(Mat3([[f64::NAN; 3]; 3]).inverse(), None); + // The determinant, 1e-312, has no finite reciprocal; the inverse itself is finite. + let tiny = Mat3::diag(1e-104, 1e-104, 1e-104).inverse().unwrap(); + close(&tiny, &[[1e104, 0.0, 0.0], [0.0, 1e104, 0.0], [0.0, 0.0, 1e104]], 1e93); + // A determinant beyond f64 in either direction still leaves a finite inverse. + let small = Mat3::diag(1e-200, 1e-200, 1e-200).inverse().unwrap(); + close(&small, &Mat3::diag(1e200, 1e200, 1e200).0, 1e186); + let large = Mat3::diag(1e110, 1e110, 1e110).inverse().unwrap(); + close(&large, &Mat3::diag(1e-110, 1e-110, 1e-110).0, 1e-124); + // An inverse with entries beyond f64 is no inverse. + assert_eq!(Mat3::diag(1e-310, 1e-310, 1e-310).inverse(), None); + assert_eq!(Mat3([[0.0; 3]; 3]).inverse(), None); + assert_eq!(Mat3::diag(f64::MAX, f64::MAX, f64::MAX).inverse().map(|m| m.0[0][0]), Some(1.0 / f64::MAX)); + // A white on a primary leaves two primaries with no weight: no space. + let on_red = RgbSpace { white: SRGB.r, ..SRGB }; + assert_eq!(on_red.to_xyz(), Mat3::IDENTITY); + assert_eq!(on_red.from_xyz(), Mat3::IDENTITY); + // Cone responses this small give scale ratios beyond f64. + assert_eq!(bradford_xyz([1e-320; 3], D65.to_xyz().unwrap()), Mat3::IDENTITY); + } + + /// A space built from primaries read at run time saves and loads, and its matrix converts + /// to and from a plain array. + #[test] + fn spaces_save_and_load() { + let read = RgbSpace { r: Xy::new(0.7, 0.29), g: Xy::new(0.16, 0.8), b: Xy::new(0.13, 0.05), white: D50 }; + let json = serde_json::to_string(&read).unwrap(); + assert_eq!(serde_json::from_str::(&json).unwrap(), read); + let m: [[f64; 3]; 3] = read.to_xyz().into(); + assert_eq!(Mat3::from(m), read.to_xyz()); + } + + #[test] + fn prophoto_and_adobe_rgb_map_white_and_roundtrip() { + for sp in [PROPHOTO, ADOBE_RGB] { + let w = sp.to_space(&REC2020).apply([1.0, 1.0, 1.0]); + assert!(w.iter().all(|c| (c - 1.0).abs() < 1e-3), "{sp:?} -> {w:?}"); + } + let a = SRGB.to_space(&PROPHOTO); + let b = PROPHOTO.to_space(&SRGB); + let v = b.apply(a.apply([0.2, 0.5, 0.9])); + assert!((v[0] - 0.2).abs() < 1e-9 && (v[1] - 0.5).abs() < 1e-9 && (v[2] - 0.9).abs() < 1e-9); + } +} diff --git a/crates/color-transform/src/tonemap.rs b/crates/color-transform/src/tonemap.rs new file mode 100644 index 0000000000..45bafd2cd5 --- /dev/null +++ b/crates/color-transform/src/tonemap.rs @@ -0,0 +1,406 @@ +//! PQ tone mapping with the reference EETF of Report ITU-R BT.2408-8, Annex 5 ("Displaying +//! PQ: calculating the EETF"), which Report ITU-R BT.2390 described before it. +//! +//! The EETF works on PQ (SMPTE ST 2084) values: it keeps the range below a knee 1:1, rolls off +//! the range above it with a Hermite spline so the source peak lands on the target peak, and +//! lifts the target black with a tapered offset. [`ToneMap`] applies it to RGB by max(R, G, B), +//! the "maxRGB" option of the Annex, as a ratio, so hue and saturation are kept. + +use craft_color::{REFERENCE_WHITE_NITS, pq_eotf, pq_inverse_eotf}; + +/// PQ code value (0..1) of `nits`. +fn pq(nits: f64) -> f64 { + f64::from(pq_inverse_eotf((nits / 10_000.0).clamp(0.0, 1.0) as f32)) +} + +/// Luminance in nits of the PQ value `e`. +fn nits(e: f64) -> f64 { + f64::from(pq_eotf(e.clamp(0.0, 1.0) as f32)) * 10_000.0 +} + +/// A luminance in nits: NaN, negative values and infinity read as 0, 0 and 10 000. +fn sane(n: f64) -> f64 { + if n.is_nan() { 0.0 } else { n.clamp(0.0, 10_000.0) } +} + +/// The reference EETF from a mastering display (black `src_black`, peak `src_peak`) to a +/// target display (black `dst_black`, peak `dst_peak`), all in nits. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Eetf { + pb: f64, + span: f64, + min_lum: f64, + max_lum: f64, + ks: f64, + /// The spline's slope at the knee: 1 within the Annex's range. + slope: f64, + /// The black lift's exponent: 4 within the Annex's range. + lift: f64, + /// The E2 at which the black lift reaches 0: 1 within the Annex's range. + reach: f64, +} + +impl Eetf { + /// Luminances are clamped to 0..10 000 nits. A target black below the source black adds no + /// lift. When the source range is empty the EETF passes values through. + /// + /// When the target peak sits below a third of the source range in PQ, the Annex's knee + /// (KS = 1.5 maxLum − 0.5) is negative. The knee is then 0 and the spline starts with a + /// slope of 3 maxLum, the steepest that keeps it rising. + /// + /// The Annex's black lift, minLum × (1 − E2)⁴, makes the output fall near black when minLum + /// is above 0.25, and lifts the peak well above the target peak. The lift here is + /// minLum × (1 − E2 / r)ᵏ with r = max(T, min(1, 2 − 4 minLum)) and k = min(4, r / minLum), + /// where T, the top of the output, is the smaller of maxLum and 1. Up to minLum 0.25 this is + /// the Annex's lift. Above it, k is the largest exponent that keeps the output rising, r moves + /// continuously from 1 down to T, and from minLum 0.5 the lift reaches 0 at the top of the + /// output. A target black above the source peak reads as the source peak. + /// + /// A target peak at or below the source black would put every output at the source black + /// or above it; the source black then reads as 0. A target black above the target peak + /// reads as the target peak. A target peak that is NaN or not above 0 names no display, and + /// the EETF then passes values through. + pub fn new(src_black: f64, src_peak: f64, dst_black: f64, dst_peak: f64) -> Eetf { + let pb = pq(sane(src_black)); + let pass = Eetf { pb, span: 0.0, min_lum: 0.0, max_lum: 1.0, ks: 1.0, slope: 1.0, lift: 4.0, reach: 1.0 }; + if dst_peak.is_nan() || dst_peak <= 0.0 { + return pass; + } + let (dst_peak, pw) = (sane(dst_peak), pq(sane(src_peak))); + let dst_black = sane(dst_black).min(dst_peak); + if pw <= pb { + return pass; + } + let pb = if pq(dst_peak) <= pb { 0.0 } else { pb }; + let span = pw - pb; + // Step 2. + let min_lum = ((pq(dst_black) - pb) / span).max(0.0); + let max_lum = ((pq(dst_peak) - pb) / span).max(0.0); + let ks = 1.5 * max_lum - 0.5; + let (ks, slope) = if ks >= 0.0 { (ks, 1.0) } else { (0.0, 3.0 * max_lum) }; + // E2 ends at `top`. The output's slope at E2 = 0 is 1 − lift × minLum / reach, so the + // exponent is at most reach / minLum. Up to minLum 0.25 this gives the Annex's (1, 4); both + // change continuously above it, and reach is `top` from minLum 0.5. A target black above + // the source peak (minLum above `top`) reads as the source peak. + let top = max_lum.min(1.0); + let min_lum = min_lum.min(top); + let reach = top.max((2.0 - 4.0 * min_lum).min(1.0)); + let lift = if min_lum > 0.0 { (reach / min_lum).min(4.0) } else { 4.0 }; + Eetf { pb, span, min_lum, max_lum, ks, slope, lift, reach } + } + + /// Whether the EETF passes every value through unchanged. + pub fn is_identity(&self) -> bool { + self.span <= 0.0 || (self.max_lum >= 1.0 && self.min_lum <= 0.0) + } + + /// The EETF on a PQ value (0..1): steps 1 to 5 of the Annex. Unless the EETF is the + /// identity, values above the source peak map as the peak does. + pub fn apply_pq(&self, e: f64) -> f64 { + if self.is_identity() || e.is_nan() { + return e; + } + // Step 1. + let e1 = ((e - self.pb) / self.span).clamp(0.0, 1.0); + // Steps 3 and 4: 1:1 below the knee, the Hermite spline above it. + let e2 = if e1 < self.ks || self.max_lum >= 1.0 { + e1 + } else { + let t = (e1 - self.ks) / (1.0 - self.ks); + let (t2, t3) = (t * t, t * t * t); + (2.0 * t3 - 3.0 * t2 + 1.0) * self.ks + (t3 - 2.0 * t2 + t) * (1.0 - self.ks) * self.slope + (-2.0 * t3 + 3.0 * t2) * self.max_lum + }; + let e3 = if self.reach == 1.0 && self.lift == 4.0 { + e2 + self.min_lum * (1.0 - e2).powi(4) + } else { + e2 + self.min_lum * (1.0 - e2 / self.reach).max(0.0).powf(self.lift) + }; + // Step 5. + e3 * self.span + self.pb + } + + /// The EETF on a luminance in nits. + pub fn apply_nits(&self, n: f64) -> f64 { + nits(self.apply_pq(pq(sane(n)))) + } +} + +/// The BT.2408 EETF from a display of peak `src_peak` to one of peak `dst_peak` (nits), with +/// black at 0. +pub fn eetf(n: f64, src_peak: f64, dst_peak: f64) -> f64 { + Eetf::new(0.0, src_peak, 0.0, dst_peak).apply_nits(n) +} + +/// Entries in [`ToneMap`]'s table. +const TABLE: usize = 4096; + +/// Tone mapping of linear RGB, where 1.0 is reference white, from a source peak to a target +/// peak. It applies the EETF (black at 0) to max(R, G, B) and scales the three channels by the +/// same ratio. The ratios are tabulated on a PQ axis once, so [`apply`](Self::apply) does not +/// allocate. +#[derive(Clone, Debug, PartialEq)] +pub struct ToneMap { + src_peak: f32, + dst_peak: f32, + white: f32, + identity: bool, + /// Output nits over input nits, at PQ values `i / (TABLE − 1)`. + ratio: Vec, +} + +impl ToneMap { + /// Tone mapping with reference white at `craft_color::REFERENCE_WHITE_NITS` (203 nits, + /// Report ITU-R BT.2408). + pub fn new(src_peak_nits: f64, dst_peak_nits: f64) -> ToneMap { + ToneMap::with_reference_white(src_peak_nits, dst_peak_nits, REFERENCE_WHITE_NITS) + } + + /// Tone mapping where 1.0 is `white_nits`. A white that is not above 0 or not finite reads + /// as 203 nits, and one above 10 000 nits as 10 000. + pub fn with_reference_white(src_peak_nits: f64, dst_peak_nits: f64, white_nits: f64) -> ToneMap { + let (s, d) = (sane(src_peak_nits), sane(dst_peak_nits)); + let white = if white_nits.is_finite() && white_nits > 0.0 { white_nits.min(10_000.0) } else { REFERENCE_WHITE_NITS }; + let curve = Eetf::new(0.0, s, 0.0, d); + let identity = curve.is_identity(); + let ratio = if identity { + Vec::new() + } else { + (0..TABLE) + .map(|i| { + let e = i as f64 / (TABLE - 1) as f64; + let n = nits(e); + if n <= 1e-6 { 1.0 } else { (curve.apply_nits(n) / n) as f32 } + }) + .collect() + }; + ToneMap { src_peak: s as f32, dst_peak: d as f32, white: white as f32, identity, ratio } + } + + /// Whether [`apply`](Self::apply) returns its input (the target peak is at or above the + /// source peak). + pub fn is_identity(&self) -> bool { + self.identity + } + + /// One pixel of linear RGB, 1.0 = reference white. + #[inline] + pub fn apply(&self, c: [f32; 3]) -> [f32; 3] { + if self.identity { + return c; + } + let m = c[0].max(c[1]).max(c[2]); + // NaN (all three channels NaN) and values at or below 0 pass through. + if m.is_nan() || m <= 0.0 { + return c; + } + let n = m * self.white; + if n >= self.src_peak { + // The brightest channel lands on the target peak. Dividing by m first keeps an + // infinite channel (or an n that overflows) from giving infinity times 0. + let peak = self.dst_peak / self.white; + return c.map(|v| if v >= m { peak } else { v / m * peak }); + } + let k = self.lookup(pq_inverse_eotf(n / 10_000.0)); + c.map(|v| v * k) + } + + #[inline] + fn lookup(&self, e: f32) -> f32 { + let last = self.ratio.len().saturating_sub(1); + let p = e.clamp(0.0, 1.0) * last as f32; + let i = (p as usize).min(last.saturating_sub(1)); + let (Some(a), Some(b)) = (self.ratio.get(i), self.ratio.get(i + 1)) else { return 1.0 }; + a + (b - a) * (p - i as f32) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Report ITU-R BT.2408 lists 203 nits (HDR reference white) at 58 % PQ and 1000 nits at + /// 75 %; these check the PQ functions the EETF stands on. + #[test] + fn pq_reference_levels() { + assert!((pq(203.0) - 0.58).abs() < 0.005, "{}", pq(203.0)); + assert!((pq(1000.0) - 0.75).abs() < 0.005, "{}", pq(1000.0)); + assert!((nits(pq(100.0)) - 100.0).abs() < 0.05); + } + + /// Values computed from the Annex 5 equations and the ST 2084 constants by a separate + /// implementation, 1000 nits to 100 nits. + #[test] + fn matches_an_independent_evaluation_of_the_annex() { + let curve = Eetf::new(0.0, 1000.0, 0.0, 100.0); + for (input, want) in ORACLE_1000_TO_100 { + let got = curve.apply_nits(input); + assert!((got - want).abs() <= want * 2e-4 + 1e-4, "{input} nits: {got} vs {want}"); + } + let lifted = Eetf::new(0.0, 1000.0, 0.1, 100.0); + for (input, want) in ORACLE_1000_TO_100_BLACK_01 { + let got = lifted.apply_nits(input); + assert!((got - want).abs() <= want * 2e-4 + 1e-4, "{input} nits: {got} vs {want}"); + } + } + + const ORACLE_1000_TO_100: [(f64, f64); 9] = [ + (0.0, 0.000000), + (1.0, 1.000000), + (10.0, 10.000000), + (50.0, 46.142955), + (100.0, 69.454403), + (203.0, 88.243641), + (400.0, 97.598409), + (700.0, 99.854805), + (1000.0, 100.000000), + ]; + const ORACLE_1000_TO_100_BLACK_01: [(f64, f64); 9] = [ + (0.0, 0.100000), + (1.0, 1.608558), + (10.0, 11.080978), + (50.0, 47.142334), + (100.0, 70.309850), + (203.0, 88.992667), + (400.0, 98.298831), + (700.0, 100.543945), + (1000.0, 100.688420), + ]; + + #[test] + fn the_knee_keeps_low_values_and_lands_the_peak_on_the_target() { + let curve = Eetf::new(0.0, 1000.0, 0.0, 100.0); + assert!((curve.apply_nits(1000.0) - 100.0).abs() < 0.05); + assert!((curve.apply_nits(5000.0) - 100.0).abs() < 0.05, "above the source peak"); + // Below the knee (KS = 1.5 maxLum − 0.5) the EETF is 1:1. + let below = nits(curve.ks * pq(1000.0) * 0.99); + assert!((curve.apply_nits(below) - below).abs() < below * 1e-4, "{below}"); + let mut prev = 0.0; + for i in 0..=1000 { + let out = curve.apply_pq(f64::from(i) / 1000.0 * pq(1000.0)); + assert!(out >= prev - 1e-12, "monotone"); + prev = out; + } + assert!(Eetf::new(0.0, 100.0, 0.0, 1000.0).is_identity()); + assert_eq!(eetf(400.0, 100.0, 1000.0) as f32, nits(pq(400.0)) as f32); + } + + /// 10 000 nits to 1 nit puts the target peak below a third of the source range: the curve + /// still rises from 0 to the target peak and keeps shadows apart. + #[test] + fn a_deep_reduction_keeps_rising_without_crushing_shadows() { + let curve = Eetf::new(0.0, 10_000.0, 0.0, 1.0); + let mut prev = -1.0; + for i in 0..=2000 { + let out = curve.apply_pq(f64::from(i) / 2000.0); + assert!(out > prev || i == 0, "{i}: {out} after {prev}"); + assert!(out >= 0.0); + prev = out; + } + assert!((curve.apply_nits(10_000.0) - 1.0).abs() < 1e-3); + assert!(curve.apply_nits(0.01) > 0.0 && curve.apply_nits(0.01) < curve.apply_nits(0.1)); + } + + #[test] + fn the_black_lift_raises_black_to_the_target_black() { + let curve = Eetf::new(0.0, 1000.0, 0.1, 100.0); + assert!((curve.apply_nits(0.0) - 0.1).abs() < 1e-3, "{}", curve.apply_nits(0.0)); + // The taper (1 − E2)⁴ is small but not 0 at the target peak, so the peak rises a little. + let peak = curve.apply_nits(1000.0); + assert!(peak > 100.0 && peak < 101.0, "{peak}"); + } + + #[test] + fn a_high_target_black_keeps_the_output_rising_and_lands_the_peak() { + let cases = [(100.0, 100.0), (50.0, 100.0), (10.0, 100.0), (1.0, 100.0), (0.1, 1000.0), (40.0, 4000.0), (2000.0, 4000.0)]; + for (black, peak) in cases { + let curve = Eetf::new(0.0, 1000.0, black, peak); + // A target black above the 1000-nit source peak reads as the source peak. + let want = f64::min(black, 1000.0); + assert!((curve.apply_nits(0.0) - want).abs() < 1e-2 * want.max(1.0), "{black} {peak}: {}", curve.apply_nits(0.0)); + // From minLum 0.5 the lift reaches 0 at the top of the output. + let top = curve.apply_nits(1000.0); + if curve.min_lum >= 0.5 { + assert!((top - f64::min(peak, 1000.0)).abs() < 1e-2 * peak, "{black} {peak}: {top}"); + } + let mut last = curve.apply_pq(0.0); + for i in 1..=1000 { + let out = curve.apply_pq(f64::from(i) / 1000.0); + assert!(out >= last - 1e-12, "{black} {peak} at {i}: {out} < {last}"); + last = out; + } + } + } + + #[test] + fn the_output_changes_continuously_with_the_target_black() { + for peak in [100.0, 300.0, 4000.0] { + let at = |black: f64, n: f64| Eetf::new(0.0, 1000.0, black, peak).apply_pq(pq(n)); + // Target blacks 1/20 000 of the target peak's PQ value apart. + let black = |i: u32| nits(f64::from(i) / 20_000.0 * pq(peak)); + for n in [0.0, 1.0, 100.0, 1000.0] { + let mut last = at(0.0, n); + for i in 1..=20_000 { + let out = at(black(i), n); + assert!((out - last).abs() < 1e-3, "peak {peak}, input {n}, black {}: {last} to {out}", black(i)); + last = out; + } + } + } + } + + #[test] + fn tone_map_keeps_hue_and_maps_the_peak() { + let tm = ToneMap::new(1000.0, 203.0); + let peak = 1000.0 / 203.0; + let out = tm.apply([peak, peak * 0.5, peak * 0.25]); + assert!((out[0] - 1.0).abs() < 1e-3, "{out:?}"); + assert!((out[1] / out[0] - 0.5).abs() < 1e-6 && (out[2] / out[0] - 0.25).abs() < 1e-6); + let low = tm.apply([0.02, 0.01, 0.0]); + assert!((low[0] - 0.02).abs() < 1e-4, "{low:?}"); + assert_eq!(tm.apply([0.0, 0.0, 0.0]), [0.0, 0.0, 0.0]); + assert_eq!(tm.apply([-1.0, -2.0, -0.5]), [-1.0, -2.0, -0.5]); + } + + /// With a target peak above the source peak nothing changes, highlights above the source + /// peak included (they are not raised to the target peak). + #[test] + fn a_brighter_target_leaves_colors_alone() { + let tm = ToneMap::new(1000.0, 4000.0); + assert!(tm.is_identity()); + let hot = [1200.0 / 203.0, 0.5, 0.1]; + assert_eq!(tm.apply(hot), hot); + } + + #[test] + fn hostile_values_do_not_panic_or_spread() { + let tm = ToneMap::new(f64::NAN, f64::INFINITY); + assert!(tm.is_identity()); + let huge = ToneMap::with_reference_white(1000.0, 100.0, 1e100); + assert!((huge.apply([1.0, 1.0, 1.0])[0] - 100.0 / 10_000.0).abs() < 1e-4, "{:?}", huge.apply([1.0, 1.0, 1.0])); + let tm = ToneMap::with_reference_white(1000.0, 100.0, -3.0); + // max() skips NaN, so the other channels are mapped by their own maximum. + assert!(tm.apply([f32::NAN, 0.5, 0.5]).iter().skip(1).all(|v| v.is_finite())); + assert_eq!(tm.apply([f32::INFINITY, 0.5, 0.0]), [100.0 / 203.0, 0.0, 0.0]); + assert_eq!(tm.apply([f32::MAX, f32::MAX, 0.0]), [100.0 / 203.0, 100.0 / 203.0, 0.0]); + // A target peak below the source black: the peak still lands on the target peak. + let dim = Eetf::new(100.0, 1000.0, 0.0, 50.0); + assert!((dim.apply_nits(1000.0) - 50.0).abs() < 0.05, "{}", dim.apply_nits(1000.0)); + assert!(Eetf::new(0.0, 1000.0, 500.0, 100.0).apply_nits(0.0) <= 100.0 + 0.05); + let e = Eetf::new(500.0, 100.0, 0.0, 50.0); + assert!(e.is_identity()); + assert_eq!(e.apply_pq(0.3), 0.3); + assert!(Eetf::new(0.0, 1000.0, 0.0, 100.0).apply_pq(f64::NAN).is_nan()); + } + + /// A target peak that is NaN, 0 or negative passes the picture through instead of turning it + /// black. + #[test] + fn a_missing_target_peak_passes_values_through() { + for peak in [f64::NAN, 0.0, -100.0, f64::NEG_INFINITY] { + let tm = ToneMap::new(1000.0, peak); + assert!(tm.is_identity(), "{peak}"); + assert_eq!(tm.apply([0.8, 0.5, 0.1]), [0.8, 0.5, 0.1], "{peak}"); + assert!(Eetf::new(0.0, 1000.0, 0.0, peak).is_identity(), "{peak}"); + } + } +} diff --git a/crates/control/Cargo.toml b/crates/control/Cargo.toml index 0a18ccc99c..5c4a902427 100644 --- a/crates/control/Cargo.toml +++ b/crates/control/Cargo.toml @@ -10,6 +10,12 @@ repository.workspace = true [lints] workspace = true +[features] +default = [] +# AI agents in the open window (desktop): the ArtCraft Launcher's switch, the connection file and +# the `-cli mcp` side that finds the open window. The agent settings build without it. +agents = ["dep:craft-ai-agents", "dep:craft-atomic", "dep:craft-flags", "dep:log"] + [dependencies] serde = { workspace = true } serde_json = { workspace = true } @@ -18,6 +24,16 @@ serde_json = { workspace = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # The per-launch control token: 32 random bytes from the operating system (MIT OR Apache-2.0). getrandom = "0.3" +# The launcher's app list, read in one place for the launcher, the gateway and every app (MIT OR Apache-2.0, ours). +craft-ai-agents = { workspace = true, optional = true } +craft-atomic = { workspace = true, optional = true } +craft-flags = { workspace = true, optional = true } +log = { workspace = true, optional = true } [dev-dependencies] proptest = { workspace = true } +tempfile = "3" + +[[test]] +name = "agents" +required-features = ["agents"] diff --git a/crates/control/src/agents/connection.rs b/crates/control/src/agents/connection.rs new file mode 100644 index 0000000000..0c0bcebc7b --- /dev/null +++ b/crates/control/src/agents/connection.rs @@ -0,0 +1,115 @@ +//! The connection file: where an app's control server listens for agents. + +use std::net::{SocketAddr, TcpStream}; +use std::path::{Path, PathBuf}; +use std::time::Duration; + +use serde::{Deserialize, Serialize}; + +/// The connection file in the app's settings folder, present while the app accepts agents. +pub const CONNECTION_FILE: &str = "agent-connection.json"; +/// The token file the app keeps in its settings folder for agent connections. +pub const TOKEN_FILE: &str = "agent-control.token"; +/// How long [`probe`] waits for a connection to be accepted. +pub const PROBE_TIMEOUT: Duration = Duration::from_millis(500); +/// The version of the [`Connection`] file format. +const CONNECTION_VERSION: u32 = 1; +/// The largest connection file read; anything bigger is not one. +const MAX_CONNECTION_BYTES: u64 = 1 << 20; + +/// What an app publishes in [`CONNECTION_FILE`]: `{"version", "pid", "port", "tokenFile", "app"}`. +/// Holds no secret: the token stays in its own owner-only file. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Connection { + /// The file format's version (1). + pub version: u32, + /// The app's process id: only that process removes the file. + pub pid: u32, + /// The loopback port of the control server. + pub port: u16, + /// The file holding the token. + pub token_file: PathBuf, + /// The app's version, for diagnostics. + pub app: String, +} + +impl Connection { + /// This process's connection on `port`, naming `app` (the app's version string). + pub fn new(port: u16, token_file: PathBuf, app: impl Into) -> Self { + Self { version: CONNECTION_VERSION, pid: std::process::id(), port, token_file, app: app.into() } + } + + /// `127.0.0.1:`. + pub fn addr(&self) -> String { + format!("127.0.0.1:{}", self.port) + } +} + +fn io_error(path: &Path, e: impl std::fmt::Display) -> std::io::Error { + std::io::Error::other(format!("{}: {e}", path.display())) +} + +/// Publishes `c` in `dir` (made if missing), written atomically so a reader never sees half a +/// file. +pub fn write_connection(dir: &Path, c: &Connection) -> std::io::Result<()> { + std::fs::create_dir_all(dir).map_err(|e| io_error(dir, e))?; + let path = dir.join(CONNECTION_FILE); + let text = serde_json::to_string_pretty(c).map_err(|e| io_error(&path, e))?; + craft_atomic::atomic_write(&path, text.as_bytes()).map_err(|e| io_error(&path, e)) +} + +/// The connection published in `dir`, if any. A file of another version, with port 0, or one +/// that does not parse (invalid UTF-8 included) counts as none: the app rewrites it when it next +/// starts listening. Anything but a regular file (a FIFO would block the read) or a file over +/// 1 MiB is an error, and left alone. +pub fn read_connection(dir: &Path) -> std::io::Result> { + let path = dir.join(CONNECTION_FILE); + let meta = match std::fs::metadata(&path) { + Ok(m) => m, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(io_error(&path, e)), + }; + if !meta.is_file() { + return Err(io_error(&path, "not a regular file")); + } + if meta.len() > MAX_CONNECTION_BYTES { + return Err(io_error(&path, format!("larger than {MAX_CONNECTION_BYTES} bytes"))); + } + let bytes = match std::fs::read(&path) { + Ok(b) => b, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(io_error(&path, e)), + }; + Ok(serde_json::from_slice::(&bytes).ok().filter(|c| c.version == CONNECTION_VERSION && c.port != 0)) +} + +/// Removes the connection file in `dir` if process `pid` wrote it (another instance may have +/// published its own since). Errors are ignored: a stale file only fails [`probe`]. +pub fn remove_connection(dir: &Path, pid: u32) { + if let Ok(Some(c)) = read_connection(dir) + && c.pid == pid + { + let _ = std::fs::remove_file(dir.join(CONNECTION_FILE)); + } +} + +/// Removes the connection file in `dir` when nothing listens on its port any more (an app that +/// crashed or quit without removing it), or when it does not read as a connection. A live +/// instance's file is kept, and so is anything but a regular file. Errors are ignored. +pub fn remove_stale_connection(dir: &Path) { + let path = dir.join(CONNECTION_FILE); + let stale = match read_connection(dir) { + Ok(Some(c)) => !probe(c.port, PROBE_TIMEOUT), + Ok(None) => path.is_file(), + Err(_) => false, + }; + if stale { + let _ = std::fs::remove_file(path); + } +} + +/// Whether something accepts connections on loopback `port` within `timeout`; port 0 never does. +pub fn probe(port: u16, timeout: Duration) -> bool { + port != 0 && TcpStream::connect_timeout(&SocketAddr::from(([127, 0, 0, 1], port)), timeout).is_ok() +} diff --git a/crates/control/src/agents/live.rs b/crates/control/src/agents/live.rs new file mode 100644 index 0000000000..49b242ff5d --- /dev/null +++ b/crates/control/src/agents/live.rs @@ -0,0 +1,601 @@ +//! The other end of the link: where an app's `-cli mcp` sends each tool call. +//! +//! [`Live::resolve`] runs before every tool call. It reads the connection file the app publishes +//! while it accepts agents ([`super::write_connection`]), so the app may start, quit or move to another port +//! while the MCP client stays connected, and the client's configuration holds no port and no +//! token: +//! +//! - An app that accepts agents: the call goes to its open window ([`Route::Window`]). The same +//! port, token and process id keep the window client the app made; any change makes a new one. +//! - No app accepts agents, and the person doesn't let agents use it ([`Consent::allowed`] off, or +//! [`AgentSettings::allow_agent_control`] off, under which the app doesn't listen either): a +//! session without a window with only the folders given to the command +//! ([`LiveConfig::read_roots`], [`LiveConfig::write_root`]), as `mcp` always worked. The app's own +//! folders are for agents the person let in. +//! - No app accepts agents, but agents are allowed: the closed-app choice decides +//! ([`LiveConfig::when_closed`], otherwise [`AgentSettings::when_app_closed`]): start the app and +//! wait until it accepts agents, a headless session with the app's agent folders (the command's +//! folders win), or an error that says what to do. +//! +//! Once headless, the session stays headless: documents don't move under the agent. Only a session +//! made with the app's agent folders ends when the person stops letting agents in; the next call +//! gets one with the command's folders. A window route is dropped as soon as no app accepts agents. +//! Lookups are +//! serialised, so concurrent calls start the app once: calls that queued while a start was under +//! way and found no window get that start's error rather than starting the app again; a call made +//! after it tries again. +//! +//! The crate has no async runtime: `resolve` blocks (file reads, a loopback probe, and while an +//! app starts, polling), so an async MCP server calls it from its blocking pool. What a window +//! client and a headless session are stays the app's ([`LiveConfig::new`]'s `connect` and +//! `headless`), and so do the tools and the transport. Errors are the tool call's result: words +//! for the agent, and through it the person, naming the app. + +use std::ffi::{OsStr, OsString}; +use std::fmt; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Mutex, PoisonError}; +use std::time::{Duration, Instant}; + +use super::{AgentSettings, Connection, PROBE_TIMEOUT, WhenAppClosed, probe, read_connection}; + +/// Starts the desktop app; see [`launcher`]. +pub type LaunchFn = Box Result<(), String> + Send + Sync>; +/// What the person allows now, read from the app's settings folder before every call. +pub type ConsentFn = Box Consent + Send + Sync>; +/// Makes the app's client for its open window. +pub type ConnectFn = Box Result + Send + Sync>; +/// Makes the app's headless session with these folders. +pub type HeadlessFn = Box Result + Send + Sync>; + +/// How long a started app gets to accept agents. A first start can take a while (GPU shader +/// caches, a font scan), but MCP clients give a tool call about a minute. +pub const LAUNCH_TIMEOUT: Duration = Duration::from_secs(45); +/// How often a starting app's connection file is looked for. +pub const LAUNCH_POLL: Duration = Duration::from_millis(250); +/// The longest [`LiveConfig::launch_timeout`] used; a longer one is cut to this. +const MAX_LAUNCH_TIMEOUT: Duration = Duration::from_secs(600); +/// The range [`LiveConfig::poll`] is kept in. +const POLL_RANGE: (Duration, Duration) = (Duration::from_millis(1), Duration::from_secs(1)); + +/// The open window a call goes to: from the connection file, with the token from its token file. +#[derive(Clone, PartialEq, Eq)] +#[non_exhaustive] +pub struct Window { + /// The loopback port of the app's control server. + pub port: u16, + /// The app's process id. + pub pid: u32, + /// The control token, as its token file holds it (trimmed). + pub token: String, +} + +impl Window { + /// `127.0.0.1:`. + pub fn addr(&self) -> String { + format!("127.0.0.1:{}", self.port) + } +} + +impl fmt::Debug for Window { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // Never the token: Debug output ends up in logs. + f.debug_struct("Window").field("port", &self.port).field("pid", &self.pid).finish_non_exhaustive() + } +} + +/// Whether the person lets agents use the app now, and its agent settings. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub struct Consent { + /// Agents may use the app: `agents_allowed(mcp_flag(), launcher_allows(""), legacy)` + /// ([`super::agents_allowed`]), with the suite's `AI-MCP` flag the ArtCraft Launcher's switch. + /// [`Live`] also requires [`AgentSettings::allow_agent_control`]. + pub allowed: bool, + /// The app's agent settings (closed-app choice, folders). + pub settings: AgentSettings, +} + +impl Consent { + /// `allowed` with these settings. + pub fn new(allowed: bool, settings: AgentSettings) -> Self { + Self { allowed, settings } + } +} + +/// The folders a headless session may open from and save to. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub struct HeadlessFolders { + /// Folders files may be opened from. + pub read: Vec, + /// The folder files may be saved to. + pub write: Option, +} + +/// Where a tool call goes: the app's window client or its headless session. +#[derive(Clone, Debug)] +pub enum Route { + /// The open window. + Window(W), + /// A session without a window. + Headless(H), +} + +/// What [`Live`] needs from the app. +#[non_exhaustive] +pub struct LiveConfig { + /// The app's name in messages (`"PhotoCraft"`). + pub app_name: String, + /// The app's settings folder: connection file, token file and settings. + pub config_dir: PathBuf, + /// Overrides the app's closed-app choice (the CLI's `--when-closed`). + pub when_closed: Option, + /// Folders a headless session may open from (the CLI's read roots); while agents are allowed + /// they replace the app's read folder. + pub read_roots: Vec, + /// The folder a headless session may save to (the CLI's write root); while agents are allowed + /// it replaces the app's write folder. + pub write_root: Option, + /// Starts the app; `None` when this install can't ([`launcher`] found nothing to start). + pub launch: Option, + /// How long a started app gets to accept agents ([`LAUNCH_TIMEOUT`]; at most 10 minutes). + pub launch_timeout: Duration, + /// How often a starting app is looked for ([`LAUNCH_POLL`]; kept between 1 ms and 1 s). + pub poll: Duration, + consent: ConsentFn, + connect: ConnectFn, + headless: HeadlessFn, +} + +impl LiveConfig { + /// The app `app_name` with its settings in `config_dir`: `consent` reads whether agents may + /// use it, `connect` makes a client for its open window, `headless` a session without one. + pub fn new( + app_name: impl Into, + config_dir: PathBuf, + consent: impl Fn(&Path) -> Consent + Send + Sync + 'static, + connect: impl Fn(&Window) -> Result + Send + Sync + 'static, + headless: impl Fn(&HeadlessFolders) -> Result + Send + Sync + 'static, + ) -> Self { + Self { + app_name: app_name.into(), + config_dir, + when_closed: None, + read_roots: Vec::new(), + write_root: None, + launch: None, + launch_timeout: LAUNCH_TIMEOUT, + poll: LAUNCH_POLL, + consent: Box::new(consent), + connect: Box::new(connect), + headless: Box::new(headless), + } + } +} + +/// The port, token and process id a window client was made for: a restarted app may keep its +/// port and token, but its old socket is gone. +type WindowKey = (u16, String, u32); +/// The route the last call took. +struct Current { + route: Route, + /// For a window, the key its client was made for. + key: Option, + /// A headless session that holds folders of the app's agent settings: it lasts only while + /// the person lets agents in. + app_folders: bool, +} + +/// Resolves each tool call of one MCP session; see the module docs. +pub struct Live { + cfg: LiveConfig, + /// Serialises lookups, so concurrent calls start the app once; holds the last start's error. + lookup: Mutex>, + /// How many starts have finished, read before a call queues for `lookup`. + finished: AtomicU64, + current: Mutex>>, +} + +impl Live { + /// A session that has not resolved anything yet. + pub fn new(cfg: LiveConfig) -> Self { + Self { cfg, lookup: Mutex::new(None), finished: AtomicU64::new(0), current: Mutex::new(None) } + } + + /// The route the last [`Self::resolve`] chose, if any. + pub fn current(&self) -> Option> { + self.current.lock().unwrap_or_else(PoisonError::into_inner).as_ref().map(|c| c.route.clone()) + } + + fn set(&self, route: Route, key: Option, app_folders: bool) -> Route { + *self.current.lock().unwrap_or_else(PoisonError::into_inner) = Some(Current { route: route.clone(), key, app_folders }); + route + } + + /// The headless session to keep using, if any: one made with the app's folders only while + /// agents are still `allowed`. A route that may not be kept is dropped. + fn sticky_headless(&self, allowed: bool) -> Option> { + let mut current = self.current.lock().unwrap_or_else(PoisonError::into_inner); + match current.as_ref() { + Some(c) if matches!(c.route, Route::Headless(_)) && (allowed || !c.app_folders) => Some(c.route.clone()), + Some(c) if matches!(c.route, Route::Headless(_)) => { + *current = None; + None + } + _ => None, + } + } + + /// Forgets a window route: no app accepts agents any more, so its client is for a socket + /// that is gone. + fn drop_window(&self) { + let mut current = self.current.lock().unwrap_or_else(PoisonError::into_inner); + if current.as_ref().is_some_and(|c| matches!(c.route, Route::Window(_))) { + *current = None; + } + } + + /// Where the next tool call goes. Blocks; see the module docs. The error is the tool call's + /// result. + pub fn resolve(&self) -> Result, String> { + let queued_at = self.finished.load(Ordering::Acquire); + let mut last_failure = self.lookup.lock().unwrap_or_else(PoisonError::into_inner); + let consent = (self.cfg.consent)(&self.cfg.config_dir); + // Allow Agent Control off refuses agents, as the app does: it doesn't listen then. + let allowed = consent.allowed && consent.settings.allow_agent_control; + if let Some(route) = self.sticky_headless(allowed) { + return Ok(route); + } + if let Some(window) = self.find_app()? { + return self.use_window(window); + } + self.drop_window(); + let app = &self.cfg.app_name; + if !allowed { + let folders = HeadlessFolders { read: self.cfg.read_roots.clone(), write: self.cfg.write_root.clone() }; + return self.headless(&folders, false, &format!("{app} can't use the automation roots")); + } + match self.cfg.when_closed.unwrap_or(consent.settings.when_app_closed) { + WhenAppClosed::Error => Err(format!("{app} isn't running: open it, then try again.")), + WhenAppClosed::Headless => { + let app_read = self.cfg.read_roots.is_empty() && consent.settings.read_folder().is_some(); + let app_write = self.cfg.write_root.is_none() && consent.settings.write_folder().is_some(); + let read = + if self.cfg.read_roots.is_empty() { consent.settings.read_folder().into_iter().collect() } else { self.cfg.read_roots.clone() }; + let write = self.cfg.write_root.clone().or_else(|| consent.settings.write_folder()); + let why = format!("{app} isn't running and its agent folders can't be used"); + self.headless(&HeadlessFolders { read, write }, app_read || app_write, &why) + } + WhenAppClosed::Launch => { + // A start finished while this call waited and the app still isn't there: its answer + // is this call's too, rather than a second start of an app that may be opening. + if self.finished.load(Ordering::Acquire) != queued_at + && let Some(failure) = last_failure.as_ref() + { + return Err(failure.clone()); + } + let started = self.launch_and_wait(); + *last_failure = started.as_ref().err().cloned(); + self.finished.fetch_add(1, Ordering::AcqRel); + started + } + } + } + + /// A headless session with `folders` from now on; `app_folders` when some of them are the + /// app's agent folders. `why` leads its error. + fn headless(&self, folders: &HeadlessFolders, app_folders: bool, why: &str) -> Result, String> { + let session = (self.cfg.headless)(folders).map_err(|e| format!("{why}: {e}"))?; + Ok(self.set(Route::Headless(session), None, app_folders)) + } + + /// Starts the app and waits until it accepts agents, at most the launch timeout. + fn launch_and_wait(&self) -> Result, String> { + let app = &self.cfg.app_name; + let Some(launch) = &self.cfg.launch else { + return Err(format!("{app} isn't running, and this install can't start it from here: open {app}, then try again.")); + }; + launch().map_err(|e| format!("couldn't start {app}: {e}"))?; + let timeout = self.cfg.launch_timeout.min(MAX_LAUNCH_TIMEOUT); + let started = Instant::now(); + let deadline = started.checked_add(timeout).unwrap_or(started); + let poll = self.cfg.poll.clamp(POLL_RANGE.0, POLL_RANGE.1); + loop { + std::thread::sleep(poll); + if let Some(window) = self.find_app()? { + return self.use_window(window); + } + if Instant::now() >= deadline { + return Err(format!( + "{app} didn't accept agents within {} s of starting. If it's still opening, try again in a moment; otherwise check that AI agents are on in ArtCraft Launcher › Settings › AI agents.", + timeout.as_secs() + )); + } + } + } + + /// The client for `window`: the one made for the same port, token and process, or a new one. + fn use_window(&self, window: Window) -> Result, String> { + let key: WindowKey = (window.port, window.token.clone(), window.pid); + { + let current = self.current.lock().unwrap_or_else(PoisonError::into_inner); + if let Some(c) = current.as_ref() + && c.key.as_ref() == Some(&key) + { + return Ok(c.route.clone()); + } + } + let client = (self.cfg.connect)(&window).map_err(|e| format!("couldn't reach {}'s window: {e}", self.cfg.app_name))?; + Ok(self.set(Route::Window(client), Some(key), false)) + } + + /// The app accepting agents now: `None` when there is no connection file, or one left by an + /// app that crashed (nothing listens on its port). + fn find_app(&self) -> Result, String> { + find_window(&self.cfg.config_dir, &self.cfg.app_name) + } +} + +/// The window accepting agents with its settings in `dir`, with the token from its token file; +/// `None` when no app accepts agents there. `app_name` names the app in errors. +pub fn find_window(dir: &Path, app_name: &str) -> Result, String> { + let Some(c): Option = read_connection(dir).map_err(|e| e.to_string())? else { return Ok(None) }; + if !probe(c.port, PROBE_TIMEOUT) { + return Ok(None); + } + let token = read_token(&c.token_file).map_err(|e| format!("{app_name}'s agent token: {e}"))?; + Ok(Some(Window { port: c.port, pid: c.pid, token })) +} + +/// The largest token file read; a token is far shorter. +const MAX_TOKEN_FILE: u64 = 4096; + +/// The token in the token file at `path`, trimmed and otherwise as written: the app's server +/// compares it exactly ([`crate::auth::tokens_match`]). The file must be a regular file of at most +/// [`MAX_TOKEN_FILE`] bytes holding one token (no spaces or control characters in it), and on Unix +/// readable by its owner only: a token others could read is refused rather than used. +fn read_token(path: &Path) -> Result { + use std::io::Read; + let shown = |e: &dyn fmt::Display| format!("{}: {e}", path.display()); + // Before opening: opening a FIFO for reading blocks until something writes to it. + if !std::fs::metadata(path).map_err(|e| shown(&e))?.is_file() { + return Err(shown(&"not a regular file")); + } + let file = std::fs::File::open(path).map_err(|e| shown(&e))?; + let meta = file.metadata().map_err(|e| shown(&e))?; + if !meta.is_file() { + return Err(shown(&"not a regular file")); + } + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + if meta.mode() & 0o077 != 0 { + return Err(shown(&format!("readable by group or others (mode {:o}); restrict it to its owner", meta.mode() & 0o777))); + } + } + let mut text = String::new(); + file.take(MAX_TOKEN_FILE + 1).read_to_string(&mut text).map_err(|e| shown(&e))?; + if text.len() as u64 > MAX_TOKEN_FILE { + return Err(shown(&format!("larger than {MAX_TOKEN_FILE} bytes"))); + } + let token = text.trim(); + if token.is_empty() || token.chars().any(|c| c.is_whitespace() || c.is_control()) { + return Err(shown(&"not a control token")); + } + Ok(token.to_owned()) +} + +/// How to start the desktop app `exe` (its file name without `.exe`) that ships beside the CLI at +/// `cli`: detached and with no standard streams, since the CLI's carry the MCP protocol and the +/// app must outlive the MCP client. `None` where this install can't start it: inside a Flatpak, +/// or with no app beside the CLI. +pub fn launcher(cli: &Path, exe: &str) -> Option { + use std::process::{Command, Stdio}; + let env = |name: &str| std::env::var_os(name).filter(|v| !v.is_empty()); + // Both sides resolved, so a mount reached through a symlink (`$TMPDIR`) still matches. + let resolved = |p: &Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()); + let install = Install { + platform: Platform::current(), + flatpak: env("FLATPAK_ID").is_some(), + appimage: env("APPIMAGE"), + appdir: env("APPDIR").map(|d| resolved(Path::new(&d)).into_os_string()), + system_root: env("SystemRoot"), + }; + let (program, args) = launch_command(&resolved(cli), exe, &install, &|p| p.is_file())?; + let mut command = Command::new(program); + command.args(args); + let command = Mutex::new(command); + Some(Box::new(move || { + let mut c = command.lock().unwrap_or_else(PoisonError::into_inner); + c.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()); + detach(&mut c); + let child = c.spawn().map_err(|e| e.to_string())?; + reap(child); + Ok(()) + })) +} + +/// Waits for `child` on a thread of its own, so a start helper that exits (`open`, Explorer) or an +/// app that quits before the CLI does is reaped rather than left a zombie until the MCP client +/// ends; the app keeps running on its own either way. Without a thread the child is dropped, as +/// before. +fn reap(mut child: std::process::Child) { + let _ = std::thread::Builder::new().name("app-start-reaper".into()).spawn(move || { + let _ = child.wait(); + }); +} + +/// What [`launch_command`] needs to know about the install, from the environment. +#[derive(Clone, Debug, Default)] +struct Install { + platform: Platform, + /// Inside a Flatpak (`FLATPAK_ID`). + flatpak: bool, + /// The AppImage the running image set (`APPIMAGE`). + appimage: Option, + /// That image's mount point (`APPDIR`). + appdir: Option, + /// `%SystemRoot%` (Windows). + system_root: Option, +} + +/// The platform [`launch_command`] plans for. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +enum Platform { + Windows, + MacOs, + #[default] + Other, +} + +impl Platform { + /// The platform this build runs on. + fn current() -> Self { + if cfg!(windows) { + Self::Windows + } else if cfg!(target_os = "macos") { + Self::MacOs + } else { + Self::Other + } + } +} + +/// The program and arguments that start the app; see [`launcher`]. +fn launch_command(cli: &Path, exe: &str, install: &Install, is_file: &dyn Fn(&Path) -> bool) -> Option<(PathBuf, Vec)> { + let platform = install.platform; + if install.flatpak { + return None; + } + // Only when this CLI runs from that image's mount: a process started from another AppImage + // (an MCP client packaged as one) inherits its `APPIMAGE` too. + if let (Some(image), Some(mount)) = (&install.appimage, &install.appdir) + && cli.starts_with(mount) + { + // The AppImage's mount point goes away with the CLI; the image itself stays. + return Some((PathBuf::from(image), Vec::new())); + } + let dir = cli.parent()?; + if platform == Platform::MacOs + && dir.ends_with("Contents/MacOS") + && let Some(bundle) = dir.parent().and_then(Path::parent) + { + // A macOS app bundle: LaunchServices starts (or brings forward) the app. + return Some((PathBuf::from("/usr/bin/open"), vec![OsString::from("-a"), bundle.as_os_str().to_os_string()])); + } + let app = dir.join(if platform == Platform::Windows { format!("{exe}.exe") } else { exe.to_owned() }); + if !is_file(&app) { + return None; + } + Some(if platform == Platform::Windows { windows_launch(&app, install.system_root.as_deref()) } else { (app, Vec::new()) }) +} + +/// How Windows starts the app at `app`: through Explorer, as a double-click would. The app is then +/// no child of the CLI, so it outlives the MCP client ending the CLI's process tree (Claude Code +/// does on Windows), and it runs outside the container of a Microsoft Store client (Claude +/// Desktop): there, new files under `%APPDATA%` land in the client's package, where the app +/// started from the Start menu would never see them. Explorer reads a comma as a separator, so +/// such a path is started directly. (`%WINDIR%` can be empty under a Store client, hence +/// `%SystemRoot%`.) +fn windows_launch(app: &Path, system_root: Option<&OsStr>) -> (PathBuf, Vec) { + if app.to_string_lossy().contains(',') { + return (app.to_path_buf(), Vec::new()); + } + let root = system_root.filter(|r| !r.is_empty()).map_or_else(|| PathBuf::from(r"C:\Windows"), PathBuf::from); + (root.join("explorer.exe"), vec![app.as_os_str().to_os_string()]) +} + +/// Keeps the app running when the MCP client stops the CLI (and its process group). +fn detach(c: &mut std::process::Command) { + #[cfg(unix)] + { + use std::os::unix::process::CommandExt; + c.process_group(0); + } + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + const DETACHED_PROCESS: u32 = 0x0000_0008; + const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200; + c.creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP); + } + #[cfg(not(any(unix, windows)))] + let _ = c; +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + use super::*; + + fn plan( + cli: &str, + platform: Platform, + flatpak: bool, + appimage: Option<&str>, + root: Option<&str>, + exists: bool, + ) -> Option<(PathBuf, Vec)> { + // An AppImage's mount is where its CLI runs from. + let appdir = appimage.map(|_| OsString::from("/tmp/.mount")); + let install = Install { platform, flatpak, appimage: appimage.map(OsString::from), appdir, system_root: root.map(OsString::from) }; + launch_command(Path::new(cli), "photocraft", &install, &|_| exists) + } + + #[test] + fn windows_starts_the_app_through_explorer() { + let app = Path::new(r"C:\Program Files\PhotoCraft\photocraft.exe"); + let (program, args) = windows_launch(app, Some(OsStr::new(r"D:\Win"))); + assert_eq!(program, Path::new(r"D:\Win").join("explorer.exe")); + assert_eq!(args, [app.as_os_str()]); + // No usable %SystemRoot%: the default Windows folder. + let default = Path::new(r"C:\Windows").join("explorer.exe"); + assert_eq!(windows_launch(app, Some(OsStr::new(""))).0, default); + assert_eq!(windows_launch(app, None).0, default); + // Explorer would split this path at the comma. + let comma = Path::new(r"C:\Apps, tools\photocraft.exe"); + assert_eq!(windows_launch(comma, None), (comma.to_path_buf(), Vec::new())); + } + + #[test] + fn the_launch_command_fits_the_install() { + // Flatpak: the sandbox can't start the app. + assert_eq!(plan("/app/bin/photocraft-cli", Platform::Other, true, Some("/x.AppImage"), None, true), None); + // An AppImage starts its image, not the mount point the CLI runs from. + assert_eq!( + plan("/tmp/.mount/usr/bin/photocraft-cli", Platform::Other, false, Some("/home/a/P.AppImage"), None, false), + Some((PathBuf::from("/home/a/P.AppImage"), Vec::new())) + ); + // Another AppImage's variables (an MCP client packaged as one): the app beside the CLI. + let client = plan("/opt/pc/photocraft-cli", Platform::Other, false, Some("/home/a/Client.AppImage"), None, true); + assert_eq!(client, Some((PathBuf::from("/opt/pc/photocraft"), Vec::new()))); + let no_mount = Install { appimage: Some("/home/a/P.AppImage".into()), ..Install::default() }; + assert_eq!( + launch_command(Path::new("/tmp/.mount/usr/bin/photocraft-cli"), "photocraft", &no_mount, &|_| false), + None, + "APPIMAGE without APPDIR" + ); + // A macOS bundle opens through LaunchServices. + let (program, args) = plan("/Applications/PhotoCraft.app/Contents/MacOS/photocraft-cli", Platform::MacOs, false, None, None, false).unwrap(); + assert_eq!(program, Path::new("/usr/bin/open")); + assert_eq!(args, [OsString::from("-a"), OsString::from("/Applications/PhotoCraft.app")]); + // Beside the CLI, by the app's file name. + assert_eq!(plan("/opt/pc/photocraft-cli", Platform::Other, false, None, None, true), Some((PathBuf::from("/opt/pc/photocraft"), Vec::new()))); + let (program, args) = plan(r"C:\PC\photocraft-cli.exe", Platform::Windows, false, None, Some(r"C:\Win"), true).unwrap(); + assert_eq!(program, Path::new(r"C:\Win").join("explorer.exe")); + // The app beside the CLI (on a non-Windows host the backslashes are no separators). + assert_eq!(args, [Path::new(r"C:\PC\photocraft-cli.exe").parent().unwrap().join("photocraft.exe").into_os_string()]); + // No app beside the CLI: nothing to start. + assert_eq!(plan("/opt/pc/photocraft-cli", Platform::Other, false, None, None, false), None); + assert_eq!(plan("photocraft-cli", Platform::Other, false, None, None, true).map(|p| p.0), Some(PathBuf::from("photocraft"))); + } + + #[test] + fn a_window_never_shows_its_token() { + let w = Window { port: 7, pid: 9, token: "ab".repeat(32) }; + assert!(!format!("{w:?}").contains("abab")); + assert_eq!(w.addr(), "127.0.0.1:7"); + } +} diff --git a/crates/control/src/agents/mod.rs b/crates/control/src/agents/mod.rs new file mode 100644 index 0000000000..e1b0b9923e --- /dev/null +++ b/crates/control/src/agents/mod.rs @@ -0,0 +1,78 @@ +//! AI agents in the open window, with no UI toolkit: whether the person lets agents use the app, +//! how the app tells its own `-cli mcp` where it listens, and where that CLI sends each tool +//! call. +//! +//! One switch serves every Craft app: the ArtCraft Launcher's *Settings › AI agents › Let AI agents +//! use Craft apps*, which writes the launcher's app list (`craft_ai_agents`). With the suite's +//! `AI-MCP` flag on, an app lets agents in when that list names it ([`AgentSwitch`]); without the +//! flag, the app's own earlier consent decides ([`agents_allowed`]). Apps have no agents switch of +//! their own. +//! +//! While agents may connect, the app serves its control channel on loopback with a token +//! ([`crate::server`], [`crate::auth`], or its own) and publishes where in its settings folder +//! ([`CONNECTION_FILE`]): the port, its process id and the token file. The app's `-cli mcp` +//! reads that file before every tool call ([`Live`]), so an MCP client's configuration never holds +//! a port or a token. The file holds no secret. A crashed app can leave it behind; nothing then +//! listens on its port ([`probe`]), and the app's next launch removes it +//! ([`remove_stale_connection`]). An app that quits removes its own ([`remove_connection`]). +//! +//! | Part | Names | Builds | +//! |---|---|---| +//! | Settings | [`AgentSettings`], [`WhenAppClosed`], [`agents_allowed`], [`folder`] | every target | +//! | The launcher's switch | [`AgentSwitch`], [`mcp_flag`], [`launcher_allows`], [`POLL`] | desktop, feature `agents` | +//! | The connection file | [`Connection`], [`write_connection`], [`read_connection`], [`remove_connection`], [`remove_stale_connection`], [`probe`] | desktop, feature `agents` | +//! | The CLI's side | [`Live`], [`LiveConfig`], [`Route`], [`Window`], [`Consent`], [`HeadlessFolders`], [`find_window`], [`launcher`] | desktop, feature `agents` | +//! +//! The same formats as `craft_automation::agents` on the shared UI library's branch (`next`), +//! which adds the egui link host and the Agents page of Preferences. +//! +//! # Adding an app +//! +//! The app (its id is the one in the ArtCraft Launcher's catalog, `"gridcraft"`): +//! +//! 1. At start, [`remove_stale_connection`] in its settings folder, then +//! `AgentSwitch::watch("gridcraft", wake)`, where `wake` asks the UI for a frame. +//! 2. Agents may connect while `settings.allow_agent_control && switch.allows(legacy)` +//! ([`AgentSettings::allow_agent_control`], [`AgentSwitch::allows`]); check it again when the +//! launcher's switch wakes the app and when the person changes the setting. When it turns true, +//! listen on loopback with a token, the token in an owner-only file +//! ([`crate::auth::App::write_token_file`]), and [`write_connection`] with [`Connection::new`]; +//! when it turns false, and when the app quits, stop listening and [`remove_connection`] with +//! the app's process id. No restart, no agents switch of its own. There is one connection file +//! per settings folder: an app that runs more than one process on one folder lets only one of +//! them accept agents. +//! +//! Its `gridcraft-cli mcp`: +//! +//! 1. A [`LiveConfig`] with the app's settings folder; `consent` reads its saved +//! [`AgentSettings`] and answers `agents_allowed(mcp_flag(), launcher_allows("gridcraft"), legacy)` +//! ([`agents_allowed`], [`mcp_flag`], [`launcher_allows`]); `connect` makes the app's client for a +//! [`Window`] (its address and token); `headless` makes its session without a window for +//! [`HeadlessFolders`]. [`launcher`] starts the app beside the CLI. +//! 2. Before every tool call, [`Live::resolve`] (from a blocking thread in an async server), then +//! the call goes to the [`Route`] it returns. `--headless` skips [`Live`] altogether. + +// The links above name items of the `agents` feature; without it they have nothing to point at, +// and the docs of the settings alone still build. +#![cfg_attr(not(all(feature = "agents", not(target_arch = "wasm32"))), allow(rustdoc::broken_intra_doc_links))] + +mod settings; +pub use settings::{AgentSettings, WhenAppClosed, agents_allowed, folder}; + +#[cfg(all(feature = "agents", not(target_arch = "wasm32")))] +mod connection; +#[cfg(all(feature = "agents", not(target_arch = "wasm32")))] +mod live; +#[cfg(all(feature = "agents", not(target_arch = "wasm32")))] +mod switch; +#[cfg(all(feature = "agents", not(target_arch = "wasm32")))] +pub use connection::{ + CONNECTION_FILE, Connection, PROBE_TIMEOUT, TOKEN_FILE, probe, read_connection, remove_connection, remove_stale_connection, write_connection, +}; +#[cfg(all(feature = "agents", not(target_arch = "wasm32")))] +pub use live::{ + ConnectFn, Consent, ConsentFn, HeadlessFn, HeadlessFolders, LAUNCH_POLL, LAUNCH_TIMEOUT, LaunchFn, Live, LiveConfig, Route, Window, find_window, + launcher, +}; +#[cfg(all(feature = "agents", not(target_arch = "wasm32")))] +pub use switch::{AgentSwitch, POLL, launcher_allows, mcp_flag}; diff --git a/crates/control/src/agents/settings.rs b/crates/control/src/agents/settings.rs new file mode 100644 index 0000000000..04ae933aaf --- /dev/null +++ b/crates/control/src/agents/settings.rs @@ -0,0 +1,124 @@ +//! The agent settings an app saves in its preferences, and who decides that agents may connect. + +use std::path::PathBuf; + +use serde::{Deserialize, Serialize}; + +/// The agent settings of an app's Preferences (its `integrations` section in PhotoCraft). Saved in +/// camelCase: `allowAgentControl`, `controlPort`, `readRoot`, `writeRoot`, `whenAppClosed`. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +#[non_exhaustive] +pub struct AgentSettings { + /// Off refuses every control server, `--control` included. + pub allow_agent_control: bool, + /// The loopback port agents connect to; 0 is any free port, published in the connection file. + pub control_port: u32, + /// The folder agents may open from; empty is none. Applied at once while agents are connected. + pub read_root: String, + /// The folder agents may save to; empty is none. Applied at once while agents are connected. + pub write_root: String, + /// What the app's `-cli mcp` does while agents may use the app but it is closed. + pub when_app_closed: WhenAppClosed, +} + +impl Default for AgentSettings { + fn default() -> Self { + Self { + allow_agent_control: true, + control_port: 0, + read_root: String::new(), + write_root: String::new(), + when_app_closed: WhenAppClosed::Launch, + } + } +} + +impl AgentSettings { + /// [`Self::read_root`] as a path ([`folder`]). + pub fn read_folder(&self) -> Option { + folder(&self.read_root) + } + + /// [`Self::write_root`] as a path ([`folder`]). + pub fn write_folder(&self) -> Option { + folder(&self.write_root) + } +} + +/// A folder setting as a path, trimmed; empty or blank is `None`. +pub fn folder(pref: &str) -> Option { + let pref = pref.trim(); + (!pref.is_empty()).then(|| PathBuf::from(pref)) +} + +/// What an app's `-cli mcp` does when an agent calls a tool while the app is closed. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum WhenAppClosed { + /// Open the app and wait for it. + #[default] + Launch, + /// Run the tool without a window. + Headless, + /// Answer with an error that asks to open the app. + Error, +} + +impl WhenAppClosed { + /// The saved names, in [`Self::ALL`] order. + pub const NAMES: &'static [&'static str] = &["launch", "headless", "error"]; + /// Every choice, in the order a menu lists them. + pub const ALL: [Self; 3] = [Self::Launch, Self::Headless, Self::Error]; + + /// The saved name. + pub fn name(self) -> &'static str { + match self { + Self::Launch => "launch", + Self::Headless => "headless", + Self::Error => "error", + } + } + + /// The choice a saved name means; `None` for any other text. + pub fn parse(name: &str) -> Option { + Self::ALL.into_iter().find(|c| c.name() == name) + } +} + +/// Whether agents may use the open window. With the suite's `AI-MCP` flag only the launcher's +/// switch decides: one switch for every Craft app, none in the app. Without the flag the app's own +/// earlier consent decides (`legacy`; PhotoCraft's is a Control Port saved before the switch). +/// Allow Agent Control off still refuses agents either way ([`AgentSettings::allow_agent_control`]). +pub fn agents_allowed(mcp_flag: bool, launcher: bool, legacy: bool) -> bool { + if mcp_flag { launcher } else { legacy } +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + + // The saved names read back as the same choice, and nothing else reads as one. + #[test] + fn when_app_closed_names_round_trip() { + for choice in WhenAppClosed::ALL { + assert_eq!(WhenAppClosed::parse(choice.name()), Some(choice)); + assert_eq!(serde_json::to_value(choice).unwrap(), serde_json::json!(choice.name())); + } + assert_eq!(WhenAppClosed::NAMES, WhenAppClosed::ALL.map(WhenAppClosed::name)); + assert_eq!(WhenAppClosed::parse("Launch"), None); + let settings: AgentSettings = serde_json::from_str(r#"{"whenAppClosed": "headless", "controlPort": 7878}"#).unwrap(); + assert_eq!((settings.when_app_closed, settings.control_port, settings.allow_agent_control), (WhenAppClosed::Headless, 7878, true)); + } + + // A folder setting is trimmed; empty or blank means no folder. + #[test] + fn folder_preferences_are_trimmed() { + assert_eq!(folder(""), None); + assert_eq!(folder(" "), None); + assert_eq!(folder(" /work/project "), Some(PathBuf::from("/work/project"))); + let settings = AgentSettings { read_root: " /in ".into(), ..AgentSettings::default() }; + assert_eq!((settings.read_folder(), settings.write_folder()), (Some(PathBuf::from("/in")), None)); + } +} diff --git a/crates/control/src/agents/switch.rs b/crates/control/src/agents/switch.rs new file mode 100644 index 0000000000..9165879dc3 --- /dev/null +++ b/crates/control/src/agents/switch.rs @@ -0,0 +1,94 @@ +//! The ArtCraft Launcher's switch for one app, behind the suite's `AI-MCP` flag. + +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Weak}; +use std::time::Duration; + +use super::agents_allowed; + +/// How often [`AgentSwitch::watch`] reads the launcher's app list. +pub const POLL: Duration = Duration::from_secs(2); + +/// The suite's `AI-MCP` flag (`ArtCraft/flags.toml` or `CRAFT_FLAGS`). Reads the flags file; +/// what could not be read is logged. +pub fn mcp_flag() -> bool { + let flags = craft_flags::load(); + for problem in &flags.problems { + log::warn!("flags: {problem}"); + } + flags.enabled(&craft_flags::AI_MCP) +} + +/// The launcher's switch (Settings › AI agents › *Let AI agents use Craft apps*) lets agents use +/// the app `app_id`: the `AI-MCP` flag is on and the launcher's app list names the app. Reads two +/// small files; ask from a timer or a command, not every frame ([`AgentSwitch`] does that). +pub fn launcher_allows(app_id: &str) -> bool { + mcp_flag() && craft_ai_agents::allowed(app_id) +} + +/// The launcher's switch for one app, watched on a thread so a frame can ask without touching the +/// disk. The `AI-MCP` flag is read once, at [`Self::watch`]; the launcher's list every [`POLL`]. +/// Clones share one watch; the thread ends after the last clone is dropped. +#[derive(Clone, Debug)] +pub struct AgentSwitch { + mcp_flag: bool, + on: Arc, +} + +impl AgentSwitch { + /// Watches the launcher's list for `app_id`; a change logs `agents on/off` and calls `wake` (the + /// app asks for a frame there). + /// Without the `AI-MCP` flag nothing is watched: the launcher's list means nothing then. + pub fn watch(app_id: &'static str, wake: impl Fn() + Send + 'static) -> Self { + let flag = mcp_flag(); + if !flag { + return Self::fixed(false, false); + } + Self::watch_with(flag, move || craft_ai_agents::allowed(app_id), POLL, wake) + } + + /// A switch that never changes: tests, the web, an app without agents. + pub fn fixed(mcp_flag: bool, launcher: bool) -> Self { + Self { mcp_flag, on: Arc::new(AtomicBool::new(launcher)) } + } + + /// [`Self::watch`] with the flag given, `probe` asked once now and then every `poll`. + #[doc(hidden)] + pub fn watch_with(mcp_flag: bool, probe: impl Fn() -> bool + Send + 'static, poll: Duration, wake: impl Fn() + Send + 'static) -> Self { + let on = Arc::new(AtomicBool::new(probe())); + let seen: Weak = Arc::downgrade(&on); + let started = std::thread::Builder::new().name("launcher-agents".into()).spawn(move || { + loop { + std::thread::sleep(poll); + let now = probe(); + let Some(seen) = seen.upgrade() else { return }; + if seen.swap(now, Ordering::Relaxed) != now { + log::info!("ArtCraft Launcher: agents {}", if now { "on" } else { "off" }); + wake(); + } + } + }); + // Without a thread a later "off" in the launcher would never be seen: agents stay out. + if let Err(e) = started { + log::warn!("couldn't watch the ArtCraft Launcher's agents switch, so agents stay off: {e}"); + on.store(false, Ordering::Relaxed); + } + Self { mcp_flag, on } + } + + /// The `AI-MCP` flag as it was when the watch started. + pub fn mcp_flag(&self) -> bool { + self.mcp_flag + } + + /// Whether the launcher's list named the app at the last poll. + pub fn launcher(&self) -> bool { + self.on.load(Ordering::Relaxed) + } + + /// Whether agents may use the open window ([`agents_allowed`]); `legacy` is the app's own + /// earlier consent, which counts only without the flag. + pub fn allows(&self, legacy: bool) -> bool { + agents_allowed(self.mcp_flag, self.launcher(), legacy) + } +} diff --git a/crates/control/src/lib.rs b/crates/control/src/lib.rs index 26727a97c8..56ce2e685a 100644 --- a/crates/control/src/lib.rs +++ b/crates/control/src/lib.rs @@ -2,12 +2,15 @@ //! files ([`auth`]), the loopback server that admits a connection by the token and hands each //! request to the window ([`server`]), the client that talks to it ([`client`]), the request the //! window answers ([`ControlRequest`]), the document watch subscribed connections wait on -//! ([`DocumentWatch`]), and what a request still running does when it finishes ([`Effect`]). It +//! ([`DocumentWatch`]), what a request still running does when it finishes ([`Effect`]), and AI +//! agents in the open window behind the ArtCraft Launcher's switch ([`agents`]: its settings on +//! every target, the switch, the connection file and the CLI's side with feature `agents`). It //! needs no UI toolkit; the window's own requests over egui (`ui.screenshot`, `ui.resize`) are //! `craft-control-egui`. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] #![forbid(unsafe_code)] +pub mod agents; pub mod auth; mod cancel; #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/control/tests/agents.rs b/crates/control/tests/agents.rs new file mode 100644 index 0000000000..95a4651eb4 --- /dev/null +++ b/crates/control/tests/agents.rs @@ -0,0 +1,572 @@ +//! AI agents in the open window: the launcher's switch, the connection file, and where an app's +//! `-cli mcp` sends each tool call. +// Unwrapping, expecting and panicking on failure is fine in tests, unlike in shipped code. +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use std::net::TcpListener; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use craft_control::agents::{ + AgentSettings, AgentSwitch, CONNECTION_FILE, Connection, Consent, HeadlessFolders, Live, LiveConfig, Route, TOKEN_FILE, WhenAppClosed, Window, + agents_allowed, find_window, read_connection, remove_connection, remove_stale_connection, write_connection, +}; + +fn temp_dir(tag: &str) -> tempfile::TempDir { + tempfile::Builder::new().prefix(&format!("craft-agents-{tag}-")).tempdir().expect("a temp dir") +} + +/// Waits up to five seconds for `done`. +fn wait_for(mut done: impl FnMut() -> bool) -> bool { + let started = Instant::now(); + while !done() { + if started.elapsed() > Duration::from_secs(5) { + return false; + } + std::thread::sleep(Duration::from_millis(5)); + } + true +} + +const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + +/// Writes `text` as an owner-only token file, as an app's own token writer does. +fn write_token(path: &Path, text: &str) { + std::fs::write(path, text).unwrap(); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600)).unwrap(); + } +} + +/// An app accepting agents in `dir`: a listener on a free loopback port, its token file and its +/// connection file. Dropping the listener leaves the file behind, as a crashed app would. +fn publish(dir: &Path) -> (TcpListener, Connection) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + write_token(&dir.join(TOKEN_FILE), &format!("{}\n", TOKEN.to_ascii_uppercase())); + let c = Connection::new(listener.local_addr().unwrap().port(), dir.join(TOKEN_FILE), "Test 1.0"); + write_connection(dir, &c).unwrap(); + (listener, c) +} + +// ---- The launcher's switch ---------------------------------------------------------------------- + +/// With the `AI-MCP` flag only the launcher's switch decides; without it the app's earlier consent. +#[test] +fn with_the_flag_only_the_launchers_switch_lets_agents_in() { + assert!(agents_allowed(false, false, true), "without the flag, the app's earlier consent"); + assert!(!agents_allowed(false, true, false), "the launcher's list means nothing without the flag"); + assert!(agents_allowed(true, true, false)); + assert!(!agents_allowed(true, false, true), "no second switch in the app"); + assert!(AgentSwitch::fixed(true, true).allows(false)); + assert!(!AgentSwitch::fixed(false, true).allows(false)); +} + +/// The switch follows the launcher's list within a poll, wakes the app once per change rather than +/// once per poll, and its thread ends once the last clone is dropped. +#[test] +fn the_switch_wakes_once_per_change() { + let launcher = Arc::new(AtomicBool::new(false)); + let probes = Arc::new(AtomicUsize::new(0)); + let wakes = Arc::new(AtomicUsize::new(0)); + // Held by the watch thread through its probe: gone once the thread has ended. + let alive = Arc::new(()); + let probe = { + let (launcher, probes, alive) = (launcher.clone(), probes.clone(), alive.clone()); + move || { + let _ = &alive; + probes.fetch_add(1, Ordering::Relaxed); + launcher.load(Ordering::Relaxed) + } + }; + let wake = { + let wakes = wakes.clone(); + move || { + wakes.fetch_add(1, Ordering::Relaxed); + } + }; + let switch = AgentSwitch::watch_with(true, probe, Duration::from_millis(5), wake); + let clone = switch.clone(); + assert!(!switch.launcher()); + + launcher.store(true, Ordering::Relaxed); + assert!(wait_for(|| clone.launcher()), "the switch follows the launcher"); + assert!(switch.allows(false)); + let polled = probes.load(Ordering::Relaxed); + assert!(wait_for(|| probes.load(Ordering::Relaxed) > polled + 10)); + launcher.store(false, Ordering::Relaxed); + assert!(wait_for(|| !switch.launcher())); + assert!(wait_for(|| wakes.load(Ordering::Relaxed) == 2)); + assert_eq!(wakes.load(Ordering::Relaxed), 2, "one wake per change, not per poll"); + + drop(switch); + drop(clone); + assert!(wait_for(|| Arc::strong_count(&alive) == 1), "the watch ended with the last switch"); + let after_end = probes.load(Ordering::Relaxed); + std::thread::sleep(Duration::from_millis(20)); + assert_eq!(probes.load(Ordering::Relaxed), after_end, "nothing probes after the watch ended"); +} + +// ---- The connection file ------------------------------------------------------------------------ + +/// A published connection reads back in its documented shape, and only the process that wrote it +/// removes it. +#[test] +fn a_published_connection_reads_back_and_only_its_writer_removes_it() { + let dir = temp_dir("conn"); + let dir = dir.path(); + assert_eq!(read_connection(dir).unwrap(), None, "no file"); + let c = Connection::new(7878, dir.join(TOKEN_FILE), "MyApp 1.2.3"); + write_connection(dir, &c).unwrap(); + assert_eq!(read_connection(dir).unwrap(), Some(c.clone())); + assert_eq!(c.addr(), "127.0.0.1:7878"); + let v: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(dir.join(CONNECTION_FILE)).unwrap()).unwrap(); + let mut keys: Vec<_> = v.as_object().unwrap().keys().cloned().collect(); + keys.sort(); + assert_eq!(keys, ["app", "pid", "port", "tokenFile", "version"]); + assert_eq!((v["version"].as_u64(), v["port"].as_u64(), v["app"].as_str()), (Some(1), Some(7878), Some("MyApp 1.2.3"))); + + remove_connection(dir, c.pid.wrapping_add(1)); + assert!(dir.join(CONNECTION_FILE).is_file(), "another process's file stays"); + remove_connection(dir, c.pid); + assert!(!dir.join(CONNECTION_FILE).exists()); +} + +/// A damaged, foreign or port-0 file reads as none; a directory in its place is an error and is +/// left alone; a file nobody listens on is removed as stale and a live one kept. +#[test] +fn a_damaged_foreign_or_stale_connection_file_counts_as_none() { + let dir = temp_dir("bad-conn"); + let dir = dir.path(); + for text in [ + &b""[..], + b"{not json", + b"[]", + br#"{"version":2,"pid":1,"port":7878,"tokenFile":"t","app":"x"}"#, + br#"{"version":1,"pid":1,"port":0,"tokenFile":"t","app":"x"}"#, + b"\xff\xfe not utf-8", + ] { + std::fs::write(dir.join(CONNECTION_FILE), text).unwrap(); + assert_eq!(read_connection(dir).unwrap(), None, "{}", String::from_utf8_lossy(text)); + remove_stale_connection(dir); + assert!(!dir.join(CONNECTION_FILE).exists(), "an unreadable file is not a connection"); + } + + std::fs::create_dir(dir.join(CONNECTION_FILE)).unwrap(); + assert!(read_connection(dir).is_err()); + remove_stale_connection(dir); + assert!(dir.join(CONNECTION_FILE).is_dir(), "anything but a regular file is left alone"); + std::fs::remove_dir(dir.join(CONNECTION_FILE)).unwrap(); + + let live = TcpListener::bind("127.0.0.1:0").unwrap(); + let live_port = live.local_addr().unwrap().port(); + write_connection(dir, &Connection { pid: 1, ..Connection::new(live_port, dir.join(TOKEN_FILE), "x") }).unwrap(); + remove_stale_connection(dir); + assert!(dir.join(CONNECTION_FILE).is_file(), "something listens on {live_port}"); + drop(live); + let closed = TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port(); + write_connection(dir, &Connection { pid: 1, ..Connection::new(closed, dir.join(TOKEN_FILE), "x") }).unwrap(); + remove_stale_connection(dir); + assert!(!dir.join(CONNECTION_FILE).exists(), "nothing listens on {closed}"); +} + +// ---- The CLI's side ----------------------------------------------------------------------------- + +/// What the stand-in app made: a window client (its window) or a headless session (its folders), +/// with counts of each. +#[derive(Default)] +struct Made { + connects: AtomicUsize, + sessions: AtomicUsize, +} + +type TestLive = Live; + +fn config(dir: &Path, consent: Consent, made: &Arc) -> LiveConfig { + let (a, b) = (made.clone(), made.clone()); + let mut cfg = LiveConfig::new( + "Test", + dir.to_path_buf(), + move |_: &Path| consent.clone(), + move |w: &Window| { + a.connects.fetch_add(1, Ordering::Relaxed); + Ok(w.clone()) + }, + move |f: &HeadlessFolders| { + b.sessions.fetch_add(1, Ordering::Relaxed); + Ok(f.clone()) + }, + ); + cfg.poll = Duration::from_millis(5); + cfg +} + +fn settings(when: WhenAppClosed, read: &str, write: &str) -> AgentSettings { + let mut s = AgentSettings::default(); + s.when_app_closed = when; + s.read_root = read.into(); + s.write_root = write.into(); + s +} + +fn allowed(when: WhenAppClosed) -> Consent { + Consent::new(true, settings(when, "/app-in", "/app-out")) +} + +fn headless_folders(route: Result, String>) -> HeadlessFolders { + match route { + Ok(Route::Headless(f)) => f, + Ok(Route::Window(w)) => panic!("expected headless, got the window {w:?}"), + Err(e) => panic!("expected headless, got the error {e}"), + } +} + +fn window(route: Result, String>) -> Window { + match route { + Ok(Route::Window(w)) => w, + Ok(Route::Headless(f)) => panic!("expected the window, went headless with {f:?}"), + Err(e) => panic!("expected the window, got the error {e}"), + } +} + +fn paths(p: &[&str]) -> Vec { + p.iter().map(PathBuf::from).collect() +} + +/// An app accepting agents takes the call; the same port, token and process keep the client, a +/// restarted app (another process on the same port and token) gets a new one. +#[test] +fn an_open_app_takes_the_call_and_keeps_its_client_until_it_restarts() { + let dir = temp_dir("window"); + let (_listener, c) = publish(dir.path()); + let made = Arc::new(Made::default()); + // Even without consent: an app publishes its connection only while it accepts agents. + let live = TestLive::new(config(dir.path(), Consent::default(), &made)); + let w = window(live.resolve()); + assert_eq!( + (w.port, w.pid, w.token.as_str(), w.addr()), + (c.port, c.pid, TOKEN.to_ascii_uppercase().as_str(), c.addr()), + "the token is trimmed, otherwise as written" + ); + window(live.resolve()); + assert_eq!(made.connects.load(Ordering::Relaxed), 1, "the same window keeps its client"); + assert!(matches!(live.current(), Some(Route::Window(_)))); + + write_connection(dir.path(), &Connection { pid: c.pid.wrapping_add(1), ..c.clone() }).unwrap(); + assert_eq!(window(live.resolve()).pid, c.pid.wrapping_add(1)); + assert_eq!(made.connects.load(Ordering::Relaxed), 2, "a restarted app gets a new client"); + assert_eq!(made.sessions.load(Ordering::Relaxed), 0); +} + +/// No app accepts agents and the person doesn't let them use it: headless with only the command's +/// folders, never the app's. +#[test] +fn without_consent_a_closed_app_runs_headless_with_the_commands_folders_only() { + let dir = temp_dir("no-consent"); + let made = Arc::new(Made::default()); + let consent = Consent::new(false, settings(WhenAppClosed::Launch, "/app-in", "/app-out")); + let live = TestLive::new(config(dir.path(), consent.clone(), &made)); + assert_eq!(headless_folders(live.resolve()), HeadlessFolders::default(), "no folders given, none used"); + + let mut cfg = config(dir.path(), consent, &made); + cfg.read_roots = paths(&["/cli-in"]); + cfg.write_root = Some(PathBuf::from("/cli-out")); + let f = headless_folders(TestLive::new(cfg).resolve()); + assert_eq!((f.read, f.write), (paths(&["/cli-in"]), Some(PathBuf::from("/cli-out")))); +} + +/// Agents allowed, app closed, "headless": the app's agent folders, each replaced by the command's. +#[test] +fn allowed_and_closed_runs_headless_with_the_apps_folders_unless_the_command_gives_its_own() { + let dir = temp_dir("headless"); + let made = Arc::new(Made::default()); + let f = headless_folders(TestLive::new(config(dir.path(), allowed(WhenAppClosed::Headless), &made)).resolve()); + assert_eq!((f.read, f.write), (paths(&["/app-in"]), Some(PathBuf::from("/app-out")))); + + let mut cfg = config(dir.path(), allowed(WhenAppClosed::Headless), &made); + cfg.read_roots = paths(&["/cli-a", "/cli-b"]); + let f = headless_folders(TestLive::new(cfg).resolve()); + assert_eq!((f.read, f.write), (paths(&["/cli-a", "/cli-b"]), Some(PathBuf::from("/app-out")))); + + let mut cfg = config(dir.path(), allowed(WhenAppClosed::Headless), &made); + cfg.write_root = Some(PathBuf::from("/cli-out")); + let f = headless_folders(TestLive::new(cfg).resolve()); + assert_eq!((f.read, f.write), (paths(&["/app-in"]), Some(PathBuf::from("/cli-out")))); +} + +/// Agents allowed, app closed, "error": the call fails with words that name the app; the command's +/// choice wins over the app's. +#[test] +fn the_closed_app_choice_decides_and_the_command_overrides_it() { + let dir = temp_dir("error"); + let made = Arc::new(Made::default()); + let err = TestLive::new(config(dir.path(), allowed(WhenAppClosed::Error), &made)).resolve().err().unwrap(); + assert_eq!(err, "Test isn't running: open it, then try again."); + + let mut cfg = config(dir.path(), allowed(WhenAppClosed::Error), &made); + cfg.when_closed = Some(WhenAppClosed::Headless); + headless_folders(TestLive::new(cfg).resolve()); + let mut cfg = config(dir.path(), allowed(WhenAppClosed::Headless), &made); + cfg.when_closed = Some(WhenAppClosed::Error); + assert!(TestLive::new(cfg).resolve().is_err()); +} + +/// Once headless, the session stays headless even when the app starts, so documents don't move +/// under the agent. +#[test] +fn a_headless_session_stays_headless() { + let dir = temp_dir("sticky"); + let made = Arc::new(Made::default()); + let live = TestLive::new(config(dir.path(), Consent::default(), &made)); + headless_folders(live.resolve()); + let _app = publish(dir.path()); + headless_folders(live.resolve()); + assert_eq!((made.sessions.load(Ordering::Relaxed), made.connects.load(Ordering::Relaxed)), (1, 0), "one session, no window"); +} + +/// A connection file left by an app that crashed counts as a closed app. +#[test] +fn a_crashed_apps_connection_file_counts_as_closed() { + let dir = temp_dir("crashed"); + drop(publish(dir.path()).0); + let made = Arc::new(Made::default()); + headless_folders(TestLive::new(config(dir.path(), Consent::default(), &made)).resolve()); + assert_eq!(find_window(dir.path(), "Test").unwrap(), None); +} + +/// A token file that isn't a token, is too big, is missing, isn't a regular file or (on Unix) +/// others can read fails the call and names the app; nothing is sent to the window. +#[test] +fn a_bad_token_file_fails_the_call() { + let dir = temp_dir("token"); + let (_listener, _c) = publish(dir.path()); + let made = Arc::new(Made::default()); + for bad in ["", " \n", "not a token", &"a".repeat(5000)] { + write_token(&dir.path().join(TOKEN_FILE), bad); + let err = TestLive::new(config(dir.path(), Consent::default(), &made)).resolve().err().unwrap(); + assert!(err.starts_with("Test's agent token: "), "{err}"); + } + // Any token the app's server accepts is passed on: upper case, dashes. + write_token(&dir.path().join(TOKEN_FILE), "AB12-cd34-EF56-0000-1111-2222-3333-4444"); + assert_eq!(find_window(dir.path(), "Test").unwrap().unwrap().token, "AB12-cd34-EF56-0000-1111-2222-3333-4444"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let token = dir.path().join(TOKEN_FILE); + write_token(&token, TOKEN); + std::fs::set_permissions(&token, std::fs::Permissions::from_mode(0o644)).unwrap(); + let err = find_window(dir.path(), "Test").err().unwrap(); + assert!(err.contains("readable by group or others"), "{err}"); + // A FIFO would block a plain open until something writes to it. + std::fs::remove_file(&token).unwrap(); + assert!(std::process::Command::new("mkfifo").arg(&token).status().unwrap().success()); + let err = find_window(dir.path(), "Test").err().unwrap(); + assert!(err.contains("not a regular file"), "{err}"); + } + std::fs::remove_file(dir.path().join(TOKEN_FILE)).unwrap(); + assert!(find_window(dir.path(), "Test").is_err()); + assert_eq!(made.connects.load(Ordering::Relaxed), 0); +} + +/// The app's own errors reach the agent with what was being done. +#[test] +fn the_apps_errors_reach_the_agent() { + let dir = temp_dir("app-errors"); + let consent = Consent::default(); + let cfg: LiveConfig = LiveConfig::new( + "Test", + dir.path().to_path_buf(), + move |_: &Path| consent.clone(), + |w: &Window| Ok(w.clone()), + |_: &HeadlessFolders| Err("no such folder".into()), + ); + assert_eq!(TestLive::new(cfg).resolve().err().unwrap(), "Test can't use the automation roots: no such folder"); + + let (_listener, _c) = publish(dir.path()); + let cfg: LiveConfig = LiveConfig::new( + "Test", + dir.path().to_path_buf(), + |_: &Path| Consent::default(), + |_: &Window| Err("refused".into()), + |f: &HeadlessFolders| Ok(f.clone()), + ); + let live = TestLive::new(cfg); + assert_eq!(live.resolve().err().unwrap(), "couldn't reach Test's window: refused"); + assert!(live.current().is_none(), "a failed connect leaves no route"); +} + +/// "Launch" without a way to start the app, a start that fails, and an app that never accepts +/// agents each end with words that say what to do. +#[test] +fn a_launch_that_cannot_happen_or_never_finishes_says_what_to_do() { + let dir = temp_dir("launch-fails"); + let made = Arc::new(Made::default()); + let err = TestLive::new(config(dir.path(), allowed(WhenAppClosed::Launch), &made)).resolve().err().unwrap(); + assert_eq!(err, "Test isn't running, and this install can't start it from here: open Test, then try again."); + + let mut cfg = config(dir.path(), allowed(WhenAppClosed::Launch), &made); + cfg.launch = Some(Box::new(|| Err("boom".into()))); + assert_eq!(TestLive::new(cfg).resolve().err().unwrap(), "couldn't start Test: boom"); + + let mut cfg = config(dir.path(), allowed(WhenAppClosed::Launch), &made); + cfg.launch = Some(Box::new(|| Ok(()))); + cfg.launch_timeout = Duration::from_millis(40); + let err = TestLive::new(cfg).resolve().err().unwrap(); + assert!(err.starts_with("Test didn't accept agents within 0 s of starting."), "{err}"); +} + +/// "Launch" starts the app once, also for calls that arrive together, and the calls go to its +/// window once it accepts agents. +#[test] +fn a_started_app_takes_the_call_and_concurrent_calls_start_it_once() { + let dir = temp_dir("launch"); + let made = Arc::new(Made::default()); + let launches = Arc::new(AtomicUsize::new(0)); + let app: Arc>> = Arc::new(Mutex::new(None)); + let mut cfg = config(dir.path(), allowed(WhenAppClosed::Launch), &made); + let (d, n, slot) = (dir.path().to_path_buf(), launches.clone(), app.clone()); + cfg.launch = Some(Box::new(move || { + n.fetch_add(1, Ordering::Relaxed); + let (d, slot) = (d.clone(), slot.clone()); + // The app takes a moment before it accepts agents. + std::thread::spawn(move || { + std::thread::sleep(Duration::from_millis(60)); + *slot.lock().unwrap() = Some(publish(&d).0); + }); + Ok(()) + })); + let live = Arc::new(TestLive::new(cfg)); + let calls: Vec<_> = (0..4) + .map(|_| { + let live = live.clone(); + std::thread::spawn(move || window(live.resolve())) + }) + .collect(); + let ports: Vec = calls.into_iter().map(|c| c.join().unwrap().port).collect(); + assert_eq!(launches.load(Ordering::Relaxed), 1, "one start for every call"); + assert!(ports.windows(2).all(|p| p[0] == p[1]), "{ports:?}"); + assert_eq!(made.connects.load(Ordering::Relaxed), 1); +} + +/// Calls that queued while a start was under way get its error when the app still isn't there, +/// rather than starting it again; a call made afterwards tries again. +#[test] +fn calls_queued_behind_a_failed_start_share_its_error() { + let dir = temp_dir("launch-shared"); + let made = Arc::new(Made::default()); + let launches = Arc::new(AtomicUsize::new(0)); + let mut cfg = config(dir.path(), allowed(WhenAppClosed::Launch), &made); + let entered = Arc::new(AtomicUsize::new(0)); + let (n, e) = (launches.clone(), entered.clone()); + // The app is started but never accepts agents within the timeout. The start waits until every + // call is under way and then a while longer, so a call that hasn't reached its lookup by the + // time the start ends is very unlikely (there is no hook between the two). + cfg.launch = Some(Box::new(move || { + n.fetch_add(1, Ordering::Relaxed); + assert!(wait_for(|| e.load(Ordering::Relaxed) == 4), "every call starts"); + std::thread::sleep(Duration::from_millis(250)); + Ok(()) + })); + cfg.launch_timeout = Duration::from_millis(150); + let live = Arc::new(TestLive::new(cfg)); + let calls: Vec<_> = (0..4) + .map(|_| { + let (live, entered) = (live.clone(), entered.clone()); + std::thread::spawn(move || { + entered.fetch_add(1, Ordering::Relaxed); + live.resolve() + }) + }) + .collect(); + let answers: Vec<_> = calls.into_iter().map(|c| c.join().unwrap()).collect(); + assert_eq!(launches.load(Ordering::Relaxed), 1, "one start for the calls that came together"); + for answer in &answers { + let err = answer.as_ref().err().unwrap(); + assert!(err.starts_with("Test didn't accept agents within"), "{err}"); + } + assert!(live.resolve().is_err()); + assert_eq!(launches.load(Ordering::Relaxed), 2, "a later call starts the app again"); +} + +/// Allow Agent Control off refuses agents on the CLI's side too, as the app does by not +/// listening: no start, and only the command's folders. +#[test] +fn allow_agent_control_off_counts_as_not_allowed() { + let dir = temp_dir("aac-off"); + let made = Arc::new(Made::default()); + let mut s = settings(WhenAppClosed::Launch, "/app-in", "/app-out"); + s.allow_agent_control = false; + let mut cfg = config(dir.path(), Consent::new(true, s), &made); + cfg.launch = Some(Box::new(|| panic!("nothing may start the app"))); + cfg.read_roots = paths(&["/cli-in"]); + let f = headless_folders(TestLive::new(cfg).resolve()); + assert_eq!((f.read, f.write), (paths(&["/cli-in"]), None)); +} + +/// A window route is dropped once no app accepts agents, so a dead client isn't kept or handed +/// back. +#[test] +fn a_window_that_went_away_is_forgotten() { + let dir = temp_dir("gone"); + let made = Arc::new(Made::default()); + let live = TestLive::new(config(dir.path(), allowed(WhenAppClosed::Error), &made)); + let (listener, c) = publish(dir.path()); + window(live.resolve()); + drop(listener); + remove_connection(dir.path(), c.pid); + assert!(live.resolve().is_err(), "the app is closed and the choice is an error"); + assert!(live.current().is_none(), "the dead window is gone"); +} + +/// A headless session with the app's agent folders ends when the person stops letting agents in; +/// one with only the command's folders stays. +#[test] +fn a_headless_session_with_the_apps_folders_ends_with_consent() { + let dir = temp_dir("revoke"); + let made = Arc::new(Made::default()); + let on = Arc::new(AtomicBool::new(true)); + let gate = on.clone(); + let (b, c) = (made.clone(), made.clone()); + let cfg: LiveConfig = LiveConfig::new( + "Test", + dir.path().to_path_buf(), + move |_: &Path| Consent::new(gate.load(Ordering::Relaxed), settings(WhenAppClosed::Headless, "/app-in", "/app-out")), + move |w: &Window| { + b.connects.fetch_add(1, Ordering::Relaxed); + Ok(w.clone()) + }, + move |f: &HeadlessFolders| { + c.sessions.fetch_add(1, Ordering::Relaxed); + Ok(f.clone()) + }, + ); + let live = TestLive::new(cfg); + assert_eq!(headless_folders(live.resolve()).read, paths(&["/app-in"])); + on.store(false, Ordering::Relaxed); + assert_eq!(headless_folders(live.resolve()), HeadlessFolders::default(), "the app's folders went with the consent"); + on.store(true, Ordering::Relaxed); + assert_eq!(headless_folders(live.resolve()), HeadlessFolders::default(), "a session without the app's folders stays"); + assert_eq!(made.sessions.load(Ordering::Relaxed), 2); +} + +/// A huge launch timeout or poll is cut down rather than overflowing the deadline or blocking. +#[test] +fn a_huge_launch_timeout_or_poll_neither_panics_nor_hangs() { + let dir = temp_dir("huge"); + let made = Arc::new(Made::default()); + let mut cfg = config(dir.path(), allowed(WhenAppClosed::Launch), &made); + let (d, app) = (dir.path().to_path_buf(), Arc::new(Mutex::new(None))); + let slot = app.clone(); + cfg.launch = Some(Box::new(move || { + *slot.lock().unwrap() = Some(publish(&d).0); + Ok(()) + })); + cfg.launch_timeout = Duration::MAX; + cfg.poll = Duration::MAX; + let started = Instant::now(); + window(TestLive::new(cfg).resolve()); + assert!(started.elapsed() < Duration::from_secs(5), "{:?}", started.elapsed()); +} diff --git a/crates/csv/README.md b/crates/csv/README.md index bfd525c8cf..7df204e104 100644 --- a/crates/csv/README.md +++ b/crates/csv/README.md @@ -11,7 +11,7 @@ mapping a SYLK sheet onto a document are the caller's work. | Function | What it does | |---|---| | `decode_text(bytes)` | UTF-8 and UTF-16 byte order marks; without one, valid UTF-8 is kept; other bytes are Windows-1256 when they read as Arabic, else Windows-1252 | -| `sniff_delimiter(text, quote)` | comma, semicolon or tab: the one with the most consistent field count over the first 20 non-blank records in a 64 KiB sample; when no candidate finds coherent complete records with a delimiter in each, a candidate that finds no complete record counts the delimiters of the first record up to the end of the sample, or up to a quoted field still open there, and a quote inside a field that does not start with one makes that record incoherent; quote boundaries must be coherent, and ties prefer tab, comma, then semicolon | +| `sniff_delimiter(text, quote)` | comma, semicolon or tab: the one with the most consistent field count over the first 20 records with other text in a sample of the first 65,536 characters; when none separates those records, the candidates whose records hold only their delimiters and whitespace (rows of empty cells) are compared the same way, leaving out any candidate that found a record with other text; when no candidate finds coherent complete records with a delimiter in each, a candidate that finds no complete record with other text counts the delimiters of the first such record up to the end of the sample, or up to a quoted field still open there (after its rows of empty cells, a record without its delimiter is left out), and a quote inside a field that does not start with one makes that record incoherent; quote boundaries must be coherent, and ties prefer tab, comma, then semicolon | | `parse_records(text, delim, quote, max_fields, on_record)` | records of fields, one callback per record (return `false` to stop); CR, LF and CRLF line ends; quoted fields with doubled quotes and line breaks; a quote in the middle of a field is text | | `records(text, delim, quote, max_records, max_fields)` | the same, collected | | `push_field(out, field, delim)`, `field_len(field, delim)` | one field as written, quoted only when it holds the delimiter, a quote or a line break; its exact length, to enforce a size cap before writing | diff --git a/crates/csv/src/lib.rs b/crates/csv/src/lib.rs index 70f6148a6e..f4a82f393f 100644 --- a/crates/csv/src/lib.rs +++ b/crates/csv/src/lib.rs @@ -33,19 +33,20 @@ pub use decode::{decode_text, decode_utf8}; /// Picks comma, semicolon or tab by consistent field counts in the first non-blank records. /// Quotes start only at a candidate field boundary; candidates with incoherent quoted fields /// are discarded when another coherent candidate separates the records. Ties prefer tab, -/// then comma, then semicolon. Comma is the fallback when no candidate separates the text. -/// Only the first 64 KiB are read. A record cut off at the end of that sample is left out, +/// then comma, then semicolon. When no candidate separates the records that hold other text, +/// the same rules pick among the candidates whose records hold only their delimiters and +/// whitespace, such as an exported row of empty cells; a candidate that finds a record with +/// other text is left out of that pick. Comma is the fallback when neither pick finds one. +/// Only the first 65,536 characters are read. A record cut off at the end of that sample is left out, /// except when no candidate finds coherent complete records with a delimiter in each. Then a candidate -/// that finds no complete record counts the delimiters read so far in the first record, and that -/// record is incoherent when it has a quote inside a field that does not start with one. When one -/// of those candidates ends the sample inside a quoted field, each of them counts only up to that -/// field's opening quote. +/// that finds no complete record with other text counts the delimiters read so far in the first +/// such record, unless it found rows of its empty cells and that record has none of its +/// delimiters. That record is incoherent when it has a quote inside a field that does not start +/// with one. When one of those candidates ends the sample inside a quoted field, each of them +/// counts only up to that field's opening quote. pub fn sniff_delimiter(text: &str, quote: char) -> u8 { - const SAMPLE_BYTES: usize = 64 * 1024; - let mut sample_end = text.len().min(SAMPLE_BYTES); - while !text.is_char_boundary(sample_end) { - sample_end -= 1; - } + const SAMPLE_CHARS: usize = 64 * 1024; + let sample_end = text.char_indices().nth(SAMPLE_CHARS).map_or(text.len(), |(at, _)| at); let sample = &text[..sample_end]; let truncated = sample_end < text.len(); let mut scans = b"\t,;".map(|delimiter| scan_delimiter(sample, delimiter, quote)); @@ -64,7 +65,15 @@ pub fn sniff_delimiter(text: &str, quote: char) -> u8 { (scan.count, scan.record_has_content, scan.record_coherent, scan.record_bare_quote) = (before_cut.count, before_cut.record_has_content, before_cut.record_coherent, before_cut.record_bare_quote); } - if scan.record_has_content { + if scan.delimiter_only_counts.is_empty() { + if scan.record_has_content { + scan.coherent &= scan.record_coherent && !scan.record_bare_quote; + scan.counts.push(scan.count); + } else { + scan.push_delimiter_only_record(); + } + } else if scan.record_has_content && scan.count > 0 { + // After rows of empty cells, a cut record without this delimiter is left out. scan.coherent &= scan.record_coherent && !scan.record_bare_quote; scan.counts.push(scan.count); } @@ -79,6 +88,8 @@ pub fn sniff_delimiter(text: &str, quote: char) -> u8 { } else if scan.record_has_content { scan.coherent &= scan.record_coherent; scan.counts.push(scan.count); + } else { + scan.push_delimiter_only_record(); } } } @@ -86,23 +97,34 @@ pub fn sniff_delimiter(text: &str, quote: char) -> u8 { } fn pick_delimiter(scans: [DelimiterScan; 3]) -> u8 { - let candidates = - scans.map(|scan| (scan.delimiter, scan.counts.iter().copied().min().unwrap_or(0), scan.counts.iter().sum::(), scan.coherent)); + let with_text = scans.each_ref().map(|scan| (scan.delimiter, scan.counts.as_slice(), scan.coherent)); + // Records of delimiters alone have no quotes, so they are coherent. + let delimiters_only = + scans.each_ref().map(|scan| (scan.delimiter, if scan.counts.is_empty() { scan.delimiter_only_counts.as_slice() } else { &[] }, true)); + best_delimiter(with_text).or_else(|| best_delimiter(delimiters_only)).unwrap_or(b',') +} + +fn best_delimiter(candidates: [(u8, &[usize], bool); 3]) -> Option { + let candidates = candidates + .map(|(delimiter, counts, coherent)| (delimiter, counts.iter().copied().min().unwrap_or(0), counts.iter().sum::(), coherent)); let prefer_coherent = candidates.iter().any(|(_, min, _, coherent)| *min > 0 && *coherent); - let mut best = (b',', 0usize, 0usize); + let mut best: Option<(u8, usize, usize)> = None; for (delimiter, min, total, coherent) in candidates { - if min > 0 && (!prefer_coherent || coherent) && (min, total) > (best.1, best.2) { - best = (delimiter, min, total); + if min > 0 && (!prefer_coherent || coherent) && best.is_none_or(|(_, best_min, best_total)| (min, total) > (best_min, best_total)) { + best = Some((delimiter, min, total)); } } - best.0 + best.map(|(delimiter, _, _)| delimiter) } /// One candidate delimiter's pass over the sample in [`sniff_delimiter`]. struct DelimiterScan { delimiter: u8, - /// Delimiter counts of the complete non-blank records, at most 20. + /// Delimiter counts of the complete records that hold other text, at most 20. counts: Vec, + /// Delimiter counts of the complete records that hold only this candidate's delimiters and + /// whitespace, at most 20. They do not count toward the 20 records above. + delimiter_only_counts: Vec, /// Whether every counted record had coherent quoted fields. coherent: bool, reached_record_limit: bool, @@ -117,10 +139,21 @@ struct DelimiterScan { open_quote: Option, } +impl DelimiterScan { + /// Keeps the open record's delimiter count when the record has no other text and at least + /// one delimiter. + fn push_delimiter_only_record(&mut self) { + if !self.record_has_content && self.count > 0 && self.delimiter_only_counts.len() < 20 { + self.delimiter_only_counts.push(self.count); + } + } +} + fn scan_delimiter(sample: &str, delimiter: u8, quote: char) -> DelimiterScan { let mut scan = DelimiterScan { delimiter, counts: Vec::new(), + delimiter_only_counts: Vec::new(), coherent: true, reached_record_limit: false, count: 0, @@ -158,6 +191,8 @@ fn scan_delimiter(sample: &str, delimiter: u8, quote: char) -> DelimiterScan { scan.reached_record_limit = true; break; } + } else { + scan.push_delimiter_only_record(); } scan.count = 0; after_quote = false; @@ -403,6 +438,73 @@ mod tests { assert_eq!(sniff_delimiter(&format!("a,b,c;\"hello,\";d\n{}", "x;y;z\n".repeat(12_000)), '"'), b';'); } + #[test] + fn delimiter_sniffing_samples_characters_not_bytes() { + // 30,000 three-byte characters are 90,000 bytes, more than 64 KiB. + assert_eq!(sniff_delimiter(&format!("{};b;c\n1;2;3\n", "€".repeat(30_000)), '"'), b';'); + // The 65,536th character is in the sample and the 65,537th is not. + assert_eq!(sniff_delimiter(&format!("{};x\n1;2\n", "€".repeat(65_535)), '"'), b';'); + assert_eq!(sniff_delimiter(&format!("{};x\n1;2\n", "€".repeat(65_536)), '"'), b','); + } + + // A sheet whose only value is far down exports rows of empty cells that fill the whole sample. + #[test] + fn delimiter_sniffing_reads_a_sparse_semicolon_export() { + assert_eq!(sniff_delimiter(&format!("{};7\n", ";\n".repeat(40_000)), '"'), b';'); + } + + #[test] + fn delimiter_sniffing_reads_a_sparse_semicolon_export_with_crlf() { + assert_eq!(sniff_delimiter(&format!("{};7\r\n", ";\r\n".repeat(40_000)), '"'), b';'); + } + + #[test] + fn delimiter_sniffing_reads_a_sparse_tab_export() { + assert_eq!(sniff_delimiter(&format!("{}\t7\r\n", "\t\r\n".repeat(40_000)), '"'), b'\t'); + } + + #[test] + fn delimiter_sniffing_reads_a_short_file_of_empty_semicolon_cells() { + assert_eq!(sniff_delimiter(";;\n;;\n", '"'), b';'); + assert_eq!(sniff_delimiter(";;\r\n;;", '"'), b';'); + } + + #[test] + fn delimiter_sniffing_reads_empty_semicolon_cells_padded_with_whitespace() { + assert_eq!(sniff_delimiter(&"; ; \n".repeat(20_000), '"'), b';'); + } + + #[test] + fn delimiter_sniffing_reads_a_first_record_of_empty_cells_longer_than_the_sample() { + assert_eq!(sniff_delimiter(&format!("{}\n1;2\n", ";".repeat(70_000)), '"'), b';'); + } + + #[test] + fn delimiter_sniffing_leaves_out_a_cut_record_after_a_row_of_empty_cells() { + assert_eq!(sniff_delimiter(&format!(";\n{};7\n", "x".repeat(70_000)), '"'), b';'); + } + + #[test] + fn delimiter_sniffing_counts_a_long_record_after_a_row_of_empty_cells() { + // Delimiters inside the long record's quoted field are text for every candidate. + assert_eq!(sniff_delimiter(&format!("\t\nname\t\"{}\"\n", "x,y".repeat(30_000)), '"'), b'\t'); + assert_eq!(sniff_delimiter(&format!(";\nname;\"{}\"\n", "x,y\t".repeat(20_000)), '"'), b';'); + } + + #[test] + fn delimiter_sniffing_lets_records_with_text_outweigh_records_of_delimiters_alone() { + // Rows of tabs alone are blank to the other candidates and do not outweigh them. + assert_eq!(sniff_delimiter(&format!("{}a;b\nc;d\n", "\t\n".repeat(25)), '"'), b';'); + assert_eq!(sniff_delimiter(&format!("{}a;b\nc;d\n", "\t\t\n".repeat(25)), '"'), b';'); + // The same holds for a record with text cut off at the end of the sample. + assert_eq!(sniff_delimiter(&format!("\t\n{}", "x;".repeat(40_000)), '"'), b';'); + // A candidate with a record of other text is judged on those records alone. + assert_eq!(sniff_delimiter(&format!("title\n{}", ";\n".repeat(25)), '"'), b','); + // Rows of empty cells in a comma file; comma is also the fallback. + assert_eq!(sniff_delimiter("a,b,c,d\n,,,\n,,,\n1,2,3,4\n", '"'), b','); + assert_eq!(sniff_delimiter(&format!("{}x;y\n", ",,,\n".repeat(20_000)), '"'), b','); + } + #[test] fn parser_stops_collecting_fields_at_the_cap() { let (max, extra) = (16_384usize, 50); diff --git a/crates/dds/README.md b/crates/dds/README.md index 2dae26bc10..37eb70cc8c 100644 --- a/crates/dds/README.md +++ b/crates/dds/README.md @@ -23,21 +23,22 @@ in part. Pixels are 8 bits per channel (`encode`, `Texture::decode`) or 16 bits per channel (`encode16`, `Texture::decode16`), interleaved and top row first, in one of four `Layout`s (`Gray`, `GrayA`, `Rgb`, `Rgba`). BC1 to BC3 are written from RGBA, BC4 from gray and BC5 from RGB -(`DdsCompression::layout` names it). BC4 reads back as gray. BC5 stores red and green only, so it -reads back as RGB with blue 0: for a tangent-space normal map, X and Y, with Z left to the -renderer. ATI2 files are read as Microsoft's tools read them (red in the first block), not with -the two blocks swapped as FFmpeg does. +(`DdsCompression::layout` names it). BC4 reads back as gray. BC5 and `R16G16_UNORM` store red +and green only, so they read as RGB with blue 0: for a tangent-space normal map, X and Y, with Z +left to the renderer. ATI2 files are read as Microsoft's tools read them (red in the first block), +not with the two blocks swapped as FFmpeg does. From 16-bit pixels, uncompressed textures keep 16 bits per channel: gray as `R16_UNORM`, gray and alpha (or any two channels, such as a normal map's X and Y) as `R16G16_UNORM`, RGB (alpha -65535) and RGBA as `R16G16B16A16_UNORM`, all with the DX10 header; they read back as gray, gray -and alpha, and RGBA. BC4 and BC5 keep 8-bit endpoints, as the formats store them, but pick them -and the indices against the 16-bit values, so a smooth height or normal map has less error than -when its 8-bit rounding is compressed. BC1 to BC3 round the pixels to 8 bits first. +65535) and RGBA as `R16G16B16A16_UNORM`, all with the DX10 header; they read back as gray, RGB +with blue 0 (red and green holding the two channels), and RGBA. BC4 and BC5 keep 8-bit +endpoints, as the formats store them, but pick them and the indices against the 16-bit values, +so a smooth height or normal map has less error than when its 8-bit rounding is compressed. BC1 to BC3 round the pixels to 8 bits first. `Texture::decode` rounds 16-bit formats to 8 bits; `Texture::decode16` returns them exactly, rescales channel-mask fields from their own width (a 16-bit luminance field exactly), -interpolates BC4 and BC5 at 16 bits, and scales BC1 to BC3 by 257. `craft-image-codecs` still -reads DDS at 8 bits. +interpolates BC4 and BC5 at 16 bits, and scales BC1 to BC3 by 257. `Texture::bits_per_channel` +gives the widest stored channel; `craft-image-codecs` reads a texture through `decode16` when it +is wider than 8 bits. ```rust use craft_dds::{DdsCompression, DdsOptions, Layout, Texture}; diff --git a/crates/dds/src/bptc.rs b/crates/dds/src/bptc.rs new file mode 100644 index 0000000000..e6a5f317de --- /dev/null +++ b/crates/dds/src/bptc.rs @@ -0,0 +1,1152 @@ +//! BPTC blocks: BC7 (8-bit RGBA) and BC6H (half-float RGB), written from the Khronos Data Format +//! Specification's "BPTC Compressed Texture Image Formats" section. Each 16-byte block holds 4×4 +//! texels, row by row. Tables are the specification's, numbered as it numbers them. + +/// A block's bits, read from the lowest up. +struct Bits { + value: u128, + pos: u32, +} + +impl Bits { + fn new(block: &[u8; 16]) -> Self { + Self { value: u128::from_le_bytes(*block), pos: 0 } + } + + /// The next `n` bits (at most 16), as a number. + fn take(&mut self, n: u8) -> u32 { + let n = u32::from(n); + let mask = !u128::MAX.checked_shl(n).unwrap_or(0); + let out = self.value.checked_shr(self.pos).unwrap_or(0) & mask; + self.pos = self.pos.saturating_add(n); + out as u32 + } +} + +/// Interpolation weights by index bits (Table 119). +const WEIGHTS_2: [i32; 4] = [0, 21, 43, 64]; +const WEIGHTS_3: [i32; 8] = [0, 9, 18, 27, 37, 46, 55, 64]; +const WEIGHTS_4: [i32; 16] = [0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64]; + +fn weight(index_bits: u8, index: u32) -> i32 { + let table: &[i32] = match index_bits { + 2 => &WEIGHTS_2, + 3 => &WEIGHTS_3, + _ => &WEIGHTS_4, + }; + table.get(index as usize).copied().unwrap_or(0) +} + +/// Equation 2: a value between two endpoints. +fn interpolate(e0: i32, e1: i32, weight: i32) -> i32 { + ((64 - weight) * e0 + weight * e1 + 32) >> 6 +} + +/// Which subset texel `i` is in, for a block with `subsets` subsets and partition `partition`. +fn subset(subsets: u8, partition: usize, i: usize) -> usize { + let part = match subsets { + 2 => u32::from(PARTITIONS_2.get(partition).copied().unwrap_or(0)) >> i & 1, + 3 => PARTITIONS_3.get(partition).copied().unwrap_or(0) >> (2 * i) & 3, + _ => 0, + }; + part as usize +} + +/// Whether texel `i` is an anchor (its index is stored with one bit fewer). +fn anchor(subsets: u8, partition: usize, i: usize) -> bool { + let at = |table: &[u8; 64]| table.get(partition).is_some_and(|a| usize::from(*a) == i); + i == 0 + || match subsets { + 2 => at(&ANCHOR_2), + 3 => at(&ANCHOR_3_SECOND) || at(&ANCHOR_3_THIRD), + _ => false, + } +} + +/// The 16 indices of a block, each `bits` wide (anchors one fewer). +fn indices(r: &mut Bits, bits: u8, subsets: u8, partition: usize) -> [u32; 16] { + let mut out = [0; 16]; + for (i, x) in out.iter_mut().enumerate() { + *x = r.take(bits - u8::from(anchor(subsets, partition, i))); + } + out +} + +/// BC7's modes (Table 109): subsets, partition bits, rotation bits, index selection bits, colour +/// bits, alpha bits, per-endpoint P-bits, shared P-bits, index bits, secondary index bits. +const BC7_MODES: [[u8; 10]; 8] = [ + [3, 4, 0, 0, 4, 0, 1, 0, 3, 0], + [2, 6, 0, 0, 6, 0, 0, 1, 3, 0], + [3, 6, 0, 0, 5, 0, 0, 0, 2, 0], + [2, 6, 0, 0, 7, 0, 1, 0, 2, 0], + [1, 0, 2, 1, 5, 6, 0, 0, 2, 3], + [1, 0, 2, 0, 7, 8, 0, 0, 2, 2], + [1, 0, 0, 0, 7, 7, 1, 0, 4, 0], + [2, 6, 0, 0, 5, 5, 1, 0, 2, 0], +]; + +/// The 16 RGBA texels of a BC7 block. +pub fn bc7(block: &[u8; 16]) -> [[u8; 4]; 16] { + let mode = block[0].trailing_zeros(); + // A low byte of 0 is reserved: every channel of every texel is 0. + let Some(&[subsets, partition_bits, rotation_bits, selection_bits, colour_bits, alpha_bits, endpoint_p, shared_p, index_bits, index2_bits]) = + BC7_MODES.get(mode as usize) + else { + return [[0; 4]; 16]; + }; + let mut r = Bits::new(block); + r.pos = mode + 1; + let partition = r.take(partition_bits) as usize; + let rotation = r.take(rotation_bits); + let swapped = r.take(selection_bits) == 1; + let ends = usize::from(subsets) * 2; + // Endpoints: every endpoint's red, then green, blue and alpha; then the P-bits. + let mut raw = [[0u32; 4]; 6]; + for channel in 0..4 { + let bits = if channel < 3 { colour_bits } else { alpha_bits }; + for e in raw.iter_mut().take(ends) { + e[channel] = r.take(bits); + } + } + let mut p = [0u32; 6]; + if endpoint_p == 1 { + for x in p.iter_mut().take(ends) { + *x = r.take(1); + } + } + if shared_p == 1 { + for pair in p.chunks_mut(2).take(usize::from(subsets)) { + pair.fill(r.take(1)); + } + } + let has_p = endpoint_p + shared_p > 0; + let endpoints: [[i32; 4]; 6] = std::array::from_fn(|e| { + std::array::from_fn(|channel| { + let bits = u32::from(if channel < 3 { colour_bits } else { alpha_bits }); + let (v, bits) = match (raw[e][channel], bits) { + (_, 0) => return 255, + (v, bits) if has_p => (v << 1 | p[e], bits + 1), + other => other, + }; + // The stored bits are a byte's top bits; its top bits repeat into the rest. + (v << (8 - bits) | v >> (2 * bits - 8)) as i32 + }) + }); + let primary = indices(&mut r, index_bits, subsets, partition); + let secondary = if index2_bits > 0 { indices(&mut r, index2_bits, 1, 0) } else { [0; 16] }; + let (colour, colour_index_bits) = if swapped { (secondary, index2_bits) } else { (primary, index_bits) }; + let (alpha, alpha_index_bits) = if index2_bits > 0 && !swapped { (secondary, index2_bits) } else { (primary, index_bits) }; + std::array::from_fn(|i| { + let s = subset(subsets, partition, i); + let (e0, e1) = (endpoints.get(2 * s).copied().unwrap_or_default(), endpoints.get(2 * s + 1).copied().unwrap_or_default()); + let (wc, wa) = (weight(colour_index_bits, colour[i]), weight(alpha_index_bits, alpha[i])); + let mut px: [u8; 4] = std::array::from_fn(|c| interpolate(e0[c], e1[c], if c < 3 { wc } else { wa }) as u8); + if (1..=3).contains(&rotation) { + px.swap(rotation as usize - 1, 3); + } + px + }) +} + +// BC6H's fields: endpoint `e`'s red, green and blue are fields `3e`, `3e + 1` and `3e + 2`. +const R0: u8 = 0; +const G0: u8 = 1; +const B0: u8 = 2; +const R1: u8 = 3; +const G1: u8 = 4; +const B1: u8 = 5; +const R2: u8 = 6; +const G2: u8 = 7; +const B2: u8 = 8; +const R3: u8 = 9; +const G3: u8 = 10; +const B3: u8 = 11; +/// The partition number. +const PB: u8 = 12; + +/// A BC6H mode (Tables 121 and 122). +struct Bc6hMode { + id: u8, + /// Endpoints after the first are stored as differences from it. + transformed: bool, + subsets: u8, + endpoint_bits: u8, + /// Red, green and blue bits of the endpoints after the first. + delta_bits: [u8; 3], + /// After the mode bits, in block order: `(field, a, b)` is the spec's `field a..b`, so the + /// next `|a - b| + 1` bits of the block go to the field's bits `b`, `b ± 1`, …, `a`. + fields: &'static [(u8, u8, u8)], +} + +fn sign_extend(v: i32, bits: u8) -> i32 { + let shift = 32 - u32::from(bits); + (v << shift) >> shift +} + +/// An endpoint value stretched to 16 bits (the unquantization pseudocode). +fn unquantize(x: i32, bits: u8, signed: bool) -> i32 { + let n = i32::from(bits); + if !signed { + return match x { + _ if n >= 15 => x, + 0 => 0, + _ if x == (1 << n) - 1 => 0xffff, + _ => ((x << 15) + 0x4000) >> (n - 1), + }; + } + if n >= 16 { + return x; + } + let magnitude = match x.abs() { + 0 => 0, + a if a >= (1 << (n - 1)) - 1 => 0x7fff, + a => ((a << 15) + 0x4000) >> (n - 1), + }; + if x < 0 { -magnitude } else { magnitude } +} + +/// An interpolated value as a half float's bits. +fn finish(i: i32, signed: bool) -> u16 { + match (signed, i < 0) { + (false, _) => ((i * 31) >> 6) as u16, + (true, true) => (((-i * 31) >> 5) | 0x8000) as u16, + (true, false) => ((i * 31) >> 5) as u16, + } +} + +/// The 16 RGB texels of a BC6H block as half floats' bits; a reserved mode gives black. +fn bc6h_halves(block: &[u8; 16], signed: bool) -> [[u16; 3]; 16] { + let mut r = Bits::new(block); + let id = match r.take(2) { + low @ 0..=1 => low, + low => low | r.take(3) << 2, + }; + let Some(mode) = BC6H_MODES.iter().find(|m| u32::from(m.id) == id) else { + return [[0; 3]; 16]; + }; + let mut fields = [0i32; 13]; + for &(field, a, b) in mode.fields { + for k in 0..=a.abs_diff(b) { + let bit = if a >= b { b + k } else { b - k }; + if let Some(f) = fields.get_mut(usize::from(field)) { + *f |= (r.take(1) as i32) << bit; + } + } + } + let bits = mode.endpoint_bits; + let ends = usize::from(mode.subsets) * 2; + let mut endpoints = [[0i32; 3]; 4]; + for (e, endpoint) in endpoints.iter_mut().enumerate().take(ends) { + for (c, v) in endpoint.iter_mut().enumerate() { + let (base, stored) = (fields[c], fields[3 * e + c]); + let x = match e { + 0 => stored, + _ if mode.transformed => (base + sign_extend(stored, mode.delta_bits[c])) & ((1 << bits) - 1), + _ => stored, + }; + *v = unquantize(if signed { sign_extend(x, bits) } else { x }, bits, signed); + } + } + let partition = fields[usize::from(PB)] as usize; + let index_bits = if mode.subsets == 2 { 3 } else { 4 }; + let index = indices(&mut r, index_bits, mode.subsets, partition); + std::array::from_fn(|i| { + let s = subset(mode.subsets, partition, i); + let (e0, e1) = (endpoints.get(2 * s).copied().unwrap_or_default(), endpoints.get(2 * s + 1).copied().unwrap_or_default()); + let w = weight(index_bits, index[i]); + std::array::from_fn(|c| finish(interpolate(e0[c], e1[c], w), signed)) + }) +} + +/// A half float's value. +pub fn half(h: u16) -> f32 { + let sign = if h & 0x8000 == 0 { 1.0 } else { -1.0 }; + let (exponent, mantissa) = (i32::from(h >> 10 & 31), f32::from(h & 1023)); + sign * match exponent { + 0 => mantissa * 2f32.powi(-24), + 31 if mantissa != 0.0 => f32::NAN, + 31 => f32::INFINITY, + e => (1024.0 + mantissa) * 2f32.powi(e - 25), + } +} + +/// A linear light value as an 8-bit sRGB value; 0..1 is kept, brighter and negative values clip. +pub fn srgb8(v: f32) -> u8 { + let v = v.clamp(0.0, 1.0); + let s = if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 }; + (s * 255.0 + 0.5) as u8 +} + +/// The 16 texels of a BC6H block, tone-mapped to 8-bit sRGB (opaque). +pub fn bc6h(block: &[u8; 16], signed: bool) -> [[u8; 4]; 16] { + bc6h_halves(block, signed).map(|[r, g, b]| [srgb8(half(r)), srgb8(half(g)), srgb8(half(b)), 255]) +} + +/// Two-subset partitions (Table 114): bit `i` is texel `i`'s subset. +const PARTITIONS_2: [u16; 64] = [ + 0xcccc, 0x8888, 0xeeee, 0xecc8, 0xc880, 0xfeec, 0xfec8, 0xec80, 0xc800, 0xffec, 0xfe80, 0xe800, 0xffe8, 0xff00, 0xfff0, 0xf000, 0xf710, 0x008e, + 0x7100, 0x08ce, 0x008c, 0x7310, 0x3100, 0x8cce, 0x088c, 0x3110, 0x6666, 0x366c, 0x17e8, 0x0ff0, 0x718e, 0x399c, 0xaaaa, 0xf0f0, 0x5a5a, 0x33cc, + 0x3c3c, 0x55aa, 0x9696, 0xa55a, 0x73ce, 0x13c8, 0x324c, 0x3bdc, 0x6996, 0xc33c, 0x9966, 0x0660, 0x0272, 0x04e4, 0x4e40, 0x2720, 0xc936, 0x936c, + 0x39c6, 0x639c, 0x9336, 0x9cc6, 0x817e, 0xe718, 0xccf0, 0x0fcc, 0x7744, 0xee22, +]; + +/// Three-subset partitions (Table 115): bits `2i` and `2i + 1` are texel `i`'s subset. +const PARTITIONS_3: [u32; 64] = [ + 0xaa685050, 0x6a5a5040, 0x5a5a4200, 0x5450a0a8, 0xa5a50000, 0xa0a05050, 0x5555a0a0, 0x5a5a5050, 0xaa550000, 0xaa555500, 0xaaaa5500, 0x90909090, + 0x94949494, 0xa4a4a4a4, 0xa9a59450, 0x2a0a4250, 0xa5945040, 0x0a425054, 0xa5a5a500, 0x55a0a0a0, 0xa8a85454, 0x6a6a4040, 0xa4a45000, 0x1a1a0500, + 0x0050a4a4, 0xaaa59090, 0x14696914, 0x69691400, 0xa08585a0, 0xaa821414, 0x50a4a450, 0x6a5a0200, 0xa9a58000, 0x5090a0a8, 0xa8a09050, 0x24242424, + 0x00aa5500, 0x24924924, 0x24499224, 0x50a50a50, 0x500aa550, 0xaaaa4444, 0x66660000, 0xa5a0a5a0, 0x50a050a0, 0x69286928, 0x44aaaa44, 0x66666600, + 0xaa444444, 0x54a854a8, 0x95809580, 0x96969600, 0xa85454a8, 0x80959580, 0xaa141414, 0x96960000, 0xaaaa1414, 0xa05050a0, 0xa0a5a5a0, 0x96000000, + 0x40804080, 0xa9a8a9a8, 0xaaaaaa44, 0x2a4a5254, +]; + +/// The anchor texel of the second subset of two-subset partitions (Table 118). +const ANCHOR_2: [u8; 64] = [ + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 2, 8, 2, 2, 8, 8, 15, 2, 8, 2, 2, 8, 8, 2, 2, 15, 15, 6, 8, 2, 8, 15, 15, 2, + 8, 2, 2, 2, 15, 15, 6, 6, 2, 6, 8, 15, 15, 2, 2, 15, 15, 15, 15, 15, 2, 2, 15, +]; + +/// The anchor texels of the second and third subsets of three-subset partitions (Tables 116, 117). +const ANCHOR_3_SECOND: [u8; 64] = [ + 3, 3, 15, 15, 8, 3, 15, 15, 8, 8, 6, 6, 6, 5, 3, 3, 3, 3, 8, 15, 3, 3, 6, 10, 5, 8, 8, 6, 8, 5, 15, 15, 8, 15, 3, 5, 6, 10, 8, 15, 15, 3, 15, 5, + 15, 15, 15, 15, 3, 15, 5, 5, 5, 8, 5, 10, 5, 10, 8, 13, 15, 12, 3, 3, +]; + +const ANCHOR_3_THIRD: [u8; 64] = [ + 15, 8, 8, 3, 15, 15, 3, 8, 15, 15, 15, 15, 15, 15, 15, 8, 15, 8, 15, 3, 15, 8, 15, 8, 3, 15, 6, 10, 15, 15, 10, 8, 15, 3, 15, 10, 10, 8, 9, 10, + 6, 15, 8, 15, 3, 6, 6, 8, 15, 3, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 3, 15, 15, 8, +]; + +/// BC6H's modes, in the order of Table 121. +const BC6H_MODES: [Bc6hMode; 14] = [ + Bc6hMode { + id: 0, + transformed: true, + subsets: 2, + endpoint_bits: 10, + delta_bits: [5, 5, 5], + fields: &[ + (G2, 4, 4), + (B2, 4, 4), + (B3, 4, 4), + (R0, 9, 0), + (G0, 9, 0), + (B0, 9, 0), + (R1, 4, 0), + (G3, 4, 4), + (G2, 3, 0), + (G1, 4, 0), + (B3, 0, 0), + (G3, 3, 0), + (B1, 4, 0), + (B3, 1, 1), + (B2, 3, 0), + (R2, 4, 0), + (B3, 2, 2), + (R3, 4, 0), + (B3, 3, 3), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 1, + transformed: true, + subsets: 2, + endpoint_bits: 7, + delta_bits: [6, 6, 6], + fields: &[ + (G2, 5, 5), + (G3, 4, 4), + (G3, 5, 5), + (R0, 6, 0), + (B3, 0, 0), + (B3, 1, 1), + (B2, 4, 4), + (G0, 6, 0), + (B2, 5, 5), + (B3, 2, 2), + (G2, 4, 4), + (B0, 6, 0), + (B3, 3, 3), + (B3, 5, 5), + (B3, 4, 4), + (R1, 5, 0), + (G2, 3, 0), + (G1, 5, 0), + (G3, 3, 0), + (B1, 5, 0), + (B2, 3, 0), + (R2, 5, 0), + (R3, 5, 0), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 2, + transformed: true, + subsets: 2, + endpoint_bits: 11, + delta_bits: [5, 4, 4], + fields: &[ + (R0, 9, 0), + (G0, 9, 0), + (B0, 9, 0), + (R1, 4, 0), + (R0, 10, 10), + (G2, 3, 0), + (G1, 3, 0), + (G0, 10, 10), + (B3, 0, 0), + (G3, 3, 0), + (B1, 3, 0), + (B0, 10, 10), + (B3, 1, 1), + (B2, 3, 0), + (R2, 4, 0), + (B3, 2, 2), + (R3, 4, 0), + (B3, 3, 3), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 6, + transformed: true, + subsets: 2, + endpoint_bits: 11, + delta_bits: [4, 5, 4], + fields: &[ + (R0, 9, 0), + (G0, 9, 0), + (B0, 9, 0), + (R1, 3, 0), + (R0, 10, 10), + (G3, 4, 4), + (G2, 3, 0), + (G1, 4, 0), + (G0, 10, 10), + (G3, 3, 0), + (B1, 3, 0), + (B0, 10, 10), + (B3, 1, 1), + (B2, 3, 0), + (R2, 3, 0), + (B3, 0, 0), + (B3, 2, 2), + (R3, 3, 0), + (G2, 4, 4), + (B3, 3, 3), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 10, + transformed: true, + subsets: 2, + endpoint_bits: 11, + delta_bits: [4, 4, 5], + fields: &[ + (R0, 9, 0), + (G0, 9, 0), + (B0, 9, 0), + (R1, 3, 0), + (R0, 10, 10), + (B2, 4, 4), + (G2, 3, 0), + (G1, 3, 0), + (G0, 10, 10), + (B3, 0, 0), + (G3, 3, 0), + (B1, 4, 0), + (B0, 10, 10), + (B2, 3, 0), + (R2, 3, 0), + (B3, 1, 1), + (B3, 2, 2), + (R3, 3, 0), + (B3, 4, 4), + (B3, 3, 3), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 14, + transformed: true, + subsets: 2, + endpoint_bits: 9, + delta_bits: [5, 5, 5], + fields: &[ + (R0, 8, 0), + (B2, 4, 4), + (G0, 8, 0), + (G2, 4, 4), + (B0, 8, 0), + (B3, 4, 4), + (R1, 4, 0), + (G3, 4, 4), + (G2, 3, 0), + (G1, 4, 0), + (B3, 0, 0), + (G3, 3, 0), + (B1, 4, 0), + (B3, 1, 1), + (B2, 3, 0), + (R2, 4, 0), + (B3, 2, 2), + (R3, 4, 0), + (B3, 3, 3), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 18, + transformed: true, + subsets: 2, + endpoint_bits: 8, + delta_bits: [6, 5, 5], + fields: &[ + (R0, 7, 0), + (G3, 4, 4), + (B2, 4, 4), + (G0, 7, 0), + (B3, 2, 2), + (G2, 4, 4), + (B0, 7, 0), + (B3, 3, 3), + (B3, 4, 4), + (R1, 5, 0), + (G2, 3, 0), + (G1, 4, 0), + (B3, 0, 0), + (G3, 3, 0), + (B1, 4, 0), + (B3, 1, 1), + (B2, 3, 0), + (R2, 5, 0), + (R3, 5, 0), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 22, + transformed: true, + subsets: 2, + endpoint_bits: 8, + delta_bits: [5, 6, 5], + fields: &[ + (R0, 7, 0), + (B3, 0, 0), + (B2, 4, 4), + (G0, 7, 0), + (G2, 5, 5), + (G2, 4, 4), + (B0, 7, 0), + (G3, 5, 5), + (B3, 4, 4), + (R1, 4, 0), + (G3, 4, 4), + (G2, 3, 0), + (G1, 5, 0), + (G3, 3, 0), + (B1, 4, 0), + (B3, 1, 1), + (B2, 3, 0), + (R2, 4, 0), + (B3, 2, 2), + (R3, 4, 0), + (B3, 3, 3), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 26, + transformed: true, + subsets: 2, + endpoint_bits: 8, + delta_bits: [5, 5, 6], + fields: &[ + (R0, 7, 0), + (B3, 1, 1), + (B2, 4, 4), + (G0, 7, 0), + (B2, 5, 5), + (G2, 4, 4), + (B0, 7, 0), + (B3, 5, 5), + (B3, 4, 4), + (R1, 4, 0), + (G3, 4, 4), + (G2, 3, 0), + (G1, 4, 0), + (B3, 0, 0), + (G3, 3, 0), + (B1, 5, 0), + (B2, 3, 0), + (R2, 4, 0), + (B3, 2, 2), + (R3, 4, 0), + (B3, 3, 3), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 30, + transformed: false, + subsets: 2, + endpoint_bits: 6, + delta_bits: [6, 6, 6], + fields: &[ + (R0, 5, 0), + (G3, 4, 4), + (B3, 0, 0), + (B3, 1, 1), + (B2, 4, 4), + (G0, 5, 0), + (G2, 5, 5), + (B2, 5, 5), + (B3, 2, 2), + (G2, 4, 4), + (B0, 5, 0), + (G3, 5, 5), + (B3, 3, 3), + (B3, 5, 5), + (B3, 4, 4), + (R1, 5, 0), + (G2, 3, 0), + (G1, 5, 0), + (G3, 3, 0), + (B1, 5, 0), + (B2, 3, 0), + (R2, 5, 0), + (R3, 5, 0), + (PB, 4, 0), + ], + }, + Bc6hMode { + id: 3, + transformed: false, + subsets: 1, + endpoint_bits: 10, + delta_bits: [10, 10, 10], + fields: &[(R0, 9, 0), (G0, 9, 0), (B0, 9, 0), (R1, 9, 0), (G1, 9, 0), (B1, 9, 0)], + }, + Bc6hMode { + id: 7, + transformed: true, + subsets: 1, + endpoint_bits: 11, + delta_bits: [9, 9, 9], + fields: &[(R0, 9, 0), (G0, 9, 0), (B0, 9, 0), (R1, 8, 0), (R0, 10, 10), (G1, 8, 0), (G0, 10, 10), (B1, 8, 0), (B0, 10, 10)], + }, + Bc6hMode { + id: 11, + transformed: true, + subsets: 1, + endpoint_bits: 12, + delta_bits: [8, 8, 8], + fields: &[(R0, 9, 0), (G0, 9, 0), (B0, 9, 0), (R1, 7, 0), (R0, 10, 11), (G1, 7, 0), (G0, 10, 11), (B1, 7, 0), (B0, 10, 11)], + }, + Bc6hMode { + id: 15, + transformed: true, + subsets: 1, + endpoint_bits: 16, + delta_bits: [4, 4, 4], + fields: &[(R0, 9, 0), (G0, 9, 0), (B0, 9, 0), (R1, 3, 0), (R0, 10, 15), (G1, 3, 0), (G0, 10, 15), (B1, 3, 0), (B0, 10, 15)], + }, +]; + +#[cfg(test)] +mod tests { + use super::{B0, B1, B2, B3, BC6H_MODES, G0, G1, G2, G3, PB, R0, R1, R2, R3, bc6h, bc6h_halves, bc7, half, srgb8}; + + /// Pack `(value, bits)` fields into a block from the lowest bit up. + fn pack(fields: &[(u32, u32)]) -> [u8; 16] { + let (mut v, mut pos) = (0u128, 0); + for &(x, n) in fields { + assert!(u128::from(x) < 1 << n, "{x} doesn't fit in {n} bits"); + v |= u128::from(x) << pos; + pos += n; + } + assert_eq!(pos, 128, "a block is 128 bits"); + v.to_le_bytes() + } + + /// The mode bits of BC7 mode `m`, then `fields`. + fn bc7_block(m: u32, fields: &[(u32, u32)]) -> [u8; 16] { + pack(&[&[(1 << m, m + 1)], fields].concat()) + } + + /// One `bits`-wide field per value. + fn each(values: &[u32], bits: u32) -> Vec<(u32, u32)> { + values.iter().map(|v| (*v, bits)).collect() + } + + /// 16 indices, `bits` wide, one bit fewer at the `anchors`. + fn texel_indices(values: [u32; 16], bits: u32, anchors: &[usize]) -> Vec<(u32, u32)> { + values.iter().enumerate().map(|(i, v)| (*v, bits - u32::from(anchors.contains(&i)))).collect() + } + + fn index_set(set: &[(usize, u32)]) -> [u32; 16] { + let mut out = [0; 16]; + for (i, v) in set { + out[*i] = *v; + } + out + } + + /// Grey endpoints: the same value in red, green and blue. + fn grey(values: &[u32], bits: u32) -> Vec<(u32, u32)> { + [each(values, bits), each(values, bits), each(values, bits)].concat() + } + + // Each case's colours are worked out from the specification by hand: endpoint bits (and the + // P-bit) are a byte's top bits with its top bits repeated below, and a texel is + // ((64 - w) × e0 + w × e1 + 32) >> 6 with the weight w of its index. + + #[test] + fn bc7_mode_0_three_subsets_with_endpoint_p_bits() { + // Partition 0 of three subsets: rows 0011 / 0011 / 0221 / 2222; anchors 0, 3 and 15. + // Endpoints (4 bits + P-bit): 15+1 → 255, 0 → 0, 8+0 → 132, 8+1 → 140, 2+1 → 41, 4+0 → 66. + let idx = index_set(&[(1, 7), (2, 4), (3, 3), (9, 5), (15, 3)]); + let b = bc7_block( + 0, + &[&[(0, 4)], &grey(&[15, 0, 8, 8, 2, 4], 4)[..], &each(&[1, 0, 0, 1, 1, 0], 1), &texel_indices(idx, 3, &[0, 3, 15])].concat(), + ); + let px = bc7(&b); + let g = |v: u8| [v, v, v, 255]; + assert_eq!(px[0], g(255)); + assert_eq!(px[1], g(0), "index 7: the second endpoint"); + assert_eq!(px[2], g(137), "subset 1, weight 37: (27×132 + 37×140 + 32) >> 6"); + assert_eq!(px[3], g(135), "subset 1's anchor, weight 27"); + assert_eq!(px[9], g(59), "subset 2, weight 46: (18×41 + 46×66 + 32) >> 6"); + assert_eq!(px[15], g(52), "subset 2's anchor, weight 27"); + assert_eq!((px[8], px[10], px[11]), (g(255), g(41), g(132))); + } + + #[test] + fn bc7_mode_1_shared_p_bits() { + // Partition 13: texels 0-7 subset 0, 8-15 subset 1 (anchor 15). Endpoints (6 bits + + // shared P-bit): 63+1 → 255, 0+1 → 2; 32+0 → 129, 16+0 → 64. + let idx = index_set(&[(1, 7), (8, 2), (15, 3)]); + let b = bc7_block(1, &[&[(13, 6)], &grey(&[63, 0, 32, 16], 6)[..], &each(&[1, 0], 1), &texel_indices(idx, 3, &[0, 15])].concat()); + let px = bc7(&b); + let g = |v: u8| [v, v, v, 255]; + assert_eq!((px[0], px[1], px[2]), (g(255), g(2), g(255))); + assert_eq!(px[8], g(111), "weight 18: (46×129 + 18×64 + 32) >> 6"); + assert_eq!(px[15], g(102), "anchor, weight 27"); + assert_eq!(px[9], g(129)); + } + + #[test] + fn bc7_mode_2_matches_the_specifications_example() { + // Partition 6 of three subsets: rows 0022 / 0022 / 1111 / 1111; anchors 0, 15 and 3 (the + // specification's worked example: texel 9 is subset 1, its index at bits 115-116). + // Endpoints (5 bits): 31 → 255, 0, 10 → 82, 20 → 165, 1 → 8, 30 → 247. + let idx = index_set(&[(0, 1), (2, 2), (3, 1), (9, 3), (15, 1)]); + let b = bc7_block(2, &[&[(6, 6)], &grey(&[31, 0, 10, 20, 1, 30], 5)[..], &texel_indices(idx, 2, &[0, 15, 3])].concat()); + assert_eq!(u128::from_le_bytes(b) >> 115 & 3, 3, "texel 9's index where the specification says"); + let px = bc7(&b); + let g = |v: u8| [v, v, v, 255]; + assert_eq!(px[0], g(171), "weight 21: (43×255 + 32) >> 6"); + assert_eq!(px[2], g(169), "subset 2, weight 43: (21×8 + 43×247 + 32) >> 6"); + assert_eq!(px[3], g(86), "subset 2's anchor, weight 21"); + assert_eq!(px[9], g(165)); + assert_eq!(px[15], g(109), "subset 1's anchor, weight 21: (43×82 + 21×165 + 32) >> 6"); + assert_eq!((px[1], px[8], px[6]), (g(255), g(82), g(8))); + } + + #[test] + fn bc7_mode_3_seven_bit_endpoints() { + // Partition 0: columns 2-3 are subset 1 (anchor 15). Endpoints (7 bits + P-bit): + // 100+1 → 201, 10+0 → 20, 127+1 → 255, 0+0 → 0. + let idx = index_set(&[(0, 1), (1, 2), (2, 3), (15, 1)]); + let b = bc7_block(3, &[&[(0, 6)], &grey(&[100, 10, 127, 0], 7)[..], &each(&[1, 0, 1, 0], 1), &texel_indices(idx, 2, &[0, 15])].concat()); + let px = bc7(&b); + let g = |v: u8| [v, v, v, 255]; + assert_eq!(px[0], g(142), "weight 21: (43×201 + 21×20 + 32) >> 6"); + assert_eq!(px[1], g(79), "weight 43"); + assert_eq!(px[2], g(0)); + assert_eq!(px[15], g(171)); + assert_eq!((px[3], px[4]), (g(255), g(201))); + } + + #[test] + fn bc7_mode_4_rotation_and_index_selection() { + // Rotation 1 swaps alpha and red; the selection bit takes colour from the 3-bit indices + // and alpha from the 2-bit ones. Red 31 → 255 to 0, green 0 to 255, blue 16 → 132, + // alpha (6 bits) 63 → 255 to 0. + let colour = index_set(&[(0, 3), (1, 7)]); + let alpha = index_set(&[(0, 1)]); + let ends = [each(&[31, 0, 0, 31, 16, 16], 5), each(&[63, 0], 6)].concat(); + let b = bc7_block(4, &[&[(1, 2), (1, 1)], &ends[..], &texel_indices(alpha, 2, &[0]), &texel_indices(colour, 3, &[0])].concat()); + let px = bc7(&b); + // Before the swap: red (37×255 + 32) >> 6 = 147, green 108, blue 132, alpha 171. + assert_eq!(px[0], [171, 108, 132, 147]); + assert_eq!(px[1], [255, 255, 132, 0]); + assert_eq!(px[2], [255, 0, 132, 255]); + } + + #[test] + fn bc7_mode_5_separate_alpha_indices() { + // Red 127 → 255 to 0, green 64 → 129, blue 0 to 255; alpha (8 bits) 200 to 100. + let colour = index_set(&[(0, 1), (5, 2)]); + let alpha = index_set(&[(0, 1), (5, 3)]); + let ends = [each(&[127, 0, 64, 64, 0, 127], 7), each(&[200, 100], 8)].concat(); + let b = bc7_block(5, &[&[(0, 2)], &ends[..], &texel_indices(colour, 2, &[0]), &texel_indices(alpha, 2, &[0])].concat()); + let px = bc7(&b); + assert_eq!(px[0], [171, 129, 84, 167], "weight 21 for colour and alpha: (43×200 + 21×100 + 32) >> 6"); + assert_eq!(px[5], [84, 129, 171, 100]); + assert_eq!(px[1], [255, 129, 0, 200]); + } + + #[test] + fn bc7_mode_6_four_bit_indices() { + // Endpoints (7 bits + P-bit) in every channel: [255, 1, 129, 255] and [0, 254, 64, 100]. + let idx = index_set(&[(0, 7), (1, 15), (2, 8)]); + let ends = each(&[127, 0, 0, 127, 64, 32, 127, 50], 7); + let b = bc7_block(6, &[&ends[..], &each(&[1, 0], 1), &texel_indices(idx, 4, &[0])].concat()); + let px = bc7(&b); + assert_eq!(px[0], [135, 120, 99, 182], "the anchor's largest index, 7: weight 30"); + assert_eq!(px[1], [0, 254, 64, 100]); + assert_eq!(px[2], [120, 135, 94, 173], "weight 34: red (30×255 + 32) >> 6"); + assert_eq!(px[3], [255, 1, 129, 255]); + } + + #[test] + fn bc7_mode_7_two_subsets_with_alpha() { + // Partition 1: column 3 is subset 1 (anchor 15). Endpoints (5 bits + P-bit): + // [255; 4], [0; 4], [130, 0, 0, 251], [4, 134, 69, 4]. + let idx = index_set(&[(0, 1), (3, 3), (15, 1)]); + let ends = [each(&[31, 0, 16, 0], 5), each(&[31, 0, 0, 16], 5), each(&[31, 0, 0, 8], 5), each(&[31, 0, 31, 0], 5)].concat(); + let b = bc7_block(7, &[&[(1, 6)], &ends[..], &each(&[1, 0, 0, 1], 1), &texel_indices(idx, 2, &[0, 15])].concat()); + let px = bc7(&b); + assert_eq!(px[0], [171; 4]); + assert_eq!(px[3], [4, 134, 69, 4]); + assert_eq!(px[15], [89, 44, 23, 170], "weight 21: red (43×130 + 21×4 + 32) >> 6"); + assert_eq!((px[7], px[1]), ([130, 0, 0, 251], [255; 4])); + } + + #[test] + fn bc7_reserved_block_is_all_zero() { + assert_eq!(bc7(&[0; 16]), [[0; 4]; 16]); + } + + /// A BC6H block of mode `id`: `values` by field (missing ones are 0), then the indices. + fn bc6h_block(id: u8, values: &[(u8, i32)], index: [u32; 16]) -> [u8; 16] { + let mode = BC6H_MODES.iter().find(|m| m.id == id).unwrap(); + let value = |f: u8| values.iter().find(|(g, _)| *g == f).map_or(0, |(_, v)| *v); + let mut fields = vec![(u32::from(id), if id < 2 { 2 } else { 5 })]; + for &(f, a, b) in mode.fields { + for k in 0..=a.abs_diff(b) { + let bit = if a >= b { b + k } else { b - k }; + fields.push(((value(f) >> bit) as u32 & 1, 1)); + } + } + let bits = if mode.subsets == 2 { 3 } else { 4 }; + let partition = value(PB) as usize; + for (i, x) in index.iter().enumerate() { + fields.push((*x, bits - u32::from(super::anchor(mode.subsets, partition, i)))); + } + pack(&fields) + } + + /// The same endpoints for every mode: the first is (largest, 0, middle); the others are small + /// differences from it (or, without transformed endpoints, values near it). Two-subset modes + /// use partition 13 (texels 8-15 are subset 1); indices pick each endpoint and two blends. + fn bc6h_case(id: u8) -> ([u8; 16], Vec) { + let mode = BC6H_MODES.iter().find(|m| m.id == id).unwrap(); + let (max, mid, d) = ((1 << mode.endpoint_bits) - 1, 1 << (mode.endpoint_bits - 1), mode.delta_bits); + let mut v = vec![(R0, max), (G0, 0), (B0, mid)]; + if mode.transformed { + v.extend([(R1, -1), (G1, 1), (B1, 0)]); + } else { + v.extend([(R1, max - 1), (G1, 1), (B1, mid)]); + } + match (mode.transformed, mode.subsets) { + (true, 2) => v.extend([(R2, 0), (G2, 2), (B2, -2), (R3, -(1 << (d[0] - 1))), (G3, 0), (B3, 1), (PB, 13)]), + (false, 2) => v.extend([(R2, max), (G2, 2), (B2, mid - 2), (R3, 0), (G3, max), (B3, mid + 1), (PB, 13)]), + _ => {} + } + if mode.subsets == 2 { + (bc6h_block(id, &v, index_set(&[(1, 7), (8, 7), (2, 4), (9, 3)])), vec![0, 1, 2, 8, 9, 15]) + } else { + (bc6h_block(id, &v, index_set(&[(1, 15), (2, 8)])), vec![0, 1, 2]) + } + } + + // Half-float results for `bc6h_case`, from a separate decoder written from the specification's + // pseudocode. By hand, for mode 3's first texel: red is the largest 10-bit value → 0xffff → + // (0xffff × 31) >> 6 = 0x7bff (65504); blue 512 → ((512 << 15) + 0x4000) >> 9 = 32800 → + // (32800 × 31) >> 6 = 0x3e0f. Signed, red is -1 → -96 → 0x805d and blue -512 → -0x7fff → 0xfbff. + + const UNSIGNED: [(u8, &[[u16; 3]]); 14] = [ + ( + 0, + &[ + [0x7bff, 0x0000, 0x3e0f], + [0x7bd1, 0x002e, 0x3e0f], + [0x7be4, 0x001b, 0x3e0f], + [0x7a00, 0x0000, 0x3e2e], + [0x7b27, 0x002d, 0x3df8], + [0x7bff, 0x004d, 0x3dd1], + ], + ), + ( + 1, + &[ + [0x7bff, 0x0000, 0x3e7c], + [0x7a8c, 0x0174, 0x3e7c], + [0x7b28, 0x00d7, 0x3e7c], + [0x5c84, 0x0000, 0x3f74], + [0x6eb7, 0x0166, 0x3dc5], + [0x7bff, 0x026c, 0x3c8c], + ], + ), + ( + 2, + &[ + [0x7bff, 0x0000, 0x3e07], + [0x7be8, 0x0017, 0x3e07], + [0x7bf2, 0x000d, 0x3e07], + [0x7b00, 0x0000, 0x3e17], + [0x7b93, 0x0016, 0x3dfc], + [0x7bff, 0x0026, 0x3de8], + ], + ), + ( + 6, + &[ + [0x7bff, 0x0000, 0x3e07], + [0x7be8, 0x0017, 0x3e07], + [0x7bf2, 0x000d, 0x3e07], + [0x7b7c, 0x0000, 0x3e17], + [0x7bc8, 0x0016, 0x3dfc], + [0x7bff, 0x0026, 0x3de8], + ], + ), + ( + 10, + &[ + [0x7bff, 0x0000, 0x3e07], + [0x7be8, 0x0017, 0x3e07], + [0x7bf2, 0x000d, 0x3e07], + [0x7b7c, 0x0000, 0x3e17], + [0x7bc8, 0x0016, 0x3dfc], + [0x7bff, 0x0026, 0x3de8], + ], + ), + ( + 14, + &[ + [0x7bff, 0x0000, 0x3e1f], + [0x7ba3, 0x005d, 0x3e1f], + [0x7bca, 0x0035, 0x3e1f], + [0x7801, 0x0000, 0x3e5d], + [0x7a4f, 0x0059, 0x3df1], + [0x7bff, 0x009b, 0x3da3], + ], + ), + ( + 18, + &[ + [0x7bff, 0x0000, 0x3e3e], + [0x7b46, 0x00ba, 0x3e3e], + [0x7b94, 0x006b, 0x3e3e], + [0x6c42, 0x0000, 0x3eba], + [0x755b, 0x00b3, 0x3de2], + [0x7bff, 0x0136, 0x3d46], + ], + ), + ( + 22, + &[ + [0x7bff, 0x0000, 0x3e3e], + [0x7b46, 0x00ba, 0x3e3e], + [0x7b94, 0x006b, 0x3e3e], + [0x7402, 0x0000, 0x3eba], + [0x78a0, 0x00b3, 0x3de2], + [0x7bff, 0x0136, 0x3d46], + ], + ), + ( + 26, + &[ + [0x7bff, 0x0000, 0x3e3e], + [0x7b46, 0x00ba, 0x3e3e], + [0x7b94, 0x006b, 0x3e3e], + [0x7402, 0x0000, 0x3eba], + [0x78a0, 0x00b3, 0x3de2], + [0x7bff, 0x0136, 0x3d46], + ], + ), + ( + 30, + &[ + [0x7bff, 0x0000, 0x3ef8], + [0x7918, 0x02e8, 0x3ef8], + [0x7a51, 0x01ae, 0x3ef8], + [0x0000, 0x7bff, 0x40e8], + [0x47af, 0x371c, 0x3d8b], + [0x7bff, 0x04d8, 0x3b18], + ], + ), + (3, &[[0x7bff, 0x0000, 0x3e0f], [0x7bd1, 0x002e, 0x3e0f], [0x7be7, 0x0018, 0x3e0f]]), + (7, &[[0x7bff, 0x0000, 0x3e07], [0x7be8, 0x0017, 0x3e07], [0x7bf3, 0x000c, 0x3e07]]), + (11, &[[0x7bff, 0x0000, 0x3e03], [0x7bf4, 0x000b, 0x3e03], [0x7bf9, 0x0006, 0x3e03]]), + (15, &[[0x7bff, 0x0000, 0x3e00], [0x7bff, 0x0000, 0x3e00], [0x7bff, 0x0000, 0x3e00]]), + ]; + const SIGNED: [(u8, &[[u16; 3]]); 14] = [ + ( + 0, + &[ + [0x805d, 0x0000, 0xfbff], + [0x809b, 0x005d, 0xfbff], + [0x8080, 0x0036, 0xfbff], + [0x843d, 0x0000, 0xfbff], + [0x81ff, 0x005a, 0x132a], + [0x805d, 0x009b, 0x7ba3], + ], + ), + ( + 1, + &[ + [0x82e8, 0x0000, 0xfbff], + [0x84d8, 0x02e8, 0xfbff], + [0x8406, 0x01ae, 0xfbff], + [0xc0e8, 0x0000, 0xfbff], + [0x9d10, 0x02cc, 0x11b1], + [0x82e8, 0x04d8, 0x7918], + ], + ), + ( + 2, + &[ + [0x802e, 0x0000, 0xfbff], + [0x804d, 0x002e, 0xfbff], + [0x803f, 0x001b, 0xfbff], + [0x821e, 0x0000, 0xfbff], + [0x80ff, 0x002c, 0x1345], + [0x802e, 0x004d, 0x7bd1], + ], + ), + ( + 6, + &[ + [0x802e, 0x0000, 0xfbff], + [0x804d, 0x002e, 0xfbff], + [0x803f, 0x001b, 0xfbff], + [0x8126, 0x0000, 0xfbff], + [0x8097, 0x002c, 0x1345], + [0x802e, 0x004d, 0x7bd1], + ], + ), + ( + 10, + &[ + [0x802e, 0x0000, 0xfbff], + [0x804d, 0x002e, 0xfbff], + [0x803f, 0x001b, 0xfbff], + [0x8126, 0x0000, 0xfbff], + [0x8097, 0x002c, 0x1345], + [0x802e, 0x004d, 0x7bd1], + ], + ), + ( + 14, + &[ + [0x80ba, 0x0000, 0xfbff], + [0x8136, 0x00ba, 0xfbff], + [0x8101, 0x006b, 0xfbff], + [0x887a, 0x0000, 0xfbff], + [0x83ff, 0x00b3, 0x12f4], + [0x80ba, 0x0136, 0x7b46], + ], + ), + ( + 18, + &[ + [0x8174, 0x0000, 0xfbff], + [0x826c, 0x0174, 0xfbff], + [0x8203, 0x00d7, 0xfbff], + [0xa074, 0x0000, 0xfbff], + [0x8e88, 0x0166, 0x1288], + [0x8174, 0x026c, 0x7a8c], + ], + ), + ( + 22, + &[ + [0x8174, 0x0000, 0xfbff], + [0x826c, 0x0174, 0xfbff], + [0x8203, 0x00d7, 0xfbff], + [0x90f4, 0x0000, 0xfbff], + [0x87fe, 0x0166, 0x1288], + [0x8174, 0x026c, 0x7a8c], + ], + ), + ( + 26, + &[ + [0x8174, 0x0000, 0xfbff], + [0x826c, 0x0174, 0xfbff], + [0x8203, 0x00d7, 0xfbff], + [0x90f4, 0x0000, 0xfbff], + [0x87fe, 0x0166, 0x1288], + [0x8174, 0x026c, 0x7a8c], + ], + ), + ( + 30, + &[ + [0x85d0, 0x0000, 0xfbff], + [0x89b0, 0x05d0, 0xfbff], + [0x880d, 0x035c, 0xfbff], + [0x0000, 0x85d0, 0xfbff], + [0x835c, 0x0326, 0x1003], + [0x85d0, 0x09b0, 0x7630], + ], + ), + (3, &[[0x805d, 0x0000, 0xfbff], [0x809b, 0x005d, 0xfbff], [0x807d, 0x0031, 0xfbff]]), + (7, &[[0x802e, 0x0000, 0xfbff], [0x804d, 0x002e, 0xfbff], [0x803e, 0x0019, 0xfbff]]), + (11, &[[0x8017, 0x0000, 0xfbff], [0x8026, 0x0017, 0xfbff], [0x801f, 0x000c, 0xfbff]]), + (15, &[[0x8000, 0x0000, 0xfc00], [0x8001, 0x0000, 0xfc00], [0x8001, 0x0000, 0xfc00]]), + ]; + + #[test] + fn bc6h_every_mode_unsigned_and_signed() { + for (signed, expected) in [(false, &UNSIGNED), (true, &SIGNED)] { + for (id, want) in expected { + let (block, texels) = bc6h_case(*id); + let got = bc6h_halves(&block, signed); + let got: Vec<[u16; 3]> = texels.iter().map(|t| got[*t]).collect(); + assert_eq!(&got[..], *want, "mode {id}, signed {signed}"); + } + } + // Reserved modes (19, 23, 27, 31) are black. + for id in [19u32, 23, 27, 31] { + assert_eq!(bc6h_halves(&pack(&[(id, 5), (0, 123)]), false), [[0; 3]; 16]); + } + } + + #[test] + fn bc6h_tone_maps_to_srgb() { + assert_eq!([0x0000, 0x3800, 0x3c00, 0x7bff, 0xfc00, 0xbc00, 0x0001].map(|h| srgb8(half(h))), [0, 188, 255, 255, 0, 0, 0]); + assert_eq!(half(0x3555), 0.333_251_95, "a normal half: (1024 + 0x155) × 2^(13 - 25)"); + assert_eq!(half(0x0200), 2f32.powi(-15), "a subnormal half"); + // Mode 3's first texel: red 65504 and blue 1.5 clip to white, green is 0. + assert_eq!(bc6h(&bc6h_case(3).0, false)[0], [255, 0, 255, 255]); + } + + #[test] + fn any_block_decodes_without_panicking() { + let mut x = 0x9e37_79b9_7f4a_7c15u128 << 64 | 0x2545_f491_4f6c_dd1d; + for _ in 0..20_000 { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + let b = x.to_le_bytes(); + let _ = (bc7(&b), bc6h(&b, false), bc6h(&b, true)); + } + } +} diff --git a/crates/dds/src/eac.rs b/crates/dds/src/eac.rs new file mode 100644 index 0000000000..fe523c90e2 --- /dev/null +++ b/crates/dds/src/eac.rs @@ -0,0 +1,107 @@ +//! ETC1 and EAC 4×4 block decoders. +//! +//! EAC's 11-bit channel blocks are reduced to display bytes after reconstruction and clamping. +//! The signed layouts map -1..1 to 0..255 so negative values remain visible; zero is 128. +//! Layout and modifier tables: . + +// Positive halves of the Khronos EAC modifier tables. Negative entries are -positive - 1. +const MODIFIERS: [[i16; 4]; 16] = [ + [2, 5, 8, 14], + [2, 6, 9, 12], + [1, 4, 7, 12], + [1, 3, 5, 12], + [2, 5, 7, 11], + [2, 6, 8, 10], + [3, 6, 7, 10], + [2, 4, 7, 10], + [1, 5, 7, 9], + [1, 4, 7, 9], + [1, 3, 7, 9], + [1, 4, 6, 9], + [2, 3, 6, 9], + [0, 1, 2, 9], + [3, 5, 7, 8], + [2, 4, 6, 8], +]; + +fn split_halves(block: &[u8; 16]) -> ([u8; 8], [u8; 8]) { + let mut first = [0u8; 8]; + let mut second = [0u8; 8]; + for (i, x) in block.iter().enumerate() { + if i < 8 { + first[i] = *x; + } else { + second[i - 8] = *x; + } + } + (first, second) +} + +/// ETC1 uses big-endian blocks and column-major selector bits, independently of the split. +/// Differential bases outside RGB555 are undefined by the format and are refused. +pub(crate) fn etc1(block: &[u8; 8]) -> Option<[[u8; 4]; 16]> { + const ETC1_MODIFIERS: [[i16; 2]; 8] = [[2, 8], [5, 17], [9, 29], [13, 42], [18, 60], [24, 80], [33, 106], [47, 183]]; + let bits = u64::from_be_bytes(*block); + let differential = bits & (1 << 33) != 0; + let horizontal = bits & (1 << 32) != 0; + let mut bases = [[0i16; 3]; 2]; + for (channel, shift) in [56, 48, 40].into_iter().enumerate() { + let value = ((bits >> shift) & 255) as i16; + let (first, second) = if differential { + let first = value >> 3; + let delta = ((value & 7) ^ 4) - 4; + let second = first + delta; + if !(0..=31).contains(&second) { + return None; + } + ((first << 3) | (first >> 2), (second << 3) | (second >> 2)) + } else { + ((value >> 4) * 17, (value & 15) * 17) + }; + bases[0][channel] = first; + bases[1][channel] = second; + } + let first_table = ETC1_MODIFIERS.get(((bits >> 37) & 7) as usize)?; + let second_table = ETC1_MODIFIERS.get(((bits >> 34) & 7) as usize)?; + Some(std::array::from_fn(|pixel| { + let (x, y) = (pixel % 4, pixel / 4); + let second = if horizontal { y >= 2 } else { x >= 2 }; + let (base, table) = if second { (bases[1], second_table) } else { (bases[0], first_table) }; + let selector = x * 4 + y; + let magnitude = if bits & (1 << selector) == 0 { table[0] } else { table[1] }; + let delta = if bits & (1 << (selector + 16)) == 0 { magnitude } else { -magnitude }; + let rgb = base.map(|channel| (channel + delta).clamp(0, 255) as u8); + [rgb[0], rgb[1], rgb[2], 255] + })) +} + +fn channel(block: &[u8; 8], signed: bool) -> [u8; 16] { + let bits = u64::from_be_bytes(*block); + let base = if signed { i16::from((block[0] as i8).max(-127)) * 8 } else { i16::from(block[0]) * 8 + 4 }; + let scale = (i16::from(block[1] >> 4) * 8).max(1); + let table = MODIFIERS.get(usize::from(block[1] & 15)).copied().unwrap_or_default(); + std::array::from_fn(|pixel| { + let (x, y) = (pixel % 4, pixel / 4); + let selector = ((bits >> (45 - 3 * (x * 4 + y))) & 7) as usize; + let magnitude = table.get(selector & 3).copied().unwrap_or_default(); + let modifier = if selector < 4 { -magnitude - 1 } else { magnitude }; + let value = base + modifier * scale; + if signed { + let value = u32::try_from(value.clamp(-1023, 1023) + 1023).unwrap_or_default(); + ((value * 255 + 1023) / 2046) as u8 + } else { + let value = u32::try_from(value.clamp(0, 2047)).unwrap_or_default(); + ((value * 255 + 1023) / 2047) as u8 + } + }) +} + +pub(crate) fn scalar(block: &[u8; 8], signed: bool) -> [[u8; 4]; 16] { + channel(block, signed).map(|value| [value, value, value, 255]) +} + +pub(crate) fn dual(block: &[u8; 16], signed: bool) -> [[u8; 4]; 16] { + let (red, green) = split_halves(block); + let (red, green) = (channel(&red, signed), channel(&green, signed)); + std::array::from_fn(|pixel| [red[pixel], green[pixel], 0, 255]) +} diff --git a/crates/dds/src/lib.rs b/crates/dds/src/lib.rs index 183ec4fa14..354067f265 100644 --- a/crates/dds/src/lib.rs +++ b/crates/dds/src/lib.rs @@ -5,9 +5,10 @@ //! described by channel masks, RGB or luminance, or 16 bits per channel: `R16_UNORM`, //! `R16G16_UNORM`, `R16G16B16A16_UNORM`) or block-compressed BC1/BC2/BC3 //! (DXT1/DXT3/DXT5) or unsigned BC4/BC5 (ATI1/ATI2), with or without the DX10 header. BC4 opens -//! as gray and BC5 as RGB with blue 0: BC5 stores only red and green, and a normal map's Z is -//! left for the caller to reconstruct. Cubemaps, texture arrays and volume textures are refused -//! rather than flattened to one face or slice, and signed (SNORM) BC4/BC5 is refused too. +//! as gray, and BC5 and `R16G16_UNORM` as RGB with blue 0: they store only red and green, and a +//! normal map's Z is left for the caller to reconstruct. Cubemaps, texture arrays and volume +//! textures are refused rather than flattened to one face or slice, and signed (SNORM) BC4/BC5 is +//! refused too. //! //! Writing: a 2D texture, uncompressed (BGRA or BGRX, luminance with or without alpha), //! BC1/BC2/BC3 or BC4/BC5, optionally with its full mip chain. The legacy header describes all @@ -18,7 +19,7 @@ //! Pixels are 8 bits per channel ([`encode`], [`Texture::decode`]) or 16 bits per channel //! ([`encode16`], [`Texture::decode16`]), interleaved, top row first, in one of the [`Layout`]s. //! Uncompressed 16-bit pixels are written with the DX10 header as `R16_UNORM` (gray), -//! `R16G16_UNORM` (gray and alpha, or any two channels such as a normal map's X and Y) or +//! `R16G16_UNORM` (two channels, such as a normal map's X and Y, read back as red and green) or //! `R16G16B16A16_UNORM` (RGB, with alpha 65535, and RGBA). BC4 and BC5 fit their 8-bit endpoints //! and their indices to the 16-bit values; BC1 to BC3 round the pixels to 8 bits first. The //! crate has no dependencies; the `parallel` feature encodes and decodes rows of blocks (and mip @@ -38,6 +39,10 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] mod bc; +pub mod bptc; +mod eac; +mod packed; +pub mod pixels; use std::borrow::Cow; use std::fmt; @@ -83,6 +88,8 @@ pub enum DdsError { Alloc(usize), /// The decoded output exceeds the caller-selected per-buffer byte limit. LimitExceeded { required: usize, limit: u64 }, + /// Input pixel data ended before the declared dimensions or block count. + Truncated(String), } impl fmt::Display for DdsError { @@ -93,6 +100,7 @@ impl fmt::Display for DdsError { DdsError::Encode(reason) => write!(f, "DDS encode failed: {reason}"), DdsError::Alloc(len) => write!(f, "cannot allocate {len} bytes for the texture"), DdsError::LimitExceeded { required, limit } => write!(f, "decoded texture requires {required} bytes, exceeding limit {limit}"), + DdsError::Truncated(reason) => write!(f, "truncated pixel data: {reason}"), } } } @@ -269,7 +277,8 @@ enum Encoding { Bc1, Bc2, Bc3, - /// 16 bits per channel, little-endian: gray, gray and alpha (or red and green), or RGBA. + /// 16 bits per channel, little-endian: gray, red and green (read as RGB with blue 0), or + /// RGBA. Unorm16 { channels: usize, }, @@ -301,7 +310,7 @@ impl Encoding { Encoding::Masks { gray: true, .. } => Layout::Gray, Encoding::Masks { fields, .. } if fields[3].is_none() => Layout::Rgb, Encoding::Unorm16 { channels: 1 } => Layout::Gray, - Encoding::Unorm16 { channels: 2 } => Layout::GrayA, + Encoding::Unorm16 { channels: 2 } => Layout::Rgb, Encoding::Bc4 => Layout::Gray, Encoding::Bc5 => Layout::Rgb, _ => Layout::Rgba, @@ -430,6 +439,7 @@ pub struct Texture<'a> { encoding: Encoding, /// Where the top level's pixel data starts. start: usize, + levels: u32, } impl<'a> Texture<'a> { @@ -456,7 +466,8 @@ impl<'a> Texture<'a> { if width == 0 || height == 0 { return Err(bad("texture dimensions must be nonzero")); } - Ok(Texture { bytes: b, flags, width, height, encoding, start }) + let levels = le32(b, 28)?.max(1).min(mip_levels(width, height)); + Ok(Texture { bytes: b, flags, width, height, encoding, start, levels }) } pub fn width(&self) -> u32 { @@ -472,6 +483,62 @@ impl<'a> Texture<'a> { self.encoding.layout() } + /// Number of declared mip levels, capped by the chain down to one pixel. + pub fn levels(&self) -> u32 { + self.levels + } + + /// Bits in the widest channel the file stores: 16 for the 16-bit formats, the widest mask + /// field for other uncompressed pixels, 8 for block compressions. Above 8, + /// [`Texture::decode`] rounds samples that [`Texture::decode16`] keeps. + pub fn bits_per_channel(&self) -> u32 { + match self.encoding { + Encoding::Unorm16 { .. } => 16, + Encoding::Masks { fields, .. } => fields.iter().flatten().map(|f| u32::BITS - f.max.leading_zeros()).max().unwrap_or(0), + Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc4 | Encoding::Bc5 => 8, + } + } + + /// The encoding named by the header, including unsigned BC4 and BC5. + pub fn compression(&self) -> DdsCompression { + match self.encoding { + Encoding::Masks { .. } | Encoding::Unorm16 { .. } => DdsCompression::None, + Encoding::Bc1 => DdsCompression::Bc1, + Encoding::Bc2 => DdsCompression::Bc2, + Encoding::Bc3 => DdsCompression::Bc3, + Encoding::Bc4 => DdsCompression::Bc4, + Encoding::Bc5 => DdsCompression::Bc5, + } + } + + /// A borrowed view of one mip level, without allocating or decoding pixels. + /// Lower levels are tightly packed; only the top level uses the header's row pitch. + pub fn mip_level(&self, index: u32) -> Result, DdsError> { + if index >= self.levels { + return Err(bad("mip level index is outside the declared chain")); + } + let (mut width, mut height, mut start) = (self.width, self.height, self.start); + for i in 0..index { + let pitch = if i == 0 && self.flags & DDSD_PITCH != 0 { Some(le32(self.bytes, 20)?) } else { None }; + let (_, size) = self.encoding.level_layout(width, height, pitch)?; + start = start.checked_add(size).ok_or_else(|| bad("mip offset overflow"))?; + if start > self.bytes.len() { + return Err(bad("truncated mip data")); + } + width = (width / 2).max(1); + height = (height / 2).max(1); + } + Ok(Texture { + bytes: self.bytes, + flags: if index == 0 { self.flags } else { self.flags & !DDSD_PITCH }, + width, + height, + encoding: self.encoding, + start, + levels: self.levels - index, + }) + } + /// The top mip level's pixels in [`Texture::layout`], bounded by [`DEFAULT_MAX_DECODE_BYTES`]. pub fn decode(&self) -> Result, DdsError> { self.decode_with_limit(DEFAULT_MAX_DECODE_BYTES) @@ -487,11 +554,8 @@ impl<'a> Texture<'a> { match encoding { Encoding::Masks { bytes, gray, fields } => decode_masks(Masks { bytes, gray, fields }, data, row_pitch, &mut pixels, row_size), // Each 16-bit sample rounded to the nearest 8-bit level. - Encoding::Unorm16 { .. } => rows(&mut pixels, row_size, |y, dst| { - let src = data.get(y * row_pitch..).unwrap_or_default(); - for (d, s) in dst.iter_mut().zip(src.as_chunks::<2>().0) { - *d = to8(u16::from_le_bytes(*s)); - } + Encoding::Unorm16 { channels: stored } => rows(&mut pixels, row_size, |y, dst| { + unorm16_row(data.get(y * row_pitch..).unwrap_or_default(), stored, dst, to8); }), Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 | Encoding::Bc4 | Encoding::Bc5 => { decode_blocks(encoding, data, (w, channels), &mut pixels) @@ -518,11 +582,8 @@ impl<'a> Texture<'a> { let mut pixels = alloc_zeroed(total / 2)?; match encoding { Encoding::Masks { bytes, gray, fields } => decode_masks16(Masks { bytes, gray, fields }, data, row_pitch, &mut pixels, row_size), - Encoding::Unorm16 { .. } => rows(&mut pixels, row_size, |y, dst| { - let src = data.get(y * row_pitch..).unwrap_or_default(); - for (d, s) in dst.iter_mut().zip(src.as_chunks::<2>().0) { - *d = u16::from_le_bytes(*s); - } + Encoding::Unorm16 { channels: stored } => rows(&mut pixels, row_size, |y, dst| { + unorm16_row(data.get(y * row_pitch..).unwrap_or_default(), stored, dst, |v| v); }), Encoding::Bc4 | Encoding::Bc5 => decode_value_blocks16(encoding, data, (w, channels), &mut pixels), Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => { @@ -556,6 +617,21 @@ impl<'a> Texture<'a> { } } +/// One row of 16-bit samples, `stored` a pixel and little-endian, into `dst` through `convert`. +/// Two stored channels are red and green: they fill RGB pixels with blue 0. +fn unorm16_row(src: &[u8], stored: usize, dst: &mut [T], convert: impl Fn(u16) -> T) { + let src = src.as_chunks::<2>().0; + if stored == 2 { + for (d, s) in dst.as_chunks_mut::<3>().0.iter_mut().zip(src.as_chunks::<2>().0) { + *d = [convert(u16::from_le_bytes(s[0])), convert(u16::from_le_bytes(s[1])), T::default()]; + } + } else { + for (d, s) in dst.iter_mut().zip(src) { + *d = convert(u16::from_le_bytes(*s)); + } + } +} + /// A 16-bit sample rounded to the nearest 8-bit level. fn to8(v: u16) -> u8 { ((u32::from(v) + 128) / 257) as u8 @@ -899,7 +975,8 @@ pub fn encode(width: u32, height: u32, layout: Layout, pixels: &[u8], options: D /// [`encode`] for 16-bit pixels. Uncompressed, they keep 16 bits per channel: gray as /// `R16_UNORM`, gray and alpha (or any two channels, such as a normal map's X and Y) as /// `R16G16_UNORM`, RGB (with alpha 65535) and RGBA as `R16G16B16A16_UNORM`, all with the DX10 -/// header. BC4 and BC5 pick their endpoints and indices against the 16-bit values; BC1 to BC3 +/// header. `R16G16_UNORM` holds red and green, so two channels read back as RGB with blue 0. BC4 +/// and BC5 pick their endpoints and indices against the 16-bit values; BC1 to BC3 /// round the pixels to 8 bits and write what [`encode`] writes from them. pub fn encode16(width: u32, height: u32, layout: Layout, pixels: &[u16], options: DdsOptions) -> Result, DdsError> { if width == 0 || height == 0 { @@ -1246,6 +1323,24 @@ mod tests { assert_eq!(Texture::parse(&file).and_then(|t| t.decode()), Ok(vec![7; 64])); } + #[test] + fn bits_per_channel_is_the_widest_stored_channel() { + let options = |compression| DdsOptions { compression, mipmaps: false }; + let gray = encode(4, 4, Layout::Gray, &[7; 16], options(DdsCompression::None)).unwrap(); + let bc4 = encode(4, 4, Layout::Gray, &[7; 16], options(DdsCompression::Bc4)).unwrap(); + let r16 = encode16(4, 4, Layout::Gray, &[7; 16], options(DdsCompression::None)).unwrap(); + let bits = |file: &[u8]| Texture::parse(file).unwrap().bits_per_channel(); + assert_eq!((bits(&gray), bits(&bc4), bits(&r16)), (8, 8, 16)); + // A2R10G10B10 as masks: 10 bits. + let mut a2r10 = gray[..HEADER].to_vec(); + a2r10[80..84].copy_from_slice(&(DDPF_RGB | DDPF_ALPHAPIXELS).to_le_bytes()); + a2r10[88..92].copy_from_slice(&32u32.to_le_bytes()); + for (i, mask) in [0x3ff0_0000u32, 0xf_fc00, 0x3ff, 0xc000_0000].into_iter().enumerate() { + a2r10[92 + 4 * i..96 + 4 * i].copy_from_slice(&mask.to_le_bytes()); + } + assert_eq!(bits(&a2r10), 10); + } + /// Samples spread over the whole 16-bit range, `channels` a pixel. fn spread16(width: usize, height: usize, channels: usize) -> Vec { (0..width * height * channels).map(|i| (i as u32 * 7919 % 65536) as u16).collect() @@ -1256,13 +1351,17 @@ mod tests { let (w, h) = (7, 5); for (layout, dxgi, read_as, pixel_bytes) in [ (Layout::Gray, 56, Layout::Gray, 2), - (Layout::GrayA, 35, Layout::GrayA, 4), + (Layout::GrayA, 35, Layout::Rgb, 4), (Layout::Rgb, 11, Layout::Rgba, 8), (Layout::Rgba, 11, Layout::Rgba, 8), ] { let pixels = spread16(w, h, layout.channels()); - let want: Vec = - if layout == Layout::Rgb { pixels.chunks(3).flat_map(|p| [p[0], p[1], p[2], 65535]).collect() } else { pixels.clone() }; + let want: Vec = match layout { + // R16G16 holds red and green: the two channels read back with blue 0. + Layout::GrayA => pixels.chunks(2).flat_map(|p| [p[0], p[1], 0]).collect(), + Layout::Rgb => pixels.chunks(3).flat_map(|p| [p[0], p[1], p[2], 65535]).collect(), + _ => pixels.clone(), + }; for mipmaps in [false, true] { let file = encode16(w as u32, h as u32, layout, &pixels, DdsOptions { compression: DdsCompression::None, mipmaps }).unwrap(); assert_eq!(le32(&file, HEADER).unwrap(), dxgi, "{layout:?}"); @@ -1311,6 +1410,7 @@ mod tests { let texture = Texture::parse(&l16).unwrap(); assert_eq!(texture.decode16().unwrap(), [300, 65000]); assert_eq!(texture.decode().unwrap(), [1, 253]); + assert_eq!(texture.bits_per_channel(), 16); // G16R16 as masks (red low, green high) reads as RGB at 16 bits, blue 0. let mut g16r16 = l16[..HEADER].to_vec(); g16r16[80..84].copy_from_slice(&DDPF_RGB.to_le_bytes()); @@ -1322,6 +1422,36 @@ mod tests { assert_eq!((texture.layout(), texture.decode16().unwrap()), (Layout::Rgb, vec![300, 40000, 0, 65535, 7, 0])); } + /// A DX10 `R16G16` file (DXGI 35, or 33 typeless) of `levels` levels: red then green, + /// little-endian, each level packed after the one above. + fn rg16_file(width: u32, height: u32, dxgi: u32, levels: u32, samples: &[u16]) -> Vec { + let mut file = header(width, height, Store::R16, levels, 0).unwrap().to_vec(); + file[HEADER..HEADER + 4].copy_from_slice(&dxgi.to_le_bytes()); + file[20..24].copy_from_slice(&(width * 4).to_le_bytes()); + file.extend(samples.iter().flat_map(|v| v.to_le_bytes())); + file + } + + #[test] + fn r16g16_textures_read_as_red_and_green_with_blue_0() { + // R16G16 stores red and green and no alpha: blue is 0, as for BC5 and G16R16 masks. + let (w, h) = (3, 2); + let top = spread16(w, h, 2); + let rgb: Vec = top.chunks(2).flat_map(|p| [p[0], p[1], 0]).collect(); + for dxgi in [35, 33] { + // 3×2, then 1×1 with red 11 and green 22. + let file = rg16_file(w as u32, h as u32, dxgi, 2, &[top.clone(), vec![11, 22]].concat()); + let texture = Texture::parse(&file).unwrap(); + assert_eq!((texture.layout(), texture.bits_per_channel()), (Layout::Rgb, 16), "DXGI {dxgi}"); + assert_eq!(texture.decode16().unwrap(), rgb, "DXGI {dxgi}"); + assert_eq!(texture.decode().unwrap(), rgb.iter().map(|&v| to8(v)).collect::>(), "DXGI {dxgi}"); + let level = texture.mip_level(1).unwrap(); + assert_eq!(level.decode16().unwrap(), [11, 22, 0]); + assert!(matches!(level.decode16_with_limit(5), Err(DdsError::LimitExceeded { required: 6, limit: 5 }))); + assert!(Texture::parse(&file[..file.len() - 5]).unwrap().decode16().is_err()); + } + } + #[test] fn typeless_and_padded_16_bit_textures_are_read() { // Rows padded to 8 bytes, as other writers may store them: the padding is skipped. @@ -1333,8 +1463,9 @@ mod tests { padded.extend_from_slice(&[0xee, 0xee]); } assert_eq!(Texture::parse(&padded).unwrap().decode16().unwrap(), [1, 2, 3, 4, 5, 6]); - // The typeless forms read as the UNORM ones; typeless RGBA refuses premultiplied alpha too. - for (unorm, typeless, layout) in [(56u32, 53u32, Layout::Gray), (35, 33, Layout::GrayA), (11, 9, Layout::Rgba)] { + // The typeless forms read as the UNORM ones (R16G16's in its own test); typeless RGBA + // refuses premultiplied alpha too. + for (unorm, typeless, layout) in [(56u32, 53u32, Layout::Gray), (11, 9, Layout::Rgba)] { let pixels = spread16(3, 2, layout.channels()); let file = encode16(3, 2, layout, &pixels, DdsOptions::default()).unwrap(); assert_eq!(le32(&file, HEADER).unwrap(), unorm); diff --git a/crates/dds/src/packed.rs b/crates/dds/src/packed.rs new file mode 100644 index 0000000000..6434a6e3ca --- /dev/null +++ b/crates/dds/src/packed.rs @@ -0,0 +1,51 @@ +//! Four-byte packed RGB layouts. Words are little-endian, with red in the low bits. +//! Unsigned floats: , sections 10.2 and 10.3. + +use crate::bptc; + +pub(crate) fn rgba1010102(bytes: &[u8; 4]) -> [u8; 4] { + let word = u32::from_le_bytes(*bytes); + let channel = |shift: u32| (((word >> shift) & 1023) * 255 + 511) / 1023; + [channel(0) as u8, channel(10) as u8, channel(20) as u8, (word >> 30) as u8 * 85] +} + +// These unsigned formats share binary16's five-bit exponent and bias. Padding the +// six- or five-bit fraction with zeroes produces the same value in a positive half. +fn unsigned_float(bits: u16, padding: u32) -> f32 { + bptc::half(bits << padding) +} + +pub(crate) fn rgb111110(bytes: &[u8; 4]) -> [u8; 4] { + let word = u32::from_le_bytes(*bytes); + let channel = |shift: u32, mask: u32, padding: u32| bptc::srgb8(unsigned_float(((word >> shift) & mask) as u16, padding)); + [channel(0, 0x7ff, 4), channel(11, 0x7ff, 4), channel(22, 0x3ff, 5), 255] +} + +pub(crate) fn rgb9e5(bytes: &[u8; 4]) -> [u8; 4] { + let packed = u32::from_le_bytes(*bytes); + let scale = 2.0f32.powi((packed >> 27) as i32 - 24); + let channel = |shift: u32| bptc::srgb8(((packed >> shift) & 0x1ff) as f32 * scale); + [channel(0), channel(9), channel(18), 255] +} + +#[cfg(test)] +mod tests { + use super::unsigned_float; + + #[test] + fn unsigned_float_boundaries_are_preserved_before_display_quantization() { + for (fraction_bits, padding, smallest, largest_subnormal, maximum) in + [(6, 4, 0x3580_0000, 0x387c_0000, 65024.0), (5, 5, 0x3600_0000, 0x3878_0000, 64512.0)] + { + let fractions = 1u16 << fraction_bits; + assert_eq!(unsigned_float(0, padding).to_bits(), 0); + assert_eq!(unsigned_float(1, padding).to_bits(), smallest); + assert_eq!(unsigned_float(fractions - 1, padding).to_bits(), largest_subnormal); + assert_eq!(unsigned_float(fractions, padding).to_bits(), 0x3880_0000); + assert_eq!(unsigned_float(31 * fractions - 1, padding), maximum); + assert_eq!(unsigned_float(31 * fractions, padding), f32::INFINITY); + assert!(unsigned_float(31 * fractions + 1, padding).is_nan()); + assert!(unsigned_float(32 * fractions - 1, padding).is_nan()); + } + } +} diff --git a/crates/dds/src/pixels.rs b/crates/dds/src/pixels.rs new file mode 100644 index 0000000000..8d39339112 --- /dev/null +++ b/crates/dds/src/pixels.rs @@ -0,0 +1,390 @@ +//! Turning GPU textures' stored pixels into RGBA: uncompressed layouts, packed 32-bit layouts, +//! and 4×4 block formats (BC1–BC7, ETC1, and EAC). Rows come out in the order they're stored. + +use crate::{DdsError, alloc_zeroed, bc, bptc, eac, packed, with_alpha}; + +/// Default RGBA output budget: 256 MiB, matching an 8192×8192 RGBA8 texture. +pub const DEFAULT_MAX_RGBA_BYTES: u64 = 256 << 20; + +/// How a texture's pixels are laid out. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PixelLayout { + /// Alpha only (shown as white with that alpha). + Alpha8, + /// One grey channel. + R8, + /// One unsigned 16-bit channel, shown as grey. + R16, + /// Two unsigned 8-bit channels; blue is zero and alpha is opaque. + Rg8, + /// Two unsigned 16-bit channels; blue is zero and alpha is opaque. + Rg16, + /// Three unsigned 16-bit channels; alpha is opaque. + Rgb16, + /// Four unsigned 16-bit channels, including alpha. + Rgba16, + RHalf, + RgHalf, + RgbaHalf, + RFloat, + RgFloat, + RgbaFloat, + /// Nine-bit RGB mantissas with a shared five-bit exponent, displayed as opaque sRGB. + Rgb9e5, + /// Packed unsigned 10-bit RGB with two-bit alpha, low bits first. + Rgba1010102, + /// Packed unsigned 11/11/10-bit floating-point RGB, displayed as opaque sRGB. + Rgb111110Float, + Rgb24, + Rgba32, + Argb32, + Bgra32, + Rgb565, + Rgba4444, + Argb4444, + /// BC1 / DXT1. + Dxt1, + /// BC2 / DXT3: four-colour blocks with one explicit four-bit alpha value per texel. + Dxt3, + /// BC3 / DXT5. + Dxt5, + /// One channel in blocks (shown as grey). + Bc4, + /// Two channels in blocks, usually a normal map (blue is rebuilt from red and green). + Bc5, + /// High dynamic range RGB in blocks (unsigned half floats), tone-mapped to 8-bit sRGB. + Bc6h, + /// BC6H with signed half floats. + Bc6hSigned, + Bc7, + /// ETC1 RGB, with individual or differential base colours. + Etc1, + /// One EAC channel, shown as grey. + EacR, + /// One signed EAC channel; -1..1 maps to 0..255. + EacRSigned, + /// Two EAC channels; blue is zero and alpha is opaque. + EacRg, + /// Two signed EAC channels; -1..1 maps to 0..255. + EacRgSigned, +} + +/// Bytes of a `width`×`height` picture in `layout`. +pub fn size(layout: PixelLayout, width: u32, height: u32) -> Option { + let (w, h) = (width as usize, height as usize); + let blocks = w.div_ceil(4).checked_mul(h.div_ceil(4))?; + match layout { + PixelLayout::Alpha8 | PixelLayout::R8 => w.checked_mul(h), + PixelLayout::Rgb565 | PixelLayout::Rgba4444 | PixelLayout::Argb4444 | PixelLayout::RHalf | PixelLayout::R16 | PixelLayout::Rg8 => { + w.checked_mul(h)?.checked_mul(2) + } + PixelLayout::Rgb24 => w.checked_mul(h)?.checked_mul(3), + PixelLayout::Rg16 => w.checked_mul(h)?.checked_mul(4), + PixelLayout::Rgb16 => w.checked_mul(h)?.checked_mul(6), + PixelLayout::Rgba16 => w.checked_mul(h)?.checked_mul(8), + PixelLayout::Rgba32 + | PixelLayout::Argb32 + | PixelLayout::Bgra32 + | PixelLayout::RgHalf + | PixelLayout::RFloat + | PixelLayout::Rgb9e5 + | PixelLayout::Rgba1010102 + | PixelLayout::Rgb111110Float => w.checked_mul(h)?.checked_mul(4), + PixelLayout::RgbaHalf | PixelLayout::RgFloat => w.checked_mul(h)?.checked_mul(8), + PixelLayout::RgbaFloat => w.checked_mul(h)?.checked_mul(16), + PixelLayout::Dxt1 | PixelLayout::Bc4 | PixelLayout::Etc1 | PixelLayout::EacR | PixelLayout::EacRSigned => blocks.checked_mul(8), + PixelLayout::Dxt3 + | PixelLayout::Dxt5 + | PixelLayout::Bc5 + | PixelLayout::Bc6h + | PixelLayout::Bc6hSigned + | PixelLayout::Bc7 + | PixelLayout::EacRg + | PixelLayout::EacRgSigned => blocks.checked_mul(16), + } +} + +/// Convert an IEEE 754 binary16 half-float's bits to `f32`. +pub fn half_to_f32(h: u16) -> f32 { + bptc::half(h) +} + +/// Convert a linear-light value to 8-bit sRGB; 0..1 is kept, brighter and negative values clip. +pub fn linear_to_srgb8(v: f32) -> u8 { + bptc::srgb8(v) +} + +/// Decode one BC1 (DXT1) 8-byte block into 16 RGBA8 texels (row by row). +pub fn decode_bc1_block(block: &[u8; 8]) -> [[u8; 4]; 16] { + bc::decode_colour(block, true) +} + +/// Decode one BC2 (DXT3) 16-byte block into 16 RGBA8 texels (row by row). +pub fn decode_bc2_block(block: &[u8; 16]) -> [[u8; 4]; 16] { + with_alpha(bc::decode_colour(block.get(8..).unwrap_or_default(), false), bc::decode_bc2_alpha(block.get(..8).unwrap_or_default())) +} + +/// Decode one BC3 (DXT5) 16-byte block into 16 RGBA8 texels (row by row). +pub fn decode_bc3_block(block: &[u8; 16]) -> [[u8; 4]; 16] { + with_alpha(bc::decode_colour(block.get(8..).unwrap_or_default(), false), bc::decode_values(block.get(..8).unwrap_or_default())) +} + +/// Decode one BC4 8-byte block into 16 greyscale RGBA8 texels (`[r, r, r, 255]`, row by row). +pub fn decode_bc4_block(block: &[u8; 8]) -> [[u8; 4]; 16] { + bc::decode_values(block).map(|r| [r, r, r, 255]) +} + +/// Decode one BC5 16-byte normal-map block into 16 RGBA8 texels with reconstructed Z (row by row). +pub fn decode_bc5_block(block: &[u8; 16]) -> [[u8; 4]; 16] { + let (r, g) = (bc::decode_values(block.get(..8).unwrap_or_default()), bc::decode_values(block.get(8..).unwrap_or_default())); + std::array::from_fn(|k| { + let (x, y) = (f32::from(r[k]) / 127.5 - 1.0, f32::from(g[k]) / 127.5 - 1.0); + let z = (1.0 - x * x - y * y).max(0.0).sqrt(); + [r[k], g[k], ((z + 1.0) * 127.5) as u8, 255] + }) +} + +/// Decode one BC6H 16-byte block into 16 sRGB8 RGBA texels (opaque). +pub fn decode_bc6h_block(block: &[u8; 16], signed: bool) -> [[u8; 4]; 16] { + bptc::bc6h(block, signed) +} + +/// Decode one BC7 16-byte block into 16 RGBA8 texels (row by row). +pub fn decode_bc7_block(block: &[u8; 16]) -> [[u8; 4]; 16] { + bptc::bc7(block) +} + +/// Decode one ETC1 8-byte block into 16 RGBA8 texels; returns `None` if differential colours overflow 5 bits. +pub fn decode_etc1_block(block: &[u8; 8]) -> Option<[[u8; 4]; 16]> { + eac::etc1(block) +} + +/// Decode one single-channel EAC 8-byte block into 16 greyscale RGBA8 texels. +pub fn decode_eac_r_block(block: &[u8; 8], signed: bool) -> [[u8; 4]; 16] { + eac::scalar(block, signed) +} + +/// Decode one dual-channel EAC 16-byte block into 16 RGBA8 texels (blue 0, alpha 255). +pub fn decode_eac_rg_block(block: &[u8; 16], signed: bool) -> [[u8; 4]; 16] { + eac::dual(block, signed) +} + +/// Decode one little-endian `RGB9E5` 4-byte pixel into opaque sRGB8 RGBA. +pub fn decode_rgb9e5(bytes: &[u8; 4]) -> [u8; 4] { + packed::rgb9e5(bytes) +} + +/// Decode one little-endian `RGBA1010102` 4-byte pixel into RGBA8. +pub fn decode_rgba1010102(bytes: &[u8; 4]) -> [u8; 4] { + packed::rgba1010102(bytes) +} + +/// Decode one little-endian `R11G11B10_FLOAT` 4-byte pixel into opaque sRGB8 RGBA. +pub fn decode_rgb111110_float(bytes: &[u8; 4]) -> [u8; 4] { + packed::rgb111110(bytes) +} + +fn unorm8(value: u16) -> u8 { + ((u32::from(value) * 255 + 32767) / 65535) as u8 +} + +fn unorm16_pixels(samples: impl Iterator, channels: usize) -> impl Iterator { + let mut samples = samples.map(unorm8); + std::iter::from_fn(move || { + let (r, g) = (samples.next()?, samples.next()?); + let b = if channels > 2 { samples.next()? } else { 0 }; + let a = if channels > 3 { samples.next()? } else { 255 }; + Some([r, g, b, a]) + }) +} + +fn float_pixels(mut samples: impl Iterator, channels: usize) -> impl Iterator { + std::iter::from_fn(move || { + let r = bptc::srgb8(samples.next()?); + if channels == 1 { + return Some([r, r, r, 255]); + } + let g = bptc::srgb8(samples.next()?); + if channels == 2 { + return Some([r, g, 0, 255]); + } + let b = bptc::srgb8(samples.next()?); + let a = (samples.next()?.clamp(0.0, 1.0) * 255.0).round() as u8; + Some([r, g, b, a]) + }) +} + +fn expand5(v: u16) -> u8 { + let v = (v & 31) as u8; + (v << 3) | (v >> 2) +} + +fn expand6(v: u16) -> u8 { + let v = (v & 63) as u8; + (v << 2) | (v >> 4) +} + +fn rgb565(v: u16) -> [u8; 4] { + [expand5(v >> 11), expand6(v >> 5), expand5(v), 255] +} + +fn nibbles(v: u16, order: [u32; 4]) -> [u8; 4] { + order.map(|shift| ((v >> shift) & 15) as u8 * 17) +} + +fn place_blocks(out: &mut [u8], w: usize, h: usize, texels: impl Iterator) { + let across = w.div_ceil(4); + for (i, block) in texels.enumerate() { + let (bx, by) = (i % across, i / across); + for (k, p) in block.iter().enumerate() { + let (x, y) = (bx * 4 + k % 4, by * 4 + k / 4); + if x < w && y < h { + let at = (y * w + x) * 4; + if let Some(dst) = out.get_mut(at..at + 4) { + dst.copy_from_slice(p); + } + } + } + } +} + +/// Decode a `width`×`height` picture (`data` starts with it) into RGBA8, rows in stored order. +/// +/// Caps the output at [`DEFAULT_MAX_RGBA_BYTES`]; use [`decode_with_limit`] for a different budget. +pub fn decode(layout: PixelLayout, width: u32, height: u32, data: &[u8]) -> Result, DdsError> { + decode_with_limit(layout, width, height, data, DEFAULT_MAX_RGBA_BYTES) +} + +/// Decode into RGBA8, allowing at most `max_bytes` of output. +/// +/// The budget applies to RGBA bytes, not the encoded input, and is checked before reading pixels +/// or reserving the output. +pub fn decode_with_limit(layout: PixelLayout, width: u32, height: u32, data: &[u8], max_bytes: u64) -> Result, DdsError> { + let need = size(layout, width, height).ok_or(DdsError::Alloc(usize::MAX))?; + let (w, h) = (width as usize, height as usize); + let out_len = + w.checked_mul(h).and_then(|pixels| pixels.checked_mul(4)).filter(|bytes| *bytes <= isize::MAX as usize).ok_or(DdsError::Alloc(usize::MAX))?; + if out_len as u64 > max_bytes { + return Err(DdsError::LimitExceeded { required: out_len, limit: max_bytes }); + } + let data = data.get(..need).ok_or_else(|| DdsError::Truncated(format!("{} bytes of pixels, {need} needed", data.len())))?; + if w == 0 || h == 0 { + return Err(DdsError::Malformed("an empty picture".into())); + } + let mut out: Vec = alloc_zeroed(out_len)?; + let mut put = |px: &mut dyn Iterator| { + for (dst, p) in out.as_chunks_mut::<4>().0.iter_mut().zip(px) { + *dst = p; + } + }; + let u16s = data.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b)); + let floats = data.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes(*b)); + match layout { + PixelLayout::RHalf => put(&mut float_pixels(u16s.map(bptc::half), 1)), + PixelLayout::RgHalf => put(&mut float_pixels(u16s.map(bptc::half), 2)), + PixelLayout::RgbaHalf => put(&mut float_pixels(u16s.map(bptc::half), 4)), + PixelLayout::RFloat => put(&mut float_pixels(floats, 1)), + PixelLayout::RgFloat => put(&mut float_pixels(floats, 2)), + PixelLayout::RgbaFloat => put(&mut float_pixels(floats, 4)), + PixelLayout::Rgba1010102 => put(&mut data.as_chunks::<4>().0.iter().map(packed::rgba1010102)), + PixelLayout::Rgb111110Float => put(&mut data.as_chunks::<4>().0.iter().map(packed::rgb111110)), + PixelLayout::Rgb9e5 => put(&mut data.as_chunks::<4>().0.iter().map(packed::rgb9e5)), + PixelLayout::Alpha8 => put(&mut data.iter().map(|a| [255, 255, 255, *a])), + PixelLayout::R8 => put(&mut data.iter().map(|r| [*r, *r, *r, 255])), + PixelLayout::R16 => put(&mut u16s.map(|value| { + let grey = unorm8(value); + [grey, grey, grey, 255] + })), + PixelLayout::Rg8 => put(&mut data.as_chunks::<2>().0.iter().map(|[r, g]| [*r, *g, 0, 255])), + PixelLayout::Rg16 => put(&mut unorm16_pixels(u16s, 2)), + PixelLayout::Rgb16 => put(&mut unorm16_pixels(u16s, 3)), + PixelLayout::Rgba16 => put(&mut unorm16_pixels(u16s, 4)), + PixelLayout::Rgb24 => put(&mut data.as_chunks::<3>().0.iter().map(|[r, g, b]| [*r, *g, *b, 255])), + PixelLayout::Rgba32 => put(&mut data.as_chunks::<4>().0.iter().copied()), + PixelLayout::Argb32 => put(&mut data.as_chunks::<4>().0.iter().map(|[a, r, g, b]| [*r, *g, *b, *a])), + PixelLayout::Bgra32 => put(&mut data.as_chunks::<4>().0.iter().map(|[b, g, r, a]| [*r, *g, *b, *a])), + PixelLayout::Rgb565 => put(&mut u16s.map(rgb565)), + PixelLayout::Rgba4444 => put(&mut u16s.map(|v| nibbles(v, [12, 8, 4, 0]))), + PixelLayout::Argb4444 => put(&mut u16s.map(|v| nibbles(v, [8, 4, 0, 12]))), + PixelLayout::Dxt1 => place_blocks(&mut out, w, h, data.as_chunks::<8>().0.iter().map(decode_bc1_block)), + PixelLayout::Dxt3 => place_blocks(&mut out, w, h, data.as_chunks::<16>().0.iter().map(decode_bc2_block)), + PixelLayout::Dxt5 => place_blocks(&mut out, w, h, data.as_chunks::<16>().0.iter().map(decode_bc3_block)), + PixelLayout::Bc4 => place_blocks(&mut out, w, h, data.as_chunks::<8>().0.iter().map(decode_bc4_block)), + PixelLayout::Bc5 => place_blocks(&mut out, w, h, data.as_chunks::<16>().0.iter().map(decode_bc5_block)), + PixelLayout::Bc6h => place_blocks(&mut out, w, h, data.as_chunks::<16>().0.iter().map(|b| bptc::bc6h(b, false))), + PixelLayout::Bc6hSigned => place_blocks(&mut out, w, h, data.as_chunks::<16>().0.iter().map(|b| bptc::bc6h(b, true))), + PixelLayout::Bc7 => place_blocks(&mut out, w, h, data.as_chunks::<16>().0.iter().map(bptc::bc7)), + PixelLayout::EacR | PixelLayout::EacRSigned => { + place_blocks(&mut out, w, h, data.as_chunks::<8>().0.iter().map(|block| eac::scalar(block, layout == PixelLayout::EacRSigned))) + } + PixelLayout::EacRg | PixelLayout::EacRgSigned => { + place_blocks(&mut out, w, h, data.as_chunks::<16>().0.iter().map(|block| eac::dual(block, layout == PixelLayout::EacRgSigned))) + } + PixelLayout::Etc1 => { + let mut valid = true; + let texels = data.as_chunks::<8>().0.iter().map_while(|block| { + let pixels = eac::etc1(block); + valid &= pixels.is_some(); + pixels + }); + place_blocks(&mut out, w, h, texels); + if !valid { + return Err(DdsError::Malformed("ETC1 differential colour is outside its five-bit range".into())); + } + } + } + Ok(out) +} + +/// Flip a `width`-wide RGBA8 picture upside down. +pub fn flip_rows(rgba: &[u8], width: u32) -> Vec { + let row = (width as usize).saturating_mul(4).max(1); + rgba.chunks(row).rev().flatten().copied().collect() +} + +#[cfg(test)] +mod tests { + use super::{PixelLayout, decode, decode_with_limit, flip_rows, size}; + use crate::DdsError; + + #[test] + fn uncompressed_and_swizzled_layouts() { + assert_eq!(decode(PixelLayout::Rgba32, 1, 2, &[1, 2, 3, 4, 5, 6, 7, 8]).unwrap(), [1, 2, 3, 4, 5, 6, 7, 8]); + assert_eq!(decode(PixelLayout::Argb32, 1, 1, &[9, 1, 2, 3]).unwrap(), [1, 2, 3, 9]); + assert_eq!(decode(PixelLayout::Bgra32, 1, 1, &[3, 2, 1, 9]).unwrap(), [1, 2, 3, 9]); + assert_eq!(decode(PixelLayout::Rgb24, 1, 1, &[1, 2, 3]).unwrap(), [1, 2, 3, 255]); + assert_eq!(decode(PixelLayout::Alpha8, 1, 1, &[7]).unwrap(), [255, 255, 255, 7]); + assert_eq!(decode(PixelLayout::R8, 1, 1, &[7]).unwrap(), [7, 7, 7, 255]); + assert_eq!(decode(PixelLayout::Rgb565, 1, 1, &0xf800u16.to_le_bytes()).unwrap(), [255, 0, 0, 255]); + assert_eq!(decode(PixelLayout::Rgba4444, 1, 1, &0xf00fu16.to_le_bytes()).unwrap(), [255, 0, 0, 255]); + assert_eq!(decode(PixelLayout::Argb4444, 1, 1, &0xff00u16.to_le_bytes()).unwrap(), [255, 0, 0, 255]); + assert_eq!(flip_rows(&[1, 1, 1, 1, 2, 2, 2, 2], 1), [2, 2, 2, 2, 1, 1, 1, 1]); + assert!(matches!(decode(PixelLayout::Rgba32, 2, 2, &[0; 15]), Err(DdsError::Truncated(_)))); + assert!(matches!(decode(PixelLayout::Rgba32, 0, 1, &[]), Err(DdsError::Malformed(_)))); + assert!(matches!(decode_with_limit(PixelLayout::R8, 2, 2, &[0; 4], 15), Err(DdsError::LimitExceeded { .. }))); + } + + #[test] + fn block_and_packed_layouts_crop_partial_blocks() { + assert_eq!(size(PixelLayout::Dxt1, 5, 5), Some(32)); + assert_eq!(decode(PixelLayout::Dxt1, 5, 5, &[0; 32]).unwrap().len(), 100); + // DXT1: colour 0 red, colour 1 blue; the first texel uses index 1, the rest index 0. + let px = decode(PixelLayout::Dxt1, 4, 4, &[0x00, 0xf8, 0x1f, 0x00, 0b01, 0, 0, 0]).unwrap(); + assert_eq!((&px[..4], &px[4..8]), (&[0, 0, 255, 255][..], &[255, 0, 0, 255][..])); + // DXT5: alpha 255 -> 0; first texel index 1 (0), rest index 0 (255). + let mut b = vec![255, 0, 1, 0, 0, 0, 0, 0]; + b.extend_from_slice(&[0x00, 0xf8, 0x1f, 0x00, 0, 0, 0, 0]); + let px = decode(PixelLayout::Dxt5, 4, 4, &b).unwrap(); + assert_eq!((px[3], px[7]), (0, 255)); + // BC4 & BC5. + assert_eq!(&decode(PixelLayout::Bc4, 4, 4, &[200, 10, 0, 0, 0, 0, 0, 0]).unwrap()[..4], &[200, 200, 200, 255]); + let mut n = vec![128, 128, 0, 0, 0, 0, 0, 0]; + n.extend_from_slice(&[128, 128, 0, 0, 0, 0, 0, 0]); + assert_eq!(&decode(PixelLayout::Bc5, 4, 4, &n).unwrap()[..4], &[128, 128, 254, 255]); + // Packed RGB9E5 & RGBA1010102. + let rgb9e5 = (256u32 | (128 << 9) | (64 << 18) | (15 << 27)).to_le_bytes(); + assert_eq!(decode(PixelLayout::Rgb9e5, 1, 1, &rgb9e5).unwrap(), [188, 137, 99, 255]); + let rgba10 = (1023u32 | (512 << 10) | (2 << 30)).to_le_bytes(); + assert_eq!(decode(PixelLayout::Rgba1010102, 1, 1, &rgba10).unwrap(), [255, 128, 0, 170]); + } +} diff --git a/crates/dds/tests/boundary.rs b/crates/dds/tests/boundary.rs index ecd3aac112..1dab9b33bf 100644 --- a/crates/dds/tests/boundary.rs +++ b/crates/dds/tests/boundary.rs @@ -44,3 +44,56 @@ fn default_budget_rejects_oversized_header_before_payload_or_allocation() { assert!(matches!(texture.decode(), Err(craft_dds::DdsError::LimitExceeded { limit: craft_dds::DEFAULT_MAX_DECODE_BYTES, .. }))); assert!(matches!(texture.decode_with_limit(u64::MAX), Err(craft_dds::DdsError::Malformed(_)))); } + +#[test] +fn mip_views_reuse_shared_decoders_and_keep_their_bounds() { + for (compression, layout, channels) in [ + (DdsCompression::None, Layout::Gray, 1), + (DdsCompression::Bc1, Layout::Rgba, 4), + (DdsCompression::Bc4, Layout::Gray, 1), + (DdsCompression::Bc5, Layout::Rgb, 3), + ] { + let bytes = encode(4, 4, layout, &vec![71; 16 * channels], DdsOptions { compression, mipmaps: true }).unwrap(); + let texture = Texture::parse(&bytes).unwrap(); + assert_eq!(texture.levels(), 3); + assert_eq!(texture.compression(), compression); + assert!(texture.mip_level(3).is_err()); + assert!(texture.mip_level(u32::MAX).is_err()); + for (index, side) in [(0, 4), (1, 2), (2, 1)] { + let level = texture.mip_level(index).unwrap(); + assert_eq!((level.width(), level.height()), (side, side)); + let len = side as usize * side as usize * channels; + assert!(matches!(level.decode_with_limit((len - 1) as u64), Err(craft_dds::DdsError::LimitExceeded { .. }))); + assert_eq!(level.decode_with_limit(len as u64).unwrap().len(), len); + } + let nested = texture.mip_level(1).unwrap().mip_level(1).unwrap(); + assert_eq!(nested.decode().unwrap(), texture.mip_level(2).unwrap().decode().unwrap()); + } +} + +#[test] +fn sixteen_bit_mip_views_preserve_samples_and_stored_offsets() { + for (layout, channels) in [(Layout::Gray, 1), (Layout::GrayA, 2), (Layout::Rgba, 4)] { + // Two channels are stored as R16G16 and read back as red and green with blue 0. + let read = + |samples: Vec| -> Vec { if channels == 2 { samples.chunks(2).flat_map(|p| [p[0], p[1], 0]).collect() } else { samples } }; + let pixels: Vec = (0..16).flat_map(|p| (0..channels).map(move |c| (p * 400 + c * 31 + 7) as u16)).collect(); + let bytes = craft_dds::encode16(4, 4, layout, &pixels, DdsOptions { compression: DdsCompression::None, mipmaps: true }).unwrap(); + let texture = Texture::parse(&bytes).unwrap(); + assert_eq!(texture.compression(), DdsCompression::None); + assert_eq!(texture.levels(), 3); + assert_eq!(texture.decode16().unwrap(), read(pixels)); + for (index, averages) in [(1, vec![1000, 1800, 4200, 5000]), (2, vec![3000])] { + let expected = read(averages.iter().flat_map(|p| (0..channels).map(move |c| (p + c * 31 + 7) as u16)).collect()); + let level = texture.mip_level(index).unwrap(); + let budget = (expected.len() * 2) as u64; + assert!(matches!(level.decode16_with_limit(budget - 1), Err(craft_dds::DdsError::LimitExceeded { .. }))); + assert_eq!(level.decode16_with_limit(budget).unwrap(), expected); + let rounded: Vec = expected.iter().map(|&v| ((u32::from(v) + 128) / 257) as u8).collect(); + assert_eq!(level.decode().unwrap(), rounded); + } + assert_eq!(texture.mip_level(1).unwrap().mip_level(1).unwrap().decode16().unwrap(), texture.mip_level(2).unwrap().decode16().unwrap()); + let truncated = Texture::parse(&bytes[..bytes.len() - 1]).unwrap(); + assert!(truncated.mip_level(2).unwrap().decode16().is_err()); + } +} diff --git a/crates/dds/tests/known_answers.rs b/crates/dds/tests/known_answers.rs new file mode 100644 index 0000000000..ce6e5e9f04 --- /dev/null +++ b/crates/dds/tests/known_answers.rs @@ -0,0 +1,114 @@ +//! Independent known-answer test vectors (raw bitstreams and hand-verified spec outputs) +//! for BC6H (unsigned/signed), BC7, ETC1, and EAC R/RG (unsigned/signed). + +use craft_dds::DdsError; +use craft_dds::pixels::{self, PixelLayout}; + +#[test] +fn bc7_mode6_known_bitstream() { + // Raw 128-bit BC7 Mode 6 block (7-bit RGBA endpoints + per-endpoint P-bits, 4-bit indices): + // e0 = (241, 121, 21, 255), e1 = (40, 200, 254, 128); texels 0..3 use indices 0, 15, 8, 4. + let block: [u8; 16] = [0x40, 0x3c, 0x85, 0x47, 0x56, 0xfc, 0xff, 0xc0, 0xf0, 0x48, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44]; + let texels = pixels::decode_bc7_block(&block); + assert_eq!(texels[0], [241, 121, 21, 255]); + assert_eq!(texels[1], [40, 200, 254, 128]); + assert_eq!(texels[2], [134, 163, 145, 188]); + assert_eq!(texels[3], [188, 142, 83, 221]); + + // Reserved mode (low byte 0x00) decodes to all zeroes. + assert_eq!(pixels::decode_bc7_block(&[0; 16]), [[0, 0, 0, 0]; 16]); + + // Cropped 3x2 image keeps the top-left 3x2 texels in row-major order. + let rgba = pixels::decode(PixelLayout::Bc7, 3, 2, &block).unwrap(); + assert_eq!(rgba.len(), 3 * 2 * 4); + assert_eq!(&rgba[..4], &[241, 121, 21, 255]); + assert_eq!(&rgba[4..8], &[40, 200, 254, 128]); + assert_eq!(&rgba[8..12], &[134, 163, 145, 188]); +} + +#[test] +fn bc6h_unsigned_and_signed_known_bitstreams() { + // Mode 11 (untransformed 10-bit endpoints, 1 subset, 4-bit indices): + // e0 = (480, 400, 200), e1 = (200, 480, 400); texels 0..2 use indices 0, 15, 8. + let block: [u8; 16] = [0x03, 0x3c, 0xc8, 0x90, 0x41, 0x06, 0x3c, 0xc8, 0xf0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; + + let unsigned = pixels::decode_bc6h_block(&block, false); + assert_eq!(unsigned[0], [228, 105, 7, 255]); + assert_eq!(unsigned[1], [7, 228, 105, 255]); + assert_eq!(unsigned[2], [51, 160, 39, 255]); + + let signed_pos = pixels::decode_bc6h_block(&block, true); + assert_eq!(signed_pos[0], [255, 255, 105, 255]); + assert_eq!(signed_pos[1], [105, 255, 255, 255]); + assert_eq!(signed_pos[2], [255, 255, 255, 255]); + + // Signed Mode 11 with negative 10-bit endpoints: R0 = -200 (0x338), G1 = -200 (0x338). + // Negative linear values clip to 0 in sRGB8 output. + let neg_block: [u8; 16] = [0x03, 0x67, 0x64, 0x20, 0x43, 0x06, 0x67, 0xc8, 0xf0, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; + let signed_neg = pixels::decode_bc6h_block(&neg_block, true); + assert_eq!(signed_neg[0], [0, 105, 255, 255]); + assert_eq!(signed_neg[1], [105, 0, 255, 255]); + assert_eq!(signed_neg[2], [0, 0, 255, 255]); + + let rgba = pixels::decode(PixelLayout::Bc6h, 2, 1, &block).unwrap(); + assert_eq!(rgba, [228, 105, 7, 255, 7, 228, 105, 255]); + let rgba_signed = pixels::decode(PixelLayout::Bc6hSigned, 2, 1, &neg_block).unwrap(); + assert_eq!(rgba_signed, [0, 105, 255, 255, 105, 0, 255, 255]); +} + +#[test] +fn etc1_known_bitstreams_and_invalid_differential() { + // Individual mode (diff=0, flip=0): left half (x<2) RGB4=(15,0,8) + 2, right half (x>=2) RGB4=(0,15,4) + 5. + let individual: [u8; 8] = [0xf0, 0x0f, 0x84, 0x04, 0x00, 0x00, 0x00, 0x00]; + let texels = pixels::decode_etc1_block(&individual).unwrap(); + for y in 0..4 { + for x in 0..4 { + let expected = if x < 2 { [255, 2, 138, 255] } else { [5, 255, 73, 255] }; + assert_eq!(texels[y * 4 + x], expected); + } + } + + // Differential mode (diff=1, flip=1 horizontal): base0=(20,10,4), delta=(+2,-2,+1), tables=(2,3). + let differential: [u8; 8] = [0xa2, 0x56, 0x21, 0x4f, 0x00, 0x00, 0x00, 0x00]; + let texels = pixels::decode_etc1_block(&differential).unwrap(); + for y in 0..4 { + for x in 0..4 { + let expected = if y < 2 { [174, 91, 42, 255] } else { [194, 79, 54, 255] }; + assert_eq!(texels[y * 4 + x], expected); + } + } + + // Differential base + delta outside 0..=31 must be rejected. + let invalid: [u8; 8] = [0x04, 0x40, 0x40, 0x02, 0x00, 0x00, 0x00, 0x00]; + assert!(pixels::decode_etc1_block(&invalid).is_none()); + assert!(matches!(pixels::decode(PixelLayout::Etc1, 4, 4, &invalid), Err(DdsError::Malformed(_)))); +} + +#[test] +fn eac_r_and_rg_unsigned_and_signed_known_bitstreams() { + // Unsigned EAC_R: base=100, mult=2, table=0, all selectors=7 (+14) -> 1028/2047 -> 128. + let eac_r_u: [u8; 8] = [0x64, 0x20, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; + assert_eq!(pixels::decode_eac_r_block(&eac_r_u, false), [[128, 128, 128, 255]; 16]); + + // Signed EAC_R: base=-64 (0xc0), mult=2, table=0, all selectors=0 (-3) -> (-560+1023)/2046 -> 58. + let eac_r_s: [u8; 8] = [0xc0, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; + assert_eq!(pixels::decode_eac_r_block(&eac_r_s, true), [[58, 58, 58, 255]; 16]); + + // Column-major selector order: texel (x=1, y=0) has selector 7 (+14 -> 142), others selector 0 (-3 -> 125). + let eac_col: [u8; 8] = [0x80, 0x10, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00]; + let col_texels = pixels::decode_eac_r_block(&eac_col, false); + assert_eq!(col_texels[1], [142, 142, 142, 255]); + assert_eq!(col_texels[0], [125, 125, 125, 255]); + assert_eq!(col_texels[4], [125, 125, 125, 255]); + + // Dual-channel EAC_RG (unsigned and signed): R then G. + let mut eac_rg_u = [0u8; 16]; + eac_rg_u[..8].copy_from_slice(&eac_r_u); + eac_rg_u[8..].copy_from_slice(&[0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); + assert_eq!(pixels::decode(PixelLayout::EacRg, 2, 2, &eac_rg_u).unwrap(), [128, 255, 0, 255].repeat(4)); + + let mut eac_rg_s = [0u8; 16]; + eac_rg_s[..8].copy_from_slice(&eac_r_s); + eac_rg_s[8..].copy_from_slice(&[0x7f, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); + assert_eq!(pixels::decode(PixelLayout::EacRgSigned, 2, 2, &eac_rg_s).unwrap(), [58, 255, 0, 255].repeat(4)); +} diff --git a/crates/designcraft-format/src/lib.rs b/crates/designcraft-format/src/lib.rs index eab431eab8..9ce481a3b1 100644 --- a/crates/designcraft-format/src/lib.rs +++ b/crates/designcraft-format/src/lib.rs @@ -127,9 +127,10 @@ pub fn starts_designcraft(prefix: &[u8]) -> bool { craft_document_bundle::starts_with_mimetype(prefix, BUNDLE) } -/// Read the first-page PNG preview from a ZIP package, with a fixed 4 MiB cap. +/// Read the first-page PNG preview from an unprefixed native ZIP package, with a fixed 4 MiB cap. +/// Nonmatching signatures are rejected after at most four bytes read. pub fn read_preview(reader: R) -> Option> { - craft_document_bundle::read_preview_from(reader, PREVIEW) + craft_document_bundle::read_unprefixed_preview_from(reader, PREVIEW) } fn is_linked(document: &Value, id: &str) -> bool { diff --git a/crates/designcraft-format/src/tests.rs b/crates/designcraft-format/src/tests.rs index 38cf1c34f5..736d2e17cd 100644 --- a/crates/designcraft-format/src/tests.rs +++ b/crates/designcraft-format/src/tests.rs @@ -167,3 +167,37 @@ fn bytes_supplied_for_a_linked_asset_are_not_written() -> Result<(), Box(&bytes)?.assets, [("8".to_owned(), b"kept".to_vec())]); Ok(()) } + +#[test] +fn preview_uses_the_strict_signature_path_without_losing_cursor_independence() -> Result<(), Box> { + struct Counted { + cursor: std::io::Cursor>, + bytes_read: usize, + } + impl std::io::Read for Counted { + fn read(&mut self, out: &mut [u8]) -> std::io::Result { + let n = self.cursor.read(out)?; + self.bytes_read += n; + Ok(n) + } + } + impl std::io::Seek for Counted { + fn seek(&mut self, position: std::io::SeekFrom) -> std::io::Result { + self.cursor.seek(position) + } + } + let mut non_zip = Counted { cursor: std::io::Cursor::new(vec![0x42; 1 << 20]), bytes_read: 0 }; + non_zip.cursor.set_position(100); + assert_eq!(read_preview(&mut non_zip), None); + assert!(non_zip.bytes_read <= 4); + + let bytes = save(&json!({}), "DesignCraft test", Some(b"png"), &[], &[])?; + let mut cursor = std::io::Cursor::new(&bytes); + cursor.set_position(bytes.len() as u64); + assert_eq!(read_preview(cursor), Some(b"png".to_vec())); + let mut prefixed = b"MZsynthetic-prefix".to_vec(); + prefixed.extend(bytes); + assert_eq!(craft_document_bundle::read_preview_from(std::io::Cursor::new(&prefixed), PREVIEW), Some(b"png".to_vec())); + assert_eq!(read_preview(std::io::Cursor::new(prefixed)), None); + Ok(()) +} diff --git a/crates/develop-model/Cargo.toml b/crates/develop-model/Cargo.toml new file mode 100644 index 0000000000..ec5848b864 --- /dev/null +++ b/crates/develop-model/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "craft-develop-model" +description = "Raw develop settings as data: the non-destructive edit model (white balance, tone, colour, detail, optics, geometry, masks), control specs with ranges and defaults, process versions, presets, and the mapping to and from Camera Raw's crs: settings." +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dependencies] +craft-geom = { workspace = true } +craft-metadata = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +miniz_oxide = "0.9" + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/develop-model/README.md b/crates/develop-model/README.md new file mode 100644 index 0000000000..c6b07f2046 --- /dev/null +++ b/crates/develop-model/README.md @@ -0,0 +1,51 @@ +# craft-develop-model + +Raw develop settings as data: the second of the three crates planned in craft-libs#587 +(`craft-raw`, `craft-develop-model`, `craft-develop`). No rendering, no GPU, no I/O. An app can +store, copy, merge, migrate and serialise a photo's edits without depending on the pipeline. + +```toml +craft-develop-model = { git = "https://github.com/storytold/craft-libs", rev = "" } +``` + +The base is LightCraft's `lightcraft-develop` and the `crs:` import from `lightcraft-engine` +(`crs.rs`, `crs_masks.rs`) at `a75497d6`. Issue numbers in comments are LightCraft's +(`storytold/lightcraft#N`). + +## What is here + +| Module | Contents | +|---|---| +| `settings` | `DevelopSettings`, the complete, serialisable description of a photo's look: white balance (Kelvin or relative scale), light, tone curve, HSL / mixer / point colour, B&W mix, colour grading, calibration, effects (vignette, grain), detail, optics (lens profile, embedded corrections, defringe), geometry and Upright, crop, masks (brush, linear, radial, range, AI segmentation), spots, red eye, lens blur, enhance, HDR. `ProcessVersion` / `Process`: which formulas an edit renders with, so later formula changes leave existing edits alone ([process versions](docs/process-versions.md)). | +| `controls` | `CONTROLS`: every numeric slider by id, with its label, section, range, default, step and track gradient. UIs and agents address sliders through it. | +| `presets` | `Preset`, `SettingsGroup` (the groups a copy, paste, sync or preset covers), `extract_groups`, `apply_partial` (a partial settings object merged at an amount), `deep_merge`. | +| `segmask` | `SegMask`: a compressed AI segmentation mask stored inside the settings. | +| `crs`, `crs_masks` | Camera Raw / Lightroom `crs:` properties (from `craft_metadata::parse_xmp`) mapped to a *partial* `DevelopSettings` JSON object, which is merged like a preset: global adjustments, tone curves, white balance (`Target` decides how a `crs:` temperature is read), crop, Upright, local corrections and retouch spots, with a report of what was read and what was left out. `preset_from_xmp` reads an XMP preset. The field table is in [xmp-interop](docs/xmp-interop.md). | + +Reading the `crs:` packet itself (names, structures, Camera Raw's number formats) is +[`craft-crs`](../crs/README.md)'s job; this crate decides what those settings mean for the edit +model. The mapping is a best-effort approximation of the other application's look, not a pixel match. + +## Different from LightCraft + +Fixes made here that LightCraft `a75497d6` does not have. It gets them when it switches to this crate. + +- `to_json_for_merge` states every setting the stored JSON leaves out at its default (HDR, the profile and its camera + local tone, the curve, Lens Blur's and Denoise's optional settings), not only Remove Hot Pixels. Copying a group + turns them off on the target, and a preset at an amount starts from them. +- `reset_section` resets the sliders of point colours and red eyes, Upright with its guides, and the lens profile and + Remove Chromatic Aberration switches. Settings from outside the app keep those sliders in range. +- `is_unedited` counts a chosen white balance as an edit. As shot, the numbers stay the photo's own. +- A preset at no amount adds no mask, and its Lens Blur focus mask follows its mask to the id the photo gives it. +- `crs:` import: a brush dab that is not a finite number is dropped, brush runs that differ in `crs:MaskInverted` stay + two components, `crs:RetouchInfo` is read only without `crs:RetouchAreas`, and an XMP preset keeps its masks. +- `Homography::from_quads` (`craft-geom`) solves a mapping that sends the origin to infinity. + +## Not here yet + +- Writing `crs:` back from `DevelopSettings`. LightCraft writes its own `lc:settings` in a sidecar; PhotoCraft's + Camera Raw writes `crs:`. That comes with the PhotoCraft switch, together with the migration of PhotoCraft's + current Camera Raw settings. +- The tests that render a mask or a spot to check its sense (inside or outside a radial, where a spot lands) + need the pipeline. They come back with `craft-develop`. The mapping half of each is kept here. +- LightCraft-only paths: `Target` from a catalog photo, and `.lrtemplate` (Lua) presets. diff --git a/crates/develop-model/docs/process-versions.md b/crates/develop-model/docs/process-versions.md new file mode 100644 index 0000000000..20443b9bce --- /dev/null +++ b/crates/develop-model/docs/process-versions.md @@ -0,0 +1,132 @@ +# Process versions + +> **Changes:** [revision history](#revision-history) + +Every photo's develop settings record the **rendering process** they are interpreted by +(`DevelopSettings::process`, a `ProcessVersion` in `crates/develop/src/settings.rs`). Lightroom calls +this the process version. It lets the look of LightCraft improve (a new default tone curve, issue #146; +stronger Light sliders, issue #196) without changing a single photo someone has already edited. + +| Process | What it changes | +|---|---| +| **V1** | LightCraft's rendering from before process versions existed. Frozen. | +| **V2** | Highlights and Shadows keep fine detail (issue #632): their base layer (`local::tone_base`) is the edge-aware filter of a separable median of log luminance (radius 0.15 % of the long edge, at most 24 px, `local::BASE_DETAIL`) instead of log luminance itself, so twigs, lines and texture take their surroundings' shift and keep their contrast; region edges stay sharp. CPU (`local::prepare`) and GPU prepare branch on `local::tone_base_detail` (0 = no median under V1); the base plane is cached per process (`local::BaseKey`). Renders without Highlights or Shadows (global or local) are the same as V1's. Tone map unchanged. | +| **V3** | Pixel-scale sharpening (issue #757): Radius uses the original photo’s pixels, Detail controls halo damping, and downsized sources retain a response-matched sharpening band. V1 and V2 keep texture-band sharpening exactly. The sharpen plane is keyed by radius and process and invalidated after noise-reduction changes. The Highlights/Shadows base and tone map are unchanged from V2. | +| **V4** | V3 plus calibrated global Contrast (per channel) and Blacks (per-channel/luminance mix) on raws with a camera tone curve. Whites behaves exactly as V2, both signs; its calibration is a planned follow-up. Other sources and local adjustments retain V2 operators. See [calibrated Light operators](https://github.com/storytold/lightcraft/blob/a75497d6/docs/light-v3.md). | +| **V5** | V4 plus: a raw's tone curve rolls off into white in stops instead of reaching it within about a stop of the sensor's white, so raising Exposure keeps the highlights the sensor recorded (issue #1632). **Camera curve** (the look fitted to the embedded JPEG, or a DNG's `ProfileToneCurve`; `CameraTone`): past its last knot `CameraTone::apply_rolloff` closes the gap to white by `gap / (1 + c·(2^t − 1))` at `t` stops past the knot, with `c` matching the curve's slope there and the gap halving every stop in the end (`tone::ROLLOFF_STOPS`); `ToneMap::camera_rolloff_hdr` builds the table from it. Under V1 to V4 the curve past its last knot (fitted only to camera JPEG tones below 0.85, so 1 to 3 stops below the sensor's white on most photos) closes on white exponentially in linear light: the sensor's white renders at 8-bit 246 to 254 at Exposure 0 and 254 to 255 from Exposure +1. On the 11 CC0 corpus files with a fitted look (8 Sony, 2 Nikon, 1 Fujifilm) it now renders at 239 to 250 at Exposure 0 and 247 to 253 at +1. A DNG curve that reaches white at the sensor's white renders as before. **Generic curve** (raws without a camera curve, `ToneMap::rolloff_hdr`): from half a stop below its white point (`tone::GENERIC_KNEE_STOPS`) the same shoulder takes over, with the extended Reinhard curve's value and slope at the join, where V1 to V4 clip at the white point (scene 1.18 at default settings, ¼ stop above the sensor's white). The sensor's white renders at about 235, 248 and 252 at Exposure 0, +1 and +2 (V1 to V4: 239, 255, 255; the CC0 Canon R8 file and 6 CC0 DNGs without a camera curve). Content past the old clip point, such as a clipped sky, now renders up to 15 codes below white at Exposure 0 and reaches 255 about 5 stops past the sensor's white. Below the last knot or the join, JPEGs and other display-referred sources, and the camera fit itself are unchanged; CPU and GPU share the tone LUT. | +| **V6** | V5 plus: colour noise reduction filters the linear colour differences R−Y and B−Y with the joint method (issue #815, `docs/nr-color-joint.md`): edge-aware weights from a denoised guide shared by both differences, anisotropic diffusion over scales, a brake for isolated colour specks; Y is kept exactly. V1 to V5 keep the blurred chromaticity (`local::denoise`). CPU (`local::denoise`) and GPU (`render::denoise`) branch on `local::joint_colour_nr`. Renders with Color 0 are the same as V2's. | +| **V7** | V6 plus: film grain from crystal layers ([grain.md](https://github.com/storytold/lightcraft/blob/a75497d6/docs/grain.md), `crates/pipeline/src/crystal.rs`), after Aurélien Pierre's model: resolution independent (a preview is the export's grain box-downscaled), fading out at black and white, calibrated to Lightroom Classic's strength and width at mid grey. V1 to V6 keep their two-octave value noise exactly (`finish::ValueGrain`; on the GPU `F_GRAIN_MODEL` 0); `finish::GrainModel::new` picks the model per process. Renders without grain don't depend on this. | +| **V8** (the latest) | V7 plus: Highlights and Shadows take their edge-aware filter of log luminance without its fine detail (`local::detail_free`: each value clamped between its separable median at the V2 radius and the median of a 3× wider neighbourhood) instead of V2's plain median (`local::base_keeps_gaps`), so the gaps in dark lettering on a bright panel are not lifted into bright blocks (issue #3460). CPU (`local::tone_base`) and GPU (`render::tone_base`) branch on it; the base plane is keyed by process. Renders without Highlights or Shadows (global or local) are the same as V7's. | + +## The rule + +**A change that alters how existing settings render ships as a new process. V1 is frozen.** + +- A deliberate change of look (tone model, slider response, default curve, how a stage interprets a + value) goes into a new process. Photos stay on the process they have until someone runs Update to + Current Process (or Reset). +- Bug fixes that make output correct where it was broken (a crash, NaN or black output, a decoder + error, CPU and GPU disagreeing) are not looks and can land in place, as before. +- Changes to how a file is decoded or which camera colour it gets change the source, not how settings + are interpreted; the process doesn't reach that stage today. + +**Neutral display tone map** (JPEG, PNG and other display-referred sources with Contrast, Whites and +Blacks at 0) is the identity up to white: the SDR shoulder no longer pulls 1.0 down to 0.9875 (8-bit +255 to 254, issue #1945). It landed in place as a bug fix, since the tone map promised identity at +neutral settings; edited curves keep the shoulder and their V1 look. + +**AI mask edges** (Object and Describe selections stored as a model's grid) follow the photo's edges +when they are enlarged to the output (issue #3272): the plain enlargement spilled 1–2 px onto the +background, which a raised Exposure turned into a bright outline around the subject in previews and +exports. Existing AI masks become 1–2 px tighter at a subject's edges, on every process; their +inside, their outside and edges without contrast in the photo are unchanged. It landed in place as +a bug fix: the outline is output that isn't in the photo nor in the edit, not a look anyone chose. + +**A mask whose first component is Intersect** starts from that component, on the CPU and the GPU +(issue #1889). It used to start from nothing, so alone it selected nothing, with a later Add it gave +only what was added, and with the mask's Invert it gave the whole photo. Such masks now include the +first component, on every process. It landed in place as a bug fix: intersecting with nothing is not +a look anyone chose. + +**Lettering under Highlights and Shadows** (V8) no longer grows blocks of the wrong tone. V2's base +is the edge-aware filter of a separable median alone. Dark lettering on a bright panel (a bus +number, a sign) has strokes and gaps about as wide as the median's radius, so each row and column +window was close to half letter and half panel, and the median picked one tone or the other in +rectangles: it filled the gaps between the letters and their counters with the letters' tone. +Shadows then lifted those patches of panel by over 1 EV, into bright blocks beside the letters. From V8 each +value moves toward that median only as far as the median of a 3× wider neighbourhood agrees +(`local::detail_free`). A twig against a wall has both medians on the wall's side and takes its tone, +as before (#632); the gap between two strokes sits in a wider neighbourhood that is mostly panel, the +two medians disagree, and the gap keeps its own tone. The letters themselves count as fine dark detail +on the panel and keep their contrast, as twigs do. V8 differs from V7 under Highlights or Shadows only in detail +about as fine as the radius, mostly where it is packed closely (lettering, grilles). V2 to V7 +are released and keep the blocks (`tests_legacy` locks V1 and V2); Update to Current Process moves a photo past them. + +## Adding a process + +1. Add a variant to `Process` (`V2`), append it to `Process::ALL` (oldest first), give it its number in + `Process::version` and make it `Process::LATEST`. +2. The compiler then points at every `match` on `Process`. The base tone map is chosen in one place, + `lightcraft_pipeline::finish::base_tone` (both the CPU and the GPU renderer take it from there): give + V2 its own arm and leave V1's alone. A process that changes another stage branches there the same + way, on `s.process.process()`; if that stage is cached (`Plan::lin_key`, the planes in `local.rs`), + add the process to its key. +3. Keep `settings_saved_before_process_versions_load_as_v1` (`crates/develop/src/tests_process.rs`) + passing: settings saved without the field must stay V1, not become the new latest. That is the + field's `#[serde(default = "ProcessVersion::legacy")]`; the struct-level `Default` gives new + settings the latest instead. +4. Render a few procedural scenes on both processes and compare, and update `docs/parity-checklist.md`. + +## What gets which process + +| | Process | +|---|---| +| A newly imported, merged or Lightroom-imported photo | the latest | +| Settings saved without the field (catalogs and their snapshots, versions and history, `lc:settings` in XMP sidecars, browser libraries: everything from before process versions), or with a damaged value | V1 | +| Reset (`develop.reset`) | the latest | +| Reset of a section or a slider | unchanged | +| Update to Current Process (`develop.updateProcess`) | the latest, for photos on an older one; one undo step. The menu item follows the selection; a call with `ids` / `id` acts on those photos whatever is selected | +| Virtual copy, duplicate | the source's | +| Copy / paste / sync / Auto Sync / presets | unchanged (see below) | +| `crs:` edits (XMP from other raw developers, Lightroom catalogs, imported presets) | unchanged; a new photo has the latest | +| Restoring a version or history step | the one it had | + +**Copy, paste, sync and presets** never carry the process: it is in no settings group, so the photos +they change keep their own and interpret the pasted values with it. (Lightroom offers the process +version as a separate checkbox when syncing; LightCraft's groups are coarser and keep it out.) Partial +settings JSON that names `process` itself (`develop.merge`, a hand-written preset) sets it. + +**Lightroom edits** are re-interpreted by LightCraft's mapper (`crates/engine/src/crs.rs`), which is +written against the current rendering. `crs:ProcessVersion` numbers Adobe's renderer, not ours, and is +ignored; a photo imported with Lightroom edits is on LightCraft's latest process, and an existing +LightCraft photo that receives them keeps its own. + +**A number above this build's latest** (a library or sidecar written by a newer LightCraft) loads, is +kept when saved, and renders with the newest process this build has. Update to Current Process never +moves a photo to an older process; Reset does move it to this build's latest. + +**A damaged value** (anything but a whole number from 0 to 2^32 - 1; `2.0` counts as 2) reads as V1, +like a missing field, and costs nothing else: the rest of the settings load from the catalog, a +snapshot, the op log or an `lc:settings` sidecar as usual. The damaged value isn't kept: the photo is +saved as V1. + +## Storage + +The process is saved as `"process": ` in the settings JSON, except V1, which is left out. +Settings made before process versions existed are therefore saved exactly as before, and their +preview-cache keys (`DevelopSettings::hash64`) don't change. `develop.get` (MCP `get_develop`) always +states the process. + +From process V3 on, persistent thumbnail, view-render and variant keys also include the +resolved rendering process. A future number can keep the same settings hash across builds +while resolving to different operators, so those keys must differ. V1 and V2 omit this +extra field and retain their existing cached renders. No cache-version bump is needed. + +## Revision history + +| Date | Change | Summary | +|---|---|---| +| 2026-11-08 | minor | V8: the Highlights/Shadows base keeps the gaps in dark lettering (#3460) | +| 2026-11-07 | minor | V7: film grain from crystal layers ([grain.md](https://github.com/storytold/lightcraft/blob/a75497d6/docs/grain.md)); V1 to V6 keep their value noise | +| 2026-11-05 | minor | V5: a raw's tone curve rolls off into white in stops, so raising Exposure keeps recorded highlights (#1632) | +| 2026-10-12 | minor | Calibrated Contrast and Blacks, V2 Whites, resolved-process cache keys and number coordination | diff --git a/crates/develop-model/docs/xmp-interop.md b/crates/develop-model/docs/xmp-interop.md new file mode 100644 index 0000000000..78cb1f467c --- /dev/null +++ b/crates/develop-model/docs/xmp-interop.md @@ -0,0 +1,342 @@ +# XMP sidecars and interchange + +LightCraft keeps its catalog as the source of truth, and can also store each photo's metadata and develop settings in +an XMP sidecar next to the original. Sidecars let edits travel with the files (backups, another LightCraft library, +other tools), and let LightCraft pick up edits made elsewhere. + +## Sidecar files + +| | | +|---|---| +| Name | `.xmp` by default (`IMG_0001.CR3` → `IMG_0001.xmp`); `.xmp` (`IMG_0001.CR3.xmp`) with `naming: "full"`. Reading accepts either (preferred first) and `.XMP`. With stem naming, files sharing a stem don't share a sidecar: see *Shared names* below. | +| Write | `photo.saveMetadataToFile {ids?}` (Photo ▸ Save Metadata to File, ⌘S), or automatically after every change with `library.xmpPreferences {autoWrite: true}` (File ▸ Automatically Write Changes into XMP). Slider drags are written once, when the drag ends; undo/redo rewrite the sidecar. An existing sidecar is **merged into, never replaced** (see *Saving into an existing sidecar*). Written atomically (temp file, fsync, rename). The result lists `written`, `merged` and `backups`. | +| Read | On import (`library.import`, the report counts `sidecars`), and `photo.readMetadataFromFile {ids?}` (one undo step). Manual rereading uses embedded XMP in raw/DNG and JPEG files when no sidecar exists. A discovered sidecar that cannot be read or parsed is reported in `failed`, without applying embedded values. `photo.readMetadataFromFile` never writes the sidecar, even with auto-write on: writing back what it has just read would add `lc:settings`, which later reads prefer over another app's `crs:` edits (issue #934). A file arriving in an import is ranked as if it were catalogued already: it reads its own `.xmp` when another file of its stem (a raw first) owns the shared stem sidecar (issue #1905). | +| Preferences | `library.xmpPreferences {autoWrite?, naming?: stem\|full}`, stored in the library's `prefs.json`. | + +What we write (standard namespaces, so other tools can read the metadata): + +| Field | XMP property | +|---|---| +| Rating 0–5 | `xmp:Rating` | +| Colour label | `xmp:Label` (`Red`, `Yellow`, `Green`, `Blue`, `Purple`) | +| Title / caption / copyright / creator | `dc:title` / `dc:description` / `dc:rights` / `dc:creator` | +| Copyright status | `xmpRights:Marked` (`True` copyrighted, `False` public domain, absent = unknown) | +| Rights usage terms / copyright info URL | `xmpRights:UsageTerms` / `xmpRights:WebStatement` | +| Keywords | `dc:subject` (names: `Places`, `Italy`, `Rome`) and, when any keyword has levels, `lr:hierarchicalSubject` (paths: `Places\|Italy\|Rome`, with the flat keywords too) | +| Capture time, GPS | `exif:DateTimeOriginal`, `photoshop:DateCreated`, `exif:GPSLatitude`/`GPSLongitude` | +| Pick / reject flag | `lc:flag` (`pick`, `reject`, `none`) | +| Location | `lc:location` | +| Develop settings | `lc:settings` — our complete `DevelopSettings` as JSON (exact round trip, incl. masks, spots, crop and the rendering process; settings without `process` are V1, see [process-versions.md](process-versions.md)) | + +`lc:` is `http://ns.lightcraft.app/lc/1.0/`. + +Reading merges into the catalog with the **sidecar winning** for every field it states; fields it doesn't state are +kept. The one exception is the **capture time**: a valid time embedded in the file (EXIF/IPTC) wins. Invalid EXIF +clock values are skipped, allowing DateTimeDigitized, IFD0 DateTime or embedded XMP to supply the time. If the file +has no valid capture time, the sidecar's `exif:DateTimeOriginal`, else `photoshop:DateCreated`, else `xmp:CreateDate` +(ISO 8601) is used — on import (it then also files a copied photo in its date folder) and by +`photo.readMetadataFromFile` (undoable). An incomplete or malformed XMP timestamp is not projected as a capture time, +remains available in the raw property map, and does not prevent the next valid capture-time property from being used. +`xmp:Rating="-1"` (the XMP convention for rejected) sets the reject flag. Develop settings come from +`lc:settings` when present (exact); otherwise from the `crs:` fields below (approximate). + +When foreign `crs:` settings are only partly transferable, `library.import` includes per-photo `warnings` in its +report. Warnings identify unsupported profile names, saved Upright geometry that will be recalculated, and other +unmapped adjustments; they do not change the source packet or become catalog edits. + +Manual rereading and undo leave the JPEG original unchanged. Manual rereading also leaves sidecar files unchanged, +even with automatic XMP writing on. With automatic XMP writing off (the default), undo/redo leave sidecars unchanged; +with it enabled, subsequent edits and undo/redo can trigger the existing sidecar writer. `photo.allMetadata` uses +its already loaded file bytes for embedded XMP, with an existing sidecar taking precedence. + +Manual JPEG rereading uses the first standard XMP APP1 before the first SOS (scan) or EOI (end) marker. +Scanning is limited to a 16 MiB header span, including SOI, segment headers and skipped payloads, and 4096 +marker/fill steps after SOI. Non-APP1 payloads are skipped without loading them. Each APP1 payload must be +complete and is at most 65,533 bytes. Header extraction succeeds for the first complete +standard XMP packet within both limits, including a packet ending exactly at either limit; otherwise it +fails without a whole-file fallback, including on I/O errors or an incomplete APP1 whose available bytes +form parseable XMP. A valid JPEG with +XMP beyond those limits therefore cannot be manually reread through this path. Extended XMP assembly and +packets between scans are not supported by manual JPEG rereading. + +Packet completeness is stricter than the shared best-effort JPEG extractor; it is not strict whole-JPEG +validation. A complete early XMP packet can be returned without examining the remaining file. Import and +`photo.allMetadata` retain their existing extraction paths. Command regressions in +`crates/engine/src/tests/xmp.rs` cover metadata/undo and sidecar precedence, an over-nested XMP packet and a +truncated APP1 without an XMP signature. Separate reader cases in `sidecar::jpeg_header_tests` use small +counted readers and injected limits for span/step boundaries, complete versus truncated APP1, early stopping, +short reads and I/O errors. + +## Saving into an existing sidecar + +A sidecar may already hold another application's data — e.g. its `crs:` develop settings and `xmpMM:History` — often +the only copy of those edits outside that application's catalog. Saving never drops it: + +- LightCraft **owns** the properties in the table above: `xmp:Rating`, `xmp:Label`, `dc:title`, `dc:description`, + `dc:rights`, `dc:creator`, `dc:subject`, `lr:hierarchicalSubject`, `Iptc4xmpCore:Location`, `Iptc4xmpCore:AltTextAccessibility`, + `Iptc4xmpCore:ExtDescrAccessibility`, `photoshop:City`/`State`/`Country`, `xmpRights:Marked`/`UsageTerms`/ + `WebStatement` and everything in `lc:`. They are replaced on every save, and removed when LightCraft has no value + (clearing a title in LightCraft clears it in the file). All of them are read back on import, so the library starts + from what the sidecar said. +- The **capture time** (`exif:DateTimeOriginal`, `photoshop:DateCreated`) and **GPS** (`exif:GPSLatitude`, + `exif:GPSLongitude`) are replaced only when LightCraft has a value; otherwise the file's stay. +- **Everything else is kept byte for byte**: other namespaces (`crs:`, `xmpMM:`, the rest of `lr:`, unknown + ones), `xmp:CreatorTool`, comments, the packet wrapper and padding. LightCraft's properties go into one + `rdf:Description` of their own; owned properties written by another application (in attribute or element form) are + removed from its description, which otherwise stays as it was. +- Ownership follows the namespace URI, not the prefix spelling. A foreign namespace bound to `lc:` or `xmp:` + stays foreign: its properties are preserved and do not become LightCraft settings or ratings. Genuine namespace + aliases remain interchangeable, including namespace declarations containing XML character references. +- A sidecar that can't be read as XMP (not well-formed, not UTF-8, no `rdf:RDF`) is first copied to + `.xmp.bak-