skeleton: merge up/next into area/ribbon-fw with conflict markers left in (land step 1)

This commit is contained in:
Martin Ball committed 2026-11-19 20:42:08 +00:00
commit 6dedbe4fe6
948 files changed
+104005 -4138

No files matched your search

+46 -7
View File
@@ -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,10 +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 }
[[profile.default.overrides]]
filter = 'package(craft-ui) & (binary(visual_regression) | test(/^combo::tests::|selected_tab_erases_a_baseline_centered_on_its_bottom_edge/))'
test-group = 'gpu'
# 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-video) & binary(h264_decode)'
@@ -42,3 +40,44 @@ 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'
# Slowest tests first (priority runs higher values earlier), so the longest ones start at the
# beginning of the run instead of holding the tail. Every test that took over 30 s in the gate's
# run on next (run 44318177438, Linux); the first tier is the one over 60 s.
[[profile.ci.overrides]]
filter = '(package(craft-media) & test(=mxf::tests::hostile::hostile_input_never_panics)) | (package(craft-table) & test(/CellGrid.*::drives$/)) | (package(craft-proto) & test(=imap::gmail_labels_tests::a_mailbox_claimed_huge_cant_fill_memory_with_flags))'
priority = 20
[[profile.ci.overrides]]
filter = '''
(package(craft-design-lint) & test(=mapped_crates_have_no_findings))
| (package(craft-ui-widgets) & test(/SwatchTable.*::activation$/))
| (package(craft-table) & test(/CellGrid.*::activation$/))
| (package(craft-pkcs7) & test(=crypto::new_digital_ids_are_self_signed_and_survive_a_p12_round_trip))
| (package(craft-image-codecs) & test(=tiff_bands::a_blank_fax_page_wider_than_4096_px_passes_the_bomb_guard))
| (package(craft-image-codecs) & test(=codecs::png::streamed_tests::streamed_rows_match_the_whole_image_encoder))
| (package(craft-image-codecs) & test(=codecs::png::parallel_tests::the_parallel_encoder_gives_the_same_bytes_on_any_thread_count))
| (package(craft-image-codecs) & test(=limits::highly_compressible_jpeg_and_gif_still_decode))
| (package(craft-color-ui) & test(=integration::sampling::an_invalid_capture_ends_the_pick_with_a_status))
| (package(craft-docx) & test(=read::chart::tests::hostile_radar_charts_never_panic))
| (package(craft-drawingml) & test(=chart::chartex_tests::large_box_hierarchy_and_stacked_charts_take_linear_work))
| (package(craft-audio) & test(=integration::flac_decode::hostile_input_is_an_error_not_a_panic))
| (package(craft-docx) & test(=comment_save_cost::many_comments_save_without_quadratic_id_searches))
| (package(craft-ui-widgets) & test(/menu::popup::Menu.*::activation$/))
| (package(craft-scraft-format) & test(=b64::tests::size_cap_counts_the_partial_quartet))
'''
priority = 10
+13
View File
@@ -17,3 +17,16 @@ crates/fonts-build/tests/fixture/craft-fonts/fonts/**/OFL.txt -text
# DXF drawing goldens assert exact writer bytes, including LF on every platform.
crates/dxf/tests/data/drawing_*.dxf -text
# The generated components index is compared byte for byte with the generator's LF output (craft-ui test
# components_index): a Windows checkout must not convert its line endings.
docs/components-index.md text eol=lf
# The built-in size files are compared byte for byte with the writer's LF output (craft-theme sizes_tests), and the
# token dumps with the dump writer's (craft-theme dumps test): a Windows checkout must not convert their line endings.
crates/theme/sizes/*.json text eol=lf
crates/theme/tests/dumps/** text eol=lf
# The facade's [features] table is compared with the table the craft features test writes, with LF (craft test
# integration::features): a Windows checkout must not convert its line endings.
crates/craft/Cargo.toml text eol=lf
+5 -24
View File
@@ -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,10 @@ 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':
'.github/workflows/docs.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/ecproj-format.yml':
- 'actions/checkout@v7.0.1'
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
@@ -52,22 +50,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 +67,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 +80,15 @@ 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':
'.github/workflows/theme.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 +101,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,7 +110,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'
- 'step-security/taiki-e-install-action@v2.87.32'
'.github/workflows/window.yml':
- 'actions/checkout@v7.0.1'
+7 -19
View File
@@ -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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-ai-core --no-deps --locked
env:
RUSTDOCFLAGS: -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
+7 -21
View File
@@ -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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,20 +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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-atomic -p craft-settings --no-deps --locked
env:
RUSTDOCFLAGS: -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:
+12 -40
View File
@@ -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
+7 -3
View File
@@ -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
+5 -46
View File
@@ -12,46 +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: ${{ github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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
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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-crw --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-crw --locked
wasm:
timeout-minutes: 10
name: CRW WASM
@@ -68,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:
@@ -89,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:
+5 -10
View File
@@ -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,10 +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
- name: Docs (links and missing docs)
run: cargo doc -p craft-dialogs --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-dialogs --locked
+7 -21
View File
@@ -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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,16 +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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-doc --all-features --no-deps --locked
env:
RUSTDOCFLAGS: -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
-46
View File
@@ -1,46 +0,0 @@
name: Document paths
on:
workflow_dispatch:
push:
branches: [main, next]
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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-docpath --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-docpath --locked -- --nocapture
+19 -3
View File
@@ -20,14 +20,30 @@ jobs:
name: Workspace docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v7.0.1
with:
# The token isn't needed after checkout; cargo runs build scripts and proc macros from
# dependencies, so don't leave it in .git/config for that third-party code to read.
persist-credentials: false
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
with:
toolchain: stable
components: clippy
# Pull requests restore the cache but never save one (a 1.1 GB entry per open PR otherwise).
- uses: step-security/rust-cache@v2.9.2
with:
save-if: ${{ github.ref == 'refs/heads/main' }}
# A lib build selects no dev-dependency, so craft-ui's `testing` stays off as in an app's
# build: warnings that show only without it fail here.
- name: Facade as an app builds it
run: cargo clippy -p craft --lib --locked --features ui,layers,color,draw,imaging -- -D warnings
- name: Docs (links and missing docs)
run: cargo doc --workspace --no-deps --locked --keep-going
env:
RUSTDOCFLAGS: -D warnings
# The crates whose features add public items document with every feature too; the
# per-crate workflows document the media, audio and video crates.
- name: Docs with all features
run: cargo doc -p craft-ui -p craft-ui-contract -p craft-ui-widgets -p craft-ui-panels -p craft-ui-canvas -p craft-ui-patterns -p craft-ui-shell -p craft-doc -p craft-pdf --all-features --no-deps --locked --keep-going
env:
RUSTDOCFLAGS: -D warnings
+7 -19
View File
@@ -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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,14 +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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-ecproj-format --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-ecproj-format --locked -- --nocapture
- run: cargo check -p craft-ecproj-format --locked --target wasm32-unknown-unknown
+7 -22
View File
@@ -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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,18 +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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-font-store --no-deps --locked
env:
RUSTDOCFLAGS: -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
+10 -22
View File
@@ -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 }}
+64
View File
@@ -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
-46
View File
@@ -1,46 +0,0 @@
name: Translations
on:
workflow_dispatch:
push:
branches: [main, next]
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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-i18n --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-i18n --features collate --locked -- --nocapture
+8 -19
View File
@@ -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,17 +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
- name: Docs (links and missing docs)
run: cargo doc -p craft-jpeg --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-jpeg --locked
- run: cargo check -p craft-jpeg --locked --target wasm32-unknown-unknown
+9 -57
View File
@@ -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,59 +44,14 @@ 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
- run: cargo clippy -p craft-keymap --all-targets --features egui --locked -- -D warnings
- name: Docs (links and missing docs)
run: cargo doc -p craft-keymap --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
# Doctests run once, in the `ui` step.
# Doctests run once, in the `egui` step.
- name: Test core
run: cargo test -p craft-keymap --no-default-features --lib --tests --locked
- name: Test egui dispatch
run: cargo test -p craft-keymap --features egui --locked
native:
# Pushes to next run Linux only.
if: github.ref != 'refs/heads/next' || github.event_name == 'workflow_dispatch'
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 egui --locked
wasm:
timeout-minutes: 10
name: Keymap WASM
@@ -112,8 +68,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 egui --locked --target wasm32-unknown-unknown
+12 -19
View File
@@ -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
- name: Docs (links and missing docs)
run: cargo doc -p craft-media -p craft-media-testkit --all-features --no-deps --locked
@@ -69,10 +66,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.
+3 -8
View File
@@ -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
+34
View File
@@ -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
+8 -19
View File
@@ -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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,14 +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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-pcraft-format --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-pcraft-format --locked -- --nocapture
- run: cargo check -p craft-pcraft-format --locked --target wasm32-unknown-unknown
+14 -61
View File
@@ -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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,14 +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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-pdf --all-features --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-pdf --all-features --locked
- run: cargo test -p craft-pdf --all-features --locked --lib -- integration::unicode_writing integration::upstream_compatibility
- run: cargo test --manifest-path crates/pdf/read-only-contract/Cargo.toml --locked
backends:
name: PDF vendored contracts
@@ -73,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
@@ -94,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"
@@ -122,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
-51
View File
@@ -1,51 +0,0 @@
name: Print spooler
on:
workflow_dispatch:
push:
branches: [main, next]
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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-print --no-deps --locked
env:
RUSTDOCFLAGS: -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' }}
+9 -17
View File
@@ -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
+3 -2
View File
@@ -17,13 +17,14 @@ jobs:
name: Theme lint, tests, WASM
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/checkout@v7.0.1
with:
# The token isn't needed after checkout; cargo runs build scripts and proc macros from
# dependencies, so don't leave it in .git/config for that third-party code to read.
persist-credentials: false
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
with:
toolchain: stable
components: rustfmt, clippy
targets: wasm32-unknown-unknown
- run: cargo fmt -p craft-theme --check
-84
View File
@@ -1,84 +0,0 @@
name: Shared UI
on:
push:
branches: [main, next]
paths: ["crates/craft/**", "crates/ui/**", "crates/ui-contract/**", "crates/ui-widgets/**", "crates/ui-panels/**", "crates/ui-canvas/**", "crates/ui-patterns/**", "crates/ui-shell/**", "crates/ui-macros/**", "crates/i18n/**", "Cargo.toml", "Cargo.lock", ".github/workflows/ui.yml"]
pull_request:
paths: ["crates/craft/**", "crates/ui/**", "crates/ui-contract/**", "crates/ui-widgets/**", "crates/ui-panels/**", "crates/ui-canvas/**", "crates/ui-patterns/**", "crates/ui-shell/**", "crates/ui-macros/**", "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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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 -p craft-ui-contract -p craft-ui-widgets -p craft-ui-panels -p craft-ui-canvas -p craft-ui-patterns -p craft-ui-shell --all-targets --locked -- -D warnings
- run: cargo clippy -p craft-ui -p craft-ui-contract -p craft-ui-widgets -p craft-ui-panels -p craft-ui-canvas -p craft-ui-patterns -p craft-ui-shell --all-targets --all-features --locked -- -D warnings
# A lib build selects no dev-dependency, so craft-ui's `testing` stays off as in an app's build: warnings that
# show only without it fail here.
- name: Facade as an app builds it
if: runner.os == 'Linux'
run: cargo clippy -p craft --lib --locked --features ui,layers,color,draw,imaging -- -D warnings
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-ui -p craft-ui-contract -p craft-ui-widgets -p craft-ui-panels -p craft-ui-canvas -p craft-ui-patterns -p craft-ui-shell --all-features --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-ui -p craft-ui-contract -p craft-ui-widgets -p craft-ui-panels -p craft-ui-canvas -p craft-ui-patterns -p craft-ui-shell --locked
- run: cargo test -p craft-ui -p craft-ui-contract -p craft-ui-widgets -p craft-ui-panels -p craft-ui-canvas -p craft-ui-patterns -p craft-ui-shell --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
+9 -20
View File
@@ -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 ui feature alone.
jobs:
update:
name: Update with ui 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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,15 +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 ui 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 ui -- -D warnings
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-update --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-update --locked -- --nocapture
- run: cargo test -p craft-update --locked --features ui -- --nocapture
+8 -19
View File
@@ -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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,14 +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
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-vectorcraft-format --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-vectorcraft-format --locked -- --nocapture
- run: cargo check -p craft-vectorcraft-format --locked --target wasm32-unknown-unknown
+12 -18
View File
@@ -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: ${{ github.ref == 'refs/heads/next' && fromJSON('["ubuntu-latest"]') || fromJSON('["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
+6 -12
View File
@@ -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,12 +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
- name: Docs (links and missing docs)
run: cargo doc -p craft-web-api --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-web-api --locked
- run: cargo test -p craft-web-api --locked --target wasm32-unknown-unknown
+7 -18
View File
@@ -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,20 +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
- name: Docs (links and missing docs)
run: cargo doc -p craft-web-host --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-web-host --locked
- name: Read the wasm-bindgen version from Cargo.lock
id: wasm-bindgen
shell: bash
+9 -20
View File
@@ -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 run in the Workspace gate on every
# OS; this runs the ui feature alone.
jobs:
window:
name: Window with ui 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"]') || github.ref == 'refs/heads/next' && fromJSON('["ubuntu-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,15 +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 ui 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 ui -- -D warnings
- name: Docs (links and missing docs)
if: runner.os == 'Linux'
run: cargo doc -p craft-window --no-deps --locked
env:
RUSTDOCFLAGS: -D warnings
- run: cargo test -p craft-window --locked -- --nocapture
- run: cargo test -p craft-window --locked --features ui -- --nocapture
+80 -38
View File
@@ -6,70 +6,111 @@ 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.
schedule:
- cron: "17 */6 * * *"
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:
native:
name: Workspace
uses: storytold/craft-common-actions/.github/workflows/gate.yml@main
with:
# Pushes to next run Linux only.
os: ${{ github.ref == 'refs/heads/next' && '["ubuntu-latest"]' || '["ubuntu-latest","windows-latest","macos-latest"]' }}
# On a pull request, macOS and Windows 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; pushes to main always run every leg. A push to next has
# only the Linux leg, so it selects none.
select-os: ${{ github.ref == 'refs/heads/next' && '[]' || '["windows-latest","macos-latest"]' }}
timeout-minutes: 120
# The Windows leg runs its tests with nextest (.config/nextest.toml).
# Every leg runs its tests with nextest, and its doctests with cargo test (.config/nextest.toml).
tools: cargo-nextest@0.9.150
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: |
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"
# 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
# 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.
# rust-lld links the test binaries faster than link.exe; /OPT:NOICF skips identical-code
# folding and /DEBUG:NONE writes no PDB, both of which only cost link time here.
windows-env: |
CRAFT_PRINT_REQUIRE_QUEUE=1
CARGO_PROFILE_DEV_DEBUG=0
CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER=rust-lld
CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTFLAGS=-Clink-arg=/OPT:NOICF -Clink-arg=/DEBUG:NONE
# 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: |
CRAFT_PRINT_REQUIRE_QUEUE=0
CRAFT_HOSTILE_DIVISOR=8
PROPTEST_CASES=64
setup-command: 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: .test-inputs/photocraft/corpus
cache-key: photocraft-corpus
cache-key-files: scripts/ci-test-inputs.sh
# A `lint (<os>)` leg beside each `gate (<os>)` leg, so clippy is off the tests' critical path.
lint-command: |
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
# A push to main builds the gate's test binaries in their own leg and saves the cache right
# after, so a failing test no longer keeps the cache from being saved. The same two builds
# the gate's nextest runs do (all features; the two crates with default features).
warm-command: |
cargo test --workspace --no-run --all-features --locked
cargo test -p craft-image-codecs -p craft-text --no-run --locked
command: |
python3 scripts/test-workspace-concurrency.py
cargo fmt --check
cargo clippy --workspace --all-targets --locked --keep-going -- -D warnings
cargo clippy --workspace --all-targets --all-features --locked --keep-going -- -D warnings
cargo test --workspace --locked
cargo test --workspace --all-features --locked
# The 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
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 --keep-going -- -D warnings
cargo clippy --workspace --all-targets --all-features --locked --keep-going -- -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
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 +125,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: ""
+53
View File
@@ -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 |
@@ -123,6 +124,16 @@ The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper
`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.
<<<<<<< HEAD
=======
## 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.
>>>>>>> up/next
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.
@@ -132,6 +143,48 @@ Sanitization, animation fitting and numeric output policy remain caller-owned.
`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.
<<<<<<< HEAD
## 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.
=======
## 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.
>>>>>>> up/next
Generated
+459 -22
View File
@@ -290,6 +290,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"
@@ -1188,6 +1199,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"
@@ -1729,9 +1752,13 @@ dependencies = [
"craft-browse",
"craft-captions",
"craft-cfb",
"craft-cloud",
"craft-cloud-local",
"craft-cloud-ui",
"craft-cms",
"craft-codepage",
"craft-color",
"craft-color-transform",
"craft-color-ui",
"craft-control",
"craft-control-protocol",
@@ -1742,6 +1769,9 @@ dependencies = [
"craft-dds",
"craft-deckcraft-format",
"craft-designcraft-format",
"craft-develop",
"craft-develop-model",
"craft-dex",
"craft-dialogs",
"craft-doc",
"craft-doc-binary",
@@ -1757,6 +1787,8 @@ dependencies = [
"craft-elf",
"craft-eps",
"craft-epub",
"craft-esign",
"craft-evm",
"craft-ews",
"craft-exr",
"craft-fcproj-format",
@@ -1775,10 +1807,18 @@ dependencies = [
"craft-frame",
"craft-gainmap",
"craft-gcode",
"craft-geom",
"craft-gif",
"craft-gpu",
<<<<<<< HEAD
=======
"craft-gpu-null",
"craft-gpu-providers",
"craft-gpu-wgpu",
>>>>>>> up/next
"craft-gridcraft-format",
"craft-gzip",
"craft-har",
"craft-hdr",
"craft-heif",
"craft-host",
@@ -1791,8 +1831,11 @@ dependencies = [
"craft-imaging",
"craft-indd",
"craft-ipc",
"craft-ipp",
"craft-java-class",
"craft-jmap",
"craft-jpeg",
"craft-jsonc",
"craft-jsonrpc",
"craft-jxl",
"craft-keymap",
@@ -1824,6 +1867,7 @@ dependencies = [
"craft-ora",
"craft-pages",
"craft-palette",
"craft-pcap",
"craft-pcraft-bundle",
"craft-pcraft-format",
"craft-pdf 0.1.0",
@@ -1834,6 +1878,7 @@ dependencies = [
"craft-platform",
"craft-plist",
"craft-plotter",
"craft-ppt-binary",
"craft-pptx",
"craft-prc",
"craft-prepress",
@@ -1859,10 +1904,12 @@ dependencies = [
"craft-sketch",
"craft-smime",
"craft-sniff",
"craft-sourcemap",
"craft-ssip",
"craft-stencilcraft-document",
"craft-stencilcraft-format",
"craft-stream-codecs",
"craft-suite-account",
"craft-svg",
"craft-table",
"craft-tar",
@@ -1871,10 +1918,12 @@ dependencies = [
"craft-texture",
"craft-tga",
"craft-theme",
"craft-theme-editor",
"craft-theme-store",
"craft-tiff",
"craft-time",
"craft-timeline",
"craft-tracked",
"craft-ui",
"craft-update",
"craft-usd",
@@ -1882,12 +1931,15 @@ dependencies = [
"craft-vectorcraft-format",
"craft-vectordrawable",
"craft-video",
"craft-wasm",
"craft-web-api",
"craft-web-host",
"craft-webp",
"craft-window",
"craft-wordmodel",
"craft-xcf",
"craft-xfa",
"craft-xfdf",
"craft-xfl",
"craft-xls",
"craft-xlsx",
@@ -1944,7 +1996,7 @@ dependencies = [
[[package]]
name = "craft-ai-agents"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-ai-integrations?rev=35e0352666a117407d62230a6c6c2366f3e21a3b#35e0352666a117407d62230a6c6c2366f3e21a3b"
source = "git+https://github.com/storytold/craft-ai-integrations?rev=d94a3593adcff504eaaad9e5aee4c84fd2f07a72#d94a3593adcff504eaaad9e5aee4c84fd2f07a72"
dependencies = [
"dirs",
"serde",
@@ -2004,10 +2056,8 @@ name = "craft-automation"
version = "0.1.0"
dependencies = [
"base64 0.22.1",
"craft-ai-agents",
"craft-atomic",
"craft-behavior",
"craft-flags",
"craft-control",
"craft-host",
"craft-keymap",
"craft-platform",
@@ -2068,6 +2118,7 @@ dependencies = [
name = "craft-captions"
version = "0.1.0"
dependencies = [
"craft-media",
"craft-media-testkit",
"craft-timeline",
"craft-xml",
@@ -2083,6 +2134,37 @@ 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-cloud-ui"
version = "0.1.0"
dependencies = [
"craft-cloud",
"craft-esign",
"egui",
"egui_kittest",
"image",
"serde_json",
]
[[package]]
name = "craft-cms"
version = "0.1.0"
@@ -2107,6 +2189,16 @@ dependencies = [
"serde",
]
[[package]]
name = "craft-color-transform"
version = "0.1.0"
dependencies = [
"craft-cms",
"craft-color",
"serde",
"serde_json",
]
[[package]]
name = "craft-color-ui"
version = "0.1.0"
@@ -2131,10 +2223,15 @@ dependencies = [
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]]
@@ -2201,6 +2298,7 @@ dependencies = [
name = "craft-design-lint"
version = "0.1.0"
dependencies = [
"dunce",
"proc-macro2",
"quote",
"serde",
@@ -2221,6 +2319,38 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-develop"
version = "0.1.0"
dependencies = [
"craft-develop-model",
"craft-render-parity",
"proptest",
"rayon",
"serde",
]
[[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"
@@ -2271,6 +2401,7 @@ dependencies = [
"craft-wordmodel",
"image",
"log",
"md4",
"miniz_oxide 0.9.1",
"proptest",
"serde_json",
@@ -2412,6 +2543,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"
@@ -2645,6 +2796,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"
@@ -2657,10 +2816,41 @@ dependencies = [
name = "craft-gpu"
version = "0.1.0"
dependencies = [
<<<<<<< HEAD
"egui-wgpu",
"log",
"pollster 1.0.1",
"tempfile",
=======
"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",
>>>>>>> up/next
]
[[package]]
@@ -2679,6 +2869,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"
@@ -2777,6 +2975,7 @@ dependencies = [
"craft-dds",
"craft-heif",
"craft-jpeg",
"craft-jxl",
"craft-media-testkit",
"craft-metadata",
"craft-tiff",
@@ -2844,6 +3043,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"
@@ -2863,6 +3075,13 @@ dependencies = [
"zune-jpeg",
]
[[package]]
name = "craft-jsonc"
version = "0.1.0"
dependencies = [
"serde_json",
]
[[package]]
name = "craft-jsonrpc"
version = "0.1.0"
@@ -2875,6 +3094,7 @@ dependencies = [
name = "craft-jxl"
version = "0.1.0"
dependencies = [
"brotli-decompressor 5.0.3",
"craft-cms",
"jixel",
"jxl-oxide",
@@ -3222,6 +3442,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"
@@ -3409,10 +3639,24 @@ dependencies = [
name = "craft-plotter"
version = "0.1.0"
dependencies = [
"kurbo",
"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"
@@ -3551,6 +3795,7 @@ name = "craft-raster-codecs"
version = "0.1.0"
dependencies = [
"base64 0.23.1",
"craft-dds",
"craft-image-codecs",
"image",
"thiserror 2.0.21",
@@ -3727,6 +3972,15 @@ 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-ssip"
version = "0.1.0"
@@ -3765,6 +4019,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"
@@ -3773,11 +4042,14 @@ dependencies = [
"craft-xml",
"data-url",
"flate2",
"gif",
"image-webp",
"kurbo",
"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",
@@ -3807,6 +4079,7 @@ dependencies = [
name = "craft-tar"
version = "0.1.0"
dependencies = [
"craft-testkit",
"serde",
]
@@ -3881,6 +4154,16 @@ dependencies = [
"smallvec",
]
[[package]]
name = "craft-theme-editor"
version = "0.1.0"
dependencies = [
"craft-behavior",
"craft-theme",
"craft-ui",
"egui",
]
[[package]]
name = "craft-theme-store"
version = "0.1.0"
@@ -3932,11 +4215,19 @@ dependencies = [
"web-time",
]
[[package]]
name = "craft-tracked"
version = "0.1.0"
dependencies = [
"craft-ui-macros",
]
[[package]]
name = "craft-ui"
version = "0.1.0"
dependencies = [
"craft-behavior",
"craft-cloud-ui",
"craft-i18n",
"craft-ui",
"craft-ui-canvas",
@@ -3997,6 +4288,7 @@ dependencies = [
"craft-settings",
"craft-svg",
"craft-theme",
"craft-tracked",
"craft-ui-contract",
"craft-ui-macros",
"craft-ui-test-kit",
@@ -4013,6 +4305,8 @@ dependencies = [
"log",
"proc-macro2",
"serde",
"serde_json",
"siphasher",
"skrifa 0.44.0",
"smallvec",
"syn 2.0.119",
@@ -4042,6 +4336,7 @@ dependencies = [
"craft-table",
"craft-text",
"craft-theme",
"craft-theme-editor",
"craft-theme-store",
"craft-timeline",
"craft-ui",
@@ -4235,6 +4530,17 @@ dependencies = [
"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"
@@ -4323,6 +4629,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"
@@ -5092,7 +5413,7 @@ dependencies = [
[[package]]
name = "ecolor"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
dependencies = [
"bytemuck",
"emath",
@@ -5148,7 +5469,7 @@ dependencies = [
[[package]]
name = "eframe"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
dependencies = [
"ahash",
"bytemuck",
@@ -5183,7 +5504,7 @@ dependencies = [
[[package]]
name = "egui"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
dependencies = [
"accesskit",
"ahash",
@@ -5202,7 +5523,7 @@ dependencies = [
[[package]]
name = "egui-wgpu"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
dependencies = [
"ahash",
"bytemuck",
@@ -5221,7 +5542,7 @@ dependencies = [
[[package]]
name = "egui-winit"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
dependencies = [
"block2 0.6.2",
"bytemuck",
@@ -5241,7 +5562,7 @@ dependencies = [
[[package]]
name = "egui_glow"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
dependencies = [
"bytemuck",
"egui",
@@ -5254,7 +5575,7 @@ dependencies = [
[[package]]
name = "egui_kittest"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
dependencies = [
"eframe",
"egui",
@@ -5287,7 +5608,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",
@@ -5331,7 +5652,7 @@ dependencies = [
[[package]]
name = "emath"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
dependencies = [
"bytemuck",
]
@@ -5404,7 +5725,7 @@ dependencies = [
[[package]]
name = "epaint"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
dependencies = [
"ahash",
"bytemuck",
@@ -5430,7 +5751,7 @@ dependencies = [
[[package]]
name = "epaint_default_fonts"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
[[package]]
name = "equator"
@@ -5955,6 +6276,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"
@@ -5975,6 +6307,7 @@ checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc"
dependencies = [
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
@@ -6432,7 +6765,7 @@ dependencies = [
[[package]]
name = "harfbuzz-subset-sys"
version = "0.1.0+harfbuzz-15.0.0"
source = "git+https://github.com/storytold/harfbuzz.git?rev=2659dd807f9a48f75604ee7c76cb709f7b70b950#2659dd807f9a48f75604ee7c76cb709f7b70b950"
source = "git+https://github.com/storytold/harfbuzz.git?rev=f32ed1e548eaa8852bff47e49a90629a013a2faa#f32ed1e548eaa8852bff47e49a90629a013a2faa"
dependencies = [
"cc",
]
@@ -6622,6 +6955,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"
@@ -7657,6 +7999,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"
@@ -8007,6 +8370,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"
@@ -9360,7 +9732,7 @@ dependencies = [
"generic-array",
"getrandom 0.2.17",
"hex",
"hkdf",
"hkdf 0.12.4",
"idea",
"k256",
"log",
@@ -10946,6 +11318,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"
@@ -13294,7 +13685,7 @@ dependencies = [
"wasmi_collections",
"wasmi_core",
"wasmi_ir",
"wasmparser",
"wasmparser 0.228.0",
]
[[package]]
@@ -13334,6 +13725,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"
@@ -13766,6 +14179,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"
@@ -14393,6 +14819,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"
@@ -14643,19 +15080,19 @@ dependencies = [
[[patch.unused]]
name = "egui_demo_lib"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
[[patch.unused]]
name = "egui_extras"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
[[patch.unused]]
name = "egui_inspection"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
[[patch.unused]]
name = "egui_system_font_provider"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=e9711ca9b69772f875ae0d1cef1977ff8c16234d#e9711ca9b69772f875ae0d1cef1977ff8c16234d"
source = "git+https://github.com/storytold/egui.git?rev=7e1c30d142bc7879d043e74b5ceb3ecfe7f51329#7e1c30d142bc7879d043e74b5ceb3ecfe7f51329"
+48 -59
View File
@@ -11,6 +11,14 @@ 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-shx = { path = "crates/shx" }
@@ -25,6 +33,7 @@ acadrust = "=0.6.6"
log = "0.4"
# internal: add each crate here as `craft-<name> = { path = "crates/<name>" }`
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" }
@@ -53,13 +62,20 @@ craft-gridcraft-format = { path = "crates/gridcraft-format" }
craft-aaf = { path = "crates/aaf" }
craft-behavior = { path = "crates/behavior" }
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-cloud-ui = { path = "crates/cloud-ui" }
craft-color = { path = "crates/color" }
craft-color-ui = { path = "crates/color-ui" }
craft-color-transform = { path = "crates/color-transform" }
craft-frame = { path = "crates/frame" }
craft-crs = { path = "crates/crs" }
craft-crw = { path = "crates/crw" }
@@ -86,8 +102,6 @@ vectorcraft-doc = { git = "https://github.com/storytold/vectorcraft.git", rev =
vectorcraft-eps = { git = "https://github.com/storytold/vectorcraft.git", rev = "6b3a6b8788ebcb420d69449199cbdf323888d27e" }
vectorcraft-geom = { git = "https://github.com/storytold/vectorcraft.git", rev = "6b3a6b8788ebcb420d69449199cbdf323888d27e" }
vectorcraft-pathops = { git = "https://github.com/storytold/vectorcraft.git", rev = "6b3a6b8788ebcb420d69449199cbdf323888d27e" }
# The ArtCraft Launcher's list of apps AI agents may use, shared with the launcher and the MCP gateway.
craft-ai-agents = { git = "https://github.com/storytold/craft-ai-integrations", rev = "35e0352666a117407d62230a6c6c2366f3e21a3b" }
craft-doc = { path = "crates/doc" }
craft-docpath = { path = "crates/docpath" }
craft-dpx = { path = "crates/dpx" }
@@ -95,6 +109,7 @@ craft-draw = { path = "crates/draw" }
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" }
@@ -108,13 +123,18 @@ 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-font-subset = { path = "crates/font-subset" }
# The HarfBuzz subsetter from storytold's HarfBuzz fork (branch rust-subset-15.0.0: HarfBuzz 15.0.0 plus rust/harfbuzz-subset-sys).
harfbuzz-subset-sys = { git = "https://github.com/storytold/harfbuzz.git", rev = "2659dd807f9a48f75604ee7c76cb709f7b70b950" }
harfbuzz-subset-sys = { git = "https://github.com/storytold/harfbuzz.git", rev = "f32ed1e548eaa8852bff47e49a90629a013a2faa" }
craft-host = { path = "crates/host" }
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" }
@@ -127,7 +147,7 @@ craft-control = { path = "crates/control" }
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" }
@@ -157,6 +177,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" }
@@ -176,6 +197,9 @@ craft-quit = { path = "crates/quit" }
craft-gpu = { path = "crates/gpu" }
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-review = { path = "crates/review" }
@@ -184,6 +208,7 @@ craft-figcraft-format = { path = "crates/figcraft-format" }
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" }
@@ -199,7 +224,9 @@ craft-tiff = { path = "crates/tiff" }
craft-metadata = { path = "crates/metadata" }
craft-table = { path = "crates/table" }
craft-theme = { path = "crates/theme" }
craft-theme-editor = { path = "crates/theme-editor" }
craft-theme-store = { path = "crates/theme-store" }
craft-tracked = { path = "crates/tracked" }
craft-ui = { path = "crates/ui" }
craft-ui-canvas = { path = "crates/ui-canvas" }
craft-ui-contract = { path = "crates/ui-contract" }
@@ -249,6 +276,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"
@@ -274,20 +304,20 @@ hayro-jbig2 = { path = "vendor/hayro-jbig2" }
hayro-cmap = { path = "vendor/hayro-cmap" }
hayro-postscript = { path = "vendor/hayro-postscript" }
linesweeper = { path = "vendor/linesweeper" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
emath = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
egui = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "e9711ca9b69772f875ae0d1cef1977ff8c16234d" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
emath = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
egui = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "7e1c30d142bc7879d043e74b5ceb3ecfe7f51329" }
winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }
# symphonia's FLAC decoder restores LPC samples as `residual + (prediction as i32)`. In a valid
@@ -305,44 +335,3 @@ opt-level = 3
# 2 MiB stack of a test thread. The apps this reader came from build their crates at this level.
[profile.dev.package.craft-affinity]
opt-level = 1
# Dev and test builds never run LTO: with `lto = false` (cargo's default) rustc runs thin-local LTO across every
# optimised crate's 256 codegen units on each rebuild, which multiplied the incremental rebuild of the framework
# crates. Off, a one-line edit re-optimises only the changed units.
[profile.dev]
lto = "off"
# Dependencies (egui, rendering, text shaping, syn) run optimised in dev and test builds, so
# screen tests, design-lint and the debug gallery run at a usable speed. They build once per
# target dir; the workspace's own crates stay unoptimised and incremental.
[profile.dev.package."*"]
opt-level = 2
# craft-theme's colour maths and the behaviour cores, which run in every screen-test frame, and craft-proof, whose
# tests parse and check whole dictionaries, build at opt-level 1: unoptimised, they made the test binaries several
# times slower, and they are rarely edited. Every other workspace crate, the framework runtime included, stays at
# opt-level 0: an edit to craft-ui-contract (65k lines) rebuilt for 35-90 s at opt-level 1 and rebuilds in seconds at 0,
# and the runtime's hot pipeline is generic, so it compiles in the dependants at their opt-level anyway.
[profile.dev.package.craft-theme]
opt-level = 1
[profile.dev.package.craft-behavior]
opt-level = 1
[profile.dev.package.craft-proof]
opt-level = 1
# The gallery on the Mac: optimised like release, but the workspace's crates build in many small
# units, incrementally, so a change to craft-ui rebuilds in minutes. Dependencies keep release
# settings and build once in target/gallery. `cargo run --profile gallery -p craft-ui-gallery`.
[profile.gallery]
inherits = "release"
incremental = true
codegen-units = 256
opt-level = 1
# The workspace's own crates build at opt-level 1 in the gallery profile: rustc shares generic instantiations across
# crates at opt-level 0, 1, s and z only, so at 1 a change in a framework crate no longer makes every dependant
# re-generate its own copies of that crate's generics and inlined functions (a one-line edit rebuilt the gallery in
# 25 s at opt-level 1 and 69–84 s at 3). `package."*"` matches the dependencies only: they keep release optimisation,
# so this changes nothing in their settings.
[profile.gallery.package."*"]
codegen-units = 16
opt-level = 3
+8
View File
@@ -114,6 +114,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,
+4 -1
View File
@@ -2,6 +2,7 @@ Craft App Libraries
===================
<a href="https://sonarcloud.io/project/overview?id=storytold_craft-libs"><img alt="Scanned by SonarCloud" src="https://img.shields.io/badge/Scanned_by-SonarCloud-4b9fd5?style=flat-square&logo=sonarqubecloud&logoColor=white"></a>
<a href="https://coderabbit.ai"><img alt="Reviewed by CodeRabbit" src="https://img.shields.io/coderabbit/prs/github/storytold/craft-libs?style=flat-square&label=Reviewed%20by%20CodeRabbit&logo=coderabbit"></a>
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.
@@ -288,7 +289,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)
@@ -341,6 +342,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`.
+8 -1
View File
@@ -6,11 +6,16 @@ use craft_atomic::{NewFilePermissions, WriteOptions, atomic_write, write_new};
use std::fs::{self, File, Permissions};
use std::io::{self, Write};
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::path::Path;
#[cfg(target_os = "linux")]
use std::path::PathBuf;
use std::process::Command;
#[cfg(target_os = "linux")]
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(target_os = "linux")]
struct TempDir(PathBuf);
#[cfg(target_os = "linux")]
impl TempDir {
fn new() -> io::Result<Self> {
static N: AtomicU64 = AtomicU64::new(0);
@@ -19,6 +24,7 @@ impl TempDir {
Ok(Self(path))
}
}
#[cfg(target_os = "linux")]
impl Drop for TempDir {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
@@ -29,6 +35,7 @@ fn mode(path: &Path) -> io::Result<u32> {
Ok(fs::metadata(path)?.permissions().mode() & 0o777)
}
#[cfg(target_os = "linux")]
/// Set umask only in a separate shell process; changing it in a parallel test process races.
fn child(dir: &Path, mask: &str, expected: &str, check_acl: bool) -> io::Result<()> {
let output = Command::new("sh")
+18
View File
@@ -37,15 +37,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"]
@@ -63,8 +71,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`).
@@ -84,6 +100,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 }
+5
View File
@@ -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 |
+247
View File
@@ -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<usize> {
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<i16>) -> 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<Vec<i16>> {
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<Vec<u8>> {
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<i16> = 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<Vec<u8>> {
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<i16> = 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));
}
}
+56
View File
@@ -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<T> = std::result::Result<T, Error>;
/// 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)
}
+244
View File
@@ -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<usize> {
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<i16>) -> 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<Vec<i16>> {
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<Vec<u8>> {
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<i16> = 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<u8>, 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<Vec<u8>> {
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<i16> = 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());
}
}
+168
View File
@@ -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<Vec<i16>> {
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<Vec<u8>> {
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<i16> = 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());
}
}
+113
View File
@@ -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<Vec<i16>> {
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<Vec<u8>> {
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());
}
}
+378
View File
@@ -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<String> {
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<RawAudio, Error> {
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<u8>> = 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<Vec<u8>, 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)
}
+391
View File
@@ -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<String> {
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<String>, Option<String>) {
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<String> {
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::<String>(),
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<u16> = 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<RawAudio, Error> {
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<u8>> = 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<u32, Error> {
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<Vec<u8>, 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)
}
+288
View File
@@ -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::<f32>()).ok_or(Error::MemoryLimitExceeded)?;
let output = (len / (factor / 8) as usize).checked_mul(std::mem::size_of::<f32>()).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<f32> {
let n = 24 * ratio + 1;
let mid = (n / 2) as f64;
let fc = 0.45 / ratio as f64;
let mut taps: Vec<f64> = (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<Vec<f32>, 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}");
}
}
+275
View File
@@ -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<usize, Error> {
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<Item = usize>, 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<String>,
/// Optional track title if present in container metadata.
pub title: Option<String>,
/// Optional artist name if present in container metadata.
pub artist: Option<String>,
}
/// 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<Vec<u8>>,
}
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<DecodedAudio, Error> {
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<DecodedAudio, Error> {
// 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<f32>` per channel.
pub channels: Vec<Vec<f32>>,
}
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<Format, Error> {
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<Metadata, Error> {
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<RawAudio, Error> {
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<DecodedAudio, Error> {
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<DecodedAudio, Error> {
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<Vec<u8>, 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<Vec<u8>, 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));
}
}
+693
View File
@@ -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<i16>) -> 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<u8>) -> 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<Vec<i16>, 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<Vec<u8>, 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<Vec<i16>, 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<Decoder> = 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<Vec<u8>, 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<Encoder> = 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);
}
}
+996
View File
@@ -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<i16>) -> 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<u8>) -> 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<u8>) -> 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<Vec<i16>, 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<Vec<u8>, 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<Vec<i16>, 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<Decoder> = 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<i16>> = 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<Vec<u8>, 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<Encoder> = 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<i16>> = 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<u8>> = 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);
}
}
+28 -5
View File
@@ -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<alac::Error> for AudioError {
}
}
#[cfg(feature = "vmd-decode")]
impl From<vmd::Error> 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<opus::encoder::Error> for AudioError {
fn from(e: opus::encoder::Error) -> Self {
+2 -2
View File
@@ -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};
+493
View File
@@ -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<T> = std::result::Result<T, EncodeError>;
/// 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<bool> {
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<u32> {
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<f32> = (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<Polyphase>,
/// Buffered input per channel, and how much of it is consumed.
input: Vec<Vec<f32>>,
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<Encoder> {
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<u8> {
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<u8> {
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<u8>) {
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::<f32>() / 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::<f32>() / 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<u8>) {
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<ChannelPlan> = 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);
}
}
+18
View File
@@ -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;
+15 -2
View File
@@ -252,12 +252,25 @@ struct DataChunk {
declared: u64,
}
/// Ids of the chunks that commonly follow a `data` chunk: metadata a writer appends after the
/// samples, sometimes without correcting the RIFF size.
const TRAILING_IDS: [&[u8; 4]; 12] = [b"LIST", b"fact", b"id3 ", b"ID3 ", b"cue ", b"JUNK", b"PEAK", b"bext", b"smpl", b"iXML", b"inst", b"PAD "];
/// Bytes of audio a `data` chunk holds: leniently, whatever is present when the size is wrong or
/// the recording was never finalised.
///
/// A size of 0 is an empty chunk when only whole chunks follow it and either the RIFF size is the
/// file's or the first of them has an id that follows audio (#2123: a `LIST` chunk appended after
/// the RIFF size was written). Otherwise it is a recording that was never finalised, and what
/// follows is audio: the bytes of PCM can spell any id, but rarely whole chunks to the end.
fn data_chunk_len(src: &Src<'_>, c: DataChunk) -> u64 {
if c.size32 != 0 || c.is_rf64 {
return c.declared.min(c.remaining);
}
let finished = u64::from(src.le_u32(4).unwrap_or(0)) + 8 == src.size();
let empty = c.size32 == 0 && !c.is_rf64 && finished && only_chunks_follow(src, c.body);
if c.size32 == 0 && !c.is_rf64 && !empty { c.remaining } else { c.declared.min(c.remaining) }
let metadata_follows = src.tag(c.body).is_some_and(|id| TRAILING_IDS.contains(&&id));
let empty = (finished || metadata_follows) && only_chunks_follow(src, c.body);
if empty { 0 } else { c.remaining }
}
/// The sample layout of a `fmt` chunk, `None` for a format we do not read natively.
+192
View File
@@ -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 <https://wiki.multimedia.cx/index.php/VMD>
//! ("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<Decoder, Error> {
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<Vec<i16>, 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<i16>) -> 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(())
}
}
+96
View File
@@ -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<u32, Eop> {
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<bool, Eop> {
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})");
}
}
}
+409
View File
@@ -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<Node>,
/// The only used entry of a single-entry book.
single: Option<u32>,
/// `entries * dimensions` values when the book has a lookup table.
vectors: Vec<f32>,
}
/// `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<Node>, 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<Self, Error> {
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<u8> = 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::<Node>();
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::<f32>())
.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<u32> = (0..values).map(|_| r.read(value_bits).unwrap_or(0)).collect();
*budget -= total * std::mem::size_of::<f32>();
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<u32, Eop> {
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<Option<&[f32]>, 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<u8>, 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<u8> {
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::<Node>() + 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");
}
}
}
+385
View File
@@ -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<Eop> for Fail {
fn from(_: Eop) -> Self {
Fail::Eop
}
}
/// What a decoded floor gives to synthesis.
pub(super) enum FloorData {
One { y: Vec<i32>, step2: Vec<bool> },
Zero { amplitude: u32, coefficients: Vec<f32> },
}
pub(super) struct Floor0 {
order: usize,
rate: u32,
bark_map_size: u32,
amplitude_bits: u32,
amplitude_offset: u32,
books: Vec<usize>,
}
pub(super) struct Floor1 {
partition_class: Vec<usize>,
class_dimensions: Vec<usize>,
class_subclasses: Vec<u32>,
class_masterbook: Vec<usize>,
subclass_books: Vec<Vec<i32>>,
multiplier: i32,
range: i32,
xlist: Vec<i32>,
/// `(low, high)` neighbour positions of each X from index 2 on.
neighbours: Vec<(usize, usize)>,
/// Indices of `xlist` in ascending X order.
sorted: Vec<usize>,
}
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<Self, Error> {
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<usize> = (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<Option<FloorData>, 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<f32> {
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<Option<FloorData>, 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<f32> {
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<Option<FloorData>, 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<f32> = 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<f32> {
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<i64> = (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<f64> = 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);
}
}
+164
View File
@@ -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<Cpx>,
post: Vec<Cpx>,
fft_tw: Vec<Cpx>,
rev: Vec<usize>,
}
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<Cpx> = (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<f32> {
(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<f32> = (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<f64> = (0..n * 6).map(|i| ((i * 37 % 101) as f64 - 50.0) / 50.0).collect();
let w: Vec<f32> = 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<f32> = (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}");
}
}
}
+552
View File
@@ -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<T> = std::result::Result<T, Error>;
/// 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<String>,
}
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<BitReader<'_>> {
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<Identification> {
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<Comments> {
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<usize>,
/// `(floor, residue)` of each submap.
submaps: Vec<(usize, usize)>,
}
struct Mode {
long: bool,
mapping: usize,
}
struct Setup {
books: Vec<Codebook>,
floors: Vec<Floor>,
residues: Vec<Residue>,
mappings: Vec<Mapping>,
modes: Vec<Mode>,
}
/// 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<Setup> {
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<Vec<f32>>,
}
/// A Vorbis I packet decoder.
pub struct Decoder {
id: Identification,
comments: Comments,
setup: Setup,
imdct: [Imdct; 2],
slopes: [Vec<f32>; 2],
previous: Option<Previous>,
}
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<Self> {
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<f32> {
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<Vec<Vec<f32>>> {
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<f32>> = 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<Vec<Vec<f32>>> {
let zero = || vec![vec![0f32; half]; channels];
let setup = &self.setup;
// Floors, in channel order.
let mut floors: Vec<Option<FloorData>> = 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<bool> = 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<usize> = (0..channels).filter(|&c| mapping.mux[c] == i).collect();
if members.is_empty() {
continue;
}
let skip: Vec<bool> = 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<Vec<f32>>,
/// 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<Decoded> {
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<f32>], pcm: Vec<Vec<f32>>, 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::<f32>()) {
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");
}
}
+196
View File
@@ -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<usize>; 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<Self, Error> {
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<f32>]) {
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<f32>]) -> 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::<u8>::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
}
}
}
+263
View File
@@ -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,
];
+5 -2
View File
@@ -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;
+516
View File
@@ -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<i16> {
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<i16>, 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<u8>)> = 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<i16> {
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<i16> {
let mut bitpos = 0usize;
let mut get = |n: usize| -> Option<u32> {
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<u8>, Vec<i16>, Vec<i16>);
// ---- 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::<Vec<u8>>()).expect("write");
}
fn read_pcm(path: &Path) -> Vec<i16> {
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<u8>) {
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<u8> {
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<String> = 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::<Vec<_>>());
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<u8> = 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<Stream> = {
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<Vec<u8>> {
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<u8>]) -> Vec<u8> {
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<i16> = 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<i16> = 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<Vec<u8>> = x.chunks(4096 * channels).map(|c| swf::encode(c, channels, bits).expect("encode")).collect();
let ours: Vec<i16> = 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<i16> = 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);
}
}
}
@@ -73,3 +73,64 @@ fn many_empty_data_chunks_parse_in_linear_time() {
assert_eq!(info.frames, 0);
assert_eq!(decode(&b, Some("wav")).unwrap().1.frames(), 0);
}
/// fmt (mono 16-bit), an empty `data` chunk, then an 8-byte `LIST` chunk (#2123).
fn empty_data_then_list() -> Vec<u8> {
riff(&[chunk(b"fmt ", &fmt16(1, 8000)), chunk(b"data", &[]), chunk(b"LIST", b"INFOabcd")])
}
/// An empty `data` chunk followed by a `LIST` chunk holds no audio, whether the RIFF size counts
/// the `LIST` chunk or was written before it was appended (#2123): the `LIST` chunk's bytes are
/// never decoded as samples.
#[test]
fn an_empty_data_chunk_followed_by_a_list_chunk_is_no_audio_whatever_the_riff_size() {
let correct = empty_data_then_list();
// The RIFF size a writer put down before appending the LIST chunk.
let mut stale = correct.clone();
let before_list = (stale.len() - 8 - 16) as u32;
stale[4..8].copy_from_slice(&before_list.to_le_bytes());
// A RIFF size past the end of the file.
let mut past = correct.clone();
past[4..8].copy_from_slice(&u32::MAX.to_le_bytes());
for (name, file) in [("correct", &correct), ("stale", &stale), ("past the end", &past)] {
assert_eq!(probe(file, Some("wav")).unwrap().frames, 0, "{name} RIFF size: probe");
assert_eq!(decode(file, Some("wav")).unwrap().1.frames(), 0, "{name} RIFF size: decode");
}
}
/// Bytes after an empty `data` chunk that only look like the start of a chunk are a recording that
/// was never finalised: a `LIST` id whose size runs past the file, or a cut-off header, stays
/// audio, as the bytes of PCM can spell any id.
#[test]
fn an_empty_data_chunk_followed_by_a_broken_chunk_header_is_still_audio() {
let base = riff(&[chunk(b"fmt ", &fmt16(1, 8000)), chunk(b"data", &[])]);
let stale = |tail: &[u8]| {
let mut f = base.clone();
f.extend_from_slice(tail);
f
};
// A LIST id whose size runs past the end of the file: 12 bytes, 6 frames.
assert_eq!(decode(&stale(b"LIST\0\x10\0\0INFO"), Some("wav")).unwrap().1.frames(), 6);
// A LIST id with a cut-off size field: 6 bytes, 3 frames.
assert_eq!(decode(&stale(b"LIST\0\0"), Some("wav")).unwrap().1.frames(), 3);
}
/// A WAV with samples in a sized `data` chunk and a `LIST` chunk after it decodes exactly as
/// before: its samples, not the `LIST` chunk.
#[test]
fn a_sized_data_chunk_followed_by_a_list_chunk_decodes_its_samples() {
let samples: Vec<i16> = (0..64).map(|i| i * 300 - 9000).collect();
let bytes: Vec<u8> = samples.iter().flat_map(|s| s.to_le_bytes()).collect();
let file = riff(&[chunk(b"fmt ", &fmt16(1, 8000)), chunk(b"data", &bytes), chunk(b"LIST", b"INFOabcd")]);
let (info, buf) = decode(&file, Some("wav")).unwrap();
assert_eq!(info.frames, 64);
assert_eq!(buf.frames(), 64);
let mut stale = file.clone();
let before_list = (stale.len() - 8 - 16) as u32;
stale[4..8].copy_from_slice(&before_list.to_le_bytes());
let (_, stale_buf) = decode(&stale, Some("wav")).unwrap();
assert_eq!(stale_buf.frames(), 64, "a stale RIFF size does not change a sized data chunk");
assert_eq!(stale_buf, buf, "the same samples");
let expected: Vec<f32> = samples.iter().map(|s| f32::from(*s) / 32768.0).collect();
assert_eq!(buf.interleaved(), expected, "the data chunk's samples");
}
+208
View File
@@ -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));
}
+155
View File
@@ -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<u8>> {
vec![vec![0x69; len]; channels]
}
fn refs(p: &[Vec<u8>]) -> 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<u8>, sub: &[u8; 4], payload: &[u8]) -> Vec<u8> {
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<u8> {
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<u8> {
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<u8> {
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:?}");
}
}
+347
View File
@@ -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<u8> {
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<Vec<u8>> = (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<f32> = 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<f32> = 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}");
}
}
+201
View File
@@ -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<i16> {
(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<i16> = 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<i16> = 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}");
}
+218
View File
@@ -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::<f64>() / (n as f64);
let mean_y = y.iter().map(|&v| v as f64).sum::<f64>() / (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<String> {
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<i16> = 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<i16> = 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:?}"
);
}
}
}
+33
View File
@@ -1,20 +1,53 @@
//! Integration tests, built into the library's own test executable.
#[cfg(feature = "adpcm")]
#[path = "adpcm_oracle.rs"]
mod adpcm_oracle;
#[cfg(all(feature = "decode", feature = "lossless-encode", feature = "peaks"))]
#[path = "audio/main.rs"]
mod audio;
#[cfg(all(feature = "decode", feature = "encode"))]
#[path = "codecs/main.rs"]
mod codecs;
#[cfg(feature = "dsd")]
#[path = "dsd_bounds.rs"]
mod dsd_bounds;
#[cfg(feature = "dsd")]
#[path = "dsd_metadata.rs"]
mod dsd_metadata;
#[cfg(feature = "dsd")]
#[path = "dsd_oracle.rs"]
mod dsd_oracle;
#[cfg(all(feature = "flac-decode", feature = "flac-encode"))]
#[path = "flac_decode.rs"]
mod flac_decode;
#[cfg(feature = "g722")]
#[path = "g722_oracle.rs"]
mod g722_oracle;
#[cfg(feature = "g726")]
#[path = "g726_oracle.rs"]
mod g726_oracle;
#[cfg(feature = "gsm")]
#[path = "gsm_oracle.rs"]
mod gsm_oracle;
#[cfg(feature = "mpa-encode")]
#[path = "mp2_encoder_oracle.rs"]
mod mp2_encoder_oracle;
#[cfg(feature = "qoa")]
#[path = "qoa_oracle.rs"]
mod qoa_oracle;
#[cfg(feature = "sbc")]
#[path = "sbc_oracle.rs"]
mod sbc_oracle;
#[cfg(feature = "vmd-decode")]
#[path = "vmd.rs"]
mod vmd;
#[cfg(all(feature = "vorbis-decode", feature = "vorbis-encode"))]
#[path = "vorbis_decode_hostile.rs"]
mod vorbis_decode_hostile;
#[cfg(all(feature = "vorbis-decode", feature = "vorbis-encode", feature = "decode"))]
#[path = "vorbis_decode_oracle.rs"]
mod vorbis_decode_oracle;
#[cfg(feature = "vorbis-decode")]
#[path = "vorbis_decode_synthetic.rs"]
mod vorbis_decode_synthetic;
+228
View File
@@ -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<f32> {
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::<f32>()
})
.collect()
}
fn noise(n: usize, amp: f32, seed: u64) -> Vec<f32> {
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<f32>], chunk: usize) -> Vec<u8> {
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<f32>,
min_snr: f64,
}
fn sig_chords(rate: u32, n: usize, c: usize) -> Vec<f32> {
chords(rate, n, 0.003 * c as f32)
}
fn sig_noise(_rate: u32, n: usize, c: usize) -> Vec<f32> {
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<Vec<f32>> {
(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<Vec<f32>>, 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<f32> = 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::<f64>() / 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);
}
}
+280
View File
@@ -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<u8>, Vec<i16>) {
let n = d.block_samples() * d.channels();
if d.bits() == 8 {
let bytes: Vec<u8> = (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<i16> = (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<u8>, Vec<i16>) {
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<i16> {
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<i16> = 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<u8>)| {
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<i16> = 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");
}
+230
View File
@@ -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<u8>,
headers: Vec<Vec<u8>>,
packets: Vec<Vec<u8>>,
}
fn parts(channels: usize) -> Parts {
let signal: Vec<Vec<f32>> = (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<u8>, 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<u8> {
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<u8>| 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(_))));
}
+216
View File
@@ -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<Vec<f32>> {
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<Vec<f32>> {
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<f32> = 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<f32>], theirs: &[Vec<f32>]) {
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<f32>], theirs: &[Vec<f32>], 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<f32> = {
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::<Vec<u8>>()).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<f32>> = 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]]);
}
@@ -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<u8>,
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<u32>)>) {
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<u8>,
coupling: Vec<(u8, u8)>,
}
fn identification(channels: u8) -> Vec<u8> {
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<u8> {
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<u8> {
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<bool> = (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<f32> = out.stdout.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes(*b)).collect();
let ch = usize::from(l.channels);
let theirs: Vec<Vec<f32>> = (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");
}
+6 -6
View File
@@ -25,12 +25,15 @@ ui = ["dep:craft-behavior", "dep:craft-keymap", "dep:craft-ui", "dep:egui"]
# The Actions panel's and the Agents page's stories and mock, and the integration binary `tests/it`.
screen-testing = ["ui", "agents", "craft-ui/testing"]
# Agents in the open window (desktop): the ArtCraft Launcher's switch, the connection file an
# app's `<id>-cli mcp` reads, and with `ui` the link host and the Agents page. The agent settings
# build on every target without it.
agents = ["dep:craft-ai-agents", "dep:craft-flags", "dep:craft-atomic", "dep:craft-host"]
# app's `<id>-cli mcp` reads and that CLI's live client (all `craft-control`'s, re-exported), and
# with `ui` the link host and the Agents page. The agent settings build on every target without it.
agents = ["craft-control/agents", "dep:craft-host"]
[dependencies]
craft-behavior = { workspace = true, optional = true }
# The agents code without a UI toolkit (settings, the launcher's switch, the connection file, the
# CLI's live client), re-exported by `agents` so every app shares one copy.
craft-control = { workspace = true }
craft-keymap = { workspace = true, optional = true }
craft-theme = { workspace = true }
craft-ui = { workspace = true, optional = true }
@@ -45,9 +48,6 @@ png = "0.17"
# The server, client, profiler and trace.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
craft-platform = { workspace = true }
craft-ai-agents = { workspace = true, optional = true }
craft-atomic = { workspace = true, optional = true }
craft-flags = { workspace = true, optional = true }
craft-host = { workspace = true, optional = true }
getrandom = "0.3"
hmac = "0.12"
+1 -1
View File
@@ -8,7 +8,7 @@ Features: `ui` (the egui parts), `agents` (agents in the open window: the launch
| --- | --- |
| `actions` | Recorded actions (every target): `ActionList` of `Action`s and `ActionStep`s, `ActionsFile`, playback with `ActionPlayer` and `ActionRunner` under a `PlaybackBudget`, and `Recording`. The app keeps the command table. |
| `ui::actions` | `ActionsPanel` (every target): sets, actions and steps as a tree, include and dialog cells, rename (`start_rename`), drag to move, Button Mode, the footer and the menus (`ActionsPanelCommands`, `ActionsMenuFacts` and `panel_rows`, which leaves out the rows the host has no command for). It runs nothing; it reports `ActionsEvent`s. `ActionsPanelState` holds its view state. `ActionsWords` holds its words. |
| `agents` | AI agents in the open window. Every target: `AgentSettings` (Control Port, the folders agents may open from and save to, `WhenAppClosed`, Allow Agent Control) and `agents_allowed`. Desktop, feature `agents`: `AgentSwitch`, the ArtCraft Launcher's switch for the app behind the suite's `AI-MCP` flag, watched on a thread; the connection file (`Connection`, `write_connection`, `read_connection`, `remove_stale_connection`, `probe`) that the app's `<id>-cli mcp` finds the open window by. With `ui`: `AgentLink`, the link host that starts, moves, stops and retries the agents' control server to match the settings and publishes the connection file. |
| `agents` | AI agents in the open window. Every target: `AgentSettings` (Control Port, the folders agents may open from and save to, `WhenAppClosed`, Allow Agent Control) and `agents_allowed`. Desktop, feature `agents`: `AgentSwitch`, the ArtCraft Launcher's switch for the app behind the suite's `AI-MCP` flag, watched on a thread; the connection file (`Connection`, `write_connection`, `read_connection`, `remove_stale_connection`, `probe`) that the app's `<id>-cli mcp` finds the open window by; the CLI's live client (`Live`, `LiveConfig`, `Route`). All of these are `craft_control::agents`, re-exported, so apps without the shared UI library use the same code. With `ui`: `AgentLink`, the link host that starts, moves, stops and retries the agents' control server to match the settings and publishes the connection file. |
| `ui::agents` | `AgentsPage` (desktop, features `agents` and `ui`): the Agents page of Preferences, a custom page over the agent settings in the Preferences draft, with the link's status; the folder rows are `PathSetting`s. `AgentsWords` holds its words. |
| `args` | `ControlOptions`: the `--control <PORT>`, `--control-token`, `--control-token-file` values and their `<APP>_CONTROL_*` environment fallbacks, checked the way the apps' command lines report them (usage errors exit 2). |
| `security` | Tokens (`generate_token`, `validate_token`, `server_token`, `client_token`), the authentication handshake (`ServerHandshake`: the challenge handshake, which never sends the token, or the one-frame `auth {token}`), bounded line reads, connection and request limits. |
+15 -12
View File
@@ -9,8 +9,8 @@
//! While agents may connect, the app serves its control channel on loopback ([`AgentLink`], over
//! [`crate::server`]) and publishes where in its settings folder ([`CONNECTION_FILE`]): the port,
//! its process id and the token file ([`TOKEN_FILE`], created `0600` on Unix). The app's
//! `<id>-cli mcp` reads that file on every tool call, then the token from the file it names, so an
//! MCP client's configuration never holds a port or a token.
//! `<id>-cli mcp` reads that file on every tool call ([`Live`]), then the token from the file it
//! names, so an MCP client's configuration never holds a port or a token.
//!
//! The file holds no secret. A crashed (or, on macOS, quit) app can leave it behind; nothing then
//! listens on its port ([`probe`]), and the app's next launch removes it
@@ -18,29 +18,32 @@
//! checks that [`Connection::pid`] is still running before it sends anything derived from the
//! token, and authenticates with the challenge handshake ([`crate::security::ServerHandshake`]).
//!
//! Everything but the link host is `craft_control::agents`, re-exported here: apps that don't use
//! the shared UI library use it from there, with the same files and formats.
//!
//! | 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 link host | [`AgentLink`], [`AgentLinkStatus`], [`LinkProblem`], [`LinkUpdate`] | desktop, features `agents` and `ui` |
//!
//! The Agents page of Preferences is [`crate::ui::agents`].
mod settings;
pub use settings::{AgentSettings, WhenAppClosed, agents_allowed, folder};
// The links above name items of the `agents` and `ui` features; without them they have nothing to
// point at, and the docs of the settings alone still build.
#![cfg_attr(not(all(feature = "agents", feature = "ui", not(target_arch = "wasm32"))), allow(rustdoc::broken_intra_doc_links))]
pub use craft_control::agents::{AgentSettings, WhenAppClosed, agents_allowed, folder};
#[cfg(all(feature = "agents", not(target_arch = "wasm32")))]
mod connection;
#[cfg(all(feature = "agents", feature = "ui", not(target_arch = "wasm32")))]
mod link;
#[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,
pub use craft_control::agents::{
AgentSwitch, CONNECTION_FILE, ConnectFn, Connection, Consent, ConsentFn, HeadlessFn, HeadlessFolders, LAUNCH_POLL, LAUNCH_TIMEOUT, LaunchFn,
Live, LiveConfig, POLL, PROBE_TIMEOUT, Route, TOKEN_FILE, Window, find_window, launcher, launcher_allows, mcp_flag, probe, read_connection,
remove_connection, remove_stale_connection, write_connection,
};
#[cfg(all(feature = "agents", feature = "ui", not(target_arch = "wasm32")))]
pub use link::{AgentLink, AgentLinkStatus, LinkProblem, LinkUpdate, RETRY};
#[cfg(all(feature = "agents", not(target_arch = "wasm32")))]
pub use switch::{AgentSwitch, POLL, launcher_allows, mcp_flag};
+4 -3
View File
@@ -236,12 +236,13 @@ impl ControlChannel {
/// Answers the [`Reply::AfterInput`] waiters from the window host's input state: with
/// `input cancelled` when the host withdrew queued input ([`TARGET_CHANGED`]), with success
/// once nothing is queued or in flight. Call it once per pass, after the pass that consumed the
/// input.
/// once nothing is queued or in flight. Call it once per pass, at the end of the pass (after its
/// UI ran): the step that pass consumed counts as run, so the reply goes out with that pass
/// ([`WindowHost::injected_idle_at_pass_end`]). Called between passes, it waits for the pass.
pub fn poll_input(&mut self, host: &WindowHost) {
if host.take_injected_withdrawn() {
self.input_cancelled(TARGET_CHANGED);
} else if host.injected_idle() {
} else if host.injected_idle_at_pass_end() {
self.input_processed();
}
}
+11 -6
View File
@@ -14,7 +14,7 @@ use craft_automation::agents::{
};
use craft_automation::server;
use craft_automation::ui::agents::AgentsPage;
use craft_host::{AppInfo, Host, PickId, Picked, ScriptedFiles, Wake};
use craft_host::{AppInfo, Host, PickId, Picked, ScriptedFiles};
use craft_ui::contract::Role;
use craft_ui::patterns::preferences::SettingsDraft;
use craft_ui::runtime::motion;
@@ -68,9 +68,9 @@ fn the_switch_wakes_once_per_change() {
};
let wake = {
let wakes = wakes.clone();
Wake::new(move || {
move || {
wakes.fetch_add(1, Ordering::Relaxed);
})
}
};
let switch = AgentSwitch::watch_with(true, probe, Duration::from_millis(5), wake);
let clone = switch.clone();
@@ -161,7 +161,9 @@ fn host() -> WindowHost {
}
fn on(port: u32) -> AgentSettings {
AgentSettings { control_port: port, ..AgentSettings::default() }
let mut s = AgentSettings::default();
s.control_port = port;
s
}
/// A port nothing listens on: bound, then closed.
@@ -188,7 +190,8 @@ fn turning_agents_on_publishes_a_reachable_connection_and_off_removes_it() {
assert!(probe(port, PROBE_TIMEOUT));
let token = std::fs::read_to_string(dir.path().join(TOKEN_FILE)).unwrap();
let folders = AgentSettings { read_root: "/in".into(), ..on(0) };
let mut folders = on(0);
folders.read_root = "/in".into();
let kept = link.sync(Some(&folders), &host);
assert!(kept.requests.is_none() && !kept.stopped && kept.changed, "a folder change keeps the server");
assert_eq!(link.status().port, Some(port));
@@ -266,7 +269,9 @@ fn allow_agent_control_off_and_no_settings_folder_keep_agents_out() {
let mut link = AgentLink::new(Some(dir.path().to_path_buf()), "x");
assert!(link.sync(Some(&on(0)), &host).requests.is_some());
let port = link.status().port.expect("listening");
let refused = link.sync(Some(&AgentSettings { allow_agent_control: false, ..on(0) }), &host);
let mut off = on(0);
off.allow_agent_control = false;
let refused = link.sync(Some(&off), &host);
assert!(refused.stopped && refused.requests.is_none());
assert_eq!(link.status().problem, Some(LinkProblem::NotAllowed));
assert!(!dir.path().join(CONNECTION_FILE).exists());
+68
View File
@@ -186,6 +186,74 @@ fn injected_caps_lock_holds_until_cleared() {
assert_eq!(rig.host().injected_caps_lock(), None);
}
/// Per pass: whether the waiter had its reply by the pass's end, and the pointer position the pass saw.
type PassLog = Rc<RefCell<Vec<(bool, Option<Pos2>)>>>;
/// A fixture whose UI polls `channel` at the end of each pass, as an application does after its UI, with `discard`
/// asking egui to run the pass again on the passes it is true for; `answered` records, per pass, whether the waiter
/// had its reply by the pass's end and the pointer position the pass saw.
fn polling_fixture(
channel: Rc<RefCell<craft_automation::control::ControlChannel>>,
reply: Rc<std::sync::mpsc::Receiver<craft_automation::control::ControlResponse>>,
discard: impl Fn(usize) -> bool + 'static,
) -> (Fixture, PassLog) {
let answered = Rc::new(RefCell::new(Vec::new()));
let seen = answered.clone();
let fx = Fixture::new(move |ui| {
let pass = seen.borrow().len();
if discard(pass) {
ui.ctx().request_discard("the test runs this pass again");
}
channel.borrow_mut().poll_input(&WindowHost::of(ui.ctx()));
let pos = ui.input(|i| i.pointer.latest_pos());
seen.borrow_mut().push((reply.try_recv().is_ok(), pos));
});
(fx, answered)
}
/// A waiter on a move is answered at the end of the pass that ran the move, not a pass later: the application polls
/// after its UI, where the running pass has not ended yet.
#[trial]
fn a_waiter_is_answered_by_the_pass_that_ran_its_step() {
use craft_automation::control::{ControlChannel, ControlRequest, Reply};
let (_transport, requests) = std::sync::mpsc::channel();
let channel = Rc::new(RefCell::new(ControlChannel::new(requests)));
let (req, reply) = ControlRequest::new("ui.move", json!({"x": 30, "y": 20}));
let (mut fx, answered) = polling_fixture(channel.clone(), Rc::new(reply), |_| false);
fx.idle(2);
answered.borrow_mut().clear();
let host = WindowHost::of(fx.ctx());
input_method("ui.move", &json!({"x": 30, "y": 20}), &host, ViewportId::ROOT, &map).expect("a method").expect("valid");
channel.borrow_mut().settle(req, Reply::AfterInput, None, 0.0);
fx.frame(Vec::new());
let passes = answered.borrow().clone();
assert_eq!(passes, vec![(true, Some(pos2(30.0, 20.0)))], "the pass that saw the move answered it");
}
/// A pass egui runs again (`request_discard`) does not answer: the pass that runs again, and draws what the step
/// did, does.
#[trial]
fn a_pass_run_again_leaves_the_answer_to_its_rerun() {
use craft_automation::control::{ControlChannel, ControlRequest, Reply};
let (_transport, requests) = std::sync::mpsc::channel();
let channel = Rc::new(RefCell::new(ControlChannel::new(requests)));
let (req, reply) = ControlRequest::new("ui.move", json!({"x": 30, "y": 20}));
let start = Rc::new(Cell::new(usize::MAX));
let first = start.clone();
let (mut fx, answered) = polling_fixture(channel.clone(), Rc::new(reply), move |pass| pass == first.get());
fx.idle(2);
start.set(answered.borrow().len());
let host = WindowHost::of(fx.ctx());
input_method("ui.move", &json!({"x": 30, "y": 20}), &host, ViewportId::ROOT, &map).expect("a method").expect("valid");
channel.borrow_mut().settle(req, Reply::AfterInput, None, 0.0);
fx.frame(Vec::new());
assert_eq!(fx.passes(), 2, "egui ran the discarded pass again");
let passes: Vec<bool> = answered.borrow()[start.get()..].iter().map(|(done, _)| *done).collect();
assert_eq!(passes, vec![false, true], "the rerun answers, the discarded pass does not");
}
/// A move with `rest` counts as processed only once the pointer has rested that long.
#[trial]
fn a_hover_rest_holds_the_reply_until_it_has_passed() {
+12 -5
View File
@@ -22,8 +22,8 @@ impl<P: Clone + Eq + Hash> Layout<P> {
/// action closed (see [`Self::closed_by`]) so the application can release their state.
/// The callback must be stable throughout one call. Registry validation remains with the
/// application, particularly Open. The column rights ([`Permissions::collapses`],
/// [`Permissions::shares_column`]) are checked on the result: an action that leaves a panel breaking them,
/// which did not before, is refused, so a drag's dry run refuses it alike.
/// [`Permissions::shares_column`], [`Permissions::docks_outside_columns`]) are checked on the result: an action
/// that leaves a panel breaking them, which did not before, is refused, so a drag's dry run refuses it alike.
pub fn apply_with_permissions(&mut self, action: Action<P>, permissions: impl Fn(&P) -> Permissions) -> Result<Vec<P>, DockError> {
self.validate()?;
let action = match action {
@@ -125,10 +125,12 @@ impl<P: Clone + Eq + Hash> Layout<P> {
}
/// The panels that break their column rights: a panel that does not collapse ([`Permissions::collapses`]) in
/// a collapsed column, and every panel of a column (or node of the docked tree) that holds a panel that does
/// not share it ([`Permissions::shares_column`]) beside another panel.
/// a collapsed column, every panel of a column (or node of the docked tree) that holds a panel that does
/// not share it ([`Permissions::shares_column`]) beside another panel, and a panel that docks only in dock
/// columns ([`Permissions::docks_outside_columns`]) in a frame of the docked tree.
pub(super) fn column_breaches(&self, permissions: &impl Fn(&P) -> Permissions) -> HashSet<P> {
let mut breaches = HashSet::new();
let mut framed = Vec::new();
let mut check = |panels: Vec<&P>, iconic: bool| {
if iconic {
breaches.extend(panels.iter().filter(|p| !permissions(p).collapses).map(|p| (*p).clone()));
@@ -147,9 +149,14 @@ impl<P: Clone + Eq + Hash> Layout<P> {
check(column.panels().collect(), column.iconic);
}
}
leaf => check(tree_panels(leaf), false),
leaf => {
let panels = tree_panels(leaf);
framed.extend(panels.iter().filter(|p| !permissions(p).docks_outside_columns).map(|p| (*p).clone()));
check(panels, false);
}
}
}
breaches.extend(framed);
breaches
}
+22 -3
View File
@@ -26,6 +26,10 @@ pub struct Permissions {
/// panels: refused are tab, center, group and seam placements across that boundary in either direction, while a
/// new column, a new dock and a float stay allowed. A toolbar takes its own column.
pub shares_column: bool,
/// The panel may dock outside a dock column: in a frame of the docked tree, as a split beside another frame or at
/// an edge of the docked area, where it can lie across the window. Without it the panel docks only in dock columns
/// or floats: refused is any action that leaves it in a frame, so a toolbar is always a vertical strip.
pub docks_outside_columns: bool,
}
impl Default for Permissions {
@@ -35,9 +39,18 @@ impl Default for Permissions {
}
impl Permissions {
/// Every right: the panel moves, floats, closes, takes tabs and splits, collapses and shares its column.
pub const ALL: Self =
Self { movable: true, floatable: true, closable: true, accepts_tabs: true, accepts_splits: true, collapses: true, shares_column: true };
/// Every right: the panel moves, floats, closes, takes tabs and splits, collapses, shares its column and docks
/// outside dock columns.
pub const ALL: Self = Self {
movable: true,
floatable: true,
closable: true,
accepts_tabs: true,
accepts_splits: true,
collapses: true,
shares_column: true,
docks_outside_columns: true,
};
/// A document canvas: it stays docked and open, and takes neither tabs nor side splits.
/// A group dropped on it floats there. An application that opens panels beside its canvas
@@ -86,6 +99,12 @@ impl Permissions {
self
}
/// These permissions with [`Self::docks_outside_columns`] set to `docks_outside_columns`.
pub const fn docks_outside_columns(mut self, docks_outside_columns: bool) -> Self {
self.docks_outside_columns = docks_outside_columns;
self
}
/// A canvas: it can neither move, close nor take tabs. `accepts_splits` and `floatable`
/// do not matter, so a canvas that opens panels beside it still counts.
pub fn is_protected(self) -> bool {
+36 -4
View File
@@ -10,7 +10,7 @@ use super::{
DockSide,
action::{Action, DockPlacement, Location},
edit::tree_panels,
groups::{Column, PanelPlace, Target},
groups::{Column, DockEdge, PanelPlace, Target},
layout::Layout,
limits::MAX_PANELS,
node::Node,
@@ -285,8 +285,9 @@ impl<P: Clone + Eq + Hash> Workspace<P> {
/// A registered panel that `permissions` says cannot close, such as a protected canvas, is
/// always shown: at its recorded or default place, else at the outer edge of the docked
/// area. A layout that breaks the column rights is repaired: a collapsed column holding a panel that does not
/// collapse ([`Permissions::collapses`]) expands, and a panel that shares no column
/// ([`Permissions::shares_column`]) moves to a column of its own. Pass the panels' own permissions, not
/// collapse ([`Permissions::collapses`]) expands, a panel that shares no column
/// ([`Permissions::shares_column`]) moves to a column of its own, and a panel that docks only in dock columns
/// ([`Permissions::docks_outside_columns`]) moves from a frame to a dock column. Pass the panels' own permissions, not
/// [`Permissions::locked`] ones.
/// `hidden` keeps at most [`MAX_PANELS`] entries, dropping the oldest unregistered ones first.
pub fn reconcile(&mut self, registered: impl Fn(&P) -> bool, permissions: impl Fn(&P) -> Permissions, default: &Workspace<P>) {
@@ -347,10 +348,41 @@ impl<P: Clone + Eq + Hash> Workspace<P> {
}
impl<P: Clone + Eq + Hash> Workspace<P> {
/// Moves each panel that docks only in dock columns ([`Permissions::docks_outside_columns`]) out of a frame of the
/// docked tree: to a new dock at the start edge, else a new dock at the end edge, else a column of its own at the
/// outer side of the first docked column. A move the layout refuses leaves the panel where it was.
fn repair_framed(&mut self, permissions: &impl Fn(&P) -> Permissions) {
let framed: Vec<P> = self
.layout
.panels()
.into_iter()
.filter(|p| !permissions(p).docks_outside_columns && matches!(self.layout.place(p), Some(PanelPlace::Node(_))))
.cloned()
.collect();
for panel in framed {
let column_anchor =
self.layout.panels().into_iter().find(|p| *p != &panel && matches!(self.layout.place(p), Some(PanelPlace::Dock { .. }))).cloned();
let targets = [Some(Target::NewDock { side: DockEdge::Start }), Some(Target::NewDock { side: DockEdge::End })]
.into_iter()
.chain([column_anchor.map(|anchor| Target::At { anchor, placement: DockPlacement::Column { after: false } })]);
for to in targets.flatten() {
let mut layout = self.layout.clone();
let moved = layout.apply_inner(Action::MovePanel { panel: panel.clone(), to }, &|p| permissions(p).is_protected()).is_ok();
if moved && layout.validate().is_ok() {
self.layout = layout;
break;
}
}
}
}
/// Moves each panel that shares no column but has company to a column of its own: a new dock column beside its
/// column, a new window over its window, or a frame beside its node of the docked tree. Then expands each
/// collapsed column that holds a panel that does not collapse. A repair the layout refuses leaves it as it was.
/// collapsed column that holds a panel that does not collapse, and moves each panel that docks only in dock
/// columns out of a frame of the docked tree into a dock column ([`Self::repair_framed`]). A repair the layout
/// refuses leaves it as it was.
fn repair_columns(&mut self, permissions: &impl Fn(&P) -> Permissions) {
self.repair_framed(permissions);
let lonely: Vec<P> = self.layout.panels().into_iter().filter(|p| !permissions(p).shares_column).cloned().collect();
for panel in lonely {
let Some(place) = self.layout.place(&panel) else { continue };
+1 -1
View File
@@ -206,7 +206,7 @@ fn squeeze(natural: &[f32], indices: &[usize], total: f32, minimum: f32) -> Vec<
}
/// Whether an item may move into the overflow menu, or lives there.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FitKeep {
/// The item leaves the strip when the room runs out, last item first.
+31 -7
View File
@@ -30,13 +30,15 @@ pub enum HitShape {
/// The other end.
b: Pos2,
},
/// A chain of segments, hit within [`LINE_REACH`].
/// A chain of segments, hit within `reach` ([`LINE_REACH`] unless [`HitShape::polyline_within`] says otherwise).
#[non_exhaustive]
Polyline {
/// The corners in order.
points: Vec<Pos2>,
/// Whether the last corner joins the first.
closed: bool,
/// How far from the chain a point still hits it, in points.
reach: f32,
},
/// A filled polygon.
Polygon(Vec<Pos2>),
@@ -62,9 +64,16 @@ impl HitShape {
HitShape::Segment { a, b }
}
/// A chain of segments, closed when `closed`.
/// A chain of segments, closed when `closed`, hit within [`LINE_REACH`].
pub fn polyline(points: Vec<Pos2>, closed: bool) -> Self {
HitShape::Polyline { points, closed }
HitShape::Polyline { points, closed, reach: LINE_REACH }
}
/// A chain of segments, closed when `closed`, hit within `reach` points: a tool whose measured reach differs from
/// a line's. A negative or non-finite `reach` is [`LINE_REACH`].
pub fn polyline_within(points: Vec<Pos2>, closed: bool, reach: f32) -> Self {
let reach = if reach.is_finite() && reach >= 0.0 { reach } else { LINE_REACH };
HitShape::Polyline { points, closed, reach }
}
/// The part of `within` outside `polygon`.
@@ -95,7 +104,8 @@ impl HitShape {
}
HitShape::Circle { center, radius } => center.distance(point) <= radius.max(across / 2.0),
HitShape::Segment { a, b } => segment_distance(point, *a, *b) <= reach,
HitShape::Polyline { points, closed } => {
HitShape::Polyline { points, closed, reach: own } => {
let reach = own.max(across / 2.0);
let closing = closed.then(|| points.first().zip(points.last())).flatten();
points.windows(2).any(|pair| segment_distance(point, pair[0], pair[1]) <= reach)
|| closing.is_some_and(|(first, last)| segment_distance(point, *last, *first) <= reach)
@@ -116,10 +126,10 @@ impl HitShape {
HitShape::Rect(rect) => rect.contains(point),
HitShape::Circle { center, radius } => center.distance(point) <= *radius,
HitShape::Segment { a, b } => segment_distance(point, *a, *b) <= LINE_REACH,
HitShape::Polyline { points, closed } => {
HitShape::Polyline { points, closed, reach } => {
let closing = closed.then(|| points.first().zip(points.last())).flatten();
points.windows(2).any(|pair| segment_distance(point, pair[0], pair[1]) <= LINE_REACH)
|| closing.is_some_and(|(first, last)| segment_distance(point, *last, *first) <= LINE_REACH)
points.windows(2).any(|pair| segment_distance(point, pair[0], pair[1]) <= *reach)
|| closing.is_some_and(|(first, last)| segment_distance(point, *last, *first) <= *reach)
}
HitShape::Polygon(points) => inside_polygon(point, points),
HitShape::OutsidePolygon { within, polygon } => within.contains(point) && !inside_polygon(point, polygon),
@@ -330,4 +340,18 @@ mod tests {
let tie = [shape(big(), Sense::CLICK, 1, 0, HitLevel::Body, 0), shape(big(), Sense::CLICK, 2, 0, HitLevel::Body, 0)];
assert_eq!(owner(&tie), Some(2));
}
#[test]
fn a_polyline_hits_within_its_own_reach_and_a_bad_reach_is_a_lines() {
let points = vec![pos2(0.0, 0.0), pos2(100.0, 0.0)];
let line = HitShape::polyline(points.clone(), false);
assert!(line.hit(pos2(50.0, LINE_REACH)) && !line.hit(pos2(50.0, LINE_REACH + 0.5)));
let wide = HitShape::polyline_within(points.clone(), false, 5.0);
assert!(wide.hit(pos2(50.0, 5.0)) && wide.hit(pos2(50.0, -5.0)) && !wide.hit(pos2(50.0, 5.5)));
let hits = |shape: HitShape, y: f32| Shape::new(shape, Sense::CLICK, 0_u8, None, HitPriority::BODY, 0).min_across(8.0).hits(pos2(50.0, y));
assert!(hits(wide.clone(), 5.0) && !hits(wide, 5.5), "a target size under twice the reach keeps the reach");
for bad in [f32::NAN, f32::INFINITY, -1.0] {
assert_eq!(HitShape::polyline_within(points.clone(), false, bad), line);
}
}
}
+1 -1
View File
@@ -8,7 +8,7 @@ use craft_host::target::Os;
use craft_keymap::{Chord, Key, Mods};
/// What the focused stop is, as far as the table's rows differ.
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct KeyRole {
/// A value control with a reset value and no text part: Delete resets it.
+27 -2
View File
@@ -471,6 +471,11 @@ struct Open<T> {
dragging: bool,
/// Moved past slop on a target that senses no drag: the click is gone and nothing starts.
abandoned: bool,
/// The pointer left the window ([`PointerEvent::Gone`]): the click is gone, but a target that senses a drag still
/// starts it from the moves that follow. The button is still down and the window keeps getting the pointer's moves
/// and release while it is outside, so a press near a window edge (a window's resize grip) that leaves the window
/// before it passes slop still drags.
left: bool,
/// A pass reported the press: later passes within slop report `Held`, not `Down`.
reported: bool,
/// A pass reported the drag's start: later passes report `Drag`.
@@ -489,7 +494,7 @@ impl<T> Open<T> {
/// A release now is a click.
fn clicks(&self) -> bool {
!self.dragging && !self.abandoned && self.sense.contains(Sense::CLICK)
!self.dragging && !self.abandoned && !self.left && self.sense.contains(Sense::CLICK)
}
}
@@ -568,7 +573,7 @@ impl<T: Copy> PressMachine<T> {
}
PointerEvent::Gone => {
if let Some(open) = &mut self.open {
open.abandoned = true;
open.left = true;
}
}
PointerEvent::Pressed { button, pos: at, time, clicks } => {
@@ -607,6 +612,7 @@ impl<T: Copy> PressMachine<T> {
start: PressStart::new(at, button, time, clicks),
dragging: false,
abandoned: false,
left: false,
reported: false,
drag_reported: false,
released_at: None,
@@ -739,6 +745,25 @@ mod tests {
assert!(machine.target().is_none(), "the release ends the press");
}
/// The pointer leaving the window (a press on a window's edge grip pulled outward) ends the press's click but not
/// its drag: the window keeps getting the held button's moves from outside, and they start the drag once past slop.
/// The resize from a window edge used to fail whenever the pointer left the window before it passed slop.
#[test]
fn a_press_whose_pointer_left_the_window_still_drags_but_no_longer_clicks() {
let moved = |x: f32| PointerEvent::Moved { at: pos2(x, 0.0), sample: None };
let up = |x: f32| PointerEvent::released(pos2(x, 0.0));
let gone = PointerEvent::Gone;
let out = [moved(SLOP / 2.0), gone];
assert_eq!(phases(&[&[press(0.0, 1.0)], &out, &[moved(SLOP * 2.0)], &[moved(SLOP * 3.0)], &[up(SLOP * 3.0)]], Sense::DRAG), "D H S G R");
assert_eq!(phases(&[&[press(0.0, 1.0)], &out, &[up(SLOP / 2.0)]], Sense::CLICK), "D H R", "a click target loses its click");
assert_eq!(
phases(&[&[press(0.0, 1.0)], &out, &[moved(0.0)], &[up(0.0)]], Sense::CLICK | Sense::DRAG),
"D H H R",
"coming back does not bring the click back"
);
assert_eq!(phases(&[&[press(0.0, 1.0)], &[moved(0.0)], &[up(0.0)]], Sense::CLICK), "D H R!", "a press that stays in still clicks");
}
#[test]
fn held_reports_the_pointer_of_its_pass_within_slop() {
let mut machine = PressMachine::new();
+45
View File
@@ -355,6 +355,51 @@ fn a_panel_that_shares_no_column_moves_only_to_a_column_of_its_own() {
assert_eq!(sorted(&saved.layout), [1, 2, 3, 4, 6]);
}
/// A panel that docks only in dock columns (a toolbar, photocraft#12517) refuses every placement that leaves it in a
/// frame of the docked tree: a split beside the canvas on any side and an edge of the docked area, so it never lies
/// across the window. A new dock, a column beside another and a float still take it, other panels still move while
/// one sits in a frame (only a new breach is refused), and reconcile moves a saved one into a dock column.
#[test]
fn a_panel_that_docks_only_in_columns_never_lands_in_a_frame() {
let strip = |p: &u32| match p {
2 => Permissions::ALL.shares_column(false).docks_outside_columns(false),
1 => Permissions::PROTECTED.accepts_splits(true),
_ => Permissions::ALL,
};
let layout = columns();
let sides = [DockSide::Top, DockSide::Bottom, DockSide::Start, DockSide::End];
let framed = sides.iter().map(|side| DockPlacement::Split(*side)).chain(sides.iter().map(|side| DockPlacement::Edge(*side)));
for placement in framed {
let mut moved = layout.clone();
let action = Action::move_column(2, Target::at(1, placement.clone()));
assert_eq!(moved.apply_with_permissions(action, strip), Err(DockError::NotAllowed), "{placement:?}");
assert_eq!(moved, layout, "{placement:?}");
}
for to in [Target::new_dock(DockEdge::Start), Target::at(3, DockPlacement::column(true)), Target::float(window())] {
let mut moved = layout.clone();
moved.apply_with_permissions(Action::move_column(2, to.clone()), strip).unwrap_or_else(|e| panic!("{to:?}: {e}"));
assert!(!matches!(moved.place(&2), Some(PanelPlace::Node(_))), "{to:?}");
}
// Without the right the same split takes it, so the refusal above is this right's.
let mut split = layout.clone();
split
.apply_with_permissions(Action::move_column(2, Target::at(1, DockPlacement::Split(DockSide::Bottom))), |p: &u32| match p {
2 => Permissions::ALL.shares_column(false),
_ => Permissions::ALL,
})
.unwrap();
assert!(matches!(split.place(&2), Some(PanelPlace::Node(_))));
// A layout saved with the panel in a frame: other panels still move, and reconcile docks it in a column.
let mut other = split.clone();
other.apply_with_permissions(Action::move_group(6, Target::at(3, DockPlacement::column(false))), strip).unwrap();
let mut saved = Workspace::new(split);
saved.reconcile(|_| true, strip, &Workspace::new(columns()));
assert!(matches!(saved.layout.place(&2), Some(PanelPlace::Dock { .. })), "reconcile docks it in a column: {:?}", saved.layout);
assert_eq!(column_of(&saved.layout, 2).panels().collect::<Vec<_>>(), [&2]);
assert_eq!(sorted(&saved.layout), [1, 2, 3, 4, 6]);
}
/// A stepped column (a toolbar's) keeps its step count docked, floating and through JSON, and its width moves only
/// by steps.
#[test]
+10 -7
View File
@@ -49,18 +49,21 @@ fn monitor(files: &Arc<Disk>, jobs: &Arc<ManualJobs>) -> LinkMonitor<u32> {
LinkMonitor::new(files.clone(), jobs.clone(), Wake::none())
}
/// Absolute on every platform: a link path that is not is refused as invalid.
const FILE: &str = if cfg!(windows) { r"C:\p\a.png" } else { "/p/a.png" };
// A file changes once: one LinkChange Ok -> Modified, and nothing on the following passes.
#[trial]
fn the_monitor_reports_a_change_once() {
let files = Disk::new(FakeLinkFiles::new().with("/p/a.png", b"abc", 1));
let a = link(1, "/p/a.png", files.identity_of("/p/a.png"));
let files = Disk::new(FakeLinkFiles::new().with(FILE, b"abc", 1));
let a = link(1, FILE, files.identity_of(FILE));
let jobs = Arc::new(ManualJobs::new());
let mut m = monitor(&files, &jobs);
m.set(&[a], None);
jobs.run_all();
assert_eq!(m.take_changes(), vec![LinkChange { key: 1, before: None, status: LinkStatus::Ok }]);
files.change(FakeLinkFiles::new().with("/p/a.png", b"abcd", 2));
files.change(FakeLinkFiles::new().with(FILE, b"abcd", 2));
m.window_focused();
jobs.run_all();
assert_eq!(m.take_changes(), vec![LinkChange { key: 1, before: Some(LinkStatus::Ok), status: LinkStatus::Modified }]);
@@ -77,10 +80,10 @@ fn the_monitor_reports_a_change_once() {
// A check that finished before the link was updated is stale when it is taken in: it reports nothing, and the newer check decides.
#[trial]
fn a_check_older_than_known_now_is_dropped() {
let files = Disk::new(FakeLinkFiles::new().with("/p/a.png", b"abc", 1));
let old = link(1, "/p/a.png", files.identity_of("/p/a.png"));
files.change(FakeLinkFiles::new().with("/p/a.png", b"abcd", 2));
let updated = link(1, "/p/a.png", files.identity_of("/p/a.png"));
let files = Disk::new(FakeLinkFiles::new().with(FILE, b"abc", 1));
let old = link(1, FILE, files.identity_of(FILE));
files.change(FakeLinkFiles::new().with(FILE, b"abcd", 2));
let updated = link(1, FILE, files.identity_of(FILE));
let jobs = Arc::new(ManualJobs::new());
let mut m = monitor(&files, &jobs);
m.set(&[old], None);
+4 -4
View File
@@ -34,10 +34,10 @@ enum StatusGlyph {
Modified,
}
static MISSING: StatusGlyph = StatusGlyph::Missing;
static UNREADABLE: StatusGlyph = StatusGlyph::Unreadable;
static REFUSED: StatusGlyph = StatusGlyph::Refused;
static MODIFIED: StatusGlyph = StatusGlyph::Modified;
const MISSING: StatusGlyph = StatusGlyph::Missing;
const UNREADABLE: StatusGlyph = StatusGlyph::Unreadable;
const REFUSED: StatusGlyph = StatusGlyph::Refused;
const MODIFIED: StatusGlyph = StatusGlyph::Modified;
impl Glyph for StatusGlyph {
fn paint(&self, painter: &Painter, tokens: &UiTokens, rect: Rect, _: Color32, _: bool) {
+6 -2
View File
@@ -101,11 +101,15 @@ fn linked_keys(result: &DialogResult<RelinkOutcome<u32>>) -> (Vec<u32>, Vec<u32>
(outcome.linked.iter().map(|(key, _)| *key).collect(), outcome.offline.clone())
}
/// A folder path is absolute only with a drive on Windows.
const DRIVE: &str = if cfg!(windows) { "D:" } else { "" };
#[trial]
fn relink_others_offers_the_rows_the_remap_fits() {
let files = files(&[("/T7/shoot/a.mov", SAME), ("/T7/shoot/b.mov", SAME), ("/T7/shoot/c.mov", OTHER)]);
let files =
files(&[(&format!("{DRIVE}/T7/shoot/a.mov"), SAME), (&format!("{DRIVE}/T7/shoot/b.mov"), SAME), (&format!("{DRIVE}/T7/shoot/c.mov"), OTHER)]);
let rows = vec![row(1, "Clip A", "C:\\shoot\\a.mov"), row(2, "Clip B", "C:\\shoot\\b.mov"), row(3, "Clip C", "C:\\shoot\\c.mov")];
let mut rig = rig(rows, &files, "/T7/shoot", true, true);
let mut rig = rig(rows, &files, &format!("{DRIVE}/T7/shoot"), true, true);
rig.search();
rig.click("Link");
rig.jobs.run_all();
+7
View File
@@ -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"] }
+4
View File
@@ -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
Loaded 100 of 948 files, more files were not shown because too many files have changed in this diff. Show more