mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-22 02:21:28 +01:00
Merge main into XLSB sizes, preserving shared styles and regression coverage
This commit is contained in:
commit
6abb8cf77e
1006 files changed
+181867
-7766
No files matched your search
@@ -1,2 +1,19 @@
|
||||
# Font-build test fixtures are compared byte for byte: a checkout must not convert them.
|
||||
crates/fonts-build/tests/fixture/** -text
|
||||
|
||||
# The pinned font index is hashed and compiled in as committed: a checkout must not convert its
|
||||
# line endings, or `Index::bundled` fails its checksum on Windows.
|
||||
crates/font-store/index/sources.json -text
|
||||
crates/font-store/index/indexes/*.gz binary
|
||||
|
||||
# Copies of the vendored krilla source diff, one per patch (see vendor/krilla/README.craft.md).
|
||||
vendor/krilla/patches/*.patch linguist-generated=true
|
||||
|
||||
# Has a pinned digest and is embedded as bytes by craft-font-store.
|
||||
crates/font-store/index/sources.json -text
|
||||
|
||||
# The build fixture verifies these license files are copied byte-for-byte.
|
||||
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
|
||||
@@ -0,0 +1,188 @@
|
||||
# This file is machine-generated by `gh actions-lock`.
|
||||
# Do not edit by hand; run `gh actions-lock` to update.
|
||||
# Docs: https://gh.io/actions-lockfile
|
||||
version: 'v0.0.3'
|
||||
workflows:
|
||||
'.github/workflows/ai-core.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/atomic.yml':
|
||||
- 'actions/checkout@v4.4.0'
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'dtolnay/rust-toolchain@stable'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/audio.yml':
|
||||
- 'actions/cache@v6.1.0'
|
||||
- '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'
|
||||
'.github/workflows/crw.yml':
|
||||
- 'actions/cache@v6.1.0'
|
||||
- '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'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/doc.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/docpath.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/ecproj-format.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/font-store.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/formats.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'dtolnay/rust-toolchain@stable'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'swatinem/rust-cache@v2.9.3'
|
||||
'.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'
|
||||
'.github/workflows/media.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/menu.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'dtolnay/rust-toolchain@stable'
|
||||
- 'swatinem/rust-cache@v2.9.3'
|
||||
'.github/workflows/pcraft-format.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/pdf.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'actions/setup-python@v7.1.0'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
- 'step-security/taiki-e-install-action@v2.87.32'
|
||||
'.github/workflows/print.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/proof.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/sonarcloud.yml': []
|
||||
'.github/workflows/ui.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/rust-cache@v2.9.2'
|
||||
'.github/workflows/update.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/vectorcraft-format.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/video.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/web-api.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
- 'step-security/taiki-e-install-action@v2.87.32'
|
||||
'.github/workflows/web-host.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'
|
||||
- 'step-security/taiki-e-install-action@v2.87.32'
|
||||
'.github/workflows/window.yml':
|
||||
- 'actions/checkout@v7.0.1'
|
||||
- 'step-security/dtolnay-rust-toolchain@v1.0.0'
|
||||
- 'step-security/harden-runner@v2.22.4'
|
||||
'.github/workflows/workspace.yml': []
|
||||
dependencies:
|
||||
'actions/cache@v6.1.0':
|
||||
ref: 'v6.1.0'
|
||||
commit: 'sha1-55cc8345863c7cc4c66a329aec7e433d2d1c52a9'
|
||||
owner_id: 44036562
|
||||
repo_id: 215566462
|
||||
'actions/checkout@v4.4.0':
|
||||
ref: 'v4.4.0'
|
||||
commit: 'sha1-11d5960a326750d5838078e36cf38b85af677262'
|
||||
owner_id: 44036562
|
||||
repo_id: 197814629
|
||||
'actions/checkout@v7.0.1':
|
||||
ref: 'v7.0.1'
|
||||
commit: 'sha1-3d3c42e5aac5ba805825da76410c181273ba90b1'
|
||||
owner_id: 44036562
|
||||
repo_id: 197814629
|
||||
'actions/setup-python@v7.1.0':
|
||||
ref: 'v7.1.0'
|
||||
commit: 'sha1-6d4fd22b20465ccc0f7354476588e8df7c9ca0e0'
|
||||
owner_id: 44036562
|
||||
repo_id: 192625525
|
||||
'dtolnay/rust-toolchain@stable':
|
||||
ref: 'stable'
|
||||
commit: 'sha1-39008827e6b4319a349e9a42dfc90579dd25f1ca'
|
||||
owner_id: 1940490
|
||||
repo_id: 260749683
|
||||
'step-security/dtolnay-rust-toolchain@v1.0.0':
|
||||
ref: 'v1.0.0'
|
||||
commit: 'sha1-8dd607977d06adf4ed49b190381e74c17a9dd25f'
|
||||
owner_id: 88700172
|
||||
repo_id: 1249865941
|
||||
'step-security/harden-runner@v2.22.4':
|
||||
ref: 'v2.22.4'
|
||||
commit: 'sha1-1d88384fd173af6ecb012dfd0264a306b711241a'
|
||||
owner_id: 88700172
|
||||
repo_id: 422287306
|
||||
'step-security/rust-cache@v2.9.2':
|
||||
ref: 'v2.9.2'
|
||||
commit: 'sha1-8871c8ba063416da7c32246932ea6ae08700453e'
|
||||
owner_id: 88700172
|
||||
repo_id: 822636713
|
||||
'step-security/taiki-e-install-action@v2.87.32':
|
||||
ref: 'v2.87.32'
|
||||
commit: 'sha1-76747d22a7567cc4438546a84f11bdf18e670ab8'
|
||||
owner_id: 88700172
|
||||
repo_id: 1382753260
|
||||
'swatinem/rust-cache@v2.9.3':
|
||||
ref: 'v2.9.3'
|
||||
commit: 'sha1-d1527c8f67ef9af357290475d4af4b0fab8f12e0'
|
||||
owner_id: 580492
|
||||
repo_id: 298565987
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
@@ -50,7 +50,7 @@ jobs:
|
||||
name: Atomic WASM
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v4.4.0
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
targets: wasm32-unknown-unknown
|
||||
|
||||
+17
-17
@@ -25,18 +25,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-audio-lint
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -44,7 +44,7 @@ jobs:
|
||||
# Each feature on its own, so code that needs another feature fails here.
|
||||
- name: Clippy, each feature alone
|
||||
run: |
|
||||
for f in "" decode decode-native symphonia encode lossless-encode pcm-encode flac-encode peaks aac-decode aac-encode "aac-encode,rayon" opus-decode ac3-decode mpa-decode serde; do
|
||||
for f in "" decode decode-native symphonia encode lossless-encode pcm-encode flac-encode peaks aac-decode aac-encode "aac-encode,rayon" opus-decode opus-encode ac3-decode mpa-decode serde; do
|
||||
echo "::group::features: ${f:-none}"
|
||||
cargo clippy -p craft-audio --all-targets --locked --no-default-features ${f:+--features "$f"} -- -D warnings
|
||||
echo "::endgroup::"
|
||||
@@ -69,15 +69,15 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- 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@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-audio-msrv
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -88,16 +88,16 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-audio-features
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -113,20 +113,20 @@ jobs:
|
||||
runs-on: ubuntu-26.04
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-audio-test
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
||||
- uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: ${{ runner.temp }}/media-fixtures/opus/vectors
|
||||
# The archive's SHA-256 (scripts/fetch-opus-vectors.sh checks it).
|
||||
|
||||
@@ -16,7 +16,7 @@ jobs:
|
||||
timeout-minutes: 20
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v4.4.0
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: clippy
|
||||
|
||||
+13
-13
@@ -29,17 +29,17 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-crw-${{ matrix.os }}
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -53,17 +53,17 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-crw-wasm
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -75,21 +75,21 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- 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@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
||||
uses: actions/cache@v6.1.0
|
||||
with:
|
||||
path: ${{ runner.temp }}/crw-samples
|
||||
key: crw-samples-${{ hashFiles('crates/crw/tests/samples.json') }}
|
||||
|
||||
@@ -23,15 +23,15 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- 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: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: clippy
|
||||
|
||||
@@ -28,13 +28,13 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,13 +28,13 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -72,17 +72,17 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
|
||||
- uses: Swatinem/rust-cache@v2.9.3
|
||||
with:
|
||||
key: ${{ matrix.crate }}
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -28,11 +28,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -25,18 +25,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-jpeg
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -27,24 +27,24 @@ jobs:
|
||||
CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Checkout test fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
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@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-keymap-linux
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -71,23 +71,23 @@ jobs:
|
||||
CRAFT_FONTS_DIR: ${{ github.workspace }}/.test-inputs/craft-fonts
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- name: Checkout test fonts
|
||||
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
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@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-keymap-${{ matrix.os }}
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -98,17 +98,17 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-keymap-wasm
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
name: Lockfile
|
||||
|
||||
# Fails when .github/workflows/actions.lock is out of sync with the workflows, or when a locked pin
|
||||
# no longer verifies upstream, so a moved tag cannot slip a different commit into a run.
|
||||
# Docs: https://gh.io/actions-lockfile
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: [main]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
|
||||
jobs:
|
||||
lockfile:
|
||||
name: Lockfile is in sync
|
||||
runs-on: ubuntu-24.04
|
||||
timeout-minutes: 10
|
||||
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: Install gh-actions-lock
|
||||
run: gh extension install github/gh-actions-lock --pin v0.4.1
|
||||
- name: Verify the lockfile
|
||||
run: gh actions-lock --verify
|
||||
@@ -25,18 +25,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-media-lint
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -56,16 +56,16 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-media-test
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -21,14 +21,14 @@ jobs:
|
||||
name: Menu (Linux)
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: rustfmt, clippy
|
||||
targets: aarch64-apple-darwin, wasm32-unknown-unknown
|
||||
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
|
||||
- uses: Swatinem/rust-cache@v2.9.3
|
||||
with:
|
||||
key: craft-menu-linux
|
||||
- run: cargo fmt -p craft-menu --check
|
||||
|
||||
@@ -28,13 +28,13 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: "1.95"
|
||||
components: rustfmt, clippy
|
||||
|
||||
+20
-20
@@ -31,19 +31,19 @@ jobs:
|
||||
RUSTFLAGS: --cfg craft_pdf_unicode_fixtures
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-pdf
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- uses: actions/setup-python@v7
|
||||
- uses: actions/setup-python@v7.1.0
|
||||
with:
|
||||
python-version: "3.12"
|
||||
- name: Provision licensed PDF test fonts
|
||||
@@ -61,14 +61,14 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-pdf-backends
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -81,19 +81,19 @@ jobs:
|
||||
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- 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@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- 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
|
||||
- uses: actions/setup-python@v7.1.0
|
||||
with:
|
||||
python-version: "3.12"
|
||||
- name: Provision licensed PDF test fonts
|
||||
@@ -111,7 +111,7 @@ jobs:
|
||||
- name: Install the matching WASM test runner
|
||||
# A prebuilt, checksum-verified binary instead of building it from source on every run.
|
||||
# A version the pinned action does not list yet falls back to `cargo install --locked`.
|
||||
uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32
|
||||
uses: step-security/taiki-e-install-action@v2.87.32
|
||||
with:
|
||||
tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }}
|
||||
fallback: cargo-install
|
||||
@@ -122,19 +122,19 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- 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@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- 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
|
||||
- uses: actions/setup-python@v7.1.0
|
||||
with:
|
||||
python-version: "3.12"
|
||||
- name: Provision licensed PDF test fonts
|
||||
|
||||
@@ -24,11 +24,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: actions/checkout@v7.0.1
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -25,27 +25,27 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
# The Language data (craft-i18n `language-data/`), embedded by crates/proof/build.rs.
|
||||
# Pinned to storytold/craft-i18n#114's head (#109's SCOWL dictionaries + judgment/judgement,
|
||||
# CP2.1); TODO(CP2.1): move to #114's merge commit once it merges.
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
repository: storytold/craft-i18n
|
||||
ref: c239f934ace90b7939afaceb23b472ceeafd6b8a
|
||||
path: craft-i18n
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-proof
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -23,5 +23,5 @@ jobs:
|
||||
# The codec and format code behind features that are off by default. Without these the
|
||||
# coverage build never compiles it, and new code there reads as untested.
|
||||
cargo-features: >-
|
||||
craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write
|
||||
craft-image-codecs/avif,craft-image-codecs/avif-decode,craft-video/dnx,craft-video/vp8,craft-video/prores,craft-video/h264,craft-video/hevc,craft-video/vp9,craft-video/av1,craft-video/threads,craft-audio/decode,craft-audio/encode,craft-audio/peaks,craft-audio/rayon,craft-audio/serde,craft-media/matroska,craft-media/ogg,craft-media/bitstream,craft-media/serde,craft-mesh/stl,craft-mesh/obj,craft-mesh/ply,craft-mesh/gltf,craft-metafile/write,craft-image-codecs/sgi,craft-image-codecs/sunraster,craft-jsonrpc/async-lines
|
||||
secrets: inherit
|
||||
@@ -30,21 +30,21 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- 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@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-ui
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -59,15 +59,15 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@v7
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- 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@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-ui-wasm
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
@@ -28,15 +28,15 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- 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: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -28,13 +28,13 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: "1.95"
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -22,18 +22,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: "1.95"
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-video-lint
|
||||
- run: cargo fmt -p craft-video --check
|
||||
@@ -60,16 +60,16 @@ jobs:
|
||||
name: Video tests (${{ matrix.os }})
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: "1.95"
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-video-${{ matrix.os }}
|
||||
- name: Install ffmpeg (Linux)
|
||||
|
||||
@@ -26,13 +26,13 @@ jobs:
|
||||
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
@@ -46,7 +46,7 @@ jobs:
|
||||
- name: Install the matching WASM test runner
|
||||
# A prebuilt, checksum-verified binary instead of building it from source on every run.
|
||||
# A version the pinned action does not list yet falls back to `cargo install --locked`.
|
||||
uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32
|
||||
uses: step-security/taiki-e-install-action@v2.87.32
|
||||
with:
|
||||
tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }}
|
||||
fallback: cargo-install
|
||||
|
||||
@@ -27,18 +27,18 @@ jobs:
|
||||
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER: wasm-bindgen-test-runner
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: actions/checkout@v7.0.1
|
||||
with:
|
||||
persist-credentials: false
|
||||
- uses: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: step-security/rust-cache@8871c8ba063416da7c32246932ea6ae08700453e # v2.9.2
|
||||
- uses: step-security/rust-cache@v2.9.2
|
||||
with:
|
||||
key: craft-web-host
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
@@ -55,7 +55,7 @@ jobs:
|
||||
- name: Install the matching WASM test runner
|
||||
# A prebuilt, checksum-verified binary instead of building it from source on every run.
|
||||
# A version the pinned action does not list yet falls back to `cargo install --locked`.
|
||||
uses: step-security/taiki-e-install-action@76747d22a7567cc4438546a84f11bdf18e670ab8 # v2.87.32
|
||||
uses: step-security/taiki-e-install-action@v2.87.32
|
||||
with:
|
||||
tool: wasm-bindgen@${{ steps.wasm-bindgen.outputs.version }}
|
||||
fallback: cargo-install
|
||||
|
||||
@@ -28,15 +28,15 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
uses: step-security/harden-runner@v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- 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: step-security/dtolnay-rust-toolchain@8dd607977d06adf4ed49b190381e74c17a9dd25f # v1.0.0
|
||||
- uses: step-security/dtolnay-rust-toolchain@v1.0.0
|
||||
with:
|
||||
toolchain: stable
|
||||
components: rustfmt, clippy
|
||||
|
||||
@@ -10,66 +10,79 @@ on:
|
||||
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' }}
|
||||
|
||||
jobs:
|
||||
native:
|
||||
name: Workspace (${{ matrix.os }})
|
||||
timeout-minutes: 30
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- 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: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: rustfmt, clippy
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
key: workspace
|
||||
- name: Prepare the Windows test driver
|
||||
if: runner.os == 'Windows'
|
||||
shell: powershell
|
||||
run: |
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Start-Service Spooler
|
||||
Add-PrinterDriver -Name 'Generic / Text Only'
|
||||
- run: cargo fmt --check
|
||||
- run: cargo clippy --workspace --all-targets --locked -- -D warnings
|
||||
- run: cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
|
||||
- run: cargo test --workspace --locked
|
||||
env:
|
||||
CRAFT_PRINT_REQUIRE_QUEUE: ${{ runner.os == 'Windows' && '1' || '0' }}
|
||||
- run: cargo test --workspace --all-features --locked
|
||||
env:
|
||||
CRAFT_PRINT_REQUIRE_QUEUE: ${{ runner.os == 'Windows' && '1' || '0' }}
|
||||
name: Workspace
|
||||
uses: storytold/craft-common-actions/.github/workflows/gate.yml@main
|
||||
with:
|
||||
os: '["ubuntu-latest","windows-latest","macos-latest"]'
|
||||
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: |
|
||||
set -euo pipefail
|
||||
corpus_fetcher=.test-inputs/photocraft
|
||||
mkdir -p "$corpus_fetcher"
|
||||
git -C "$corpus_fetcher" init
|
||||
git -C "$corpus_fetcher" remote add origin https://github.com/storytold/PhotoCraft.git
|
||||
git -C "$corpus_fetcher" fetch --quiet --depth=1 origin 74330157cb601573ae4523a95bb9132822fc3918
|
||||
git -C "$corpus_fetcher" checkout --quiet --detach FETCH_HEAD
|
||||
(cd "$corpus_fetcher" && cargo xtask corpus --psd --psd-tools --atn --exr --heif --pngsuite)
|
||||
mkdir -p corpus
|
||||
cp -a "$corpus_fetcher/corpus/." corpus/
|
||||
python scripts/test-fetch-pdf-test-fonts.py
|
||||
python scripts/fetch-pdf-test-fonts.py "$RUNNER_TEMP/craft-pdf-test-fonts"
|
||||
printf 'CRAFT_PSD_CORPUS=%s\n' "$GITHUB_WORKSPACE/$corpus_fetcher/corpus" >> "$GITHUB_ENV"
|
||||
printf 'CRAFT_PDF_TEST_FONT_DIR=%s\n' "$RUNNER_TEMP/craft-pdf-test-fonts" >> "$GITHUB_ENV"
|
||||
command: |
|
||||
python3 scripts/test-workspace-concurrency.py
|
||||
cargo fmt --check
|
||||
cargo clippy --workspace --all-targets --locked -- -D warnings
|
||||
cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
|
||||
cargo test --workspace --locked
|
||||
cargo test --workspace --all-features --locked
|
||||
windows-command: |
|
||||
powershell.exe -NoProfile -Command '$ErrorActionPreference = "Stop"; Start-Service Spooler; Add-PrinterDriver -Name "Generic / Text Only"'
|
||||
cargo fmt --check
|
||||
cargo clippy --workspace --all-targets --locked -- -D warnings
|
||||
cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
|
||||
cargo test --workspace --locked
|
||||
cargo test --workspace --all-features --locked
|
||||
|
||||
msrv:
|
||||
name: Rust 1.95 (Linux libraries)
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@1.95.0
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
key: workspace-msrv
|
||||
- run: cargo check --workspace --lib --all-features --locked
|
||||
name: Rust 1.95
|
||||
uses: storytold/craft-common-actions/.github/workflows/gate.yml@main
|
||||
with:
|
||||
os: '["ubuntu-latest"]'
|
||||
toolchain: 1.95.0
|
||||
targets: ""
|
||||
tools: ""
|
||||
craft-fonts-ref: ""
|
||||
require-craft-fonts: false
|
||||
timeout-minutes: 15
|
||||
dev-debug: ""
|
||||
command: cargo check --workspace --lib --all-features --locked
|
||||
|
||||
wasm:
|
||||
name: UI and settings WASM
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
key: workspace-wasm
|
||||
- run: cargo check -p craft-ui -p craft-settings --all-features --locked --target wasm32-unknown-unknown
|
||||
uses: storytold/craft-common-actions/.github/workflows/gate.yml@main
|
||||
with:
|
||||
os: '["ubuntu-latest"]'
|
||||
targets: wasm32-unknown-unknown
|
||||
tools: ""
|
||||
craft-fonts-ref: ""
|
||||
require-craft-fonts: false
|
||||
timeout-minutes: 15
|
||||
command: cargo check -p craft-ui -p craft-settings --all-features --locked --target wasm32-unknown-unknown
|
||||
@@ -6,6 +6,9 @@ author, source and license. Library code and original assets are under the repos
|
||||
|
||||
| File | Author | Source | License |
|
||||
|---|---|---|---|
|
||||
| `licenses/jpeg-encoder-0.6.1/`, `crates/image-codecs/NOTICE` | jpeg-encoder contributors and the IJG, libjpeg-turbo and SIMD authors named in the retained notices | [jpeg-encoder 0.6.1](https://crates.io/crates/jpeg-encoder/0.6.1), used unmodified; source checksums in `SOURCE.json` | (MIT OR Apache-2.0) AND IJG |
|
||||
| `crates/smime/testdata/` | MailCraft contributors; RFC 9216 example certificates | [MailCraft at 240d1d42](https://github.com/UCS-Care-Group/mailcraft/tree/240d1d42fe394cf3274402b180663ba9b43ca180/crates/smime/testdata), restored unmodified; provenance in the directory README | MIT OR Apache-2.0 (source repository) |
|
||||
| `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/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 |
|
||||
@@ -82,6 +85,7 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso
|
||||
| `hayro-interpret 0.7.0 / src/font/generated/metrics.rs` | Adobe (Core 14 AFM metrics, PDF specification encodings); tables by The Hayro Authors | https://github.com/storytold/pdfcraft/blob/7437bde378f43856eada40af25b983812e615e8a/vendor/hayro-interpret/src/font/generated/metrics.rs | MIT OR Apache-2.0 | `2ec9e608573eda459de1e1d9bda3bb494315e36bee885acc88912a11fe0caa41` | Glyph widths and encodings for non-embedded standard fonts. Non-visual dependency data; existing embedding unchanged |
|
||||
| `crates/audio/src/ac3/golden_frames.bin` | FilmCraft contributors (original work: 12 AC-3 and E-AC-3 syncframes that ffmpeg's `ac3` and `eac3` encoders made from tones and seeded noise synthesised by ffmpeg's lavfi generators; no third-party or recorded audio) | [FilmCraft `crates/ac3`](https://github.com/storytold/filmcraft/tree/1c9c2e82/crates/ac3/src) | MIT OR Apache-2.0 |
|
||||
| `crates/audio/tests/fixtures/` | SoundCraft and craft-libs contributors (original work: 0.1 s test tones from ffmpeg's `sine` generator and hand-built silent MPEG frames; no third-party or recorded audio; commands in `crates/audio/tests/fixtures/README.md`) | [SoundCraft `crates/audio-io/tests/fixtures`](https://github.com/storytold/soundcraft/tree/e86d09a/crates/audio-io/tests/fixtures); `tone.mov`, `tone.mka`, `tone.opus`, `tone.ac3` and `tone.eac3` made in this repository | MIT OR Apache-2.0 |
|
||||
| `crates/image-codecs/src/codecs/xpm/x11_colors.rs` | X.Org Foundation and X11 contributors (X11 `rgb.txt` colour names and values, normalised into a lookup table) | <https://cgit.freedesktop.org/xorg/app/rgb/tree/rgb.txt>, via [PhotoCraft `crates/codecs`](https://github.com/storytold/photocraft/tree/5000240c6/crates/codecs/src/codecs/xpm) | MIT (X.Org app/rgb) |
|
||||
| `crates/raster-codecs/src/xpm/x11_colors.rs` | X.Org Foundation and X11 contributors (X11 `rgb.txt` colour names and values, normalised into a lookup table) | <https://cgit.freedesktop.org/xorg/app/rgb/tree/rgb.txt>, via [PhotoCraft `crates/codecs`](https://github.com/storytold/photocraft/tree/63b641b55e54bf8f5c83efd977fdb16a2c255431/crates/codecs/src/codecs/xpm) | MIT (X.Org app/rgb) |
|
||||
| `crates/psd/tests/fixtures/atn/*.atn` (seven synthetic action sets, encoded field by field; `crates/psd/tests/fixtures/atn/README.md`) | PhotoCraft contributors | [PhotoCraft `crates/psd/tests/fixtures/atn`](https://github.com/storytold/photocraft/tree/63b641b55e54bf8f5c83efd977fdb16a2c255431/crates/psd/tests/fixtures/atn) | MIT OR Apache-2.0 |
|
||||
| `crates/lottie/tests/fixtures/` | EffectCraft contributors (original work: four hand-written Lottie JSON documents, written from the public Lottie format description; not exported from any Adobe product or third-party file) | [EffectCraft `crates/lottie/tests/fixtures`](https://github.com/storytold/effectcraft/tree/e18f48fb/crates/lottie/tests/fixtures) | MIT OR Apache-2.0 |
|
||||
@@ -92,3 +96,10 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso
|
||||
| `crates/raw/docs/raf-corpus.sha256` (hashes only; the files are not bundled) | The photographers who contributed each file to raw.pixls.us | [raw.pixls.us](https://raw.pixls.us/) | CC0-1.0 |
|
||||
| `crates/drawingml/src/preset_text.txt` | DeckCraft contributors (Nikita Rakitin); generated by `crates/drawingml/tests/fixtures/make_preset_guides.py` from the guide formulas of ECMA-376 Part 1 `presetShapeDefinitions.xml` | [storytold/deckcraft#650](https://github.com/storytold/deckcraft/pull/650) | MIT OR Apache-2.0 (the formulas are the format's definition in ECMA-376) |
|
||||
| `crates/drawingml/tests/fixtures/preset_table.txt` | DeckCraft contributors (Nikita Rakitin); generated by `crates/drawingml/tests/fixtures/make_preset_table.py` from ECMA-376 Part 1 `presetShapeDefinitions.xml` | [storytold/deckcraft#650](https://github.com/storytold/deckcraft/pull/650) | MIT OR Apache-2.0 (the formulas are the format's definition in ECMA-376) |
|
||||
|
||||
## Binary Apple property lists
|
||||
|
||||
`crates/plist` is extracted from `storytold/craft-anything` (`crates/formats/src/plist.rs` and checked byte reads), at source main `88d1d9cac852aec1c7fd97886db451e005e49d2a`. MIT OR Apache-2.0; source license texts are retained in the crate. Primary import Git authors preserve Brandon Thomas, BFlat, RRFZ and Afloat. Regression imports preserve BFlat. XML, JSON projection, calendar formatting and app policy were not imported into the public API.
|
||||
|
||||
`crates/tga/src/metadata_tests.rs` and metadata-boundary helpers in `crates/tga/src/lib.rs` are adapted from nuclearspike's FilmCraft #1932 at `e7842f76f887005c460c64a8ab51d59d086e6f27` (MIT OR Apache-2.0). Synthetic fixtures are original contributor test data; primary Git author retained.
|
||||
The XML reader in `crates/plist/src/xml.rs` and its base64 compatibility helper are ported from Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77` (same MIT OR Apache-2.0 source licenses). Brandon Thomas is the primary import author; retained hardening credits include Afloat, BFlat, Rahul, RRFZ and NetterSilver. The finite-real fix preserves gmrmad1 as author from Craft Anything #1905 (`d85e104f`); quoted-attribute/line-ending regressions preserve RRFZ. Bounds/adaptation and bounded entity scanning are subsequent Dylan M. Taylor changes. App-specific JSON projections and keyed-archive interpretation remain outside this crate.
|
||||
Generated
+1615
-40
File diff suppressed because it is too large.
Load diff
+48
-1
@@ -11,14 +11,29 @@ rust-version = "1.95"
|
||||
repository = "https://github.com/storytold/craft-libs"
|
||||
|
||||
[workspace.dependencies]
|
||||
craft-shx = { path = "crates/shx" }
|
||||
craft-http-url = { path = "crates/http-url" }
|
||||
craft-usd = { path = "crates/usd" }
|
||||
craft-smime = { path = "crates/smime" }
|
||||
craft-ber = { path = "crates/ber" }
|
||||
craft-proto = { path = "crates/proto" }
|
||||
acadrust = "=0.6.6"
|
||||
log = "0.4"
|
||||
# internal: add each crate here as `craft-<name> = { path = "crates/<name>" }`
|
||||
craft-plist = { path = "crates/plist" }
|
||||
craft-platform = { path = "crates/platform" }
|
||||
craft-aep = { path = "crates/aep" }
|
||||
craft-affinity = { path = "crates/affinity" }
|
||||
craft-ai-core = { path = "crates/ai-core" }
|
||||
craft-anicraft-format = { path = "crates/anicraft-format" }
|
||||
craft-pdf = { path = "crates/pdf", version = "=0.1.0" }
|
||||
craft-ps = { path = "crates/ps", version = "=0.1.0" }
|
||||
craft-pdn = { path = "crates/pdn" }
|
||||
craft-plate-test = { path = "crates/plate-test" }
|
||||
pdf-writer = "0.15"
|
||||
miniz_oxide = "0.8"
|
||||
weezl = "0.1"
|
||||
skrifa = "0.47"
|
||||
krilla = { version = "=0.8.2", default-features = false, features = ["raster-images"] }
|
||||
hayro = { version = "=0.7.1", default-features = false }
|
||||
hayro-interpret = { version = "=0.7.0", default-features = false, features = ["embed-fonts", "embed-cmaps"] }
|
||||
@@ -26,6 +41,8 @@ hayro-syntax = { version = "=0.7.2", default-features = false, features = ["std"
|
||||
bytemuck = "1"
|
||||
kurbo = "0.13"
|
||||
thiserror = "2"
|
||||
craft-gzip = { path = "crates/gzip" }
|
||||
craft-gridcraft-format = { path = "crates/gridcraft-format" }
|
||||
craft-aaf = { path = "crates/aaf" }
|
||||
craft-atomic = { path = "crates/atomic" }
|
||||
craft-captions = { path = "crates/captions" }
|
||||
@@ -39,12 +56,24 @@ craft-dav = { path = "crates/dav" }
|
||||
craft-dialogs = { path = "crates/dialogs" }
|
||||
craft-doc-binary = { path = "crates/doc-binary" }
|
||||
craft-docx = { path = "crates/docx" }
|
||||
craft-document-bundle = { path = "crates/document-bundle" }
|
||||
craft-mcmat = { path = "crates/mcmat" }
|
||||
craft-deckcraft-format = { path = "crates/deckcraft-format" }
|
||||
craft-designcraft-format = { path = "crates/designcraft-format" }
|
||||
craft-dxf = { path = "crates/dxf" }
|
||||
craft-drawingml = { path = "crates/drawingml" }
|
||||
craft-eps = { path = "crates/eps" }
|
||||
craft-file-formats = { path = "crates/file-formats" }
|
||||
craft-file-import = { path = "crates/file-import" }
|
||||
vectorcraft-doc = { git = "https://github.com/storytold/vectorcraft.git", rev = "6b3a6b8788ebcb420d69449199cbdf323888d27e" }
|
||||
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" }
|
||||
craft-doc = { path = "crates/doc" }
|
||||
craft-docpath = { path = "crates/docpath" }
|
||||
craft-dpx = { path = "crates/dpx" }
|
||||
craft-dwg = { path = "crates/dwg" }
|
||||
craft-fcproj-format = { path = "crates/fcproj-format" }
|
||||
craft-ecproj-format = { path = "crates/ecproj-format" }
|
||||
craft-figma-rest = { path = "crates/figma-rest" }
|
||||
craft-exr = { path = "crates/exr" }
|
||||
@@ -64,19 +93,25 @@ craft-fig = { path = "crates/fig" }
|
||||
craft-fonts-build = { path = "crates/fonts-build" }
|
||||
craft-i18n = { path = "crates/i18n" }
|
||||
craft-idml = { path = "crates/idml" }
|
||||
craft-indd = { path = "crates/indd" }
|
||||
craft-jmap = { path = "crates/jmap" }
|
||||
craft-ical = { path = "crates/ical" }
|
||||
craft-jpeg = { path = "crates/jpeg" }
|
||||
craft-control-protocol = { path = "crates/control-protocol" }
|
||||
craft-jsonrpc = { path = "crates/jsonrpc" }
|
||||
craft-mcp = { path = "crates/mcp" }
|
||||
craft-jxl = { path = "crates/jxl" }
|
||||
craft-kiwi = { path = "crates/kiwi" }
|
||||
craft-keymap = { path = "crates/keymap" }
|
||||
craft-assuan = { path = "crates/assuan" }
|
||||
craft-kra = { path = "crates/kra" }
|
||||
craft-audio = { path = "crates/audio" }
|
||||
# No default features, so craft-audio's `encode` builds craft-media without `isobmff`.
|
||||
craft-latex = { path = "crates/latex" }
|
||||
craft-markdown = { path = "crates/markdown" }
|
||||
craft-math = { path = "crates/math" }
|
||||
# No default features: craft-audio turns on the craft-media modules each of its features needs.
|
||||
craft-media = { path = "crates/media", default-features = false }
|
||||
craft-materialx = { path = "crates/materialx" }
|
||||
craft-media-testkit = { path = "crates/media-testkit" }
|
||||
craft-midi = { path = "crates/midi" }
|
||||
craft-sf2 = { path = "crates/sf2" }
|
||||
@@ -94,12 +129,14 @@ craft-ora = { path = "crates/ora" }
|
||||
craft-pptx = { path = "crates/pptx" }
|
||||
craft-penpot = { path = "crates/penpot" }
|
||||
craft-pcraft-format = { path = "crates/pcraft-format" }
|
||||
craft-pcraft-bundle = { path = "crates/pcraft-bundle" }
|
||||
craft-pkcs7 = { path = "crates/pkcs7" }
|
||||
craft-plotter = { path = "crates/plotter" }
|
||||
craft-prepress = { path = "crates/prepress" }
|
||||
craft-print = { path = "crates/print" }
|
||||
craft-proof = { path = "crates/proof" }
|
||||
craft-raster-codecs = { path = "crates/raster-codecs" }
|
||||
craft-image-codecs = { path = "crates/image-codecs" }
|
||||
craft-psd = { path = "crates/psd" }
|
||||
craft-pst = { path = "crates/pst" }
|
||||
craft-quit = { path = "crates/quit" }
|
||||
@@ -108,11 +145,17 @@ craft-raw = { path = "crates/raw" }
|
||||
craft-sheet = { path = "crates/sheet" }
|
||||
craft-sketch = { path = "crates/sketch" }
|
||||
craft-settings = { path = "crates/settings" }
|
||||
craft-scraft-format = { path = "crates/scraft-format" }
|
||||
craft-time = { path = "crates/time" }
|
||||
craft-silhouette-studio = { path = "crates/silhouette-studio" }
|
||||
craft-svg = { path = "crates/svg" }
|
||||
craft-stencilcraft-format = { path = "crates/stencilcraft-format" }
|
||||
craft-stencilcraft-document = { path = "crates/stencilcraft-document" }
|
||||
craft-svg-paths = { path = "crates/svg-paths" }
|
||||
craft-stream-codecs = { path = "crates/stream-codecs" }
|
||||
craft-text = { path = "crates/text" }
|
||||
craft-tcraft = { path = "crates/tcraft" }
|
||||
craft-texture = { path = "crates/texture" }
|
||||
craft-tga = { path = "crates/tga" }
|
||||
craft-timeline = { path = "crates/timeline" }
|
||||
craft-tiff = { path = "crates/tiff" }
|
||||
@@ -123,8 +166,10 @@ craft-vcard = { path = "crates/vcard" }
|
||||
craft-vectorcraft-format = { path = "crates/vectorcraft-format" }
|
||||
craft-vectordrawable = { path = "crates/vectordrawable" }
|
||||
craft-xls = { path = "crates/xls" }
|
||||
craft-xfl = { path = "crates/xfl" }
|
||||
craft-wordmodel = { path = "crates/wordmodel" }
|
||||
craft-xlsx = { path = "crates/xlsx" }
|
||||
indd = { git = "https://github.com/storytold/indd-utils", rev = "7f3060e5bce78d21ea70e0d5eacb96edce15dfbe" }
|
||||
craft-xcf = { path = "crates/xcf" }
|
||||
craft-xml = { path = "crates/xml" }
|
||||
craft-zip = { path = "crates/zip" }
|
||||
@@ -138,6 +183,8 @@ craft-web-host = { path = "crates/web-host" }
|
||||
craft-webp = { path = "crates/webp" }
|
||||
egui = { version = "0.37", default-features = false }
|
||||
md5 = { package = "md-5", version = "0.10", default-features = false }
|
||||
jxl-oxide = { version = "0.12", default-features = false }
|
||||
jixel = "=0.3.4"
|
||||
rayon = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
|
||||
@@ -5,6 +5,17 @@ Licensed under the MIT License or the Apache License, Version 2.0, at your optio
|
||||
|
||||
Third-party material
|
||||
|
||||
Optional DWG bridge
|
||||
|
||||
`craft-dwg` uses `acadrust` 0.6.6 unmodified as a separate dependency for DWG reading and writing.
|
||||
It is licensed under the Mozilla Public License 2.0. The license text and upstream third-party
|
||||
notices are retained in `licenses/dwg/`; the crate is available from crates.io and
|
||||
https://github.com/hakanaktt/acadrust.
|
||||
|
||||
The byte-level bridge source was moved from CADCraft `crates/dwg` at revision
|
||||
`664837df8925f932e90a7ae37c58d2f54e7a1320`. The bridge itself is MIT OR Apache-2.0; its only
|
||||
MPL-2.0 component is the unmodified `acadrust` dependency.
|
||||
|
||||
This product bundles third-party material under its own licenses, listed below. The per-file
|
||||
list (author, source, license) is in ATTRIBUTION.md.
|
||||
|
||||
@@ -89,6 +100,14 @@ The optional PDF writer bundles four CC0 ICC profiles by Clinton Ingram. See ATT
|
||||
|
||||
Source provenance
|
||||
|
||||
crates/image-codecs (craft-image-codecs) is moved from PhotoCraft's standalone `photocraft-codecs`
|
||||
crate, including its bounded decoders, encoders, format registry, image model, metadata handling,
|
||||
orientation, fidelity warnings, and tests, from revision 5000240c6:
|
||||
https://github.com/storytold/photocraft
|
||||
Copyright (c) 2026 ArtCraft Team and the PhotoCraft contributors. The format implementations are
|
||||
clean-room work based on their public specifications; dependency licences and X.Org's `rgb.txt`
|
||||
attribution are listed in ATTRIBUTION.md.
|
||||
|
||||
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,
|
||||
@@ -235,9 +254,10 @@ applications retain their own document-font selection and licensing policy.
|
||||
linesweeper
|
||||
|
||||
linesweeper 0.5.0 (https://crates.io/crates/linesweeper/0.5.0), MIT OR Apache-2.0, is stored in
|
||||
vendor/linesweeper with its upstream license files and one local patch. Its manifest names Joe Neeman
|
||||
as author, and its LICENSE-MIT reads "Copyright (c) 2018 Raph Levien". See
|
||||
vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for provenance, the local patch
|
||||
vendor/linesweeper with its upstream license files and local patches, to src/sweep/sweep_line.rs and
|
||||
docs/sweep.typ. Its manifest names Joe Neeman as author, and its LICENSE-MIT reads "Copyright (c) 2018
|
||||
Raph Levien". See vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for
|
||||
provenance, the local patches
|
||||
and the files left out.
|
||||
|
||||
Platform lifecycle source provenance
|
||||
@@ -276,3 +296,126 @@ implementation, https://github.com/macosforge/alac at c38887c5c5e64a4b31108733bd
|
||||
(LICENSE-APACHE). The files are modified: translated to Rust, with bounds checks on hostile input,
|
||||
wrapping integer arithmetic, the unrolled predictor and per-width matrix routines folded into one,
|
||||
and the average bit rate filled in. Those files are Apache-2.0 only.
|
||||
|
||||
JPEG encoding dependency: jpeg-encoder 0.6.1
|
||||
|
||||
Source: https://crates.io/crates/jpeg-encoder/0.6.1
|
||||
Declared license: (MIT OR Apache-2.0) AND IJG.
|
||||
This software is based in part on the work of the Independent JPEG Group.
|
||||
|
||||
The MIT license and unaltered upstream IJG and SIMD notices follow. The Apache-2.0
|
||||
alternative is also retained in licenses/jpeg-encoder-0.6.1 at the repository root.
|
||||
The dependency is used unmodified; no encoder implementation is vendored here.
|
||||
|
||||
LICENSE-MIT
|
||||
|
||||
Copyright (c) 2021 Volker Ströbel <volkerstroebel@mysurdity.de>
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
Original src/fdct.rs opening notice
|
||||
|
||||
/*
|
||||
* Ported from mozjpeg to rust
|
||||
*
|
||||
* This file was part of the Independent JPEG Group's software:
|
||||
* Copyright (C) 1991-1996, Thomas G. Lane.
|
||||
* libjpeg-turbo Modifications:
|
||||
* Copyright (C) 2015, 2020, D. R. Commander.
|
||||
*
|
||||
* Conditions of distribution and use:
|
||||
* In plain English:
|
||||
*
|
||||
* 1. We don't promise that this software works. (But if you find any bugs,
|
||||
* please let us know!)
|
||||
* 2. You can use this software for whatever you want. You don't have to pay us.
|
||||
* 3. You may not pretend that you wrote this software. If you use it in a
|
||||
* program, you must acknowledge somewhere in your documentation that
|
||||
* you've used the IJG code.
|
||||
*
|
||||
* In legalese:
|
||||
*
|
||||
* The authors make NO WARRANTY or representation, either express or implied,
|
||||
* with respect to this software, its quality, accuracy, merchantability, or
|
||||
* fitness for a particular purpose. This software is provided "AS IS", and you,
|
||||
* its user, assume the entire risk as to its quality and accuracy.
|
||||
*
|
||||
* This software is copyright (C) 1991-2020, Thomas G. Lane, Guido Vollbeding.
|
||||
* All Rights Reserved except as specified below.
|
||||
*
|
||||
* Permission is hereby granted to use, copy, modify, and distribute this
|
||||
* software (or portions thereof) for any purpose, without fee, subject to these
|
||||
* conditions:
|
||||
* (1) If any part of the source code for this software is distributed, then this
|
||||
* README file must be included, with this copyright and no-warranty notice
|
||||
* unaltered; and any additions, deletions, or changes to the original files
|
||||
* must be clearly indicated in accompanying documentation.
|
||||
* (2) If only executable code is distributed, then the accompanying
|
||||
* documentation must state that "this software is based in part on the work of
|
||||
* the Independent JPEG Group".
|
||||
* (3) Permission for use of this software is granted only if the user accepts
|
||||
* full responsibility for any undesirable consequences; the authors accept
|
||||
* NO LIABILITY for damages of any kind.
|
||||
*
|
||||
* These conditions apply to any software derived from or based on the IJG code,
|
||||
* not just to the unmodified library. If you use our work, you ought to
|
||||
* acknowledge us.
|
||||
*
|
||||
* Permission is NOT granted for the use of any IJG author's name or company name
|
||||
* in advertising or publicity relating to this software or products derived from
|
||||
* it. This software may be referred to only as "the Independent JPEG Group's
|
||||
* software".
|
||||
*
|
||||
* We specifically permit and encourage the use of this software as the basis of
|
||||
* commercial products, provided that all warranty or liability claims are
|
||||
* assumed by the product vendor.
|
||||
*
|
||||
* This file contains a slower but more accurate integer implementation of the
|
||||
* forward DCT (Discrete Cosine Transform).
|
||||
*
|
||||
* A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
|
||||
* on each column. Direct algorithms are also available, but they are
|
||||
* much more complex and seem not to be any faster when reduced to code.
|
||||
*
|
||||
* This implementation is based on an algorithm described in
|
||||
* C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
|
||||
* Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
|
||||
* Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
|
||||
* The primary algorithm described there uses 11 multiplies and 29 adds.
|
||||
* We use their alternate method with 12 multiplies and 32 adds.
|
||||
* The advantage of this method is that no data path contains more than one
|
||||
* multiplication; this allows a very simple and accurate implementation in
|
||||
* scaled fixed-point arithmetic, with a minimal number of shifts.
|
||||
*/
|
||||
|
||||
Original src/avx2/fdct.rs opening notice
|
||||
|
||||
/*
|
||||
* Ported from mozjpeg / jfdctint-avx2.asm to rust
|
||||
* Copyright 2009 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
||||
* Copyright (C) 2009, 2016, 2018, 2020, D. R. Commander.
|
||||
*
|
||||
* Based on the x86 SIMD extension for IJG JPEG library
|
||||
* Copyright (C) 1999-2006, MIYASAKA Masaru.
|
||||
*/
|
||||
@@ -120,6 +120,13 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
pages and version detection, and written back; and `model`, the drawing above the tags (header
|
||||
variables, tables, blocks, layouts, entities) and its writer (R12 to 2018), merged from CADCraft's and VectorCraft's readers. Bounded
|
||||
on hostile input. Mapping the model onto a document stays in each app.
|
||||
- [`craft-deckcraft-format`](crates/deckcraft-format/README.md): the DeckCraft `.deckcraft` package:
|
||||
identity and sniffing, the `presentation.json` envelope with the presentation as a type
|
||||
parameter, NaN-safe JSON and the reader for older files with `null` numbers, member names and
|
||||
compression, and bounded, budgeted member reads. The presentation model and its mapping stay in DeckCraft.
|
||||
- [`craft-dwg`](crates/dwg/README.md): bounded byte-level DWG↔DXF conversion with diagnostics,
|
||||
capped output and an optional child-process worker; the `acadrust` dependency remains isolated
|
||||
behind bytes and is unavailable on wasm32 and FreeBSD/aarch64.
|
||||
- [`craft-ecproj-format`](crates/ecproj-format/README.md): the envelope of EffectCraft `.ecproj`
|
||||
project files: `savedBy`, the schema version and its check, and the file object with the
|
||||
project and its saved view as type parameters. The project model stays in EffectCraft.
|
||||
@@ -169,6 +176,9 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
every frame's position, delay, disposal and transparent colour, the loop count and the first
|
||||
frame's colour table, without decompressing pixels. Tells an animation from a still and whether
|
||||
a file can hold the picture it declares before a decoder runs.
|
||||
- [`craft-gridcraft-format`](crates/gridcraft-format/README.md): GridCraft's native JSON workbook
|
||||
(`.json`, `.scjson`): the extensions, the reader, the pretty writer, and newer entries kept
|
||||
unread and written back into a list. The workbook model and what follows a read stay in GridCraft.
|
||||
- [`craft-hdr`](crates/hdr/src/lib.rs): Radiance HDR (RGBE) picture reader, from FilmCraft: flat,
|
||||
old and new run-length scan lines and all eight axis orders, to linear float RGB. Scan lines are
|
||||
validated before the picture is allocated.
|
||||
@@ -203,6 +213,8 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
decoder is bounded and returns its partial output on damage. From PdfCraft; a PDF filter chain
|
||||
is built on it in `craft-pdf`.
|
||||
- [`craft-timeline`](crates/timeline/README.md): editorial timeline interchange: exact media time and SMPTE timecode, import/export reports, media path and `file://` URL handling, Avid Log Exchange (ALE) and CMX 3600 EDL documents, format detection. From FilmCraft.
|
||||
- [`craft-time`](crates/time/README.md): exact rational ticks and SMPTE time across media, with a `soundcraft` compatibility module retaining SoundCraft's sample-index timebase, tempo maps, grids and session serialization semantics.
|
||||
- [`craft-scraft-format`](crates/scraft-format/README.md): SoundCraft's typed `.scraft` session schema, versioned reader/writer, hostile-input validation, and `.scraftz` bundle reader/writer. Engine and UI mapping stays in SoundCraft.
|
||||
- [`craft-metadata`](crates/metadata/README.md): photo metadata, from LightCraft: EXIF (read and write,
|
||||
maker-note lens and focus point, every tag by name), IPTC-IIM, XMP packets (read, write, and a merge
|
||||
that keeps what another program wrote), GPX track logs, and the blocks that carry them in JPEG, PNG,
|
||||
@@ -226,9 +238,10 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
sockets: session, requests of method calls with back-references, responses, ids, address
|
||||
templates, credentials and the judgement of HTTP answers, over any HTTP implementation. Bounded
|
||||
on hostile answers; what a program asks and makes of the answers stays in it.
|
||||
- [`craft-jpeg`](crates/jpeg/README.md): clean-room decoder for 12-bit and arithmetic-coded
|
||||
JPEG (T.81 SOF0/SOF1 at 12 bits, SOF9, SOF10), from PhotoCraft, and a frame probe for routing.
|
||||
Metadata stays in each app.
|
||||
- [`craft-jpeg`](crates/jpeg/README.md): clean-room JPEG decoder for 12-bit and arithmetic-coded
|
||||
frames (T.81 SOF0/SOF1 at 12 bits, SOF9, SOF10), band decoding, and an optimized 8-bit baseline
|
||||
encoder, from PhotoCraft; includes a frame probe for routing. Metadata interpretation stays in
|
||||
each app.
|
||||
- [`craft-raster-codecs`](crates/raster-codecs/README.md): small raster formats, one module each,
|
||||
from PhotoCraft: DDS (BC1 to BC3, read and write), DICOM, FITS (read and write), IFF/ILBM (read
|
||||
and write), PCX, PNM/PAM/PFM (read and write) and XPM, into one flat image type. Bounded and
|
||||
@@ -325,6 +338,8 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
- [`craft-sheet`](crates/sheet/README.md): spreadsheet vocabulary shared by `craft-xls`, `craft-xlsx` and `craft-ods`: cell positions and A1 text, cell values and error values, sheet-name quoting, Excel's built-in number formats and indexed colour palette. Not a file format; no dependencies.
|
||||
- [`craft-sf2`](crates/sf2/README.md): SoundFont 2 (`.sf2`) reader: the sample pool and, per preset, the key and velocity regions a note sounds, with tuning, loop points, pan, level and volume envelope resolved from the preset and instrument zones. Bounded on hostile input. From LoopCraft.
|
||||
- [`craft-silhouette-studio`](crates/silhouette-studio/src/lib.rs): bounded, fail-closed reader for Silhouette Studio `.studio` and `.studio3` documents: outlines, line colours and page size in millimetres. Protected designs are refused.
|
||||
- [`craft-stencilcraft-format`](crates/stencilcraft-format/README.md): the versioned `.stencilcraft` JSON wire schema and envelope: required members, version 1 defaults, optional fields, and header checks.
|
||||
- `craft-stencilcraft-document`: StencilCraft's typed document model, geometry helpers, validation, and native `.stencilcraft` codec.
|
||||
- [`craft-sketch`](crates/sketch/README.md): clean-room reader for the `.sketch` container (Sketch 48 and newer): the ZIP archive, the version cut, `document.json`, the pages in document order and images, as the JSON the file holds. Bounded on hostile input. Carved from FigCraft.
|
||||
- [`craft-svg`](crates/svg/src/lib.rs): SVG. `sniff` recognises SVG and SVGZ data; `svgz` unpacks
|
||||
SVGZ within a byte limit, decodes UTF-8, UTF-16 and ISO 8859-1, and compresses; `guard` bounds
|
||||
@@ -340,6 +355,9 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit
|
||||
- [`craft-text`](crates/text/README.md): text processing below the composer. Its `hyphen` module finds
|
||||
hyphenation points, with a bundled US English dictionary and patterns and optional hypher patterns
|
||||
for 47 more languages (one cargo feature each).
|
||||
- [`craft-texture`](crates/texture/README.md): linear float image planes, gray or RGBA, with
|
||||
nearest and bilinear sampling that repeats or clamps, resizing and display conversion. Sizes are
|
||||
bounded by `Limits`. From MaterialCraft.
|
||||
- [`craft-ui`](crates/ui/README.md): SVG catalogs, icon buttons, tabs, pane controls, transactional docking, print settings and theme helpers for egui frontends;
|
||||
assets, themes, localization and command handling stay in each app. Requires Rust 1.95 or later.
|
||||
- [`craft-vcard`](crates/vcard/README.md): vCard contacts, from MailCraft: vCard 2.1, 3.0 and 4.0 read
|
||||
|
||||
+45
-2
@@ -57,13 +57,56 @@ pub struct WriteOptions<'a> {
|
||||
/// reported.
|
||||
pub fn write(doc: &Document, opts: &WriteOptions<'_>, report: &mut Report) -> Result<Vec<u8>> {
|
||||
let version = if opts.small_sectors { craft_cfb::Version::V3 } else { craft_cfb::Version::V4 };
|
||||
write::write(doc, version, &opts.identity, report)
|
||||
write::write(doc, version, &opts.identity, &ReferenceOptions::default(), report)
|
||||
}
|
||||
|
||||
/// Optional interpretation choices for applications retaining older interchange behavior.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct ReadOptions {
|
||||
/// Interpret a source clip's start using its target slot's edit rate.
|
||||
pub source_slot_rate: bool,
|
||||
/// Use the referenced mob name when the clip has no explicit name.
|
||||
pub infer_clip_name: bool,
|
||||
/// Decode percent escapes in relative network locators.
|
||||
pub decode_relative_locator: bool,
|
||||
/// Report selector and nested-scope flattening.
|
||||
pub report_flattening: bool,
|
||||
/// Accept truncated PCM WAVE essence summaries.
|
||||
pub permissive_pcm: bool,
|
||||
}
|
||||
impl Default for ReadOptions {
|
||||
fn default() -> Self {
|
||||
Self { source_slot_rate: true, infer_clip_name: true, decode_relative_locator: true, report_flattening: true, permissive_pcm: false }
|
||||
}
|
||||
}
|
||||
/// Read using explicitly selected interpretation choices.
|
||||
pub fn read_with_options(bytes: &[u8], options: &ReadOptions, report: &mut Report) -> Result<Document> {
|
||||
read::read(bytes, options, report)
|
||||
}
|
||||
|
||||
/// Optional compatibility choices for writing source references and locators.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct ReferenceOptions {
|
||||
/// Write source offsets in target-slot units.
|
||||
pub source_slot_rate: bool,
|
||||
/// Percent-encode relative locators.
|
||||
pub encode_relative_locator: bool,
|
||||
}
|
||||
impl Default for ReferenceOptions {
|
||||
fn default() -> Self {
|
||||
Self { source_slot_rate: true, encode_relative_locator: true }
|
||||
}
|
||||
}
|
||||
/// Write with explicitly selected reference and locator conventions.
|
||||
pub fn write_with_references(doc: &Document, opts: &WriteOptions<'_>, references: &ReferenceOptions, report: &mut Report) -> Result<Vec<u8>> {
|
||||
let version = if opts.small_sectors { craft_cfb::Version::V3 } else { craft_cfb::Version::V4 };
|
||||
write::write(doc, version, &opts.identity, references, report)
|
||||
}
|
||||
|
||||
/// Read an AAF file: its top-level compositions, the compositions nested in them and their
|
||||
/// sources. What cannot be represented is skipped and named in `report`.
|
||||
pub fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
read::read(bytes, report)
|
||||
read::read(bytes, &ReadOptions::default(), report)
|
||||
}
|
||||
|
||||
/// Whether `bytes` look like an AAF file (a compound file whose root has the AAF root class or a
|
||||
|
||||
@@ -108,3 +108,19 @@ fn objects_nested_too_deeply_are_an_error() {
|
||||
let e = store::read(&chain(250)).unwrap_err();
|
||||
assert!(e.contains("nested too deeply"), "{e}");
|
||||
}
|
||||
|
||||
/// Many distinct property IDs still reject a duplicate without quadratic scanning.
|
||||
#[test]
|
||||
fn duplicate_after_many_distinct_properties_is_rejected() {
|
||||
let mut props = vec![0x4C, 0x20];
|
||||
props.extend_from_slice(&8193u16.to_le_bytes());
|
||||
for id in (0..8192u16).chain(std::iter::once(7)) {
|
||||
props.extend_from_slice(&id.to_le_bytes());
|
||||
props.extend_from_slice(&0x82u16.to_le_bytes());
|
||||
props.extend_from_slice(&0u16.to_le_bytes());
|
||||
}
|
||||
let mut writer = Writer::new(Version::V3);
|
||||
writer.stream(Writer::ROOT, "properties", props).unwrap();
|
||||
let e = store::read(&writer.finish()).unwrap_err();
|
||||
assert!(e.contains("duplicate property id"), "{e}");
|
||||
}
|
||||
@@ -67,6 +67,7 @@ fn indirect_f64(d: &[u8]) -> Option<f64> {
|
||||
|
||||
struct R<'a> {
|
||||
report: &'a mut Report,
|
||||
options: super::ReadOptions,
|
||||
mobs: HashMap<MobId, &'a Obj>,
|
||||
essence: HashMap<MobId, &'a [u8]>,
|
||||
doc: Document,
|
||||
@@ -90,7 +91,7 @@ struct NestClip<'a> {
|
||||
/// Compositions nested deeper than this are read as gaps.
|
||||
const MAX_NESTING: usize = 16;
|
||||
|
||||
pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
pub(crate) fn read(bytes: &[u8], options: &super::ReadOptions, report: &mut Report) -> Result<Document> {
|
||||
let root = store::read(bytes).map_err(|e| Error::Parse { format: "AAF", message: e })?;
|
||||
let header = root.strong(pid::ROOT_HEADER).ok_or_else(|| Error::Parse { format: "AAF", message: "no header object".into() })?;
|
||||
let content = header.strong(pid::CONTENT).ok_or_else(|| Error::Parse { format: "AAF", message: "no content storage".into() })?;
|
||||
@@ -106,7 +107,8 @@ pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
essence.insert(id, d);
|
||||
}
|
||||
}
|
||||
let mut r = R { report, mobs, essence, doc: Document::default(), sources: HashMap::new(), nests: HashMap::new(), open: Vec::new() };
|
||||
let mut r =
|
||||
R { report, options: *options, mobs, essence, doc: Document::default(), sources: HashMap::new(), nests: HashMap::new(), open: Vec::new() };
|
||||
// Top-level compositions: those no other composition uses, among the ones tagged top-level
|
||||
// when any are. (Premiere Pro tags a nested sequence's composition top-level too; it is still
|
||||
// only a nested sequence.) Compositions that only use each other in a circle all count. The
|
||||
@@ -331,7 +333,9 @@ impl R<'_> {
|
||||
items
|
||||
}
|
||||
x if x == cls::SELECTOR => {
|
||||
self.report.info("a selector was flattened to its selected input");
|
||||
if self.options.report_flattening {
|
||||
self.report.info("a selector was flattened to its selected input");
|
||||
}
|
||||
match c.strong(pid::SELECTED) {
|
||||
Some(s) => {
|
||||
let mut items = self.segment(s, rate, kind, depth);
|
||||
@@ -342,7 +346,9 @@ impl R<'_> {
|
||||
}
|
||||
}
|
||||
x if x == cls::NESTED_SCOPE => {
|
||||
self.report.info("a nested scope was flattened to its final input");
|
||||
if self.options.report_flattening {
|
||||
self.report.info("a nested scope was flattened to its final input");
|
||||
}
|
||||
match c.objs(pid::NESTED_SLOTS).last() {
|
||||
Some(s) => {
|
||||
let mut items = self.segment(s, rate, kind, depth);
|
||||
@@ -388,7 +394,12 @@ impl R<'_> {
|
||||
let len = units_to_ticks(len_of(c), rate.0, rate.1);
|
||||
let tagged_name = tagged_values(c, pid::COMPONENT_USER_COMMENTS).into_iter().find(|(k, _)| k == "Clip Name").map(|(_, v)| v);
|
||||
let (Some(id), Some(slot)) = (c.mob_id(pid::SOURCE_ID), c.u32(pid::SOURCE_MOB_SLOT_ID)) else { return CItem::Filler(len) };
|
||||
let name = tagged_name.or_else(|| self.mobs.get(&id).and_then(|mob| mob.string(pid::MOB_NAME))).unwrap_or_default();
|
||||
let name = if self.options.infer_clip_name {
|
||||
tagged_name.or_else(|| self.mobs.get(&id).and_then(|mob| mob.string(pid::MOB_NAME)))
|
||||
} else {
|
||||
tagged_name
|
||||
}
|
||||
.unwrap_or_default();
|
||||
if id == [0; 32] {
|
||||
return CItem::Filler(len);
|
||||
}
|
||||
@@ -401,6 +412,7 @@ impl R<'_> {
|
||||
.and_then(|mob| mob.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(slot)))
|
||||
.and_then(|s| rate_of(s, pid::EDIT_RATE))
|
||||
.unwrap_or(rate);
|
||||
let source_rate = if self.options.source_slot_rate { source_rate } else { rate };
|
||||
let start = units_to_ticks(c.i64(pid::START_TIME).unwrap_or(0), source_rate.0, source_rate.1);
|
||||
match self.resolve(id, slot, start, kind, master_key, 0) {
|
||||
Some((source, t)) => CItem::Clip(CClip { len, source, start: t, gain: None, name }),
|
||||
@@ -506,6 +518,7 @@ impl R<'_> {
|
||||
.and_then(|m| m.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(next_slot)))
|
||||
.and_then(|s| rate_of(s, pid::EDIT_RATE))
|
||||
.unwrap_or(rate);
|
||||
let source_rate = if self.options.source_slot_rate { source_rate } else { rate };
|
||||
let inner = units_to_ticks(clip.i64(pid::START_TIME).unwrap_or(0), source_rate.0, source_rate.1);
|
||||
if next == [0; 32] {
|
||||
return None;
|
||||
@@ -545,7 +558,11 @@ impl R<'_> {
|
||||
DataKind::Sound => CKind::Sound,
|
||||
_ => kind,
|
||||
};
|
||||
let path = d.objs(pid::LOCATOR).iter().find_map(|l| l.string(pid::URL_STRING)).map(|u| locator_path(&u));
|
||||
let path = d
|
||||
.objs(pid::LOCATOR)
|
||||
.iter()
|
||||
.find_map(|l| l.string(pid::URL_STRING))
|
||||
.map(|u| if self.options.decode_relative_locator { locator_path(&u) } else { craft_timeline::path::file_url_to_path(&u) });
|
||||
let file_rate = rate_of(d, pid::SAMPLE_RATE).unwrap_or(rate);
|
||||
let length = d
|
||||
.i64(pid::FILE_LENGTH)
|
||||
@@ -604,7 +621,8 @@ impl R<'_> {
|
||||
if let Some(data) = self.essence.get(&id) {
|
||||
s.embedded = Some(data.to_vec());
|
||||
// embedded essence may be a whole WAVE / AIFF file
|
||||
if let Some((pcm, ch, sr, bits)) = crate::pcm::parse_wav(data) {
|
||||
let pcm = if self.options.permissive_pcm { craft_audio::essence_pcm::parse_wav(data) } else { crate::pcm::parse_wav(data) };
|
||||
if let Some((pcm, ch, sr, bits)) = pcm {
|
||||
s.embedded = Some(pcm);
|
||||
s.file_channels = ch as u32;
|
||||
s.channels = if s.channel.is_some() { 1 } else { ch as u32 };
|
||||
|
||||
@@ -461,11 +461,13 @@ impl R<'_> {
|
||||
if at > props.len() {
|
||||
return Err(bad("truncated property index"));
|
||||
}
|
||||
// A properties stream can name tens of thousands of ids: do not rescan all earlier ids.
|
||||
let mut seen = std::collections::HashSet::with_capacity(n);
|
||||
for i in 0..n {
|
||||
let (Some(pid), Some(sf), Some(len)) = (rd_u16(&props, 4 + i * 6), rd_u16(&props, 6 + i * 6), rd_u16(&props, 8 + i * 6)) else {
|
||||
return Err(bad("truncated property index"));
|
||||
};
|
||||
if o.props.iter().any(|(previous, _)| *previous == pid) {
|
||||
if !seen.insert(pid) {
|
||||
return Err(bad("duplicate property id"));
|
||||
}
|
||||
let v = props.get(at..at + len as usize).ok_or_else(|| bad("truncated property value"))?;
|
||||
|
||||
@@ -557,3 +557,32 @@ fn relative_media_locator_round_trips_escaped_characters() {
|
||||
let got = read_clean(&bytes);
|
||||
assert!(got.sources.iter().any(|s| s.path.as_deref() == Some("Audio Files/100%20 #1.wav")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compatibility_options_keep_relative_paths_and_parent_rate() {
|
||||
let mut doc = crate::testdoc::sound_only(None);
|
||||
doc.compositions[0].sample_rate = 44_100;
|
||||
doc.sources[0].path = Some("Audio Files/100%20 #1.wav".into());
|
||||
let mut report = Report::default();
|
||||
let references = ReferenceOptions { source_slot_rate: false, encode_relative_locator: false };
|
||||
let bytes = write_with_references(&doc, &WriteOptions { identity: IDENTITY, small_sectors: false }, &references, &mut report).unwrap();
|
||||
let root = store::read(&bytes).unwrap();
|
||||
let mut all = Vec::new();
|
||||
walk(&root, &mut all);
|
||||
assert!(
|
||||
all.iter().filter(|o| o.class == cls::NETWORK_LOCATOR).any(|o| o.string(pid::URL_STRING).as_deref() == Some("Audio Files/100%20 #1.wav"))
|
||||
);
|
||||
let starts: Vec<i64> = all.iter().filter(|o| o.class == cls::SOURCE_CLIP).filter_map(|o| o.i64(pid::START_TIME)).collect();
|
||||
assert!(starts.contains(&17_640), "parent rate start: {starts:?}");
|
||||
let opts = ReadOptions {
|
||||
source_slot_rate: false,
|
||||
infer_clip_name: false,
|
||||
decode_relative_locator: false,
|
||||
report_flattening: false,
|
||||
permissive_pcm: true,
|
||||
};
|
||||
let back = read_with_options(&bytes, &opts, &mut report).unwrap();
|
||||
assert!(back.sources.iter().any(|s| s.path.as_deref() == Some("Audio Files/100%20 #1.wav")));
|
||||
let clip = back.compositions[0].tracks[0].items.iter().find_map(|i| if let CItem::Clip(c) = i { Some(c) } else { None }).unwrap();
|
||||
assert_eq!(clip.start, FrameRate::FPS_25.tick_of(10));
|
||||
}
|
||||
@@ -75,6 +75,7 @@ fn definition(class: Auid, id: Auid, name: &str) -> Obj {
|
||||
struct W<'a> {
|
||||
report: &'a mut Report,
|
||||
inexact: bool,
|
||||
references: super::ReferenceOptions,
|
||||
}
|
||||
|
||||
struct IdGen {
|
||||
@@ -350,9 +351,15 @@ fn add_tape_mob(first: &Source, tape_id: [u8; 32], timecode: Option<(i64, FrameR
|
||||
}
|
||||
|
||||
/// Serialise a composition document.
|
||||
pub(crate) fn write(doc: &Document, version: craft_cfb::Version, identity: &Identity<'_>, report: &mut Report) -> Result<Vec<u8>> {
|
||||
pub(crate) fn write(
|
||||
doc: &Document,
|
||||
version: craft_cfb::Version,
|
||||
identity: &Identity<'_>,
|
||||
references: &super::ReferenceOptions,
|
||||
report: &mut Report,
|
||||
) -> Result<Vec<u8>> {
|
||||
let comp = doc.compositions.first().ok_or(Error::Empty)?;
|
||||
let mut w = W { report, inexact: false };
|
||||
let mut w = W { report, inexact: false, references: *references };
|
||||
let mut ids = IdGen::for_document(doc, comp);
|
||||
let MediaOutput { mut mobs, essence, master_ref } = write_media(doc, &mut w, &mut ids);
|
||||
let mut used_ops: Vec<Auid> = Vec::new();
|
||||
@@ -360,7 +367,8 @@ pub(crate) fn write(doc: &Document, version: craft_cfb::Version, identity: &Iden
|
||||
|
||||
// ---- nested compositions: a clip refers to one as it does to a master mob, through the
|
||||
// composition's first track of the clip's kind (a broken-out channel: that channel's track)
|
||||
let mut refs = Refs { mobs: master_ref, rates: doc.sources.iter().enumerate().map(|(i, s)| (i, source_rate(s))).collect() };
|
||||
let rates = if references.source_slot_rate { doc.sources.iter().enumerate().map(|(i, s)| (i, source_rate(s))).collect() } else { HashMap::new() };
|
||||
let mut refs = Refs { mobs: master_ref, rates };
|
||||
let nested_ids: Vec<[u8; 32]> = doc.nested.iter().map(|_| ids.next()).collect();
|
||||
for (si, s) in doc.sources.iter().enumerate() {
|
||||
let Some((comp, id)) = s.nested.and_then(|n| Some((doc.nested.get(n)?, nested_ids.get(n)?))) else { continue };
|
||||
@@ -795,8 +803,10 @@ fn descriptor(w: &mut W, s: &Source) -> Obj {
|
||||
if let Some(p) = &s.path {
|
||||
let url = if craft_timeline::path::is_absolute(p) {
|
||||
craft_timeline::path::path_to_file_url(p, false)
|
||||
} else {
|
||||
} else if w.references.encode_relative_locator {
|
||||
craft_timeline::path::encode_uri_path(p)
|
||||
} else {
|
||||
p.clone()
|
||||
};
|
||||
d.set(pid::LOCATOR, Value::StrongVec(vec![Obj::new(cls::NETWORK_LOCATOR).data_prop(pid::URL_STRING, utf16z(&url))]));
|
||||
}
|
||||
|
||||
@@ -29,7 +29,11 @@ mod roundtrip_tests;
|
||||
#[cfg(test)]
|
||||
mod testdoc;
|
||||
|
||||
/// Diagnostic types used by readers and writers.
|
||||
pub use craft_timeline::report;
|
||||
pub use craft_timeline::report::Report;
|
||||
/// Exact time types used by the composition model.
|
||||
pub use craft_timeline::time;
|
||||
|
||||
/// Error from a reader or writer. Unsupported *features* never error; they go in the [`Report`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
//! transition's name) are not in the specification; they were first written by FilmCraft and keep
|
||||
//! their names so that existing files read back.
|
||||
|
||||
pub(crate) mod bento;
|
||||
pub mod bento;
|
||||
mod read;
|
||||
mod write;
|
||||
|
||||
|
||||
@@ -35,8 +35,8 @@
|
||||
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
pub(crate) const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7];
|
||||
pub(crate) const LABEL_SIZE: usize = 24;
|
||||
pub const MAGIC: [u8; 8] = [0xA4, b'C', b'M', 0xA5, b'H', b'd', b'r', 0xD7];
|
||||
pub const LABEL_SIZE: usize = 24;
|
||||
|
||||
// standard object ids
|
||||
const TOC_OBJECT: u32 = 1;
|
||||
@@ -82,7 +82,7 @@ const MAX_VALUE_BYTES: usize = 16 << 20;
|
||||
const MAX_NAME_BYTES: usize = 4 << 20;
|
||||
|
||||
/// Builds a Bento container.
|
||||
pub(crate) struct Writer {
|
||||
pub struct Writer {
|
||||
data: Vec<u8>,
|
||||
names: HashMap<(u32, String), u32>,
|
||||
next: u32,
|
||||
@@ -90,6 +90,12 @@ pub(crate) struct Writer {
|
||||
entries: Vec<(u32, u32, u32, Vec<Seg>)>,
|
||||
}
|
||||
|
||||
impl Default for Writer {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
pub fn new() -> Writer {
|
||||
Writer { data: Vec::new(), names: HashMap::new(), next: MIN_USER_ID, entries: Vec::new() }
|
||||
@@ -217,9 +223,9 @@ pub(crate) struct Prop {
|
||||
}
|
||||
|
||||
/// A parsed Bento container.
|
||||
pub(crate) struct Container<'a> {
|
||||
pub struct Container<'a> {
|
||||
data: &'a [u8],
|
||||
pub objects: BTreeMap<u32, Vec<Prop>>,
|
||||
pub(crate) objects: BTreeMap<u32, Vec<Prop>>,
|
||||
names: HashMap<u32, String>,
|
||||
by_name: HashMap<String, u32>,
|
||||
}
|
||||
@@ -229,11 +235,16 @@ fn be32(b: &[u8], at: usize) -> Option<u32> {
|
||||
}
|
||||
|
||||
/// Whether `b` ends with a Bento label.
|
||||
pub(crate) fn sniff(b: &[u8]) -> bool {
|
||||
pub fn sniff(b: &[u8]) -> bool {
|
||||
b.len() >= LABEL_SIZE && b[b.len() - LABEL_SIZE..b.len() - LABEL_SIZE + 8] == MAGIC
|
||||
}
|
||||
|
||||
impl<'a> Container<'a> {
|
||||
/// Object identifiers in ascending order.
|
||||
pub fn object_ids(&self) -> impl Iterator<Item = u32> + '_ {
|
||||
self.objects.keys().copied()
|
||||
}
|
||||
|
||||
pub fn open(data: &'a [u8]) -> Result<Container<'a>, String> {
|
||||
if !sniff(data) {
|
||||
return Err("no Bento label at the end of the file".into());
|
||||
@@ -324,7 +335,7 @@ impl<'a> Container<'a> {
|
||||
}
|
||||
|
||||
/// The bytes of a value.
|
||||
pub fn value(&self, p: &Prop) -> Result<Vec<u8>, String> {
|
||||
pub(crate) fn value(&self, p: &Prop) -> Result<Vec<u8>, String> {
|
||||
let total = p.segs.iter().try_fold(0usize, |total, s| {
|
||||
let len = match s {
|
||||
Seg::Imm(b) => b.len(),
|
||||
|
||||
+340
-10
@@ -12,6 +12,10 @@ type Uid = [u8; 12];
|
||||
|
||||
const MAX_REFERENCE_VISITS: usize = 65_536;
|
||||
|
||||
/// A source-clip search stopped at its work limit.
|
||||
#[derive(Clone, Copy)]
|
||||
struct SearchLimit;
|
||||
|
||||
struct R<'a, 'b> {
|
||||
c: &'b Container<'a>,
|
||||
report: &'b mut Report,
|
||||
@@ -21,15 +25,49 @@ struct R<'a, 'b> {
|
||||
media: HashMap<Uid, Vec<u8>>,
|
||||
doc: Document,
|
||||
sources: HashMap<(Uid, u32, String), usize>,
|
||||
/// Component visits left for [`R::segment`] over the whole document.
|
||||
budget: usize,
|
||||
/// Outcome of the source-clip search under each segment: deterministic, so it is made once.
|
||||
clip_search: HashMap<u32, std::result::Result<Option<u32>, SearchLimit>>,
|
||||
/// Visits and queued references left for source-clip searches over the whole document:
|
||||
/// distinct roots over one shared graph would otherwise each search it in full.
|
||||
search_budget: usize,
|
||||
}
|
||||
|
||||
/// Components one document may expand into timeline items; a small file can reference the same
|
||||
/// sequence from many places and would otherwise multiply without bound.
|
||||
const MAX_COMPONENT_VISITS: usize = 1_000_000;
|
||||
|
||||
/// Reported when the shared component budget runs out.
|
||||
const BUDGET_WARNING: &str = "OMF composition is too large to import completely (the remaining components are dropped)";
|
||||
/// Most visited components, and most queued references, one source-clip search may spend.
|
||||
const MAX_CLIP_SEARCH_VISITS: usize = 100_000;
|
||||
const MAX_CLIP_SEARCH_PENDING: usize = 1_000_000;
|
||||
/// Work all source-clip searches in one document may spend together.
|
||||
const MAX_DOCUMENT_SEARCH_WORK: usize = 2_000_000;
|
||||
|
||||
pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
read_with_budget(bytes, report, MAX_COMPONENT_VISITS)
|
||||
}
|
||||
|
||||
fn read_with_budget(bytes: &[u8], report: &mut Report, budget: usize) -> Result<Document> {
|
||||
let perr = |m: String| Error::Parse { format: "OMF", message: m };
|
||||
let c = Container::open(bytes).map_err(perr)?;
|
||||
let class_of = |o: u32| c.get(o, "OMFI:OOBJ:ObjClass").and_then(|v| v.get(..4).map(|b| [b[0], b[1], b[2], b[3]]));
|
||||
let head = c.objects.keys().copied().find(|&o| class_of(o) == Some(*b"HEAD")).ok_or_else(|| perr("no OMF header object".into()))?;
|
||||
let be = c.get(head, "OMFI:HEAD:ByteOrder").is_none_or(|v| v.first() != Some(&0x49));
|
||||
let mut r = R { c: &c, report, be, mobs: HashMap::new(), media: HashMap::new(), doc: Document::default(), sources: HashMap::new() };
|
||||
let mut r = R {
|
||||
c: &c,
|
||||
report,
|
||||
be,
|
||||
mobs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
doc: Document::default(),
|
||||
sources: HashMap::new(),
|
||||
budget,
|
||||
clip_search: HashMap::new(),
|
||||
search_budget: MAX_DOCUMENT_SEARCH_WORK,
|
||||
};
|
||||
let mob_list = r.refs(head, "OMFI:HEAD:Mobs");
|
||||
let mob_list = if mob_list.is_empty() {
|
||||
c.objects.keys().copied().filter(|&o| matches!(class_of(o).as_ref(), Some(b"CMOB" | b"MMOB" | b"SMOB"))).collect()
|
||||
@@ -66,8 +104,14 @@ pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
cmobs.iter().copied().filter(|m| r.uid(*m, "OMFI:MOBJ:MobID").is_none_or(|id| !referenced.contains(&id))).collect()
|
||||
};
|
||||
for m in top {
|
||||
// every composition spends from the budget too: a list of up to 65,535 repeated entries must not yield that many
|
||||
if r.budget == 0 && !r.doc.compositions.is_empty() {
|
||||
r.report.warn(BUDGET_WARNING);
|
||||
break;
|
||||
}
|
||||
let comp = r.composition(m);
|
||||
r.doc.compositions.push(comp);
|
||||
r.budget = r.budget.saturating_sub(1);
|
||||
}
|
||||
if r.doc.compositions.is_empty() {
|
||||
return Err(Error::Empty);
|
||||
@@ -166,6 +210,13 @@ impl R<'_, '_> {
|
||||
let slots = self.refs(m, "OMFI:MOBJ:Slots");
|
||||
let mut na = 0;
|
||||
for s in slots {
|
||||
if self.budget == 0 {
|
||||
self.report.warn(BUDGET_WARNING);
|
||||
break;
|
||||
}
|
||||
// Every slot spends a unit, skipped ones too: a list of up to 65,535 repeated slots
|
||||
// must not cost that much work in each of 65,535 repeated compositions.
|
||||
self.budget -= 1;
|
||||
let Some(seg) = self.r#ref(s, "OMFI:MSLT:Segment") else { continue };
|
||||
let rate = self.rational(s, "OMFI:MSLT:EditRate").filter(|r| r.0 > 0 && r.1 > 0).unwrap_or((25, 1));
|
||||
let kind = self.data_kind(seg);
|
||||
@@ -213,13 +264,32 @@ impl R<'_, '_> {
|
||||
|
||||
fn segment(&mut self, seg: u32, rate: (i64, i64), depth: usize) -> Vec<CItem> {
|
||||
let len = units_to_ticks(self.len(seg), rate.0, rate.1);
|
||||
// Depth alone does not bound a graph that references the same component many times at
|
||||
// every level, so each visit also spends from a per-document budget.
|
||||
// A visit stopped by depth spends from it too.
|
||||
if self.budget == 0 {
|
||||
self.report.warn(BUDGET_WARNING);
|
||||
return vec![CItem::Filler(len)];
|
||||
}
|
||||
self.budget -= 1;
|
||||
if depth > 32 {
|
||||
return vec![CItem::Filler(len)];
|
||||
}
|
||||
match &self.class(seg) {
|
||||
b"SEQU" => {
|
||||
let mut out = Vec::new();
|
||||
let mut expanded = 0i64;
|
||||
for c in self.refs(seg, "OMFI:SEQU:Components") {
|
||||
if self.budget == 0 {
|
||||
// the rest of the sequence keeps its duration as one gap, not a gap per reference
|
||||
self.report.warn(BUDGET_WARNING);
|
||||
let rest = self.len(seg).saturating_sub(expanded);
|
||||
if rest > 0 {
|
||||
out.push(CItem::Filler(units_to_ticks(rest, rate.0, rate.1)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
expanded = expanded.saturating_add(self.len(c));
|
||||
out.extend(self.segment(c, rate, depth + 1));
|
||||
}
|
||||
out
|
||||
@@ -306,12 +376,66 @@ impl R<'_, '_> {
|
||||
.find(|&s| self.r#ref(s, "OMFI:MSLT:TrackDesc").and_then(|t| self.u32(t, "OMFI:TRKD:TrackID")) == Some(track))
|
||||
}
|
||||
|
||||
fn first_clip(&self, seg: u32) -> Option<u32> {
|
||||
match &self.class(seg) {
|
||||
b"SCLP" => Some(seg),
|
||||
b"SEQU" => self.refs(seg, "OMFI:SEQU:Components").into_iter().find_map(|c| self.first_clip(c)),
|
||||
_ => None,
|
||||
/// First source clip under `seg`, depth-first; a sequence referencing itself (directly or
|
||||
/// through a cycle) ends the search rather than the stack. `Err` when the search spent its
|
||||
/// budget of visited components or queued references before finishing (the answer is unknown).
|
||||
fn first_clip(&mut self, seg: u32) -> std::result::Result<Option<u32>, SearchLimit> {
|
||||
if let Some(found) = self.clip_search.get(&seg) {
|
||||
return *found;
|
||||
}
|
||||
let mut spent = 0;
|
||||
let found = if self.search_budget == 0 {
|
||||
Err(SearchLimit)
|
||||
} else {
|
||||
let visits = MAX_CLIP_SEARCH_VISITS.min(self.search_budget);
|
||||
let pending = MAX_CLIP_SEARCH_PENDING.min(self.search_budget);
|
||||
self.first_clip_within(seg, visits, pending, &mut spent)
|
||||
};
|
||||
self.search_budget = self.search_budget.saturating_sub(spent);
|
||||
self.clip_search.insert(seg, found);
|
||||
found
|
||||
}
|
||||
|
||||
/// `spent` receives the visits plus queued references the search used.
|
||||
fn first_clip_within(&self, seg: u32, max_visits: usize, max_pending: usize, spent: &mut usize) -> std::result::Result<Option<u32>, SearchLimit> {
|
||||
let mut visited = HashSet::new();
|
||||
let mut todo = vec![seg];
|
||||
let mut queued = 1usize;
|
||||
let found = Self::search_from(self, &mut visited, &mut todo, &mut queued, max_visits, max_pending);
|
||||
*spent = visited.len().saturating_add(queued);
|
||||
found
|
||||
}
|
||||
|
||||
fn search_from(
|
||||
&self,
|
||||
visited: &mut HashSet<u32>,
|
||||
todo: &mut Vec<u32>,
|
||||
queued: &mut usize,
|
||||
max_visits: usize,
|
||||
max_pending: usize,
|
||||
) -> std::result::Result<Option<u32>, SearchLimit> {
|
||||
while let Some(seg) = todo.pop() {
|
||||
if !visited.insert(seg) {
|
||||
continue;
|
||||
}
|
||||
if visited.len() > max_visits {
|
||||
return Err(SearchLimit);
|
||||
}
|
||||
match &self.class(seg) {
|
||||
b"SCLP" => return Ok(Some(seg)),
|
||||
b"SEQU" => {
|
||||
let comps = self.refs(seg, "OMFI:SEQU:Components");
|
||||
// sequences may share one component list, so queued references grow with expansions, not visits
|
||||
*queued = queued.saturating_add(comps.len());
|
||||
if *queued > max_pending {
|
||||
return Err(SearchLimit);
|
||||
}
|
||||
todo.extend(comps.into_iter().rev());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn resolve(&mut self, id: Uid, track: u32, start: Tick, master: Option<String>, depth: usize) -> Option<(usize, Tick)> {
|
||||
@@ -328,7 +452,13 @@ impl R<'_, '_> {
|
||||
match &self.class(mob) {
|
||||
b"MMOB" => {
|
||||
let key = master.unwrap_or_else(|| id.iter().map(|b| format!("{b:02x}")).collect());
|
||||
let clip = self.first_clip(seg)?;
|
||||
let clip = match self.first_clip(seg) {
|
||||
Ok(c) => c?,
|
||||
Err(SearchLimit) => {
|
||||
self.report.warn("a master mob is too large to search for its source clip (imported as a gap)");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let inner = units_to_ticks(self.i32(clip, "OMFI:SCLP:StartTime").unwrap_or(0), rate.0, rate.1);
|
||||
let next = self.uid(clip, "OMFI:SCLP:SourceID")?;
|
||||
let next_track = self.u32(clip, "OMFI:SCLP:SourceTrackID")?;
|
||||
@@ -353,7 +483,9 @@ impl R<'_, '_> {
|
||||
let file_rate = sr.filter(|r| r.0 > 0 && r.1 > 0).unwrap_or(rate);
|
||||
let length = units_to_ticks(self.i32(desc, "OMFI:MDFL:Length").unwrap_or(0), file_rate.0, file_rate.1);
|
||||
let summary = self.c.get(desc, "OMFI:WAVD:Summary").or_else(|| self.c.get(desc, "OMFI:AIFD:Summary")).unwrap_or_default();
|
||||
let fmt = crate::pcm::parse_wav(&summary).or_else(|| crate::pcm::parse_aiff(&summary)).map(|(_, ch, sr, bits)| (ch as u32, sr, bits));
|
||||
let fmt = craft_audio::essence_pcm::parse_wav(&summary)
|
||||
.or_else(|| craft_audio::essence_pcm::parse_aiff(&summary))
|
||||
.map(|(_, ch, sr, bits)| (ch as u32, sr, bits));
|
||||
let mut path = None;
|
||||
for l in self.refs(desc, "OMFI:MDES:Locator") {
|
||||
let p = match &self.class(l) {
|
||||
@@ -373,7 +505,8 @@ impl R<'_, '_> {
|
||||
let (file_channels, sample_rate, bits) = fmt.unwrap_or((1, file_rate.0 as u32, 16));
|
||||
let channel = (file_channels > 1 && sound_slots > 1).then(|| track.saturating_sub(1).min(file_channels - 1));
|
||||
// timecode of the physical source
|
||||
let tc = self.first_clip(seg).and_then(|clip| {
|
||||
let first = self.first_clip(seg).ok().flatten();
|
||||
let tc = first.and_then(|clip| {
|
||||
let tid = self.uid(clip, "OMFI:SCLP:SourceID")?;
|
||||
let tape = *self.mobs.get(&tid)?;
|
||||
let (start_tc, fps, trate) = self.refs(tape, "OMFI:MOBJ:Slots").into_iter().find_map(|s| {
|
||||
@@ -415,7 +548,7 @@ impl R<'_, '_> {
|
||||
nested: None,
|
||||
};
|
||||
if let Some(d) = self.media.get(&id) {
|
||||
if let Some((pcm, ch, sr, b)) = crate::pcm::parse_wav(d).or_else(|| crate::pcm::parse_aiff(d)) {
|
||||
if let Some((pcm, ch, sr, b)) = craft_audio::essence_pcm::parse_wav(d).or_else(|| craft_audio::essence_pcm::parse_aiff(d)) {
|
||||
s.embedded = Some(pcm);
|
||||
s.file_channels = ch as u32;
|
||||
s.channels = if s.channel.is_some() { 1 } else { ch as u32 };
|
||||
@@ -437,3 +570,200 @@ impl R<'_, '_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::bento::Writer;
|
||||
use super::*;
|
||||
|
||||
fn obj(w: &mut Writer, class: &[u8; 4]) -> u32 {
|
||||
let id = w.new_object();
|
||||
w.set(id, "OMFI:OOBJ:ObjClass", "omfi:ClassID", class);
|
||||
id
|
||||
}
|
||||
fn list(w: &mut Writer, o: u32, p: &str, targets: &[u32]) {
|
||||
let mut v = (targets.len() as u16).to_be_bytes().to_vec();
|
||||
for t in targets {
|
||||
v.extend_from_slice(&t.to_be_bytes());
|
||||
}
|
||||
w.set(o, p, "omfi:ObjRefArray", &v);
|
||||
}
|
||||
|
||||
fn search(max_visits: usize, max_pending: usize, build: impl FnOnce(&mut Writer) -> u32) -> std::result::Result<Option<u32>, SearchLimit> {
|
||||
let mut w = Writer::new();
|
||||
let root = build(&mut w);
|
||||
let bytes = w.finish();
|
||||
let c = Container::open(&bytes).unwrap();
|
||||
let mut report = Report::default();
|
||||
let r = R {
|
||||
c: &c,
|
||||
report: &mut report,
|
||||
be: true,
|
||||
mobs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
doc: Document::default(),
|
||||
sources: HashMap::new(),
|
||||
budget: 0,
|
||||
clip_search: HashMap::new(),
|
||||
search_budget: MAX_DOCUMENT_SEARCH_WORK,
|
||||
};
|
||||
r.first_clip_within(root, max_visits, max_pending, &mut 0)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clip_search_reports_its_limits() {
|
||||
// found within the limits
|
||||
assert!(matches!(
|
||||
search(10, 10, |w| {
|
||||
let (s, c) = (obj(w, b"SEQU"), obj(w, b"SCLP"));
|
||||
list(w, s, "OMFI:SEQU:Components", &[c]);
|
||||
s
|
||||
}),
|
||||
Ok(Some(_))
|
||||
));
|
||||
// too many distinct components before any clip
|
||||
assert!(
|
||||
search(5, 1000, |w| {
|
||||
let (s, f) = (obj(w, b"SEQU"), (0..20).map(|_| obj(w, b"FILL")).collect::<Vec<_>>());
|
||||
list(w, s, "OMFI:SEQU:Components", &f);
|
||||
s
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
// few visits, but sequences that each queue a long list of references
|
||||
assert!(
|
||||
search(1000, 500, |w| {
|
||||
let f = obj(w, b"FILL");
|
||||
let inner: Vec<u32> = (0..20).map(|_| obj(w, b"SEQU")).collect();
|
||||
for &i in &inner {
|
||||
list(w, i, "OMFI:SEQU:Components", &[f; 100]);
|
||||
}
|
||||
let s = obj(w, b"SEQU");
|
||||
list(w, s, "OMFI:SEQU:Components", &inner);
|
||||
s
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
// a search that ends without a clip is not a limit
|
||||
assert!(matches!(search(10, 10, |w| obj(w, b"FILL")), Ok(None)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skipped_slots_spend_the_budget() {
|
||||
let mut w = Writer::new();
|
||||
let head = obj(&mut w, b"HEAD");
|
||||
let cm = obj(&mut w, b"CMOB");
|
||||
let slot = obj(&mut w, b"MSLT");
|
||||
list(&mut w, cm, "OMFI:MOBJ:Slots", &[slot; 200]);
|
||||
list(&mut w, head, "OMFI:HEAD:Mobs", &[cm]);
|
||||
list(&mut w, head, "OMFI:HEAD:PrimaryMobs", &[cm]);
|
||||
let bytes = w.finish();
|
||||
let mut report = Report::default();
|
||||
read_with_budget(&bytes, &mut report, 50).unwrap();
|
||||
assert!(report.mentions(BUDGET_WARNING), "slots with no segment cost nothing before");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_source_clip_search_is_made_once_per_segment() {
|
||||
let mut w = Writer::new();
|
||||
let (s, c) = (obj(&mut w, b"SEQU"), obj(&mut w, b"SCLP"));
|
||||
list(&mut w, s, "OMFI:SEQU:Components", &[c]);
|
||||
let bytes = w.finish();
|
||||
let c = Container::open(&bytes).unwrap();
|
||||
let mut report = Report::default();
|
||||
let mut r = R {
|
||||
c: &c,
|
||||
report: &mut report,
|
||||
be: true,
|
||||
mobs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
doc: Document::default(),
|
||||
sources: HashMap::new(),
|
||||
budget: 0,
|
||||
clip_search: HashMap::new(),
|
||||
search_budget: MAX_DOCUMENT_SEARCH_WORK,
|
||||
};
|
||||
let first = r.first_clip(s).ok();
|
||||
assert!(matches!(first, Some(Some(_))));
|
||||
assert_eq!(r.clip_search.len(), 1);
|
||||
assert_eq!(r.first_clip(s).ok(), first);
|
||||
assert_eq!(r.clip_search.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_slots_and_compositions_spend_the_budget() {
|
||||
let mut w = Writer::new();
|
||||
let head = obj(&mut w, b"HEAD");
|
||||
let cm = obj(&mut w, b"CMOB");
|
||||
let dd = obj(&mut w, b"DDEF");
|
||||
w.set(dd, "OMFI:DDEF:DataKindID", "omfi:UniqueName", b"omfi:data:Sound\0");
|
||||
let fill = obj(&mut w, b"FILL");
|
||||
w.set(fill, "OMFI:CPNT:DataKind", "omfi:ObjRef", &dd.to_be_bytes());
|
||||
let slot = obj(&mut w, b"MSLT");
|
||||
w.set(slot, "OMFI:MSLT:Segment", "omfi:ObjRef", &fill.to_be_bytes());
|
||||
list(&mut w, cm, "OMFI:MOBJ:Slots", &[slot; 200]);
|
||||
list(&mut w, head, "OMFI:HEAD:Mobs", &[cm]);
|
||||
list(&mut w, head, "OMFI:HEAD:PrimaryMobs", &[cm; 200]);
|
||||
let bytes = w.finish();
|
||||
let mut report = Report::default();
|
||||
let doc = read_with_budget(&bytes, &mut report, 50).unwrap();
|
||||
let tracks: usize = doc.compositions.iter().map(|c| c.tracks.len()).sum();
|
||||
assert!(tracks <= 50, "{tracks} tracks from a budget of 50");
|
||||
assert!(doc.compositions.len() <= 2, "{} compositions", doc.compositions.len());
|
||||
assert!(report.has_warnings());
|
||||
}
|
||||
|
||||
fn with_reader<T>(bytes: &[u8], budget: usize, search_budget: usize, f: impl FnOnce(&mut R) -> T) -> (T, Report) {
|
||||
let c = Container::open(bytes).unwrap();
|
||||
let mut report = Report::default();
|
||||
let mut r = R {
|
||||
c: &c,
|
||||
report: &mut report,
|
||||
be: true,
|
||||
mobs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
doc: Document::default(),
|
||||
sources: HashMap::new(),
|
||||
budget,
|
||||
clip_search: HashMap::new(),
|
||||
search_budget,
|
||||
};
|
||||
let out = f(&mut r);
|
||||
(out, report)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn depth_limited_components_spend_the_budget() {
|
||||
// a chain 33 sequences deep whose last one repeats a depth-limited sequence many times
|
||||
let mut w = Writer::new();
|
||||
let deep = obj(&mut w, b"SEQU");
|
||||
let chain: Vec<u32> = (0..33).map(|_| obj(&mut w, b"SEQU")).collect();
|
||||
for pair in chain.windows(2) {
|
||||
list(&mut w, pair[0], "OMFI:SEQU:Components", &[pair[1]]);
|
||||
}
|
||||
list(&mut w, chain[32], "OMFI:SEQU:Components", &[deep; 1000]);
|
||||
let bytes = w.finish();
|
||||
let (items, report) = with_reader(&bytes, 100, MAX_DOCUMENT_SEARCH_WORK, |r| r.segment(chain[0], (48_000, 1), 0));
|
||||
assert!(items.len() <= 100, "{} items from a budget of 100", items.len());
|
||||
assert!(report.mentions(BUDGET_WARNING));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_clip_searches_share_a_document_budget() {
|
||||
// distinct roots over one shared clip-free graph
|
||||
let mut w = Writer::new();
|
||||
let fills: Vec<u32> = (0..50).map(|_| obj(&mut w, b"FILL")).collect();
|
||||
let shared = obj(&mut w, b"SEQU");
|
||||
list(&mut w, shared, "OMFI:SEQU:Components", &fills);
|
||||
let roots: Vec<u32> = (0..30).map(|_| obj(&mut w, b"SEQU")).collect();
|
||||
for &root in &roots {
|
||||
list(&mut w, root, "OMFI:SEQU:Components", &[shared]);
|
||||
}
|
||||
let bytes = w.finish();
|
||||
let (results, _) = with_reader(&bytes, 0, 500, |r| roots.iter().map(|&root| r.first_clip(root)).collect::<Vec<_>>());
|
||||
assert!(matches!(results[0], Ok(None)), "the first search fits");
|
||||
assert!(results.iter().any(|r| r.is_err()), "later searches run out of the shared budget");
|
||||
let (results, _) = with_reader(&bytes, 0, MAX_DOCUMENT_SEARCH_WORK, |r| roots.iter().map(|&root| r.first_clip(root)).collect::<Vec<_>>());
|
||||
assert!(results.iter().all(|r| matches!(r, Ok(None))));
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,3 @@
|
||||
pub(crate) fn parse_wav(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
|
||||
craft_audio::wav_pcm(b).map(|p| (p.data, p.channels, p.sample_rate, p.bits))
|
||||
}
|
||||
|
||||
/// The same for an AIFF / AIFF-C (`NONE`, `twos`, `sowt`) file.
|
||||
pub(crate) fn parse_aiff(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
|
||||
craft_audio::aiff_pcm(b).map(|p| (p.data, p.channels, p.sample_rate, p.bits))
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
use craft_aaf::omf::bento::{Container, Writer, sniff};
|
||||
|
||||
#[test]
|
||||
fn external_consumer_reads_named_values_and_references() {
|
||||
let mut writer = Writer::new();
|
||||
let first = writer.new_object();
|
||||
let second = writer.new_object();
|
||||
writer.set(first, "test:value", "test:type", b"long value");
|
||||
writer.set_references(first, &[second]);
|
||||
let bytes = writer.finish();
|
||||
assert!(sniff(&bytes));
|
||||
let container = Container::open(&bytes).unwrap();
|
||||
assert!(container.object_ids().any(|id| id == first));
|
||||
assert_eq!(container.get(first, "test:value").as_deref(), Some(b"long value".as_slice()));
|
||||
assert_eq!(container.resolve(first, second), second);
|
||||
assert!(Container::open(&bytes[..bytes.len() - 1]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_writer_builds_a_readable_empty_container() -> Result<(), String> {
|
||||
let bytes = Writer::default().finish();
|
||||
let container = Container::open(&bytes)?;
|
||||
let ids: Vec<_> = container.object_ids().collect();
|
||||
assert!(!ids.is_empty());
|
||||
assert!(ids.windows(2).all(|pair| pair[0] < pair[1]));
|
||||
Ok(())
|
||||
}
|
||||
@@ -679,7 +679,11 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> {
|
||||
}
|
||||
let class = s.class(id).unwrap_or(Tag::of(b"ShpN"));
|
||||
let (path, even_odd) = crate::geometry::outline(self, id, class, world).unwrap_or_else(|| {
|
||||
self.warn("an artboard whose outline could not be read (clipped to its rectangle)");
|
||||
if class == Tag::of(b"PCrv") {
|
||||
self.warn("a converted artboard outline could not be read (clipped to its rectangle)");
|
||||
} else {
|
||||
self.warn("an artboard whose outline could not be read (clipped to its rectangle)");
|
||||
}
|
||||
(Path { subpaths: vec![crate::shapes::rectangle(b.x0, b.y0, b.x1, b.y1)] }, false)
|
||||
});
|
||||
let (background, strokes) = paint::node_paint(self, id, world);
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "craft-anicraft-format"
|
||||
description = "Versioned JSON envelope codec for AniCraft native documents"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
rust-version.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
//! The versioned JSON envelope used by AniCraft's native project file.
|
||||
//!
|
||||
//! The app owns its document model and the path-table scope used while serializing it. This
|
||||
//! crate owns the format envelope: field order, duplicate-field handling, and the point at which
|
||||
//! the caller decodes the document relative to the `paths` field.
|
||||
//!
|
||||
//! [`read`] passes the actual nested Serde deserializer to the caller's [`PayloadDecoder`]. That
|
||||
//! keeps payload parsing in the app's model crate while preserving the original stream order,
|
||||
//! global JSON error locations, and path-table guard lifetime.
|
||||
|
||||
use serde::{
|
||||
Serialize,
|
||||
de::{Deserialize, DeserializeSeed, MapAccess, Visitor},
|
||||
};
|
||||
use std::{fmt, marker::PhantomData};
|
||||
|
||||
/// The native project envelope's identifying format string.
|
||||
pub const FORMAT_NAME: &str = "anicraft";
|
||||
/// Current envelope version (v1 inline paths; v2 shared path table; v3 named scene timelines).
|
||||
pub const FORMAT_VERSION: u32 = 3;
|
||||
/// The largest native project file AniCraft accepts.
|
||||
pub const MAX_FILE_BYTES: u64 = 1024 * 1024 * 1024;
|
||||
/// Files below this size are pretty-printed for diffs and manual inspection.
|
||||
pub const PRETTY_LIMIT: usize = 1024 * 1024;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct EnvelopeOut<'a, P, T> {
|
||||
format: &'a str,
|
||||
version: u32,
|
||||
generator: &'a str,
|
||||
paths: &'a [P],
|
||||
document: &'a T,
|
||||
}
|
||||
|
||||
/// Serializes the envelope in its stable wire order and uses AniCraft's compact/pretty threshold.
|
||||
pub fn write<P: Serialize, T: Serialize>(generator: &str, paths: &[P], document: &T) -> Result<String, serde_json::Error> {
|
||||
let envelope = EnvelopeOut { format: FORMAT_NAME, version: FORMAT_VERSION, generator, paths, document };
|
||||
let compact = serde_json::to_string(&envelope)?;
|
||||
if compact.len() < PRETTY_LIMIT { serde_json::to_string_pretty(&envelope) } else { Ok(compact) }
|
||||
}
|
||||
|
||||
/// Decodes one native-document payload under the path table that preceded it in the envelope.
|
||||
pub trait PayloadDecoder<P> {
|
||||
type Document;
|
||||
|
||||
fn decode<'de, D>(&mut self, deserializer: D, paths: Option<Vec<P>>) -> Result<Self::Document, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>;
|
||||
}
|
||||
|
||||
/// The parsed top-level format, version and document payload.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Envelope<T> {
|
||||
pub format: String,
|
||||
pub version: u32,
|
||||
/// Missing is distinct from JSON `null`, which the payload decoder decides whether to accept.
|
||||
pub document: Option<T>,
|
||||
}
|
||||
|
||||
struct DocumentSeed<'a, P, D> {
|
||||
decoder: &'a mut D,
|
||||
paths: Option<Vec<P>>,
|
||||
}
|
||||
|
||||
impl<'de, P, D> DeserializeSeed<'de> for DocumentSeed<'_, P, D>
|
||||
where
|
||||
D: PayloadDecoder<P>,
|
||||
{
|
||||
type Value = D::Document;
|
||||
|
||||
fn deserialize<De>(self, deserializer: De) -> Result<Self::Value, De::Error>
|
||||
where
|
||||
De: serde::Deserializer<'de>,
|
||||
{
|
||||
self.decoder.decode(deserializer, self.paths)
|
||||
}
|
||||
}
|
||||
|
||||
struct EnvelopeSeed<'a, P, D> {
|
||||
decoder: &'a mut D,
|
||||
marker: PhantomData<fn() -> P>,
|
||||
}
|
||||
|
||||
impl<'de, P, D> DeserializeSeed<'de> for EnvelopeSeed<'_, P, D>
|
||||
where
|
||||
P: Deserialize<'de> + Clone,
|
||||
D: PayloadDecoder<P>,
|
||||
{
|
||||
type Value = Envelope<D::Document>;
|
||||
|
||||
fn deserialize<De>(self, deserializer: De) -> Result<Self::Value, De::Error>
|
||||
where
|
||||
De: serde::Deserializer<'de>,
|
||||
{
|
||||
struct EnvelopeVisitor<'a, P, D> {
|
||||
decoder: &'a mut D,
|
||||
marker: PhantomData<fn() -> P>,
|
||||
}
|
||||
|
||||
impl<'de, P, D> Visitor<'de> for EnvelopeVisitor<'_, P, D>
|
||||
where
|
||||
P: Deserialize<'de> + Clone,
|
||||
D: PayloadDecoder<P>,
|
||||
{
|
||||
type Value = Envelope<D::Document>;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
formatter.write_str("an .anicraft envelope object")
|
||||
}
|
||||
|
||||
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: MapAccess<'de>,
|
||||
{
|
||||
let (mut format, mut version, mut paths, mut document) = (None, None, None, None);
|
||||
while let Some(key) = map.next_key::<String>()? {
|
||||
match key.as_str() {
|
||||
"format" => format = Some(map.next_value::<String>()?),
|
||||
"version" => version = Some(map.next_value::<u32>()?),
|
||||
"paths" => paths = Some(map.next_value::<Vec<P>>()?),
|
||||
"document" => {
|
||||
document = Some(map.next_value_seed(DocumentSeed { decoder: self.decoder, paths: paths.clone() })?);
|
||||
}
|
||||
_ => {
|
||||
map.next_value::<serde::de::IgnoredAny>()?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Envelope {
|
||||
format: format.ok_or_else(|| serde::de::Error::missing_field("format"))?,
|
||||
version: version.ok_or_else(|| serde::de::Error::missing_field("version"))?,
|
||||
document,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.deserialize_map(EnvelopeVisitor { decoder: self.decoder, marker: PhantomData })
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads the envelope while delegating its typed document payload to `decoder`.
|
||||
///
|
||||
/// Unknown top-level fields are ignored. Repeated recognized keys use the last successful value,
|
||||
/// and each `document` is decoded immediately with the latest preceding `paths` field. A
|
||||
/// malformed earlier `document` therefore errors before a later duplicate is considered, just as
|
||||
/// it does with a streaming Serde visitor.
|
||||
pub fn read<'de, P, D>(json: &'de str, decoder: &mut D) -> Result<Envelope<D::Document>, serde_json::Error>
|
||||
where
|
||||
P: Deserialize<'de> + Clone,
|
||||
D: PayloadDecoder<P>,
|
||||
{
|
||||
let mut deserializer = serde_json::Deserializer::from_str(json);
|
||||
let envelope = EnvelopeSeed { decoder, marker: PhantomData }.deserialize(&mut deserializer)?;
|
||||
deserializer.end()?;
|
||||
Ok(envelope)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, PartialEq)]
|
||||
struct Document {
|
||||
value: u32,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct CaptureDocument {
|
||||
calls: usize,
|
||||
}
|
||||
|
||||
impl PayloadDecoder<String> for CaptureDocument {
|
||||
type Document = (Document, Option<Vec<String>>);
|
||||
|
||||
fn decode<'de, D>(&mut self, deserializer: D, paths: Option<Vec<String>>) -> Result<Self::Document, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
self.calls += 1;
|
||||
Ok((Document::deserialize(deserializer)?, paths))
|
||||
}
|
||||
}
|
||||
|
||||
struct ValueDocument;
|
||||
|
||||
impl PayloadDecoder<String> for ValueDocument {
|
||||
type Document = (Value, Option<Vec<String>>);
|
||||
|
||||
fn decode<'de, D>(&mut self, deserializer: D, paths: Option<Vec<String>>) -> Result<Self::Document, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
Ok((Value::deserialize(deserializer)?, paths))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writer_keeps_the_versioned_field_order_and_pretty_threshold() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let paths = ["path-a", "path-b"];
|
||||
let document = Document { value: 3 };
|
||||
let json = write("AniCraft 0.1", &paths, &document)?;
|
||||
let keys = ["\"format\"", "\"version\"", "\"generator\"", "\"paths\"", "\"document\""];
|
||||
let offsets: Vec<usize> = keys.iter().map(|key| json.find(key).unwrap_or(usize::MAX)).collect();
|
||||
assert!(offsets.windows(2).all(|pair| pair[0] < pair[1]));
|
||||
assert!(json.contains("\n \"format\""), "small envelopes stay human-readable");
|
||||
assert!(json.contains("\"format\": \"anicraft\""));
|
||||
assert!(json.contains("\"version\": 3"));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_fields_and_paths_before_document_keep_streaming_semantics() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let json = r#"{"format":"old","format":"anicraft","version":1,"paths":["first"],"paths":["chosen"],"document":{"value":7},"ignored":true}"#;
|
||||
let mut decoder = CaptureDocument::default();
|
||||
let envelope = read::<String, _>(json, &mut decoder)?;
|
||||
assert_eq!(envelope.format, "anicraft");
|
||||
assert_eq!(envelope.version, 1);
|
||||
assert_eq!(envelope.document, Some((Document { value: 7 }, Some(vec!["chosen".to_string()]))));
|
||||
assert_eq!(decoder.calls, 1);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paths_after_document_do_not_retroactively_change_its_scope() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let json = r#"{"format":"anicraft","version":1,"document":{"value":1},"paths":["later"]}"#;
|
||||
let mut decoder = ValueDocument;
|
||||
let envelope = read::<String, _>(json, &mut decoder)?;
|
||||
assert_eq!(envelope.document, Some((serde_json::json!({"value":1}), None)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_earlier_duplicate_document_stops_before_later_document() {
|
||||
let json = r#"{"format":"anicraft","version":1,"document":null,"document":{"value":2}}"#;
|
||||
let mut decoder = CaptureDocument::default();
|
||||
let result = read::<String, _>(json, &mut decoder);
|
||||
assert!(result.is_err());
|
||||
assert_eq!(decoder.calls, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_top_level_keeps_the_native_envelope_diagnostic() {
|
||||
let mut decoder = CaptureDocument::default();
|
||||
let error = read::<String, _>("[]", &mut decoder).unwrap_err();
|
||||
assert!(error.to_string().contains("expected an .anicraft envelope object"));
|
||||
assert_eq!(decoder.calls, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_documents_use_the_last_successful_value() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let json = r#"{"format":"anicraft","version":1,"document":{"value":1},"document":{"value":2}}"#;
|
||||
let mut decoder = CaptureDocument::default();
|
||||
let envelope = read::<String, _>(json, &mut decoder)?;
|
||||
assert_eq!(envelope.document, Some((Document { value: 2 }, None)));
|
||||
assert_eq!(decoder.calls, 2);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
+29
-7
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "craft-audio"
|
||||
description = "Audio file I/O and codecs: WAV/AIFF read and write, FLAC encoder, AAC and ALAC decoders and encoders, AC-3/E-AC-3 decoder and encoder, MP3 decoder and encoder, Opus and MP1/MP2 decoders."
|
||||
description = "Audio file I/O and codecs: WAV/AIFF read and write, FLAC encoder, AAC and ALAC decoders and encoders, AC-3/E-AC-3 decoder and encoder, MP3 decoder and encoder, Opus decoder and encoder, Vorbis encoder, MP1/MP2 decoders."
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
@@ -19,28 +19,41 @@ workspace = true
|
||||
# µ-law and free-format MP3 through symphonia.
|
||||
decode = ["decode-native", "symphonia"]
|
||||
# `decode` without symphonia: only the formats with a native decoder (others are Unsupported).
|
||||
decode-native = ["aac-decode", "alac-decode", "opus-decode", "ac3-decode", "mpa-decode", "mp3-decode", "craft-media/isobmff", "craft-media/matroska", "craft-media/ogg", "dep:log"]
|
||||
decode-native = ["aac-decode", "alac-decode", "opus-decode", "ac3-decode", "mpa-decode", "mp3-decode", "craft-media/isobmff", "craft-media/matroska", "craft-media/ogg", "dep:log", "dep:craft-xml"]
|
||||
# The symphonia fallback for the formats with no native decoder yet.
|
||||
symphonia = ["decode-native", "dep:symphonia"]
|
||||
# Every encoder: the lossless file writers and the AAC, ALAC, AC-3/E-AC-3 and MP3 encoders.
|
||||
encode = ["lossless-encode", "aac-encode", "alac-encode", "ac3-encode", "eac3-encode", "mp3-encode"]
|
||||
# Every encoder: the lossless file writers and the AAC, ALAC, AC-3/E-AC-3, MP3 and Opus encoders.
|
||||
# All but Opus are also file formats of `encode` and `FileEncoder`.
|
||||
encode = ["lossless-encode", "aac-encode", "alac-encode", "ac3-encode", "eac3-encode", "mp3-encode", "opus-encode", "vorbis-encode"]
|
||||
# Encode WAV/BWF/RF64, AIFF/AIFF-C and FLAC files.
|
||||
lossless-encode = ["pcm-encode", "flac-encode"]
|
||||
# Encode WAV/BWF/RF64 and AIFF/AIFF-C files.
|
||||
pcm-encode = []
|
||||
# Decode FLAC files and frames natively (`flac::decode`, `flac::Decoder`).
|
||||
flac-decode = []
|
||||
# Encode FLAC files.
|
||||
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"]
|
||||
# Codec modules on their own, for packet-level use.
|
||||
aac-decode = ["craft-media/bitstream"]
|
||||
aac-encode = ["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 = []
|
||||
# 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 = []
|
||||
opus-encode = []
|
||||
ac3-decode = ["craft-media/bitstream"]
|
||||
mpa-decode = ["craft-media/bitstream"]
|
||||
# MPEG-1/2 Layer III (MP3) decoder (`mp3::Decoder`).
|
||||
mp3-decode = ["mpa-decode"]
|
||||
# ALAC packet decoder and encoder (`alac`), ported from Apple's reference implementation.
|
||||
# ALAC packet decoder and encoder (`alac`), ported from Apple's reference implementation. The
|
||||
# encoder also writes ALAC in M4A through `encode` and `FileEncoder`.
|
||||
alac-decode = []
|
||||
alac-encode = []
|
||||
alac-encode = ["craft-media/isobmff"]
|
||||
ac3-encode = ["craft-media/bitstream"]
|
||||
eac3-encode = ["craft-media/bitstream"]
|
||||
# MPEG-1/2 Layer III (MP3) encoder (`mp3`).
|
||||
@@ -54,6 +67,7 @@ serde = ["dep:serde", "craft-media/serde"]
|
||||
|
||||
[dependencies]
|
||||
craft-media = { workspace = true }
|
||||
craft-xml = { workspace = true, optional = true }
|
||||
thiserror = { workspace = true }
|
||||
serde = { workspace = true, optional = true }
|
||||
log = { version = "0.4", optional = true }
|
||||
@@ -74,6 +88,14 @@ required-features = ["decode", "lossless-encode", "peaks"]
|
||||
name = "codecs"
|
||||
required-features = ["decode", "encode"]
|
||||
|
||||
[[test]]
|
||||
name = "flac_decode"
|
||||
required-features = ["flac-decode", "flac-encode"]
|
||||
|
||||
[[test]]
|
||||
name = "qoa_oracle"
|
||||
required-features = ["qoa"]
|
||||
|
||||
[[example]]
|
||||
name = "info"
|
||||
required-features = ["decode"]
|
||||
|
||||
+189
-31
@@ -1,7 +1,8 @@
|
||||
# craft-audio
|
||||
|
||||
Audio file I/O and codecs for the Craft apps. Native implementations: WAV and AIFF read/write,
|
||||
FLAC encoder, AAC and ALAC decoders/encoders, AC-3/E-AC-3 decoder/encoder, MP3 decoder/encoder, Opus and MP1/MP2 decoders.
|
||||
FLAC encoder, AAC and ALAC decoders/encoders, AC-3/E-AC-3 decoder/encoder, MP3 decoder/encoder, Opus
|
||||
decoder/encoder, MP1/MP2 decoders.
|
||||
Formats without a native decoder are decoded by [symphonia](#symphonia), which is to be replaced
|
||||
by native decoders.
|
||||
|
||||
@@ -28,19 +29,21 @@ No default features. The shared types (`AudioBuffer`, `AudioInfo`, `ChannelLayou
|
||||
| `decode` | `decode`, `probe`, `Decoder`, `tracks`, `TrackDecoder`; every format in [Format support](#format-support) | `decode-native`, `symphonia` |
|
||||
| `decode-native` | the same, native formats only (others are `Unsupported`); `wav_pcm`, `aiff_pcm` (a file's integer PCM bit for bit, as `RawPcm`) | the six decoders below, craft-media `isobmff`/`matroska`/`ogg`, `log` |
|
||||
| `symphonia` | the fallback for formats with no native decoder; turning it on is the same as `decode` | `decode-native`, symphonia |
|
||||
| `encode` | every encoder: the WAV, AIFF and FLAC file writers, and the AAC, ALAC, AC-3/E-AC-3 and MP3 encoders | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode` |
|
||||
| `encode` | every encoder, and every format of `encode` and `FileEncoder` | `lossless-encode`, `aac-encode`, `alac-encode`, `ac3-encode`, `eac3-encode`, `mp3-encode`, `opus-encode`, `vorbis-encode` |
|
||||
| `lossless-encode` | WAV, AIFF and FLAC writing | `pcm-encode`, `flac-encode` |
|
||||
| `pcm-encode` | `encode`, `FileEncoder` for WAV and AIFF; `wav_float_header`; `pcm_wav_file`, `pcm_wav_header`, `pcm_aiff_file`, `pcm_aiff_header` (minimal files around integer PCM bytes, for essence embedded in AAF / OMF) | none |
|
||||
| `flac-decode` | `flac::decode` (whole `.flac` files) and `flac::Decoder` (single frames, for callers that demux FLAC themselves); native, CRC-checked, 4 to 32 bits. `decode` still uses symphonia for FLAC files | none |
|
||||
| `flac-encode` | `encode`, `FileEncoder` for FLAC; `flac::Encoder`, the packet encoder for callers that mux FLAC themselves | craft-media `bitstream` |
|
||||
| `aac-decode`, `aac-encode` | `aac::Decoder`, `aac::Encoder`; with `aac-decode`, `aac::latm_config` / `latm_split` for LOAS / LATM frames (AAC in MPEG transport streams) | craft-media `bitstream` |
|
||||
| `aac-decode`, `aac-encode` | `aac::Decoder`, `aac::Encoder`; with `aac-decode`, `aac::latm_config` / `latm_split` for LOAS / LATM frames (AAC in MPEG transport streams); with `aac-encode`, `encode`, `FileEncoder` for AAC in M4A | craft-media `bitstream`; `aac-encode` also `isobmff` |
|
||||
| `alac-decode` | `alac::Decoder` | none |
|
||||
| `alac-encode` | `alac::Encoder` | none |
|
||||
| `opus-decode` | `opus::Decoder`/`StreamDecoder` | none |
|
||||
| `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` |
|
||||
| `ac3-decode` | `ac3::Decoder`, `ac3::ProgrammeDecoder` | craft-media `bitstream` |
|
||||
| `ac3-encode`, `eac3-encode` | `ac3::encoder::Encoder` | 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` | craft-media `bitstream` |
|
||||
| `mp3-encode` | `mp3::Encoder`; `encode`, `FileEncoder` for MP3 | craft-media `bitstream` |
|
||||
| `peaks` | `peaks::Peaks` | none |
|
||||
| `rayon` | parallel AAC encoding (with `aac-encode`) | `rayon` |
|
||||
| `serde` | `Serialize`/`Deserialize` on the shared types | `serde` |
|
||||
@@ -229,17 +232,86 @@ let wav = encode(&surround, &EncodeOptions::default().with_layout(ChannelLayout:
|
||||
let mut enc = FileEncoder::new(std::fs::File::create("take.wav")?, &EncodeOptions::default(), 48_000, 2)?;
|
||||
enc.write(&buf.channels, buf.frames())?; // one slice (or Vec) per channel
|
||||
enc.finish()?;
|
||||
|
||||
// The coded formats go the same way: AAC in M4A at 192 kbit/s, 5.1 as E-AC-3, 16-bit ALAC.
|
||||
let m4a = encode(&buf, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(192_000))?;
|
||||
let eac3 = encode(&surround, &EncodeOptions::new(EncodeFormat::Eac3).with_layout(ChannelLayout::SURROUND_5_1_SIDE))?;
|
||||
let alac = encode(&buf, &EncodeOptions::new(EncodeFormat::Alac).with_bit_depth(BitDepth::Int16))?;
|
||||
```
|
||||
|
||||
`EncodeOptions::default()` is 24-bit WAV. `with_bwf` adds a BWF `bext` chunk and
|
||||
`with_force_rf64` writes RF64 below 4 GiB; both apply to WAV only. `encode_format_for("aiff")`
|
||||
maps an extension to an `EncodeFormat`, and `ENCODE_EXTENSIONS` lists the extensions the enabled
|
||||
features can write. `EncodeFormat::ALL` lists the formats; `extension()` is the extension a new
|
||||
file gets and `mime_type()` its media type. `write` refuses a block without one slice per channel, or with a slice
|
||||
shorter than `frames`, and writes nothing. After a write fails for another reason, every later
|
||||
maps an extension to an `EncodeFormat` (`"m4a"` to AAC; `"alac"` names ALAC), and `ENCODE_EXTENSIONS` lists the extensions the enabled
|
||||
features can write. `EncodeFormat::ALL` lists the formats and `is_built()` says whether this build
|
||||
writes one; `extension()` is the extension a new file gets and `mime_type()` its media type.
|
||||
`write` refuses a block without one slice per channel, or with a slice shorter than `frames`, and
|
||||
writes nothing. After a write fails for another reason, every later
|
||||
`write` and `finish` returns an error.
|
||||
|
||||
### Encode AAC
|
||||
| `EncodeFormat` | File | Audio | Options used |
|
||||
|---|---|---|---|
|
||||
| `Wav`, `Aiff`, `Flac` | WAV / BWF / RF64, AIFF / AIFF-C, FLAC | any rate; FLAC up to 8 channels | `bit_depth`, `dither`, `layout` (WAV, FLAC), `bwf`, `force_rf64` (WAV) |
|
||||
| `Alac` | M4A | 1–8 channels, any rate | `bit_depth` (16, 24, 32), `dither`, `layout` |
|
||||
| `Aac` | M4A | AAC-LC, 1–8 channels, the 13 AAC rates (7350–96000 Hz) | `rate_control`, `layout` |
|
||||
| `Mp3` | `.mp3` stream | mono or stereo; 32, 44.1, 48 kHz or 16, 22.05, 24 kHz | `rate_control` (bit rate), `layout` |
|
||||
| `Ac3`, `Eac3` | `.ac3` / `.eac3` stream | up to 5.1 (E-AC-3: 7.1); 32, 44.1, 48 kHz | `rate_control` (bit rate), `layout` |
|
||||
|
||||
`with_bitrate(bits_per_second)` sets a constant bit rate for all channels together. A rate the
|
||||
stream cannot carry is moved to the nearest one it can: MP3 and AC-3 use the highest rate of their
|
||||
tables not above it, AAC and E-AC-3 keep it inside their range (AAC from 6 kbit/s or a quarter of
|
||||
the sample rate per channel, whichever is more: from about half of that down the encoder codes
|
||||
silence). `with_quality(0.0..=1.0)` is AAC's
|
||||
constant quality (VBR); the other lossy formats return `Unsupported` for it. Without either, AAC
|
||||
is written at 64 kbit/s per channel, MP3 at 96 kbit/s per channel, and AC-3 and E-AC-3 at
|
||||
96 kbit/s per full-bandwidth channel (the LFE not counted).
|
||||
|
||||
The audio is in WAV channel order for every format; `layout` (the usual layout for the channel
|
||||
count when unset) names the speakers, and the channels are reordered into the codec's own order.
|
||||
Each coded format has a fixed set of layouts, and audio with other speakers is `Unsupported`
|
||||
rather than written to the wrong ones:
|
||||
|
||||
- AAC: C; L R; L R C; L R C Cb; L R C Ls Rs; 5.1; 6.1; and for 8 channels L R C LFE Lb Rb Lc Rc
|
||||
(a front pair left and right of centre, mask `0xFF`). ALAC: the same, with 6.1's back centre last.
|
||||
WAV 7.1 (backs and sides) is not that 7.1, and quad (the usual 4-channel layout) has no centre:
|
||||
both need another format.
|
||||
- AC-3 and E-AC-3: the layout picks the coding mode (1/0 to 3/2, with or without LFE; E-AC-3 also
|
||||
7.1 with backs and sides).
|
||||
- A surround pair is a surround pair: side and back surrounds are interchangeable where the format
|
||||
has one pair.
|
||||
|
||||
Before a file is created, the options and the format say what will happen:
|
||||
|
||||
```rust
|
||||
let opts = EncodeOptions::new(EncodeFormat::Ac3).with_layout(info.layout).with_bitrate(200_000);
|
||||
opts.check(info.sample_rate, info.channels)?; // the error FileEncoder::new would return, nothing written
|
||||
let bps = opts.bitrate(info.sample_rate, info.channels)?; // Some(192_000): the rate actually used; None when lossless or by quality
|
||||
|
||||
EncodeFormat::Ac3.sample_rates(); // Some(&[32_000, 44_100, 48_000]); None when any rate will do
|
||||
EncodeFormat::Ac3.max_channels(); // 6
|
||||
EncodeFormat::Ac3.fixed_layouts(3); // L R C, L R Cb, L R LFE; None for WAV, AIFF and FLAC (any layout)
|
||||
EncodeFormat::Ac3.supports_layout(&info.layout); // one of those, sides and backs standing for each other
|
||||
EncodeFormat::Ac3.supports_quality(); // false: AAC only
|
||||
```
|
||||
|
||||
`FileEncoder::bitrate()` gives the rate of a file being written. The `EncodeFormat` queries answer
|
||||
for a format whose feature is off too; `check` and `EncodeOptions::bitrate` (there with any
|
||||
encoder feature) return `Unsupported` for it.
|
||||
|
||||
The layout `decode` reports (`AudioInfo::layout`) can be passed to `with_layout` as it is. When a
|
||||
coded format refuses it (2.1 or quad into AAC, a single channel tagged as left, channels with no
|
||||
speaker assignment), the choices are another format, or audio rearranged into one of the layouts
|
||||
above with that layout set; leaving `layout` unset declares the usual layout for the count. This
|
||||
crate's decoder returns 7- and 8-channel AAC without a speaker assignment, in AAC order, so such a
|
||||
file needs that step before it is written again.
|
||||
|
||||
An M4A's edit list skips the AAC encoder's priming (1024 frames) and ends at the last frame
|
||||
written, so the file decodes to the length that went in. An MP3, AC-3 or E-AC-3 stream has no
|
||||
place for that (see [Limits](#limits)). For a container of your own, use the codec modules below.
|
||||
|
||||
### Encode AAC packets
|
||||
|
||||
For an `.m4a` file, use `EncodeFormat::Aac` above. `aac::Encoder` is the packet encoder behind it,
|
||||
for muxing into a container of your own:
|
||||
|
||||
```rust
|
||||
use craft_audio::AudioError;
|
||||
@@ -264,6 +336,65 @@ Ls Rs) has no equivalent in that layout; passed unchanged, its channels go to th
|
||||
are a playable `.aac` file. To write MP4 or Matroska, pass the access units to craft-media's
|
||||
muxers ([MP4](../media/README.md#muxing), [Matroska](../media/README.md#muxing-1)).
|
||||
|
||||
### Encode Opus
|
||||
|
||||
```rust
|
||||
use craft_audio::opus::Encoder;
|
||||
|
||||
let mut enc = Encoder::new(2, 128_000)?; // channels, bits per second
|
||||
let frame_len = Encoder::FRAME_SIZE * 2; // 20 ms, interleaved stereo
|
||||
let mut samples = buf.interleaved(); // 48 kHz audio
|
||||
// Silence after the audio flushes the encoder's delay (the pre-skip) and fills the last frame.
|
||||
let frames = samples.len() / 2 + enc.pre_skip() as usize;
|
||||
samples.resize(frames.div_ceil(Encoder::FRAME_SIZE) * frame_len, 0.0);
|
||||
let mut packets = Vec::new();
|
||||
for frame in samples.chunks_exact(frame_len) {
|
||||
packets.push(enc.encode_float(frame)?);
|
||||
}
|
||||
let head = enc.opus_head(); // Matroska's CodecPrivate
|
||||
let pre_skip = enc.pre_skip(); // samples the player drops at the start
|
||||
```
|
||||
|
||||
The encoder takes 48 kHz only, and craft-audio has no resampler.
|
||||
|
||||
Opus goes into Ogg, Matroska/WebM or MP4:
|
||||
|
||||
- **Ogg (`.opus`):** `craft_media::ogg::PageWriter` with `head` and `opus_tags` as the two header
|
||||
packets, each packet's granule position the samples decoded so far (pre-skip included, as
|
||||
the input above starts with it), and `finish(Some(pre_skip + length))` to trim the end
|
||||
(RFC 7845 §4).
|
||||
|
||||
- **Matroska/WebM:** `head` is the track's `CodecPrivate`, and the pre-skip goes in
|
||||
`TrackSpec::codec_delay_ns` (in nanoseconds at 48 kHz). The Matroska muxer cannot write
|
||||
`DiscardPadding`, so the padding at the end stays in the file.
|
||||
- **MP4:** the sample entry takes an `isobmff::OpusConfig` (`dOps`) with the same fields as the
|
||||
`OpusHead`: channels, pre-skip, input rate, gain and mapping family. `TrackConfig::media_start`
|
||||
set to the pre-skip skips it. To drop the padding as well, give `TrackConfig::edits` one
|
||||
`Edit` instead, which replaces the edit `media_start` makes: `media_time` the pre-skip (track
|
||||
timescale), `segment_duration` the audio's length (movie timescale), `media_rate`
|
||||
`0x0001_0000`.
|
||||
|
||||
### Encode Ogg Vorbis
|
||||
|
||||
```rust
|
||||
use craft_audio::{OggVorbisOptions, encode_ogg_vorbis};
|
||||
|
||||
// 1–8 channels in Vorbis order (5.1: L C R Ls Rs LFE), any sample rate.
|
||||
let opts = OggVorbisOptions::new(6.0).with_tag("TITLE", "Take 3").with_tag("ARTIST", "Me");
|
||||
let ogg: Vec<u8> = encode_ogg_vorbis(&buf, &opts)?;
|
||||
```
|
||||
|
||||
The encoder is pure Rust (`vorbis::encoder`, from FilmCraft #4481) and builds for wasm32. Quality
|
||||
runs from -1 to 10 at a variable bit rate: about 18 kbit/s per channel at -1, 100 at 6 and 140
|
||||
at 10 on music. `encode` with `EncodeFormat::Ogg` takes a WAV-order layout and writes it in
|
||||
Vorbis order; its `RateControl::Bitrate` (64–500 kbit/s) picks the quality whose nominal rate
|
||||
that is (`quality_for_bitrate`), and `RateControl::Quality` 0.0–1.0 is quality -1 to 10. For
|
||||
streaming, feed `vorbis::encoder::Encoder` and write its packets with `craft_media::ogg::PageWriter`
|
||||
(identification, `vorbis_comment`, setup, then the audio with each packet's granule position).
|
||||
|
||||
Not yet: short blocks (transients may pre-echo), channel coupling (stereo costs
|
||||
about twice mono) and bit-rate management.
|
||||
|
||||
### Waveform overviews
|
||||
|
||||
```rust
|
||||
@@ -308,13 +439,14 @@ match craft_audio::decode(&bytes, None) {
|
||||
| WAV, BWF, RF64/BW64 | native | native |
|
||||
| AIFF, AIFF-C | native | native |
|
||||
| FLAC | symphonia | native |
|
||||
| AAC-LC | native | native |
|
||||
| HE-AAC v1/v2 | native, core only (see [Limits](#limits)) | — |
|
||||
| ALAC | native | native, packets (`alac::Encoder`; no file writer) |
|
||||
| AAC-LC | native | native; M4A through `encode` / `FileEncoder`, packets through `aac::Encoder` |
|
||||
| HE-AAC v1 | native (SBR) | — |
|
||||
| HE-AAC v2 | native, without parametric stereo (see [Limits](#limits)) | — |
|
||||
| ALAC | native | native; M4A through `encode` / `FileEncoder`, packets through `alac::Encoder` |
|
||||
| MP1, MP2 (MPEG audio Layer I/II) | native | — |
|
||||
| MP3 (Layer III) | native | native, frames (`mp3::Encoder`; the frames in a row are an `.mp3` file) |
|
||||
| Opus | native | — |
|
||||
| AC-3, E-AC-3 | native | native packet encoder |
|
||||
| MP3 (Layer III) | native | native; `.mp3` through `encode` / `FileEncoder`, frames through `mp3::Encoder` |
|
||||
| Opus | native | native, packets (`opus::Encoder`; no file writer) |
|
||||
| AC-3, E-AC-3 | native | native; `.ac3` / `.eac3` through `encode` / `FileEncoder`, packets through `ac3::encoder::Encoder` |
|
||||
| Vorbis, ADPCM, A-law, µ-law | symphonia | — |
|
||||
|
||||
| Container | Demuxer | Timing |
|
||||
@@ -329,10 +461,10 @@ match craft_audio::decode(&bytes, None) {
|
||||
|
||||
## Limits
|
||||
|
||||
- HE-AAC: the AAC-LC core is decoded. `decode`, `Decoder` and `TrackDecoder` upsample it 2×;
|
||||
`aac::Decoder` alone outputs the core rate. SBR is not reconstructed, so there is no content
|
||||
above the core's Nyquist frequency.
|
||||
- HE-AAC v2: parametric stereo is not reconstructed either, so the mono core decodes as mono.
|
||||
- HE-AAC: `decode`, `Decoder` and `TrackDecoder` decode SBR at twice the core rate.
|
||||
`aac::Decoder` outputs the core rate unless `set_sbr(true)` is called. Downsampled SBR (an
|
||||
output rate equal to the core rate) plays the core alone.
|
||||
- HE-AAC v2: parametric stereo is not reconstructed, so the mono core decodes as mono, with SBR.
|
||||
- AAC: Main, SSR and LTP profiles, coupling channels and 960-sample frames are unsupported.
|
||||
- Raw ADTS: frames with several raw data blocks are unsupported: a stream of them is refused, and
|
||||
one among other frames decodes as silence of its length and counts as damaged, as does a frame
|
||||
@@ -374,10 +506,26 @@ match craft_audio::decode(&bytes, None) {
|
||||
- `TrackDecoder` returns `Unsupported` for FLAC and Vorbis; `decode` and `Decoder` read
|
||||
those through symphonia. ALAC in CAF still goes through symphonia (craft-media has no CAF
|
||||
demuxer).
|
||||
- ALAC: `EncodeFormat` has no ALAC entry; `alac::Encoder` writes packets and the configuration,
|
||||
and the caller muxes them (MP4 `alac` box, Matroska `A_ALAC`). craft-media's MP4 muxer writes no
|
||||
`chan` box, so an MP4 of more than two channels carries no explicit layout; decoders then use the
|
||||
standard ALAC layout for the count.
|
||||
- ALAC: craft-media's MP4 muxer writes no `chan` box, so an M4A of more than two channels (from
|
||||
`EncodeFormat::Alac` or muxed from `alac::Encoder` packets) carries no explicit layout; decoders
|
||||
then use the standard ALAC layout for the count, which is the one written.
|
||||
- `EncodeFormat::Mp3`, `Ac3` and `Eac3` write raw streams, which cannot signal the codec's delay or
|
||||
the length: the file decodes with 1057 (MP3) or 256 (AC-3, E-AC-3) frames of delay in front and
|
||||
padded to whole frames at the end. MP3 gets no Xing / Info frame. `EncodeFormat::Aac` and `Alac`
|
||||
(M4A) decode to the length written.
|
||||
- An AAC M4A's length is in the edit list and the last packet's duration. This crate, Apple's
|
||||
decoders and ffmpeg 9 end the audio there; ffmpeg 8 skips the priming but plays the last
|
||||
packet's padding (under 1024 frames of near silence).
|
||||
- `EncodeFormat::Aac` and `Alac` write the `moov` box after the audio (no fast start), and no
|
||||
metadata. Raw ADTS AAC is not a file format of `encode`; `aac::EncoderConfig::with_adts` gives
|
||||
the frames.
|
||||
- This crate's file reader plays a raw E-AC-3 7.1 stream as its 5.1 independent substream.
|
||||
- `EncodeFormat::Aac` with 8 channels is AAC channel configuration 7 (C, Lc Rc, L R, Ls Rs, LFE),
|
||||
which Apple's decoders read as written. ffmpeg assumes such a stream is a mislabelled 7.1 with
|
||||
backs and sides and moves the channels accordingly, unless run with `-strict 1`.
|
||||
- A file with no audio: MP3, AC-3 and E-AC-3 are then zero bytes, which nothing can probe. An
|
||||
empty AAC M4A (a track with no samples) is read by ffmpeg; this crate's native reader passes it
|
||||
on, so `decode-native` alone refuses it and symphonia reads only some as zero frames.
|
||||
- ALAC 32-bit streams: ffmpeg does not decode their uncompressed (escaped) stereo frames, which
|
||||
Apple's encoder and this one write for audio that does not compress; Apple's decoder and this
|
||||
one do.
|
||||
@@ -413,8 +561,8 @@ Each codec module's rustdoc lists the coding tools it implements.
|
||||
|---|---|---|---|
|
||||
| WAV, AIFF | Microsoft RIFF, Apple AIFF/AIFF-C | read: integer 8–32-bit, float 32/64-bit, `WAVE_FORMAT_EXTENSIBLE` masks, RF64/BW64, BWF `bext`, truncated files; write: 16/24/32-bit integer and 32-bit float, RF64 above 4 GiB, any layout, TPDF dither | round trips; hostile-input tests |
|
||||
| FLAC encoder | RFC 9639 | 16, 24, 32 bits; 1–8 channels, layouts decoders would not assume for the count as a `WAVEFORMATEXTENSIBLE_CHANNEL_MASK` comment; up to 655,350 Hz; LPC, stereo decorrelation, wasted bits, STREAMINFO MD5 | bit-exact through ffmpeg, STREAMINFO MD5 checked against ffmpeg |
|
||||
| AAC (`aac`) | ISO/IEC 14496-3 | AAC-LC decode and encode (CBR, VBR, ADTS), 1–8 channels; HE-AAC: see [Limits](#limits) | ffmpeg: ~135 dB SNR; PNS noise differs per sample at equal band energy |
|
||||
| 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 | all 36 RFC 8251 conformance cases |
|
||||
| AAC (`aac`) | ISO/IEC 14496-3; SBR tables from ETSI ES 201 980 Annex I | AAC-LC decode and encode (CBR, VBR, ADTS), 1–8 channels; HE-AAC (SBR) decode, see [Limits](#limits) | ffmpeg: ~135 dB SNR; PNS noise differs per sample at equal band energy. HE-AAC from AudioToolbox, 1–6 channels: same 962-sample delay as ffmpeg, ~54 dB SNR, high band level within 0.01 dB |
|
||||
| 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 |
|
||||
| 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) |
|
||||
@@ -445,7 +593,7 @@ finite count as silence and the rest are clamped to ±4, so no input makes an in
|
||||
decoder's 7680-bit input buffer.
|
||||
- Not yet: block switching (short blocks; transients pre-echo), `scfsi` and `preflag`, intensity
|
||||
stereo, a tonality-aware model, VBR/ABR, the Xing/Info and LAME gapless tags (the delay is not
|
||||
signalled), CRC, free format, and an `EncodeFormat` entry for `encode`/`FileEncoder`.
|
||||
signalled), CRC and free format.
|
||||
|
||||
### AC-3 and E-AC-3 encoding
|
||||
|
||||
@@ -582,6 +730,8 @@ the full tests with ffmpeg and the vectors required.
|
||||
| MP3 decoder | written here, from ISO/IEC 11172-3 and 13818-3; shares the encoder's tables and the MP1/MP2 synthesis filter |
|
||||
| MP3 encoder | FilmCraft `filmcraft-mp3enc` [4bec22b](https://github.com/storytold/filmcraft/commit/4bec22bd03bd1d995c9cf72e02a284f96d3d72cd) |
|
||||
| ALAC | Apple's reference implementation [macosforge/alac@c38887c](https://github.com/macosforge/alac/tree/c38887c5c5e64a4b31108733bd79ca9b2496d987/codec), Copyright © 2011 Apple Inc., Apache-2.0; ported to Rust and modified (see the `alac` module docs and [NOTICE](../../NOTICE)) |
|
||||
| Opus encoder | EffectCraft `effectcraft-opusenc` [c618429](https://github.com/storytold/effectcraft/commit/c618429252587525a2a6d4b5bccbe4ec5261e1b9) |
|
||||
| Vorbis encoder | FilmCraft `filmcraft-vorbis` by @JordyUSA ([storytold/filmcraft#4481](https://github.com/storytold/filmcraft/pull/4481)), with partitions of whole sample frames so libvorbis reads surround |
|
||||
|
||||
Where apps had overlapping implementations, the one that measured better was kept, with the
|
||||
tests of both:
|
||||
@@ -598,7 +748,8 @@ For the apps switching from their own code; to be removed once all three have sw
|
||||
|
||||
The codec modules' public items keep their names, so apps can re-export them
|
||||
(`pub use craft_audio::aac::*;`). Names kept only for that are `#[doc(hidden)]`:
|
||||
`ac3::Decoded` (now `ac3::Frame`), `aac::Upsample2x`, `aac::ProgramConfig::parse`,
|
||||
`ac3::Decoded` (now `ac3::Frame`), `opus::encoder::OpusEncoder` (now `opus::Encoder`),
|
||||
`aac::Upsample2x`, `aac::ProgramConfig::parse`,
|
||||
`aac::config_layout` and `aac::sample_rate_index`. What else an app adjusts:
|
||||
|
||||
- `AudioInfo` has `container`, `codec`, `layout` and `damaged` instead of SoundCraft's `format`.
|
||||
@@ -624,8 +775,10 @@ The codec modules' public items keep their names, so apps can re-export them
|
||||
- `TrackInfo::sample_rate` and `channels` are `Option`s.
|
||||
- Features are per codec. SoundCraft's `encode` (WAV, AIFF, FLAC) is now `lossless-encode`, made
|
||||
of `pcm-encode` (WAV, AIFF) and `flac-encode`. `encode` turns on every encoder, including AAC,
|
||||
ALAC and AC-3/E-AC-3. Writing a format whose feature is off returns `AudioError::Unsupported`.
|
||||
ALAC, AC-3/E-AC-3 and Opus. Writing a format whose feature is off returns `AudioError::Unsupported`.
|
||||
- AAC encoder settings it cannot use are `aac::Error::InvalidSettings` (`AudioError::Encode`).
|
||||
- The Opus encoder's constructors and `encode_float` return `Result` instead of panicking on an
|
||||
invalid channel count, mode/bandwidth or frame length.
|
||||
- `TrackDecoder::decode` returns an error for a corrupt Opus packet, as for the other codecs;
|
||||
call `conceal`.
|
||||
- `FileEncoder::write` takes any per-channel slices, but refuses a block with missing channels or
|
||||
@@ -636,3 +789,8 @@ The codec modules' public items keep their names, so apps can re-export them
|
||||
CAF.
|
||||
MP4 and Matroska files decode to presentation length. A packet that fails is concealed, not
|
||||
dropped. A failed source read is `AudioError::Io`.
|
||||
|
||||
WAV timestamps use `bext` when present, including a zero timestamp. Otherwise a bounded iXML
|
||||
`BWFXML/BEXT` time reference supplies the timestamp (two unsigned 32-bit words; an omitted high
|
||||
word is zero). Only the first iXML chunk is considered. Invalid or oversized optional XML is
|
||||
ignored without rejecting the audio. This works for byte-backed and streaming sources.
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Write a tone as WAV and FLAC, whole and streamed, and as AAC packets.
|
||||
//! Write a tone as WAV, FLAC and AAC in M4A, whole and streamed, and as AAC packets.
|
||||
//!
|
||||
//! `cargo run -p craft-audio --example encode --features lossless-encode,aac-encode -- out`
|
||||
|
||||
@@ -28,7 +28,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
enc.finish()?;
|
||||
|
||||
// AAC packets for an MP4 or Matroska muxer, with the configuration its sample entry needs.
|
||||
// AAC in M4A at 160 kbit/s: the same call, with a bit rate in place of a bit depth.
|
||||
let m4a = encode(&stereo, &EncodeOptions::new(EncodeFormat::Aac).with_bitrate(160_000))?;
|
||||
std::fs::write(dir.join("tone.m4a"), m4a)?;
|
||||
|
||||
// AAC packets for a muxer of your own, with the configuration its sample entry needs.
|
||||
let mut aac = craft_audio::aac::Encoder::new(craft_audio::aac::EncoderConfig::cbr(rate as u32, 2, 128_000))?;
|
||||
let mut units = aac.encode(&[&tone, &tone]);
|
||||
units.extend(aac.flush());
|
||||
|
||||
@@ -343,6 +343,51 @@ impl AudioSpecificConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// Why an AudioSpecificConfig prefix cannot be written as an ADTS header.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AdtsConfigError {
|
||||
/// Fewer than two bytes were supplied.
|
||||
Short,
|
||||
/// The prefix names a profile, indexed rate or channel configuration ADTS cannot carry.
|
||||
Unsupported {
|
||||
/// Audio object type from the prefix.
|
||||
object_type: u8,
|
||||
/// Sampling frequency index from the prefix.
|
||||
sf_index: u8,
|
||||
/// Channel configuration from the prefix.
|
||||
channel_config: u8,
|
||||
},
|
||||
}
|
||||
|
||||
/// ADTS fixed fields taken from the prefix of an AudioSpecificConfig.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct AdtsConfig {
|
||||
/// Audio object type, 1 through 4.
|
||||
pub object_type: u8,
|
||||
/// Indexed sample rate, 0 through 12.
|
||||
pub sf_index: u8,
|
||||
/// Channel configuration, 1 through 7.
|
||||
pub channel_config: u8,
|
||||
}
|
||||
impl AdtsConfig {
|
||||
/// Parse only the first two ASC bytes. Unlike a decoder's full ASC parser, this accepts all
|
||||
/// four object types representable by ADTS and rejects configurations needing a PCE.
|
||||
pub fn from_asc_prefix(asc: &[u8]) -> std::result::Result<Self, AdtsConfigError> {
|
||||
let (Some(&a), Some(&b)) = (asc.first(), asc.get(1)) else { return Err(AdtsConfigError::Short) };
|
||||
let object_type = a >> 3;
|
||||
let sf_index = ((a & 7) << 1) | (b >> 7);
|
||||
let channel_config = (b >> 3) & 15;
|
||||
if !(1..=4).contains(&object_type) || sf_index > 12 || !(1..=7).contains(&channel_config) {
|
||||
return Err(AdtsConfigError::Unsupported { object_type, sf_index, channel_config });
|
||||
}
|
||||
Ok(Self { object_type, sf_index, channel_config })
|
||||
}
|
||||
/// Write a no-CRC ADTS header with variable-rate buffer fullness for one payload.
|
||||
pub fn header(self, payload_len: usize) -> Result<[u8; 7]> {
|
||||
AdtsHeader::write(self.object_type, self.sf_index, self.channel_config, payload_len, 0x7ff)
|
||||
}
|
||||
}
|
||||
|
||||
/// A parsed ADTS header.
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -519,6 +564,15 @@ mod tests {
|
||||
assert_eq!(AudioSpecificConfig::parse(&asc.to_bytes().unwrap()).unwrap(), asc);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adts_config_preserves_prefix_and_frame_limits() {
|
||||
let config = AdtsConfig::from_asc_prefix(&[0x12, 0x10]).unwrap();
|
||||
assert_eq!(config.header(100).unwrap(), AdtsHeader::write(2, 4, 2, 100, 0x7ff).unwrap());
|
||||
assert!(config.header(8185).is_err());
|
||||
assert_eq!(AdtsConfig::from_asc_prefix(&[]), Err(AdtsConfigError::Short));
|
||||
assert!(matches!(AdtsConfig::from_asc_prefix(&[0x2a, 0x10]), Err(AdtsConfigError::Unsupported { object_type: 5, .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adts_headers_refuse_fields_they_cannot_hold() {
|
||||
for (object_type, sf_index, channel_config) in [(0, 4, 2), (5, 4, 2), (29, 4, 2), (2, 13, 2), (2, 15, 2), (2, 4, 8), (2, 4, 11), (2, 4, 12)] {
|
||||
|
||||
+158
-15
@@ -6,6 +6,7 @@ use crate::aac::config::{AudioSpecificConfig, ElementType, ProgramConfig};
|
||||
use crate::aac::huffman::{self, INTENSITY_HCB, INTENSITY_HCB2, NOISE_HCB, ZERO_HCB};
|
||||
use crate::aac::ics::{IcsInfo, WindowSequence};
|
||||
use crate::aac::mdct::{MdctScratch, WindowShape, mdct_long, mdct_short, window};
|
||||
use crate::aac::sbr;
|
||||
use crate::aac::tables::{pow43, pow43_table, sf_gain};
|
||||
use crate::aac::tns::{self, TnsData};
|
||||
use crate::aac::{Error, Result};
|
||||
@@ -63,7 +64,14 @@ impl Scratch {
|
||||
}
|
||||
}
|
||||
|
||||
/// AAC-LC decoder.
|
||||
/// Where the SBR data of a channel element sits in its access unit: the bit range of the fill
|
||||
/// element's payload after its extension type, and whether a CRC leads it.
|
||||
type SbrRef = (usize, usize, bool);
|
||||
|
||||
/// An access unit's core output, and the SBR data of each layout entry.
|
||||
type Decoded = (Vec<Vec<f32>>, Vec<Option<SbrRef>>);
|
||||
|
||||
/// AAC-LC decoder, with the SBR tool of HE-AAC when [`Decoder::set_sbr`] turns it on.
|
||||
#[derive(Clone)]
|
||||
pub struct Decoder {
|
||||
asc: AudioSpecificConfig,
|
||||
@@ -75,6 +83,11 @@ pub struct Decoder {
|
||||
scratch: Scratch,
|
||||
/// A fill element carried SBR data (implicitly signalled HE-AAC).
|
||||
sbr_seen: bool,
|
||||
/// The SBR decoder of each layout entry, when output is at twice the core rate.
|
||||
sbr_out: Option<Vec<sbr::Element>>,
|
||||
/// The layout `sbr_out` was built for. Kept apart from `layout`, so that an access unit whose
|
||||
/// new program config fails (and whose layout is put back) leaves the SBR history alone.
|
||||
sbr_layout: Vec<(ElementType, u8)>,
|
||||
}
|
||||
|
||||
impl Decoder {
|
||||
@@ -110,6 +123,8 @@ impl Decoder {
|
||||
noise_state: NOISE_SEED,
|
||||
scratch: Scratch::new(),
|
||||
sbr_seen: false,
|
||||
sbr_out: None,
|
||||
sbr_layout: Vec::new(),
|
||||
};
|
||||
d.set_layout(layout);
|
||||
Ok(d)
|
||||
@@ -131,16 +146,27 @@ impl Decoder {
|
||||
pub fn config(&self) -> &AudioSpecificConfig {
|
||||
&self.asc
|
||||
}
|
||||
/// Output sampling rate in Hz: the AAC-LC core rate.
|
||||
/// Output sampling rate in Hz: the AAC-LC core rate, or twice it with [`Self::set_sbr`] on.
|
||||
pub fn sample_rate(&self) -> u32 {
|
||||
self.asc.sample_rate
|
||||
if self.sbr_out.is_some() { self.asc.sample_rate.saturating_mul(2) } else { self.asc.sample_rate }
|
||||
}
|
||||
/// Whether the stream is HE-AAC: SBR signalled in the `AudioSpecificConfig` (explicit) or SBR
|
||||
/// data seen in a fill element of a decoded access unit (implicit). Only the AAC-LC core is
|
||||
/// decoded, at [`Self::sample_rate`]; the SBR high band is not reconstructed.
|
||||
/// data seen in a fill element of a decoded access unit (implicit). Without [`Self::set_sbr`],
|
||||
/// only the AAC-LC core is decoded.
|
||||
pub fn sbr(&self) -> bool {
|
||||
self.asc.sbr || self.sbr_seen
|
||||
}
|
||||
/// Decode HE-AAC at twice the core rate (`true`) or the AAC-LC core alone (`false`, the
|
||||
/// default). With SBR on, [`Self::decode`] returns 2048 samples per channel: the core through
|
||||
/// the SBR filter banks, delayed by [`SBR_DELAY`](crate::aac::SBR_DELAY) samples, with the high
|
||||
/// band rebuilt from each frame's SBR data. Until the stream's first SBR header, and for a
|
||||
/// frame whose SBR data is missing or damaged, the high band is empty. Parametric stereo
|
||||
/// (HE-AAC v2) is not decoded: its mono core stays mono.
|
||||
pub fn set_sbr(&mut self, on: bool) {
|
||||
if on != self.sbr_out.is_some() {
|
||||
self.sbr_out = on.then(Vec::new);
|
||||
}
|
||||
}
|
||||
/// Output channels of the current element layout.
|
||||
pub fn channels(&self) -> usize {
|
||||
self.channels.len()
|
||||
@@ -158,30 +184,61 @@ impl Decoder {
|
||||
c.overlap.fill(0.0);
|
||||
c.prev_shape = WindowShape::Sine;
|
||||
}
|
||||
for e in self.sbr_out.iter_mut().flatten() {
|
||||
e.reset();
|
||||
}
|
||||
}
|
||||
|
||||
fn sf_index(&self) -> u8 {
|
||||
self.asc.sf_index
|
||||
}
|
||||
|
||||
/// Decode one access unit (one `raw_data_block`) into 1024 samples per channel. An access unit
|
||||
/// that fails leaves the decoder as it was, but for [`Self::sbr`]: its damage, including a damaged
|
||||
/// program config element, does not reach the access units after it.
|
||||
/// Decode one access unit (one `raw_data_block`) into 1024 samples per channel (2048 with
|
||||
/// [`Self::set_sbr`] on). An access unit that fails leaves the decoder as it was, but for
|
||||
/// [`Self::sbr`]: its damage, including a damaged program config element, does not reach the
|
||||
/// access units after it. Damaged SBR data alone does not fail it.
|
||||
pub fn decode(&mut self, au: &[u8]) -> Result<Vec<Vec<f32>>> {
|
||||
let before = (self.asc.pce.clone(), self.layout.clone(), self.offsets.clone(), self.channels.clone(), self.noise_state);
|
||||
let decoded = self.decode_block(au, true);
|
||||
if decoded.is_err() {
|
||||
(self.asc.pce, self.layout, self.offsets, self.channels, self.noise_state) = before;
|
||||
match self.decode_block(au, true) {
|
||||
Ok((pcm, sbr_refs)) => Ok(self.apply_sbr(au, pcm, &sbr_refs)),
|
||||
Err(e) => {
|
||||
(self.asc.pce, self.layout, self.offsets, self.channels, self.noise_state) = before;
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
decoded
|
||||
}
|
||||
|
||||
fn decode_block(&mut self, au: &[u8], allow_reconfig: bool) -> Result<Vec<Vec<f32>>> {
|
||||
/// Run each layout entry's channels through its SBR decoder, when SBR is on.
|
||||
fn apply_sbr(&mut self, au: &[u8], pcm: Vec<Vec<f32>>, refs: &[Option<SbrRef>]) -> Vec<Vec<f32>> {
|
||||
let rate = self.sample_rate();
|
||||
let Some(elements) = self.sbr_out.as_mut() else {
|
||||
return pcm;
|
||||
};
|
||||
// a new layout (a program config element) starts the SBR decoders afresh: their history
|
||||
// belongs to the old element order, even where the channel counts match
|
||||
if elements.len() != self.layout.len() || self.sbr_layout != self.layout {
|
||||
*elements = self.layout.iter().map(|l| sbr::Element::new(l.0.channels(), rate)).collect();
|
||||
self.sbr_layout.clone_from(&self.layout);
|
||||
}
|
||||
let mut out = Vec::with_capacity(pcm.len());
|
||||
for (i, e) in elements.iter_mut().enumerate() {
|
||||
let first = self.offsets.get(i).copied().unwrap_or(0);
|
||||
let core = pcm.get(first..first + e.channels()).unwrap_or_default();
|
||||
let payload = refs.get(i).copied().flatten().map(|(start, end, crc)| sbr::Payload { data: au, start, end, crc });
|
||||
out.extend(e.process(core, payload));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn decode_block(&mut self, au: &[u8], allow_reconfig: bool) -> Result<Decoded> {
|
||||
let mut br = BitReader::new(au);
|
||||
let nch = self.channels.len();
|
||||
let mut saw_element = false;
|
||||
let mut out: Vec<Option<Vec<f32>>> = vec![None; nch];
|
||||
let mut used = vec![false; self.layout.len()];
|
||||
// SBR data belongs to the single channel or pair element before its fill element
|
||||
let mut sbr_refs: Vec<Option<SbrRef>> = vec![None; self.layout.len()];
|
||||
let mut last_slot = None;
|
||||
loop {
|
||||
let id = br.read_bits(3)?;
|
||||
saw_element |= matches!(id, ID_SCE | ID_CPE | ID_LFE);
|
||||
@@ -191,6 +248,7 @@ impl Decoder {
|
||||
let ty = if id == ID_SCE { ElementType::Sce } else { ElementType::Lfe };
|
||||
let slot = self.find_slot(ty, tag, &used)?;
|
||||
used[slot] = true;
|
||||
last_slot = (id == ID_SCE).then_some(slot);
|
||||
let mut ics = self.decode_ics(&mut br, None)?;
|
||||
let ch = self.offsets[slot];
|
||||
self.fill_noise(&mut ics, None);
|
||||
@@ -200,6 +258,7 @@ impl Decoder {
|
||||
let tag = br.read_bits(4)? as u8;
|
||||
let slot = self.find_slot(ElementType::Cpe, tag, &used)?;
|
||||
used[slot] = true;
|
||||
last_slot = Some(slot);
|
||||
let ch = self.offsets[slot];
|
||||
let [a, b] = self.decode_cpe(&mut br, ch)?;
|
||||
out[ch] = Some(a);
|
||||
@@ -237,8 +296,13 @@ impl Decoder {
|
||||
}
|
||||
if count > 0 {
|
||||
let ext = br.read_bits(4)?;
|
||||
self.sbr_seen |= matches!(ext, EXT_SBR_DATA | EXT_SBR_DATA_CRC);
|
||||
let is_sbr = matches!(ext, EXT_SBR_DATA | EXT_SBR_DATA_CRC);
|
||||
self.sbr_seen |= is_sbr;
|
||||
let start = br.position();
|
||||
br.skip(count * 8 - 4)?;
|
||||
if let (true, Some(slot)) = (is_sbr, last_slot.take()) {
|
||||
sbr_refs[slot] = Some((start, br.position(), ext == EXT_SBR_DATA_CRC));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => break, // ID_END
|
||||
@@ -251,7 +315,7 @@ impl Decoder {
|
||||
for o in out {
|
||||
res.push(o.ok_or(Error::Bitstream("access unit is missing a channel element"))?);
|
||||
}
|
||||
Ok(res)
|
||||
Ok((res, sbr_refs))
|
||||
}
|
||||
|
||||
fn find_slot(&self, ty: ElementType, tag: u8, used: &[bool]) -> Result<usize> {
|
||||
@@ -637,6 +701,85 @@ mod tests {
|
||||
assert_eq!(dec.sample_rate(), 22_050);
|
||||
}
|
||||
|
||||
/// With SBR on, an access unit decodes to 2048 samples, and over a silent core the high band
|
||||
/// its SBR fill element describes is there; with SBR off, the core alone (silence).
|
||||
#[test]
|
||||
fn sbr_fill_elements_rebuild_the_high_band() {
|
||||
let au = sbr_au(0);
|
||||
// AAC-LC, 22.05 kHz, mono
|
||||
let asc = AudioSpecificConfig::lc(7, 1);
|
||||
let mut dec = Decoder::from_config(asc.clone()).unwrap();
|
||||
let core = dec.decode(&au).unwrap();
|
||||
assert_eq!((core[0].len(), dec.sample_rate()), (1024, 22_050));
|
||||
assert!(dec.sbr() && core[0].iter().all(|&s| s == 0.0));
|
||||
let mut dec = Decoder::from_config(asc).unwrap();
|
||||
dec.set_sbr(true);
|
||||
assert_eq!(dec.sample_rate(), 44_100);
|
||||
let mut out = Vec::new();
|
||||
for _ in 0..4 {
|
||||
out.extend(dec.decode(&au).unwrap().remove(0));
|
||||
}
|
||||
assert_eq!(out.len(), 4 * 2048);
|
||||
let rms = (out[2048..].iter().map(|s| s * s).sum::<f32>() / (out.len() - 2048) as f32).sqrt();
|
||||
assert!(rms > 1e-4, "{rms}");
|
||||
}
|
||||
|
||||
/// A program config that changes the layout restarts the SBR decoders, even where the
|
||||
/// channel counts match, and one in an access unit that fails leaves their history alone: the
|
||||
/// access units after it decode as if it had not been there.
|
||||
#[test]
|
||||
fn a_new_layout_restarts_sbr_and_a_failed_one_keeps_it() {
|
||||
let au = sbr_au(0);
|
||||
let one = ProgramConfig { front: vec![(false, 0)], ..Default::default() };
|
||||
let six = ProgramConfig { front: vec![(false, 0), (true, 0)], back: vec![(true, 1)], lfe: vec![0], ..Default::default() };
|
||||
let decoder = || {
|
||||
let mut d = Decoder::from_config(AudioSpecificConfig { pce: Some(one.clone()), ..AudioSpecificConfig::lc(7, 0) }).unwrap();
|
||||
d.set_sbr(true);
|
||||
d
|
||||
};
|
||||
let (mut kept, mut failed) = (decoder(), decoder());
|
||||
for _ in 0..3 {
|
||||
assert_eq!(kept.decode(&au).unwrap(), failed.decode(&au).unwrap());
|
||||
}
|
||||
// the new layout's elements are missing from this access unit
|
||||
assert!(failed.decode(&silent_au(Some(&six), &[(ID_SCE, 0)])).is_err());
|
||||
for _ in 0..2 {
|
||||
assert_eq!(kept.decode(&au).unwrap(), failed.decode(&au).unwrap());
|
||||
}
|
||||
// the same channel count under another tag is a new layout: the SBR decoder starts over,
|
||||
// as in a decoder that had that layout from the start
|
||||
let tag1 = ProgramConfig { front: vec![(false, 1)], ..Default::default() };
|
||||
let mut fresh = Decoder::from_config(AudioSpecificConfig { pce: Some(tag1.clone()), ..AudioSpecificConfig::lc(7, 0) }).unwrap();
|
||||
fresh.set_sbr(true);
|
||||
kept.set_layout(tag1.layout());
|
||||
assert_eq!(kept.decode(&sbr_au(1)).unwrap(), fresh.decode(&sbr_au(1)).unwrap());
|
||||
}
|
||||
|
||||
/// One access unit of SCE `tag` with an SBR fill element of [`sbr::test_payload`] (44.1 kHz
|
||||
/// output).
|
||||
fn sbr_au(tag: u32) -> Vec<u8> {
|
||||
let payload = sbr::test_payload(44_100, |_| 40, |_| 0).unwrap();
|
||||
let mut bw = BitWriter::new();
|
||||
bw.write_bits(ID_SCE, 3);
|
||||
bw.write_bits(tag, 4);
|
||||
bw.write_bits(0, 8 + 11 + 3); // global_gain, ics_info (max_sfb 0), no pulse/TNS/gain control
|
||||
bw.write_bits(ID_FIL, 3);
|
||||
let count = (4 + 8 * payload.len()).div_ceil(8);
|
||||
if count < 15 {
|
||||
bw.write_bits(count as u32, 4);
|
||||
} else {
|
||||
bw.write_bits(15, 4);
|
||||
bw.write_bits((count - 14) as u32, 8);
|
||||
}
|
||||
bw.write_bits(EXT_SBR_DATA, 4);
|
||||
for &b in &payload {
|
||||
bw.write_bits(u32::from(b), 8);
|
||||
}
|
||||
bw.write_bits(0, 4);
|
||||
bw.write_bits(ID_END, 3);
|
||||
bw.finish()
|
||||
}
|
||||
|
||||
/// An access unit whose elements are all empty: `elements` are (id, tag), then ID_END.
|
||||
fn silent_au(pce: Option<&ProgramConfig>, elements: &[(u32, u32)]) -> Vec<u8> {
|
||||
let mut bw = BitWriter::new();
|
||||
|
||||
@@ -162,6 +162,16 @@ const HISTORY: usize = 2048;
|
||||
/// Bits per channel in a silent frame, rounded up: the smallest frame the encoder writes.
|
||||
const MIN_FRAME_BITS_PER_CHANNEL: u64 = 48;
|
||||
|
||||
/// The lowest and highest constant bit rate (bits per second) [`Encoder::new`] accepts for a
|
||||
/// stream without ADTS headers: at least 1 kbit/s per channel and a silent frame, at most the
|
||||
/// 6144 bits per channel and frame of the decoder buffer.
|
||||
pub(crate) fn cbr_range(sample_rate: u32, channels: usize) -> (u32, u32) {
|
||||
let (rate, channels) = (u64::from(sample_rate), channels as u64);
|
||||
let silent = (MIN_FRAME_BITS_PER_CHANNEL * channels * rate).div_ceil(FRAME_LEN as u64);
|
||||
let most = 6144 * channels * rate / FRAME_LEN as u64;
|
||||
(u32::try_from(silent.max(1000 * channels)).unwrap_or(u32::MAX), u32::try_from(most).unwrap_or(u32::MAX))
|
||||
}
|
||||
|
||||
/// AAC-LC encoder.
|
||||
pub struct Encoder {
|
||||
cfg: EncoderConfig,
|
||||
|
||||
@@ -62,12 +62,12 @@ const SUB: u32 = 1 << 31;
|
||||
/// entry with `SUB` set is `SUB | offset << 5 | width` and selects a sub-table of `1 << width`
|
||||
/// leaves indexed by the next `width` bits. An entry of 0 is a bit pattern that no codeword starts
|
||||
/// with (the codebooks are complete, so this does not occur).
|
||||
struct Lut {
|
||||
pub(crate) struct Lut {
|
||||
t: Vec<u32>,
|
||||
}
|
||||
|
||||
impl Lut {
|
||||
fn build(codes: &[(u32, u8)]) -> Lut {
|
||||
pub(crate) fn build(codes: &[(u32, u8)]) -> Lut {
|
||||
let mut t = vec![0u32; 1 << L1];
|
||||
// sub-table width per first-level prefix
|
||||
let mut width = vec![0u32; 1 << L1];
|
||||
@@ -103,7 +103,7 @@ impl Lut {
|
||||
|
||||
/// The symbol of the codeword at the reader position; `Eof` if the codeword is cut off.
|
||||
#[inline]
|
||||
fn decode(&self, br: &mut BitReader) -> Result<u32> {
|
||||
pub(crate) fn decode(&self, br: &mut BitReader) -> Result<u32> {
|
||||
let bits = br.peek(32);
|
||||
let mut e = self.t[(bits >> (32 - L1)) as usize];
|
||||
if e & SUB != 0 {
|
||||
|
||||
@@ -5,13 +5,13 @@
|
||||
//! model, window switching, M/S, TNS, bit reservoir, CBR/VBR, 1–8 channels.
|
||||
//! - [`Decoder`] (feature `aac-decode`): `AudioSpecificConfig` + raw access units → planar f32.
|
||||
//! Supports M/S, intensity stereo, PNS, TNS, pulse data, both window shapes and every window
|
||||
//! sequence.
|
||||
//! sequence, and the SBR tool of HE-AAC ([`Decoder::set_sbr`]).
|
||||
//! - [`latm_config`] / [`latm_split`] (feature `aac-decode`): the configuration and payload of
|
||||
//! LOAS / LATM frames (AAC in MPEG transport streams), for the decoder.
|
||||
//!
|
||||
//! HE-AAC (SBR/PS), Main/SSR/LTP profiles, coupling channels and 960-sample frames are not
|
||||
//! supported; for HE-AAC only the AAC-LC core is decoded (at the core rate; [`Decoder::sbr`] says
|
||||
//! when).
|
||||
//! Parametric stereo (HE-AAC v2), Main/SSR/LTP profiles, coupling channels and 960-sample frames
|
||||
//! are not supported. HE-AAC decodes at twice the core rate with [`Decoder::set_sbr`] on
|
||||
//! ([`Decoder::sbr`] says when a stream uses SBR), and as its AAC-LC core otherwise.
|
||||
//!
|
||||
//! From FilmCraft's `filmcraft-aac` (storytold/filmcraft@8fda2f2). Public items keep their names,
|
||||
//! so FilmCraft can re-export this module.
|
||||
@@ -53,21 +53,31 @@ mod ics;
|
||||
#[cfg(feature = "aac-decode")]
|
||||
mod latm;
|
||||
mod mdct;
|
||||
#[cfg(feature = "aac-decode")]
|
||||
mod sbr;
|
||||
mod tables;
|
||||
mod tns;
|
||||
|
||||
#[cfg(feature = "aac-decode")]
|
||||
pub(crate) use config::split_adts_upto;
|
||||
pub use config::{AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, config_layout, split_adts};
|
||||
pub use config::{AdtsConfig, AdtsConfigError, AdtsHeader, AudioSpecificConfig, ElementType, ProgramConfig, config_layout, split_adts};
|
||||
#[cfg(feature = "aac-decode")]
|
||||
pub use decoder::Decoder;
|
||||
#[cfg(feature = "aac-encode")]
|
||||
pub(crate) use encoder::cbr_range;
|
||||
#[cfg(feature = "aac-encode")]
|
||||
pub use encoder::{BitrateMode, Encoder, EncoderConfig, EncoderStats};
|
||||
#[cfg(feature = "aac-decode")]
|
||||
pub use latm::{LatmConfig, latm_config, latm_split};
|
||||
pub use mdct::WindowShape;
|
||||
pub use tables::{SAMPLE_RATES, sample_rate_index};
|
||||
|
||||
/// Output samples by which [`Decoder`] with [`Decoder::set_sbr`] on delays the core: the SBR filter
|
||||
/// banks and the SBR frame's lag behind the core frame. HE-AAC encoders count it in the priming the
|
||||
/// container signals.
|
||||
#[cfg(feature = "aac-decode")]
|
||||
pub const SBR_DELAY: usize = sbr::DELAY;
|
||||
|
||||
/// Samples per AAC-LC frame (per channel).
|
||||
pub const FRAME_LEN: usize = 1024;
|
||||
|
||||
@@ -197,9 +207,9 @@ const UPSAMPLE_DELAY: usize = 481;
|
||||
#[cfg(feature = "aac-decode")]
|
||||
const UPSAMPLE_HISTORY: usize = UPSAMPLE_DELAY + UPSAMPLE_HALF;
|
||||
|
||||
/// 2× upsampler for HE-AAC. The decoder reconstructs the AAC-LC core at half the output rate and
|
||||
/// not yet the SBR high band, so the core is interpolated to the output rate: timing and pitch are
|
||||
/// right and the band above the core's Nyquist stays empty. Even outputs are the input samples,
|
||||
/// 2× upsampler: interpolates an AAC-LC core to twice its rate, leaving the band above the core's
|
||||
/// Nyquist empty. [`Decoder::set_sbr`] decodes HE-AAC with its high band instead; this stays for
|
||||
/// callers that want the core alone at the output rate. Even outputs are the input samples,
|
||||
/// odd ones a Blackman-windowed sinc half-band interpolation over 2 × `UPSAMPLE_HALF` inputs.
|
||||
/// The output is delayed by `UPSAMPLE_DELAY` input samples, as an SBR decoder's is; the state
|
||||
/// carries across packets, so a seek primed with the preceding packet gives the same samples as
|
||||
|
||||
@@ -0,0 +1,493 @@
|
||||
//! The SBR bitstream (ISO/IEC 14496-3 §4.4.2.8 syntax, §4.5.2.8 semantics): the header and the
|
||||
//! single and channel pair elements of an `EXT_SBR_DATA` fill element.
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use craft_media::bitstream::BitReader;
|
||||
|
||||
use super::freq::FreqTables;
|
||||
use super::tables::{
|
||||
F_ENV_1_5DB, F_ENV_3_0DB, F_ENV_BAL_1_5DB, F_ENV_BAL_3_0DB, T_ENV_1_5DB, T_ENV_3_0DB, T_ENV_BAL_1_5DB, T_ENV_BAL_3_0DB, T_NOISE_3_0DB,
|
||||
T_NOISE_BAL_3_0DB,
|
||||
};
|
||||
use super::{Header, MAX_ENV};
|
||||
use crate::aac::huffman::Lut;
|
||||
use crate::aac::{Error, Result};
|
||||
|
||||
/// `bs_frame_class`: fixed leading and trailing frame borders.
|
||||
pub(crate) const FIXFIX: u8 = 0;
|
||||
/// Fixed leading border, variable trailing border.
|
||||
pub(crate) const FIXVAR: u8 = 1;
|
||||
/// Variable leading border, fixed trailing border.
|
||||
pub(crate) const VARFIX: u8 = 2;
|
||||
/// Variable leading and trailing borders.
|
||||
pub(crate) const VARVAR: u8 = 3;
|
||||
|
||||
/// SBR time slots per frame (`numTimeSlots`; two QMF slots each).
|
||||
const TIME_SLOTS: usize = 16;
|
||||
|
||||
/// Decoding tables for the SBR Huffman codes, in [`Book`] order.
|
||||
fn luts() -> &'static [Lut; 10] {
|
||||
static LUTS: OnceLock<[Lut; 10]> = OnceLock::new();
|
||||
LUTS.get_or_init(|| {
|
||||
[
|
||||
Lut::build(&T_ENV_1_5DB),
|
||||
Lut::build(&F_ENV_1_5DB),
|
||||
Lut::build(&T_ENV_BAL_1_5DB),
|
||||
Lut::build(&F_ENV_BAL_1_5DB),
|
||||
Lut::build(&T_ENV_3_0DB),
|
||||
Lut::build(&F_ENV_3_0DB),
|
||||
Lut::build(&T_ENV_BAL_3_0DB),
|
||||
Lut::build(&F_ENV_BAL_3_0DB),
|
||||
Lut::build(&T_NOISE_3_0DB),
|
||||
Lut::build(&T_NOISE_BAL_3_0DB),
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
/// One of the SBR Huffman codes, and its largest absolute value.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Book(usize, i32);
|
||||
|
||||
const T_ENV_15: Book = Book(0, 60);
|
||||
const F_ENV_15: Book = Book(1, 60);
|
||||
const T_BAL_15: Book = Book(2, 24);
|
||||
const F_BAL_15: Book = Book(3, 24);
|
||||
const T_ENV_30: Book = Book(4, 31);
|
||||
const F_ENV_30: Book = Book(5, 31);
|
||||
const T_BAL_30: Book = Book(6, 12);
|
||||
const F_BAL_30: Book = Book(7, 12);
|
||||
const T_NOISE: Book = Book(8, 31);
|
||||
const T_NOISE_BAL: Book = Book(9, 12);
|
||||
|
||||
fn huff(br: &mut BitReader, book: Book) -> Result<i32> {
|
||||
Ok(luts()[book.0].decode(br)? as i32 - book.1)
|
||||
}
|
||||
|
||||
fn bits(br: &mut BitReader, n: u32) -> Result<usize> {
|
||||
Ok(br.read_bits(n)? as usize)
|
||||
}
|
||||
|
||||
impl Header {
|
||||
/// `sbr_header()`, with the defaults of the fields its extra parts leave out.
|
||||
pub(crate) fn read(br: &mut BitReader) -> Result<Header> {
|
||||
let amp_res = br.read_flag()?;
|
||||
let start_freq = br.read_bits(4)? as u8;
|
||||
let stop_freq = br.read_bits(4)? as u8;
|
||||
let xover_band = br.read_bits(3)? as u8;
|
||||
br.skip(2)?; // bs_reserved
|
||||
let (extra_1, extra_2) = (br.read_flag()?, br.read_flag()?);
|
||||
let mut h = Header {
|
||||
amp_res,
|
||||
start_freq,
|
||||
stop_freq,
|
||||
xover_band,
|
||||
freq_scale: 2,
|
||||
alter_scale: true,
|
||||
noise_bands: 2,
|
||||
limiter_bands: 2,
|
||||
limiter_gains: 2,
|
||||
interpol_freq: true,
|
||||
smoothing_mode: true,
|
||||
};
|
||||
if extra_1 {
|
||||
h.freq_scale = br.read_bits(2)? as u8;
|
||||
h.alter_scale = br.read_flag()?;
|
||||
h.noise_bands = br.read_bits(2)? as u8;
|
||||
}
|
||||
if extra_2 {
|
||||
h.limiter_bands = br.read_bits(2)? as u8;
|
||||
h.limiter_gains = br.read_bits(2)? as u8;
|
||||
h.interpol_freq = br.read_flag()?;
|
||||
h.smoothing_mode = br.read_flag()?;
|
||||
}
|
||||
Ok(h)
|
||||
}
|
||||
}
|
||||
|
||||
/// The time/frequency grid of one channel in one frame (`sbr_grid()`, §4.6.18.3.3).
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct Grid {
|
||||
pub(crate) class: u8,
|
||||
/// Envelopes (1 to 5).
|
||||
pub(crate) num_env: usize,
|
||||
/// Envelope borders in time slots; `t_env[num_env]` may reach past the frame (up to 19).
|
||||
pub(crate) t_env: [usize; MAX_ENV + 1],
|
||||
/// Noise floors (1 or 2).
|
||||
pub(crate) num_noise: usize,
|
||||
/// Noise floor borders.
|
||||
pub(crate) t_noise: [usize; 3],
|
||||
/// High frequency resolution per envelope.
|
||||
pub(crate) freq_res: [bool; MAX_ENV],
|
||||
/// The transient envelope `l_A`, if any (it may be `num_env`: the next frame's first).
|
||||
pub(crate) la: Option<usize>,
|
||||
/// 3 dB envelope steps (the header's choice, but 1.5 dB for a single FIXFIX envelope).
|
||||
pub(crate) amp_res: bool,
|
||||
}
|
||||
|
||||
/// `ceil(log2(num_env + 1))`: the width of `bs_pointer`.
|
||||
fn pointer_bits(num_env: usize) -> u32 {
|
||||
usize::BITS - num_env.leading_zeros()
|
||||
}
|
||||
|
||||
fn read_rel(br: &mut BitReader, n: usize) -> Result<[usize; 3]> {
|
||||
let mut rel = [0; 3];
|
||||
for r in rel.iter_mut().take(n) {
|
||||
*r = 2 * bits(br, 2)? + 2;
|
||||
}
|
||||
Ok(rel)
|
||||
}
|
||||
|
||||
impl Grid {
|
||||
fn read(br: &mut BitReader, amp_res: bool) -> Result<Grid> {
|
||||
let bad = || Error::Bitstream("invalid SBR time grid");
|
||||
let class = br.read_bits(2)? as u8;
|
||||
let mut t = [0i64; MAX_ENV + 1];
|
||||
let mut freq_res = [false; MAX_ENV];
|
||||
let (n, pointer) = match class {
|
||||
FIXFIX => {
|
||||
let n = 1usize << br.read_bits(2)?;
|
||||
if n > 4 {
|
||||
return Err(bad());
|
||||
}
|
||||
freq_res = [br.read_flag()?; MAX_ENV];
|
||||
for (l, t) in t.iter_mut().enumerate().take(n + 1) {
|
||||
*t = (l * TIME_SLOTS / n) as i64;
|
||||
}
|
||||
(n, 0)
|
||||
}
|
||||
FIXVAR | VARFIX => {
|
||||
let abs = bits(br, 2)?;
|
||||
let rels = bits(br, 2)?;
|
||||
let n = rels + 1;
|
||||
let rel = read_rel(br, rels)?;
|
||||
let pointer = bits(br, pointer_bits(n))?;
|
||||
for l in 0..n {
|
||||
freq_res[if class == FIXVAR { n - 1 - l } else { l }] = br.read_flag()?;
|
||||
}
|
||||
if class == FIXVAR {
|
||||
t[n] = (abs + TIME_SLOTS) as i64;
|
||||
for l in (1..n).rev() {
|
||||
t[l] = t[l + 1] - rel[n - 1 - l] as i64;
|
||||
}
|
||||
} else {
|
||||
(t[0], t[n]) = (abs as i64, TIME_SLOTS as i64);
|
||||
for l in 1..n {
|
||||
t[l] = t[l - 1] + rel[l - 1] as i64;
|
||||
}
|
||||
}
|
||||
(n, pointer)
|
||||
}
|
||||
_ => {
|
||||
let abs0 = bits(br, 2)?;
|
||||
let abs1 = bits(br, 2)? + TIME_SLOTS;
|
||||
let (rels0, rels1) = (bits(br, 2)?, bits(br, 2)?);
|
||||
let n = rels0 + rels1 + 1;
|
||||
if n > MAX_ENV {
|
||||
return Err(bad());
|
||||
}
|
||||
let (rel0, rel1) = (read_rel(br, rels0)?, read_rel(br, rels1)?);
|
||||
let pointer = bits(br, pointer_bits(n))?;
|
||||
for r in freq_res.iter_mut().take(n) {
|
||||
*r = br.read_flag()?;
|
||||
}
|
||||
(t[0], t[n]) = (abs0 as i64, abs1 as i64);
|
||||
for l in 1..=rels0 {
|
||||
t[l] = t[l - 1] + rel0[l - 1] as i64;
|
||||
}
|
||||
for l in (rels0 + 1..n).rev() {
|
||||
t[l] = t[l + 1] - rel1[n - 1 - l] as i64;
|
||||
}
|
||||
(n, pointer)
|
||||
}
|
||||
};
|
||||
if t[0] < 0 || t[..=n].windows(2).any(|w| w[0] >= w[1]) {
|
||||
return Err(bad());
|
||||
}
|
||||
// l_A: in ISO/IEC 14496-3 a VARFIX pointer of 1, like 0, marks no transient envelope
|
||||
let la = match (pointer, class) {
|
||||
(0, _) | (1, VARFIX) => None,
|
||||
(p, FIXVAR | VARVAR) => Some((n + 1).checked_sub(p).ok_or_else(bad)?),
|
||||
(p, _) => Some(p - 1),
|
||||
};
|
||||
if la.is_some_and(|la| la > n) {
|
||||
return Err(bad());
|
||||
}
|
||||
let t_env: [usize; MAX_ENV + 1] = std::array::from_fn(|i| t[i] as usize);
|
||||
let (num_noise, t_noise) = if n == 1 {
|
||||
(1, [t_env[0], t_env[1], 0])
|
||||
} else {
|
||||
let middle = match (class, pointer) {
|
||||
(FIXFIX, _) => n / 2,
|
||||
(VARFIX, 0) => 1,
|
||||
(VARFIX, 1) => n - 1,
|
||||
(VARFIX, p) => p - 1,
|
||||
(_, 0 | 1) => n - 1,
|
||||
(_, p) => (n + 1).checked_sub(p).ok_or_else(bad)?,
|
||||
};
|
||||
if middle == 0 || middle >= n {
|
||||
return Err(bad());
|
||||
}
|
||||
(2, [t_env[0], t_env[middle], t_env[n]])
|
||||
};
|
||||
Ok(Grid { class, num_env: n, t_env, num_noise, t_noise, freq_res, la, amp_res: amp_res && !(class == FIXFIX && n == 1) })
|
||||
}
|
||||
}
|
||||
|
||||
/// The data of one channel as transmitted: delta-coded envelope and noise floor values (the
|
||||
/// first frequency-coded value of each is absolute), not yet scaled for a balance channel.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct ChannelData {
|
||||
pub(crate) grid: Grid,
|
||||
pub(crate) df_env: [bool; MAX_ENV],
|
||||
pub(crate) df_noise: [bool; 2],
|
||||
pub(crate) invf: [u8; 5],
|
||||
pub(crate) env: [[i32; 64]; MAX_ENV],
|
||||
pub(crate) noise: [[i32; 5]; 2],
|
||||
/// `bs_add_harmonic` per high-resolution band, when `bs_add_harmonic_flag` is set.
|
||||
pub(crate) harmonic: Option<[bool; 64]>,
|
||||
}
|
||||
|
||||
impl ChannelData {
|
||||
fn new(grid: Grid) -> ChannelData {
|
||||
ChannelData {
|
||||
grid,
|
||||
df_env: [false; MAX_ENV],
|
||||
df_noise: [false; 2],
|
||||
invf: [0; 5],
|
||||
env: [[0; 64]; MAX_ENV],
|
||||
noise: [[0; 5]; 2],
|
||||
harmonic: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn read_dtdf(&mut self, br: &mut BitReader) -> Result<()> {
|
||||
for d in self.df_env.iter_mut().take(self.grid.num_env) {
|
||||
*d = br.read_flag()?;
|
||||
}
|
||||
for d in self.df_noise.iter_mut().take(self.grid.num_noise) {
|
||||
*d = br.read_flag()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_invf(&mut self, br: &mut BitReader, t: &FreqTables) -> Result<()> {
|
||||
for m in self.invf.iter_mut().take(t.n_q()) {
|
||||
*m = br.read_bits(2)? as u8;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `sbr_envelope()`; `balance` for the second channel of a coupled pair.
|
||||
fn read_envelope(&mut self, br: &mut BitReader, t: &FreqTables, balance: bool) -> Result<()> {
|
||||
let (t_book, f_book, start_bits) = match (self.grid.amp_res, balance) {
|
||||
(false, false) => (T_ENV_15, F_ENV_15, 7),
|
||||
(false, true) => (T_BAL_15, F_BAL_15, 6),
|
||||
(true, false) => (T_ENV_30, F_ENV_30, 6),
|
||||
(true, true) => (T_BAL_30, F_BAL_30, 5),
|
||||
};
|
||||
for l in 0..self.grid.num_env {
|
||||
let n = t.bands(self.grid.freq_res[l]).len() - 1;
|
||||
let row = &mut self.env[l];
|
||||
if self.df_env[l] {
|
||||
for v in row.iter_mut().take(n) {
|
||||
*v = huff(br, t_book)?;
|
||||
}
|
||||
} else {
|
||||
row[0] = br.read_bits(start_bits)? as i32;
|
||||
for v in row.iter_mut().take(n).skip(1) {
|
||||
*v = huff(br, f_book)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `sbr_noise()`.
|
||||
fn read_noise(&mut self, br: &mut BitReader, t: &FreqTables, balance: bool) -> Result<()> {
|
||||
let (t_book, f_book) = if balance { (T_NOISE_BAL, F_BAL_30) } else { (T_NOISE, F_ENV_30) };
|
||||
for l in 0..self.grid.num_noise {
|
||||
let row = &mut self.noise[l];
|
||||
if self.df_noise[l] {
|
||||
for v in row.iter_mut().take(t.n_q()) {
|
||||
*v = huff(br, t_book)?;
|
||||
}
|
||||
} else {
|
||||
row[0] = br.read_bits(5)? as i32;
|
||||
for v in row.iter_mut().take(t.n_q()).skip(1) {
|
||||
*v = huff(br, f_book)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `bs_add_harmonic_flag` and `sbr_sinusoidal_coding()`.
|
||||
fn read_harmonics(&mut self, br: &mut BitReader, t: &FreqTables) -> Result<()> {
|
||||
if br.read_flag()? {
|
||||
let mut h = [false; 64];
|
||||
for v in h.iter_mut().take(t.n_high()) {
|
||||
*v = br.read_flag()?;
|
||||
}
|
||||
self.harmonic = Some(h);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Skip `bs_extended_data` (parametric stereo, for one, is not decoded).
|
||||
fn skip_extended(br: &mut BitReader) -> Result<()> {
|
||||
if br.read_flag()? {
|
||||
let mut count = bits(br, 4)?;
|
||||
if count == 15 {
|
||||
count += bits(br, 8)?;
|
||||
}
|
||||
br.skip(8 * count)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The SBR data of one channel element.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Frame {
|
||||
/// The pair codes its second channel as a balance against the first.
|
||||
pub(crate) coupling: bool,
|
||||
pub(crate) channels: Vec<ChannelData>,
|
||||
}
|
||||
|
||||
/// `sbr_single_channel_element()` (`channels` 1) or `sbr_channel_pair_element()` (2).
|
||||
pub(crate) fn read_data(br: &mut BitReader, t: &FreqTables, amp_res: bool, channels: usize) -> Result<Frame> {
|
||||
if br.read_flag()? {
|
||||
br.skip(4 * channels)?; // bs_reserved
|
||||
}
|
||||
if channels == 1 {
|
||||
let mut c = ChannelData::new(Grid::read(br, amp_res)?);
|
||||
c.read_dtdf(br)?;
|
||||
c.read_invf(br, t)?;
|
||||
c.read_envelope(br, t, false)?;
|
||||
c.read_noise(br, t, false)?;
|
||||
c.read_harmonics(br, t)?;
|
||||
skip_extended(br)?;
|
||||
return Ok(Frame { coupling: false, channels: vec![c] });
|
||||
}
|
||||
let coupling = br.read_flag()?;
|
||||
let (mut a, mut b);
|
||||
if coupling {
|
||||
a = ChannelData::new(Grid::read(br, amp_res)?);
|
||||
b = ChannelData::new(a.grid.clone());
|
||||
a.read_dtdf(br)?;
|
||||
b.read_dtdf(br)?;
|
||||
a.read_invf(br, t)?;
|
||||
b.invf = a.invf;
|
||||
a.read_envelope(br, t, false)?;
|
||||
a.read_noise(br, t, false)?;
|
||||
b.read_envelope(br, t, true)?;
|
||||
b.read_noise(br, t, true)?;
|
||||
} else {
|
||||
a = ChannelData::new(Grid::read(br, amp_res)?);
|
||||
b = ChannelData::new(Grid::read(br, amp_res)?);
|
||||
a.read_dtdf(br)?;
|
||||
b.read_dtdf(br)?;
|
||||
a.read_invf(br, t)?;
|
||||
b.read_invf(br, t)?;
|
||||
a.read_envelope(br, t, false)?;
|
||||
b.read_envelope(br, t, false)?;
|
||||
a.read_noise(br, t, false)?;
|
||||
b.read_noise(br, t, false)?;
|
||||
}
|
||||
a.read_harmonics(br, t)?;
|
||||
b.read_harmonics(br, t)?;
|
||||
skip_extended(br)?;
|
||||
Ok(Frame { coupling, channels: vec![a, b] })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use craft_media::bitstream::BitWriter;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Every SBR Huffman table is a complete prefix code, with the shortest codeword for a zero
|
||||
/// delta, and its decoder reads back every codeword.
|
||||
#[test]
|
||||
fn huffman_tables_are_complete_prefix_codes() {
|
||||
let tables: [&[(u32, u8)]; 10] = [
|
||||
&T_ENV_1_5DB,
|
||||
&F_ENV_1_5DB,
|
||||
&T_ENV_BAL_1_5DB,
|
||||
&F_ENV_BAL_1_5DB,
|
||||
&T_ENV_3_0DB,
|
||||
&F_ENV_3_0DB,
|
||||
&T_ENV_BAL_3_0DB,
|
||||
&F_ENV_BAL_3_0DB,
|
||||
&T_NOISE_3_0DB,
|
||||
&T_NOISE_BAL_3_0DB,
|
||||
];
|
||||
let lavs = [60, 60, 24, 24, 31, 31, 12, 12, 31, 12];
|
||||
for (i, (codes, lav)) in tables.iter().zip(lavs).enumerate() {
|
||||
assert_eq!(codes.len(), 2 * lav + 1, "table {i}");
|
||||
let max = u32::from(codes.iter().map(|c| c.1).max().unwrap());
|
||||
assert_eq!(codes.iter().map(|&(_, l)| 1u64 << (max - u32::from(l))).sum::<u64>(), 1 << max, "table {i}: Kraft sum");
|
||||
let shortest = codes.iter().map(|c| c.1).min().unwrap();
|
||||
assert_eq!(codes[lav].1, shortest, "table {i}");
|
||||
for (sym, &(code, len)) in codes.iter().enumerate() {
|
||||
let mut bw = BitWriter::new();
|
||||
bw.write_bits(code, u32::from(len));
|
||||
bw.write_bits(0x5A5A5A, 24);
|
||||
let data = bw.finish();
|
||||
let mut br = BitReader::new(&data);
|
||||
assert_eq!(huff(&mut br, Book(i, lav as i32)).unwrap(), sym as i32 - lav as i32, "table {i}");
|
||||
assert_eq!(br.position(), usize::from(len));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn grid(bits: &[(u32, u32)]) -> Result<Grid> {
|
||||
let mut bw = BitWriter::new();
|
||||
for &(v, n) in bits {
|
||||
bw.write_bits(v, n);
|
||||
}
|
||||
bw.write_bits(0, 16);
|
||||
let data = bw.finish();
|
||||
Grid::read(&mut BitReader::new(&data), true)
|
||||
}
|
||||
|
||||
/// The four frame classes give the borders, noise floors and transient envelope of
|
||||
/// §4.6.18.3.3; a FIXFIX frame of one envelope uses 1.5 dB steps.
|
||||
#[test]
|
||||
fn grids_of_each_frame_class() {
|
||||
// FIXFIX, 4 envelopes, high resolution
|
||||
let g = grid(&[(FIXFIX as u32, 2), (2, 2), (1, 1)]).unwrap();
|
||||
assert_eq!((g.num_env, &g.t_env[..5], g.num_noise, g.t_noise, g.la, g.amp_res), (4, &[0, 4, 8, 12, 16][..], 2, [0, 8, 16], None, true));
|
||||
let g = grid(&[(FIXFIX as u32, 2), (0, 2), (0, 1)]).unwrap();
|
||||
assert_eq!((g.num_env, g.t_noise, g.amp_res), (1, [0, 16, 0], false));
|
||||
// FIXVAR: trailing border 16 + 2, two relative borders (4, 2) back from it, pointer 1,
|
||||
// resolutions sent last envelope first
|
||||
let g = grid(&[(FIXVAR as u32, 2), (2, 2), (2, 2), (1, 2), (0, 2), (1, 2), (0, 1), (1, 1), (1, 1)]).unwrap();
|
||||
assert_eq!((g.num_env, &g.t_env[..4]), (3, &[0, 12, 14, 18][..]));
|
||||
assert_eq!((g.la, g.t_noise, g.freq_res[..3].to_vec()), (Some(3), [0, 14, 18], vec![true, true, false]));
|
||||
// VARFIX: leading border 1, relative 2, pointer 2: l_A = 1
|
||||
let g = grid(&[(VARFIX as u32, 2), (1, 2), (1, 2), (0, 2), (2, 2), (1, 1), (0, 1)]).unwrap();
|
||||
assert_eq!((g.num_env, &g.t_env[..3], g.la, g.t_noise), (2, &[1, 3, 16][..], Some(1), [1, 3, 16]));
|
||||
// the same with pointer 1: no transient envelope, the noise floors still split at 3
|
||||
let g = grid(&[(VARFIX as u32, 2), (1, 2), (1, 2), (0, 2), (1, 2), (1, 1), (0, 1)]).unwrap();
|
||||
assert_eq!((g.la, g.t_noise), (None, [1, 3, 16]));
|
||||
// VARVAR: 1 .. 17 with one border from each end
|
||||
let g = grid(&[(VARVAR as u32, 2), (1, 2), (1, 2), (1, 2), (1, 2), (0, 2), (0, 2), (0, 2), (0, 1), (0, 1), (0, 1)]).unwrap();
|
||||
assert_eq!((g.num_env, &g.t_env[..4], g.la), (3, &[1, 3, 15, 17][..], None));
|
||||
}
|
||||
|
||||
/// Grids whose borders do not increase, too many envelopes and a pointer past the envelopes
|
||||
/// are refused.
|
||||
#[test]
|
||||
fn invalid_grids_are_refused() {
|
||||
// FIXFIX with 8 envelopes
|
||||
assert!(grid(&[(FIXFIX as u32, 2), (3, 2), (0, 1)]).is_err());
|
||||
// VARVAR with 7 envelopes
|
||||
assert!(grid(&[(VARVAR as u32, 2), (0, 2), (0, 2), (3, 2), (3, 2)]).is_err());
|
||||
// FIXVAR whose relative borders run back past the start: 16 - (8 + 8 + 8)
|
||||
assert!(grid(&[(FIXVAR as u32, 2), (0, 2), (3, 2), (3, 2), (3, 2), (3, 2), (0, 3), (0, 4)]).is_err());
|
||||
// VARFIX, 2 envelopes, pointer 3: the middle border would be the trailing one
|
||||
assert!(grid(&[(VARFIX as u32, 2), (0, 2), (1, 2), (0, 2), (3, 2), (0, 1), (0, 1)]).is_err());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
//! Frequency band tables (ISO/IEC 14496-3 §4.6.18.3.2) and the patches and limiter bands built on
|
||||
//! them (§4.6.18.6.3, §4.6.18.7.3). Every table holds QMF channel borders.
|
||||
|
||||
use super::Header;
|
||||
|
||||
/// The most patches the HF generator may build.
|
||||
const MAX_PATCHES: usize = 5;
|
||||
|
||||
/// The tables of one SBR header at one output rate.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct FreqTables {
|
||||
/// First QMF channel of the master table (the start of the SBR range before the crossover).
|
||||
pub(crate) k0: usize,
|
||||
/// First QMF channel of the SBR range.
|
||||
pub(crate) kx: usize,
|
||||
/// Channels in the SBR range (`kx + m` is its end).
|
||||
pub(crate) m: usize,
|
||||
/// High-resolution envelope bands (`n_high + 1` borders).
|
||||
pub(crate) high: Vec<usize>,
|
||||
/// Low-resolution envelope bands.
|
||||
pub(crate) low: Vec<usize>,
|
||||
/// Noise floor bands (1 to 5).
|
||||
pub(crate) noise: Vec<usize>,
|
||||
/// Limiter bands.
|
||||
pub(crate) lim: Vec<usize>,
|
||||
/// `(first source channel, channels)` of each patch, in order from `kx` up.
|
||||
pub(crate) patches: Vec<(usize, usize)>,
|
||||
}
|
||||
|
||||
/// Round half up, as the specification's `NINT`.
|
||||
fn nint(x: f64) -> i64 {
|
||||
(x + 0.5).floor() as i64
|
||||
}
|
||||
|
||||
/// The start channel offsets of `bs_start_freq`, by output rate (§4.6.18.3.2.1).
|
||||
fn start_offsets(fs: u32) -> &'static [i8; 16] {
|
||||
match fs {
|
||||
0..=16_000 => &[-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7],
|
||||
16_001..=22_050 => &[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13],
|
||||
22_051..=24_000 => &[-5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16],
|
||||
24_001..=32_000 => &[-6, -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16],
|
||||
32_001..=64_000 => &[-4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20],
|
||||
_ => &[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24],
|
||||
}
|
||||
}
|
||||
|
||||
/// `NINT(f · 128 / fs)` for the 3/4/5 kHz (start) or 6/8/10 kHz (stop) minimum at `fs`.
|
||||
fn min_channel(fs: u32, low: f64, mid: f64, high: f64) -> i64 {
|
||||
let f = if fs < 32_000 {
|
||||
low
|
||||
} else if fs < 64_000 {
|
||||
mid
|
||||
} else {
|
||||
high
|
||||
};
|
||||
nint(f * 128.0 / f64::from(fs))
|
||||
}
|
||||
|
||||
/// The start and stop channels `k0`, `k2` of the master table.
|
||||
fn start_stop(h: &Header, fs: u32) -> Option<(usize, usize)> {
|
||||
let k0 = min_channel(fs, 3000.0, 4000.0, 5000.0) + i64::from(start_offsets(fs)[usize::from(h.start_freq & 15)]);
|
||||
let k2 = match h.stop_freq {
|
||||
14 => 2 * k0,
|
||||
15 => 3 * k0,
|
||||
s => {
|
||||
let stop_min = min_channel(fs, 6000.0, 8000.0, 10_000.0);
|
||||
if stop_min <= 0 {
|
||||
return None;
|
||||
}
|
||||
let ratio = 64.0 / stop_min as f64;
|
||||
let at = |p: i64| nint(stop_min as f64 * ratio.powf(p as f64 / 13.0));
|
||||
let mut dk: Vec<i64> = (0..13).map(|p| at(p + 1) - at(p)).collect();
|
||||
dk.sort_unstable();
|
||||
stop_min + dk.iter().take(usize::from(s)).sum::<i64>()
|
||||
}
|
||||
}
|
||||
.min(64);
|
||||
// the low band reaches only the analysis bank's 32 channels (`kx` is checked against them
|
||||
// once the crossover is known)
|
||||
(1..=32).contains(&k0).then_some(())?;
|
||||
(k2 > k0).then_some((k0 as usize, k2 as usize))
|
||||
}
|
||||
|
||||
/// The master table for `bs_freq_scale` 0: bands of one or two channels.
|
||||
fn master_linear(k0: usize, k2: usize, alter_scale: bool) -> Option<Vec<usize>> {
|
||||
let (dk, bands) = if alter_scale { (2, 2 * nint((k2 - k0) as f64 / 4.0) as usize) } else { (1, 2 * ((k2 - k0) / 2)) };
|
||||
if bands == 0 {
|
||||
return None;
|
||||
}
|
||||
let mut v = vec![dk as i64; bands];
|
||||
let mut diff = k2 as i64 - (k0 + bands * dk) as i64;
|
||||
// take the difference from the lowest bands (too wide) or give it to the highest (too narrow)
|
||||
let mut k = if diff < 0 { 0 } else { bands - 1 };
|
||||
while diff != 0 {
|
||||
let step = if diff < 0 { 1 } else { -1 };
|
||||
*v.get_mut(k)? -= step;
|
||||
diff += step;
|
||||
k = if step == 1 { k + 1 } else { k.checked_sub(1)? };
|
||||
}
|
||||
cumulative(k0, &v)
|
||||
}
|
||||
|
||||
/// The borders `start, start + d[0], …`; `None` unless every band is at least a channel wide.
|
||||
fn cumulative(start: usize, d: &[i64]) -> Option<Vec<usize>> {
|
||||
let mut out = Vec::with_capacity(d.len() + 1);
|
||||
out.push(start);
|
||||
let mut at = start as i64;
|
||||
for &d in d {
|
||||
if d <= 0 {
|
||||
return None;
|
||||
}
|
||||
at += d;
|
||||
out.push(at as usize);
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// The band widths of `2 · NINT(bands · log2(hi / lo) / (2 · warp))` logarithmic bands.
|
||||
fn log_widths(lo: usize, hi: usize, bands: f64, warp: f64) -> Vec<i64> {
|
||||
let ratio = hi as f64 / lo as f64;
|
||||
let n = 2 * nint(bands * ratio.log2() / (2.0 * warp));
|
||||
let at = |k: i64| nint(lo as f64 * ratio.powf(k as f64 / n as f64));
|
||||
let mut d: Vec<i64> = (0..n.max(0)).map(|k| at(k + 1) - at(k)).collect();
|
||||
d.sort_unstable();
|
||||
d
|
||||
}
|
||||
|
||||
/// The master table for `bs_freq_scale` 1–3: 12, 10 or 8 bands per octave, warped above
|
||||
/// `2 · k0` by `bs_alter_scale`.
|
||||
fn master_log(k0: usize, k2: usize, freq_scale: u8, alter_scale: bool) -> Option<Vec<usize>> {
|
||||
let bands = [12.0, 10.0, 8.0][usize::from(freq_scale.clamp(1, 3) - 1)];
|
||||
let two_regions = k2 as f64 / k0 as f64 > 2.2449;
|
||||
let k1 = if two_regions { 2 * k0 } else { k2 };
|
||||
let d0 = log_widths(k0, k1, bands, 1.0);
|
||||
let mut table = cumulative(k0, &d0)?;
|
||||
if table.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
if two_regions {
|
||||
let mut d1 = log_widths(k1, k2, bands, if alter_scale { 1.3 } else { 1.0 });
|
||||
let max0 = d0.iter().copied().max()?;
|
||||
if d1.first().is_some_and(|&d| d < max0) {
|
||||
// widen the first upper band to the widest lower one, at the cost of the last
|
||||
let change = max0 - d1[0];
|
||||
d1[0] = max0;
|
||||
*d1.last_mut()? -= change;
|
||||
d1.sort_unstable();
|
||||
}
|
||||
table.extend(cumulative(k1, &d1)?.into_iter().skip(1));
|
||||
}
|
||||
Some(table)
|
||||
}
|
||||
|
||||
impl FreqTables {
|
||||
/// The tables of `h` at output rate `fs`, or `None` if the header asks for an invalid layout.
|
||||
pub(crate) fn new(h: &Header, fs: u32) -> Option<FreqTables> {
|
||||
let (k0, k2) = start_stop(h, fs)?;
|
||||
let master = if h.freq_scale == 0 { master_linear(k0, k2, h.alter_scale)? } else { master_log(k0, k2, h.freq_scale, h.alter_scale)? };
|
||||
let n_master = master.len().checked_sub(1)?;
|
||||
let xover = usize::from(h.xover_band);
|
||||
if xover >= n_master {
|
||||
return None;
|
||||
}
|
||||
let high: Vec<usize> = master.get(xover..)?.to_vec();
|
||||
let n_high = high.len() - 1;
|
||||
let (kx, end) = (high[0], *high.last()?);
|
||||
if kx > 32 || end > 64 || end <= kx {
|
||||
return None;
|
||||
}
|
||||
let m = end - kx;
|
||||
// every other border, keeping the first (and the second when the count is odd)
|
||||
let n_low = n_high - n_high / 2;
|
||||
let low: Vec<usize> = (0..=n_low).map(|k| high[if k == 0 { 0 } else { 2 * k - (n_high & 1) }]).collect();
|
||||
let n_q = if h.noise_bands == 0 { 1 } else { (nint(f64::from(h.noise_bands) * (end as f64 / kx as f64).log2()) as usize).max(1) };
|
||||
if n_q > 5 || n_q > n_low {
|
||||
return None;
|
||||
}
|
||||
let mut noise = vec![low[0]];
|
||||
let mut i = 0;
|
||||
for k in 1..=n_q {
|
||||
i += (n_low - i) / (n_q + 1 - k);
|
||||
noise.push(*low.get(i)?);
|
||||
}
|
||||
let patches = patches(&master, k0, kx, m, fs)?;
|
||||
let lim = limiter(&low, &patches, kx, h.limiter_bands);
|
||||
Some(FreqTables { k0, kx, m, high, low, noise, lim, patches })
|
||||
}
|
||||
|
||||
/// High-resolution envelope bands.
|
||||
pub(crate) fn n_high(&self) -> usize {
|
||||
self.high.len() - 1
|
||||
}
|
||||
|
||||
/// Noise floor bands.
|
||||
pub(crate) fn n_q(&self) -> usize {
|
||||
self.noise.len() - 1
|
||||
}
|
||||
|
||||
/// The envelope band table of a resolution (`true`: high).
|
||||
pub(crate) fn bands(&self, high_res: bool) -> &[usize] {
|
||||
if high_res { &self.high } else { &self.low }
|
||||
}
|
||||
}
|
||||
|
||||
/// Map the low band onto the SBR range (§4.6.18.6.3): runs of source channels, each ending below
|
||||
/// `k0`, that together cover `kx .. kx + m`.
|
||||
fn patches(master: &[usize], k0: usize, kx: usize, m: usize, fs: u32) -> Option<Vec<(usize, usize)>> {
|
||||
let n_master = master.len() - 1;
|
||||
let goal = nint(2.048e6 / f64::from(fs)).max(0) as usize;
|
||||
let mut k = if goal < kx + m { master.iter().position(|&f| f >= goal).unwrap_or(n_master) } else { n_master };
|
||||
let (mut msb, mut usb) = (k0, kx);
|
||||
let mut out: Vec<(usize, usize)> = Vec::new();
|
||||
// each pass either adds a patch or lowers `msb` to `kx`; a stream that does neither twice
|
||||
// running cannot finish. One patch over the limit may still be a short last one that is
|
||||
// dropped below.
|
||||
for _ in 0..2 * (MAX_PATCHES + 2) {
|
||||
// the highest master border the patch can reach from below k0
|
||||
let mut j = k + 1;
|
||||
let (sb, odd) = loop {
|
||||
j = j.checked_sub(1)?;
|
||||
let sb = *master.get(j)?;
|
||||
let odd = (sb + k0) % 2;
|
||||
if sb + odd < k0 + msb {
|
||||
break (sb, odd);
|
||||
}
|
||||
};
|
||||
let count = sb.saturating_sub(usb);
|
||||
if count > 0 {
|
||||
let start = (k0 - odd).checked_sub(count)?;
|
||||
out.push((start, count));
|
||||
if out.len() > MAX_PATCHES + 1 {
|
||||
return None;
|
||||
}
|
||||
(usb, msb) = (sb, sb);
|
||||
} else {
|
||||
msb = kx;
|
||||
}
|
||||
if master.get(k)?.saturating_sub(sb) < 3 {
|
||||
k = n_master;
|
||||
}
|
||||
if sb == kx + m {
|
||||
if out.len() > 1 && out.last().is_some_and(|p| p.1 < 3) {
|
||||
out.pop();
|
||||
}
|
||||
return (!out.is_empty() && out.len() <= MAX_PATCHES).then_some(out);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// The limiter band table (§4.6.18.7.3): one band over the range, or about 1.2, 2 or 3 bands per
|
||||
/// octave on the low-resolution and patch borders.
|
||||
fn limiter(low: &[usize], patches: &[(usize, usize)], kx: usize, limiter_bands: u8) -> Vec<usize> {
|
||||
let (Some(&first), Some(&last)) = (low.first(), low.last()) else {
|
||||
return Vec::new();
|
||||
};
|
||||
if limiter_bands == 0 {
|
||||
return vec![first, last];
|
||||
}
|
||||
let per_octave = [1.2, 2.0, 3.0][usize::from(limiter_bands.clamp(1, 3) - 1)];
|
||||
let mut borders = vec![kx];
|
||||
for p in patches {
|
||||
borders.push(borders.last().copied().unwrap_or(kx) + p.1);
|
||||
}
|
||||
let mut t: Vec<usize> = low.to_vec();
|
||||
t.extend(borders.iter().skip(1).take(patches.len().saturating_sub(1)));
|
||||
t.sort_unstable();
|
||||
let is_border = |v: usize| borders.contains(&v);
|
||||
let mut k = 1;
|
||||
while k < t.len() {
|
||||
let octaves = if t[k - 1] == 0 { f64::INFINITY } else { (t[k] as f64 / t[k - 1] as f64).log2() };
|
||||
if octaves * per_octave >= 0.49 {
|
||||
k += 1;
|
||||
} else if t[k] == t[k - 1] || !is_border(t[k]) {
|
||||
t.remove(k);
|
||||
} else if !is_border(t[k - 1]) {
|
||||
t.remove(k - 1);
|
||||
} else {
|
||||
k += 1;
|
||||
}
|
||||
}
|
||||
if t.len() < 2 {
|
||||
return vec![first, last];
|
||||
}
|
||||
t
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn header(start: u8, stop: u8, xover: u8, freq_scale: u8, alter: bool, noise: u8) -> Header {
|
||||
Header {
|
||||
amp_res: true,
|
||||
start_freq: start,
|
||||
stop_freq: stop,
|
||||
xover_band: xover,
|
||||
freq_scale,
|
||||
alter_scale: alter,
|
||||
noise_bands: noise,
|
||||
limiter_bands: 2,
|
||||
limiter_gains: 2,
|
||||
interpol_freq: true,
|
||||
smoothing_mode: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Every header field combination at every output rate gives tables that are strictly
|
||||
/// increasing, start at `kx`, end at `kx + m`, keep the low band within the analysis bank and
|
||||
/// patch from below `k0` (or is refused).
|
||||
#[test]
|
||||
fn every_header_gives_consistent_tables_or_none() {
|
||||
let mut valid = 0;
|
||||
for fs in [16_000, 22_050, 24_000, 32_000, 44_100, 48_000, 64_000, 88_200, 96_000] {
|
||||
for start in 0..16 {
|
||||
for stop in 0..16 {
|
||||
for xover in 0..8 {
|
||||
for fsc in 0..4 {
|
||||
for alter in [false, true] {
|
||||
for noise in 0..4 {
|
||||
let Some(t) = FreqTables::new(&header(start, stop, xover, fsc, alter, noise), fs) else { continue };
|
||||
valid += 1;
|
||||
for v in [&t.high, &t.low, &t.noise, &t.lim] {
|
||||
assert!(v.windows(2).all(|w| w[0] < w[1]), "{v:?}");
|
||||
assert_eq!(v[0], t.kx);
|
||||
}
|
||||
for v in [&t.high, &t.low, &t.noise] {
|
||||
assert_eq!(*v.last().unwrap(), t.kx + t.m);
|
||||
}
|
||||
assert!(t.kx <= 32 && t.kx + t.m <= 64 && t.n_q() <= 5);
|
||||
// a last patch of under 3 channels is dropped, and the limiter
|
||||
// bands may then stop short of the range's end
|
||||
assert!(t.patches.iter().map(|p| p.1).sum::<usize>() <= t.m);
|
||||
assert!(t.lim.len() >= 2 && *t.lim.last().unwrap() <= t.kx + t.m);
|
||||
assert!(t.patches.iter().all(|&(s, n)| s + n <= t.k0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(valid > 10_000, "{valid}");
|
||||
}
|
||||
|
||||
/// The highest start a header can give, 32 at 24 kHz (start 15), is valid: with stop 14
|
||||
/// (twice the start) and linear bands it covers channels 32 to 64.
|
||||
#[test]
|
||||
fn a_start_at_channel_32_is_accepted() {
|
||||
let t = FreqTables::new(&header(15, 14, 0, 0, false, 2), 24_000).unwrap();
|
||||
assert_eq!((t.k0, t.kx, t.m), (32, 32, 32));
|
||||
}
|
||||
|
||||
/// At 96 kHz this header builds patches of 6, 6, 6, 6, 5 and 2 channels: the short sixth is
|
||||
/// dropped, as any short last patch is, and the five left are within the limit.
|
||||
#[test]
|
||||
fn a_short_sixth_patch_is_dropped_before_the_limit() {
|
||||
let t = FreqTables::new(&header(3, 9, 0, 0, false, 2), 96_000).unwrap();
|
||||
assert_eq!((t.k0, t.kx + t.m), (8, 39));
|
||||
assert_eq!(t.patches.iter().map(|p| p.1).collect::<Vec<_>>(), [6, 6, 6, 6, 5]);
|
||||
}
|
||||
|
||||
/// A typical 44.1 kHz header: start channel 12 + offset, stop near 64, three noise bands.
|
||||
#[test]
|
||||
fn a_44k_header_has_the_expected_layout() {
|
||||
let t = FreqTables::new(&header(5, 9, 0, 2, true, 2), 44_100).unwrap();
|
||||
// startMin 12 + offset 2 = 14
|
||||
assert_eq!((t.k0, t.kx), (14, 14));
|
||||
assert_eq!(t.low.len() - 1, t.n_high() - t.n_high() / 2);
|
||||
assert!(t.n_q() >= 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
//! HF generation (ISO/IEC 14496-3 §4.6.18.6) and HF adjustment (§4.6.18.7) of one channel.
|
||||
|
||||
use super::bitstream::Grid;
|
||||
use super::freq::FreqTables;
|
||||
use super::tables::NOISE;
|
||||
use super::{Cx, Header, MAX_ENV, T_HF_ADJ, X_SLOTS};
|
||||
|
||||
/// Low band subband samples of a frame: slots -8 .. 32 relative to the frame (`X_Low`).
|
||||
pub(crate) type LowBand = [[Cx; 32]; X_SLOTS];
|
||||
/// High band subband samples, at slot `l + T_HF_ADJ` for QMF slot `l` of the frame's envelopes.
|
||||
pub(crate) type HighBand = [[Cx; 64]; X_SLOTS];
|
||||
|
||||
/// The per-channel state the HF tools carry from frame to frame.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct History {
|
||||
/// `bs_invf_mode` and chirp factor per noise band.
|
||||
invf: [u8; 5],
|
||||
bw: [f32; 5],
|
||||
/// The last frame's transient envelope was its last (`l_A = L_E`): no noise in this frame's
|
||||
/// first.
|
||||
la_at_end: bool,
|
||||
/// Sinusoids present in the last envelope, by channel above `kx`.
|
||||
sine: [bool; 64],
|
||||
/// Gains and noise levels of the last four slots, newest first (smoothing).
|
||||
gains: [[f32; 64]; 4],
|
||||
noise_levels: [[f32; 64]; 4],
|
||||
/// The smoothing history holds data of the current tables.
|
||||
primed: bool,
|
||||
noise_index: usize,
|
||||
sine_index: usize,
|
||||
}
|
||||
|
||||
impl History {
|
||||
pub(crate) fn new() -> History {
|
||||
History {
|
||||
invf: [0; 5],
|
||||
bw: [0.0; 5],
|
||||
la_at_end: false,
|
||||
sine: [false; 64],
|
||||
gains: [[0.0; 64]; 4],
|
||||
noise_levels: [[0.0; 64]; 4],
|
||||
primed: false,
|
||||
noise_index: 0,
|
||||
sine_index: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Forget what depends on the frequency tables (after a header that changes them).
|
||||
pub(crate) fn reset_tables(&mut self) {
|
||||
self.sine = [false; 64];
|
||||
self.primed = false;
|
||||
self.noise_index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// The chirp factor of a noise band from its inverse filtering mode and the last frame's.
|
||||
fn chirp(mode: u8, prev_mode: u8, prev_bw: f32) -> f32 {
|
||||
let new_bw = match (mode, prev_mode) {
|
||||
(0, 1) => 0.6,
|
||||
(0, _) => 0.0,
|
||||
(1, 0) => 0.6,
|
||||
(1, _) => 0.75,
|
||||
(2, _) => 0.9,
|
||||
_ => 0.98,
|
||||
};
|
||||
let bw = if new_bw < prev_bw { 0.75 * new_bw + 0.25 * prev_bw } else { 0.90625 * new_bw + 0.09375 * prev_bw };
|
||||
if bw < 0.015625 { 0.0 } else { bw }
|
||||
}
|
||||
|
||||
/// Second order linear prediction of one low band channel over the frame and the 6 slots
|
||||
/// before it (the covariance method, §4.6.18.6.2): `(α0, α1)`.
|
||||
fn predictor(x: &LowBand, k: usize) -> (Cx, Cx) {
|
||||
// φ(i, j) = Σ x(n − i + 2) · conj(x(n − j + 2)), n = 0 .. 37
|
||||
let phi = |i: usize, j: usize| {
|
||||
let (mut re, mut im) = (0f64, 0f64);
|
||||
for n in 0..SLOTS_PLUS_6 {
|
||||
let (a, b) = (x[n + 2 - i][k], x[n + 2 - j][k]);
|
||||
re += f64::from(a.re) * f64::from(b.re) + f64::from(a.im) * f64::from(b.im);
|
||||
im += f64::from(a.im) * f64::from(b.re) - f64::from(a.re) * f64::from(b.im);
|
||||
}
|
||||
(re, im)
|
||||
};
|
||||
let (p01, p02, p11, p12, p22) = (phi(0, 1), phi(0, 2), phi(1, 1).0, phi(1, 2), phi(2, 2).0);
|
||||
let d = p22 * p11 - (p12.0 * p12.0 + p12.1 * p12.1) / (1.0 + 1e-6);
|
||||
let a1 = if d != 0.0 {
|
||||
// (φ01 · φ12 − φ02 · φ11) / d
|
||||
((p01.0 * p12.0 - p01.1 * p12.1 - p02.0 * p11) / d, (p01.0 * p12.1 + p01.1 * p12.0 - p02.1 * p11) / d)
|
||||
} else {
|
||||
(0.0, 0.0)
|
||||
};
|
||||
let a0 = if p11 != 0.0 {
|
||||
// −(φ01 + α1 · conj(φ12)) / φ11
|
||||
(-(p01.0 + a1.0 * p12.0 + a1.1 * p12.1) / p11, -(p01.1 + a1.1 * p12.0 - a1.0 * p12.1) / p11)
|
||||
} else {
|
||||
(0.0, 0.0)
|
||||
};
|
||||
let mag = |a: (f64, f64)| (a.0 * a.0 + a.1 * a.1).sqrt();
|
||||
if mag(a0) >= 4.0 || mag(a1) >= 4.0 {
|
||||
return (Cx::default(), Cx::default());
|
||||
}
|
||||
(Cx { re: a0.0 as f32, im: a0.1 as f32 }, Cx { re: a1.0 as f32, im: a1.1 as f32 })
|
||||
}
|
||||
|
||||
/// Slots of the covariance sums: the frame's 32 and 6 before it.
|
||||
const SLOTS_PLUS_6: usize = super::SLOTS + 6;
|
||||
|
||||
/// The index of the band of `bands` holding channel `k` (bands are increasing borders).
|
||||
fn band_of(bands: &[usize], k: usize) -> usize {
|
||||
bands.partition_point(|&b| b <= k).saturating_sub(1).min(bands.len().saturating_sub(2))
|
||||
}
|
||||
|
||||
/// Patch the low band into the SBR range, inverse filtered (§4.6.18.6.3), for QMF slots
|
||||
/// `from .. to` of the frame.
|
||||
pub(crate) fn generate(x: &LowBand, t: &FreqTables, invf: &[u8; 5], h: &mut History, (from, to): (usize, usize), out: &mut HighBand) {
|
||||
let mut bw = [0f32; 5];
|
||||
for i in 0..t.n_q() {
|
||||
bw[i] = chirp(invf[i], h.invf[i], h.bw[i]);
|
||||
}
|
||||
(h.invf, h.bw) = (*invf, bw);
|
||||
let mut pred: [Option<(Cx, Cx)>; 32] = [None; 32];
|
||||
let mut k = t.kx;
|
||||
for &(start, count) in &t.patches {
|
||||
for p in start..start + count {
|
||||
let (a0, a1) = *pred[p].get_or_insert_with(|| predictor(x, p));
|
||||
let g = bw[band_of(&t.noise, k)];
|
||||
let (c0, c1) = (a0.scale(g), a1.scale(g * g));
|
||||
for l in from..to {
|
||||
let i = l + T_HF_ADJ;
|
||||
out[i][k] = x[i][p] + c0 * x[i - 1][p] + c1 * x[i - 2][p];
|
||||
}
|
||||
k += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Dequantised envelope and noise floor values of one channel in one frame.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Levels {
|
||||
/// Envelope energy per envelope and band (of that envelope's resolution).
|
||||
pub(crate) energy: [[f32; 64]; MAX_ENV],
|
||||
/// Noise to signal ratio per noise floor and noise band.
|
||||
pub(crate) noise: [[f32; 5]; 2],
|
||||
}
|
||||
|
||||
/// Gains for one envelope, per channel of the SBR range.
|
||||
struct EnvelopeGains {
|
||||
gain: [f32; 64],
|
||||
noise: [f32; 64],
|
||||
sine: [f32; 64],
|
||||
}
|
||||
|
||||
const LIMITER_GAINS: [f32; 4] = [0.70795, 1.0, 1.41254, 1e10];
|
||||
/// `h_smooth`, as the specification gives it.
|
||||
#[allow(clippy::excessive_precision)]
|
||||
const SMOOTHING: [f32; 5] = [0.33333333333333, 0.30150283239582, 0.21816949906249, 0.11516383427084, 0.03183050093751];
|
||||
const EPS: f32 = 1.0;
|
||||
const EPS0: f32 = 1e-12;
|
||||
|
||||
/// What the adjustment of one channel's frame works from.
|
||||
pub(crate) struct Frame<'a> {
|
||||
/// The generated high band.
|
||||
pub(crate) x: &'a HighBand,
|
||||
pub(crate) t: &'a FreqTables,
|
||||
pub(crate) hdr: &'a Header,
|
||||
pub(crate) g: &'a Grid,
|
||||
pub(crate) lv: &'a Levels,
|
||||
/// `bs_add_harmonic`, when the frame codes sinusoids.
|
||||
pub(crate) harmonic: Option<&'a [bool; 64]>,
|
||||
}
|
||||
|
||||
/// The gains of envelope `l` (§4.6.18.7.3–4): `delta` is 0 for a transient envelope, which gets
|
||||
/// no noise.
|
||||
fn envelope_gains(f: &Frame, l: usize, sine_index: &[bool; 64], delta: bool) -> EnvelopeGains {
|
||||
let Frame { x, t, hdr, g, lv, .. } = *f;
|
||||
let (kx, m) = (t.kx, t.m);
|
||||
let bands = t.bands(g.freq_res[l]);
|
||||
let (from, to) = (2 * g.t_env[l] + T_HF_ADJ, 2 * g.t_env[l + 1] + T_HF_ADJ);
|
||||
let slots = (to - from) as f32;
|
||||
let floor = usize::from(g.num_noise == 2 && g.t_env[l] >= g.t_noise[1]);
|
||||
// per channel: original energy, current energy, noise ratio, sinusoid in its band
|
||||
let mut e_orig = [0f32; 64];
|
||||
let mut e_curr = [0f32; 64];
|
||||
let mut q = [0f32; 64];
|
||||
let mut s_band = [false; 64];
|
||||
for j in 0..bands.len() - 1 {
|
||||
let (lo, hi) = (bands[j] - kx, bands[j + 1] - kx);
|
||||
let any_sine = sine_index[lo..hi].iter().any(|&s| s);
|
||||
let mut sum = 0f32;
|
||||
for c in lo..hi {
|
||||
let ch = c + kx;
|
||||
let e: f32 = (from..to).map(|i| x[i][ch].norm_sqr()).sum::<f32>() / slots;
|
||||
e_curr[c] = e;
|
||||
sum += e;
|
||||
e_orig[c] = lv.energy[l][j];
|
||||
q[c] = lv.noise[floor][band_of(&t.noise, ch)];
|
||||
s_band[c] = any_sine;
|
||||
}
|
||||
if !hdr.interpol_freq {
|
||||
e_curr[lo..hi].fill(sum / (hi - lo) as f32);
|
||||
}
|
||||
}
|
||||
let mut out = EnvelopeGains { gain: [0.0; 64], noise: [0.0; 64], sine: [0.0; 64] };
|
||||
for c in 0..m {
|
||||
// `q / (1 + q)` first: energy and noise ratio are each clamped, their product is not
|
||||
let (eo, ec, q) = (e_orig[c], e_curr[c], q[c]);
|
||||
let share = q / (1.0 + q);
|
||||
out.noise[c] = (eo * share).sqrt();
|
||||
if sine_index[c] {
|
||||
out.sine[c] = (eo / (1.0 + q)).sqrt();
|
||||
}
|
||||
out.gain[c] = if s_band[c] { (eo / (EPS + ec) * share).sqrt() } else { (eo / ((EPS + ec) * (1.0 + if delta { q } else { 0.0 }))).sqrt() };
|
||||
}
|
||||
// limit per limiter band, then boost back the energy the limiter took
|
||||
let lim_gain = LIMITER_GAINS[usize::from(hdr.limiter_gains & 3)];
|
||||
for w in t.lim.windows(2) {
|
||||
let (lo, hi) = (w[0] - kx, w[1] - kx);
|
||||
let (so, sc) = (e_orig[lo..hi].iter().sum::<f32>(), e_curr[lo..hi].iter().sum::<f32>());
|
||||
let max = (lim_gain * ((EPS0 + so) / (EPS0 + sc)).sqrt()).min(1e5);
|
||||
let mut den = 0f32;
|
||||
for c in lo..hi {
|
||||
if out.gain[c] > max {
|
||||
out.noise[c] *= max / out.gain[c];
|
||||
out.gain[c] = max;
|
||||
}
|
||||
den += e_curr[c] * out.gain[c] * out.gain[c];
|
||||
if out.sine[c] != 0.0 {
|
||||
den += out.sine[c] * out.sine[c];
|
||||
} else if delta {
|
||||
den += out.noise[c] * out.noise[c];
|
||||
}
|
||||
}
|
||||
let boost = ((EPS0 + so) / (EPS0 + den)).sqrt().min(1.584_893_2);
|
||||
for c in lo..hi {
|
||||
out.gain[c] *= boost;
|
||||
out.noise[c] *= boost;
|
||||
out.sine[c] *= boost;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Adjust the generated high band to the transmitted envelopes and add noise and sinusoids
|
||||
/// (§4.6.18.7), for the frame's envelopes, into `y`.
|
||||
pub(crate) fn adjust(f: &Frame, h: &mut History, y: &mut HighBand) {
|
||||
let Frame { x, t, hdr, g, harmonic, .. } = *f;
|
||||
let (kx, m) = (t.kx, t.m);
|
||||
let smooth = !hdr.smoothing_mode;
|
||||
let prev_la_at_end = h.la_at_end;
|
||||
for l in 0..g.num_env {
|
||||
// sinusoids start at the transient envelope, unless one was already playing in the band
|
||||
let mut sine_index = [false; 64];
|
||||
if let Some(harm) = harmonic {
|
||||
for i in 0..t.n_high() {
|
||||
let c = (t.high[i] + t.high[i + 1]) / 2 - kx;
|
||||
sine_index[c] = harm[i] && (g.la.is_none_or(|la| l >= la) || h.sine[c]);
|
||||
}
|
||||
}
|
||||
let transient = g.la == Some(l) || (l == 0 && prev_la_at_end);
|
||||
let e = envelope_gains(f, l, &sine_index, !transient);
|
||||
if !h.primed {
|
||||
h.gains = [e.gain; 4];
|
||||
h.noise_levels = [e.noise; 4];
|
||||
h.primed = true;
|
||||
}
|
||||
for i in 2 * g.t_env[l]..2 * g.t_env[l + 1] {
|
||||
let (mut gain, mut noise) = (e.gain, e.noise);
|
||||
if smooth && !transient {
|
||||
for c in 0..m {
|
||||
gain[c] = SMOOTHING[0] * e.gain[c] + (0..4).map(|j| SMOOTHING[j + 1] * h.gains[j][c]).sum::<f32>();
|
||||
noise[c] = SMOOTHING[0] * e.noise[c] + (0..4).map(|j| SMOOTHING[j + 1] * h.noise_levels[j][c]).sum::<f32>();
|
||||
}
|
||||
}
|
||||
h.gains.rotate_right(1);
|
||||
h.noise_levels.rotate_right(1);
|
||||
(h.gains[0], h.noise_levels[0]) = (e.gain, e.noise);
|
||||
let (sin_re, sin_im) = [(1.0, 0.0), (0.0, 1.0), (-1.0, 0.0), (0.0, -1.0)][h.sine_index];
|
||||
let row = &mut y[i + T_HF_ADJ];
|
||||
for c in 0..m {
|
||||
let ch = c + kx;
|
||||
let mut v = x[i + T_HF_ADJ][ch].scale(gain[c]);
|
||||
h.noise_index = (h.noise_index + 1) & 511;
|
||||
if e.sine[c] != 0.0 {
|
||||
let sign = if ch % 2 == 0 { 1.0 } else { -1.0 };
|
||||
v.re += e.sine[c] * sin_re;
|
||||
v.im += e.sine[c] * sin_im * sign;
|
||||
} else if !transient {
|
||||
let [nr, ni] = NOISE[h.noise_index];
|
||||
v.re += noise[c] * nr;
|
||||
v.im += noise[c] * ni;
|
||||
}
|
||||
row[ch] = v;
|
||||
}
|
||||
h.sine_index = (h.sine_index + 1) & 3;
|
||||
}
|
||||
if l + 1 == g.num_env {
|
||||
h.sine = sine_index;
|
||||
}
|
||||
}
|
||||
h.la_at_end = g.la == Some(g.num_env);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The chirp factor follows §4.6.18.6.2: rising quickly, falling slowly, and zero below 1/64.
|
||||
#[test]
|
||||
fn chirp_factors() {
|
||||
assert!((chirp(3, 3, 0.0) - 0.90625 * 0.98).abs() < 1e-6);
|
||||
assert!((chirp(0, 3, 0.98) - 0.25 * 0.98).abs() < 1e-6);
|
||||
assert_eq!(chirp(0, 0, 0.05), 0.0);
|
||||
assert!((chirp(1, 0, 0.0) - 0.90625 * 0.6).abs() < 1e-6);
|
||||
}
|
||||
|
||||
/// A channel that is a decaying complex exponential is predicted exactly by its first order
|
||||
/// term.
|
||||
#[test]
|
||||
fn prediction_of_a_resonance() {
|
||||
let mut x: LowBand = [[Cx::default(); 32]; X_SLOTS];
|
||||
let r = Cx { re: 0.9 * 0.3f32.cos(), im: 0.9 * 0.3f32.sin() };
|
||||
let mut v = Cx { re: 1.0, im: 0.0 };
|
||||
for slot in x.iter_mut() {
|
||||
slot[3] = v;
|
||||
v = v * r;
|
||||
}
|
||||
let (a0, a1) = predictor(&x, 3);
|
||||
// x(n) + α0·x(n−1) + α1·x(n−2) = 0 with α0 = −r, α1 = 0
|
||||
assert!((a0.re + r.re).abs() < 1e-3 && (a0.im + r.im).abs() < 1e-3, "{a0:?}");
|
||||
assert!(a1.re.abs() < 1e-3 && a1.im.abs() < 1e-3, "{a1:?}");
|
||||
// a silent channel has no predictor
|
||||
assert_eq!(predictor(&x, 4), (Cx::default(), Cx::default()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,544 @@
|
||||
//! Spectral band replication (HE-AAC, ISO/IEC 14496-3 §4.6.18): rebuilds the band above the
|
||||
//! AAC-LC core from the core's own low band and the envelope data of the stream's SBR fill
|
||||
//! elements, at twice the core rate.
|
||||
//!
|
||||
//! Each channel's core output goes through a 32-band QMF analysis bank. The HF generator copies
|
||||
//! (patches) low band channels into the SBR range, inverse filtered; the HF adjuster scales them
|
||||
//! to the transmitted envelopes and adds noise and sinusoids; a 64-band QMF synthesis bank merges
|
||||
//! the delayed low band and the new high band. A frame without usable SBR data (none sent yet, or
|
||||
//! damaged) still passes through the banks, so the output keeps its rate and delay.
|
||||
//!
|
||||
//! Written from the SBR description and tables of ETSI ES 201 980 (DRM, Annex I has the tables it
|
||||
//! shares with ISO/IEC 14496-3) and the decoder process of ISO/IEC 14496-3. Parametric stereo
|
||||
//! (HE-AAC v2) is not decoded: its extension is skipped and the mono core is extended alone.
|
||||
|
||||
mod bitstream;
|
||||
mod freq;
|
||||
mod hf;
|
||||
mod qmf;
|
||||
mod tables;
|
||||
|
||||
use std::ops::{Add, Mul};
|
||||
|
||||
use craft_media::bitstream::BitReader;
|
||||
|
||||
use self::bitstream::{ChannelData, Frame};
|
||||
use self::freq::FreqTables;
|
||||
use self::hf::{HighBand, History, Levels, LowBand};
|
||||
use self::qmf::{Analysis, Synthesis};
|
||||
|
||||
/// QMF slots per frame: 1024 core samples in slots of 32.
|
||||
pub(crate) const SLOTS: usize = 32;
|
||||
/// The most envelopes in a frame.
|
||||
pub(crate) const MAX_ENV: usize = 5;
|
||||
/// Slots of the last frame kept ahead of this one's low band (`t_HFGen`).
|
||||
const T_HF_GEN: usize = 8;
|
||||
/// The SBR frame lags the core frame by `T_HF_GEN − T_HF_ADJ` slots.
|
||||
pub(crate) const T_HF_ADJ: usize = 2;
|
||||
/// Slots in the low and high band buffers: the frame and `T_HF_GEN` before it.
|
||||
pub(crate) const X_SLOTS: usize = SLOTS + T_HF_GEN;
|
||||
/// The 16-bit full scale the specification's energies are in.
|
||||
const PCM_SCALE: f32 = 32768.0;
|
||||
/// `NOISE_FLOOR_OFFSET`.
|
||||
const NOISE_OFFSET: i32 = 6;
|
||||
|
||||
/// Output samples by which the SBR decoder delays the core: the filter banks, and the SBR frame
|
||||
/// lagging the core frame by `T_HF_GEN − T_HF_ADJ` slots of 64.
|
||||
pub const DELAY: usize = qmf::DELAY + 64 * (T_HF_GEN - T_HF_ADJ);
|
||||
|
||||
/// A complex subband sample.
|
||||
#[derive(Clone, Copy, Default, Debug, PartialEq)]
|
||||
pub(crate) struct Cx {
|
||||
pub(crate) re: f32,
|
||||
pub(crate) im: f32,
|
||||
}
|
||||
|
||||
impl Cx {
|
||||
fn scale(self, g: f32) -> Cx {
|
||||
Cx { re: self.re * g, im: self.im * g }
|
||||
}
|
||||
fn norm_sqr(self) -> f32 {
|
||||
self.re * self.re + self.im * self.im
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Cx {
|
||||
type Output = Cx;
|
||||
fn add(self, o: Cx) -> Cx {
|
||||
Cx { re: self.re + o.re, im: self.im + o.im }
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul for Cx {
|
||||
type Output = Cx;
|
||||
fn mul(self, o: Cx) -> Cx {
|
||||
Cx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re }
|
||||
}
|
||||
}
|
||||
|
||||
/// `sbr_header()` (§4.5.2.8.1).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct Header {
|
||||
pub(crate) amp_res: bool,
|
||||
pub(crate) start_freq: u8,
|
||||
pub(crate) stop_freq: u8,
|
||||
pub(crate) xover_band: u8,
|
||||
pub(crate) freq_scale: u8,
|
||||
pub(crate) alter_scale: bool,
|
||||
pub(crate) noise_bands: u8,
|
||||
pub(crate) limiter_bands: u8,
|
||||
pub(crate) limiter_gains: u8,
|
||||
pub(crate) interpol_freq: bool,
|
||||
pub(crate) smoothing_mode: bool,
|
||||
}
|
||||
|
||||
impl Header {
|
||||
/// Whether `o` keeps the frequency band tables (a change of any of these fields is an SBR
|
||||
/// reset).
|
||||
fn same_tables(&self, o: &Header) -> bool {
|
||||
(self.start_freq, self.stop_freq, self.freq_scale, self.alter_scale, self.xover_band, self.noise_bands)
|
||||
== (o.start_freq, o.stop_freq, o.freq_scale, o.alter_scale, o.xover_band, o.noise_bands)
|
||||
}
|
||||
}
|
||||
|
||||
/// Where an element's SBR data is: the access unit, and the bit range of the fill element's
|
||||
/// payload after its extension type.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct Payload<'a> {
|
||||
pub(crate) data: &'a [u8],
|
||||
pub(crate) start: usize,
|
||||
pub(crate) end: usize,
|
||||
/// `EXT_SBR_DATA_CRC`: a 10-bit CRC leads the data.
|
||||
pub(crate) crc: bool,
|
||||
}
|
||||
|
||||
/// Quantised envelope values per envelope and band, and noise floor values per floor and band.
|
||||
type Quantised = ([[i32; 64]; MAX_ENV], [[i32; 5]; 2]);
|
||||
|
||||
/// One channel's state across frames.
|
||||
#[derive(Clone)]
|
||||
struct Channel {
|
||||
analysis: Analysis,
|
||||
synthesis: Synthesis,
|
||||
/// Low band of the frame, with the last `T_HF_GEN` slots of the frame before ahead of it.
|
||||
x_low: Box<LowBand>,
|
||||
/// Adjusted high band of the last frame (its envelopes may reach into this one).
|
||||
y_prev: Box<HighBand>,
|
||||
/// Where `y_prev` holds data: QMF channels, and the end of its slots.
|
||||
y_prev_range: (usize, usize),
|
||||
y_prev_end: usize,
|
||||
/// Quantised envelope values of the last envelope, and its resolution; noise floor values of
|
||||
/// the last noise floor (time-direction delta coding).
|
||||
env: [i32; 64],
|
||||
env_high_res: bool,
|
||||
noise: [i32; 5],
|
||||
history: History,
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
fn new() -> Channel {
|
||||
Channel {
|
||||
analysis: Analysis::new(),
|
||||
synthesis: Synthesis::new(),
|
||||
x_low: Box::new([[Cx::default(); 32]; X_SLOTS]),
|
||||
y_prev: Box::new([[Cx::default(); 64]; X_SLOTS]),
|
||||
y_prev_range: (0, 0),
|
||||
y_prev_end: 0,
|
||||
env: [0; 64],
|
||||
env_high_res: false,
|
||||
noise: [0; 5],
|
||||
history: History::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode the delta-coded envelope and noise floor values (§4.6.18.3.4); `scale` is 2 for a
|
||||
/// balance channel.
|
||||
fn absolute(&mut self, d: &ChannelData, t: &FreqTables, scale: i32) -> Quantised {
|
||||
let mut env = [[0i32; 64]; MAX_ENV];
|
||||
for l in 0..d.grid.num_env {
|
||||
let res = d.grid.freq_res[l];
|
||||
let n = t.bands(res).len() - 1;
|
||||
if d.df_env[l] {
|
||||
for k in 0..n {
|
||||
// the value this band refers to in the last envelope, mapped across resolutions
|
||||
let reference = if res == self.env_high_res {
|
||||
k
|
||||
} else if res {
|
||||
hf_band_of(t.bands(false), t.high[k])
|
||||
} else {
|
||||
t.high.iter().position(|&b| b == t.low[k]).unwrap_or(0)
|
||||
};
|
||||
env[l][k] = self.env.get(reference).copied().unwrap_or(0) + scale * d.env[l][k];
|
||||
}
|
||||
} else {
|
||||
env[l][0] = scale * d.env[l][0];
|
||||
for k in 1..n {
|
||||
env[l][k] = env[l][k - 1] + scale * d.env[l][k];
|
||||
}
|
||||
}
|
||||
(self.env, self.env_high_res) = (env[l], res);
|
||||
}
|
||||
let mut noise = [[0i32; 5]; 2];
|
||||
for l in 0..d.grid.num_noise {
|
||||
for k in 0..t.n_q() {
|
||||
let delta = scale * d.noise[l][k];
|
||||
noise[l][k] = if d.df_noise[l] {
|
||||
self.noise[k] + delta
|
||||
} else if k == 0 {
|
||||
delta
|
||||
} else {
|
||||
noise[l][k - 1] + delta
|
||||
};
|
||||
}
|
||||
self.noise = noise[l];
|
||||
}
|
||||
(env, noise)
|
||||
}
|
||||
}
|
||||
|
||||
/// The index of the band of `bands` holding channel `k`.
|
||||
fn hf_band_of(bands: &[usize], k: usize) -> usize {
|
||||
bands.partition_point(|&b| b <= k).saturating_sub(1)
|
||||
}
|
||||
|
||||
/// `2^x`, kept to a range whose energies stay finite.
|
||||
fn exp2(x: f32) -> f32 {
|
||||
x.clamp(-64.0, 64.0).exp2()
|
||||
}
|
||||
|
||||
/// Dequantise one channel (§4.6.18.3.5), or a coupled pair from its level and balance channels.
|
||||
fn dequantise(frame: &Frame, values: &[Quantised], t: &FreqTables) -> Vec<Levels> {
|
||||
let g = &frame.channels[0].grid;
|
||||
let amp = if g.amp_res { 1.0 } else { 0.5 };
|
||||
let mut out = vec![Levels { energy: [[0.0; 64]; MAX_ENV], noise: [[0.0; 5]; 2] }; values.len()];
|
||||
if frame.coupling && values.len() == 2 {
|
||||
let pan = if g.amp_res { 12.0 } else { 24.0 };
|
||||
let ((e0, q0), (e1, q1)) = (&values[0], &values[1]);
|
||||
for l in 0..g.num_env {
|
||||
for k in 0..t.bands(g.freq_res[l]).len() - 1 {
|
||||
let level = 64.0 * exp2(amp * e0[l][k] as f32 + 1.0);
|
||||
let balance = amp * (pan - e1[l][k] as f32);
|
||||
out[0].energy[l][k] = level / (1.0 + exp2(balance));
|
||||
out[1].energy[l][k] = level / (1.0 + exp2(-balance));
|
||||
}
|
||||
}
|
||||
for l in 0..g.num_noise {
|
||||
for k in 0..t.n_q() {
|
||||
let level = exp2((NOISE_OFFSET - q0[l][k] + 1) as f32);
|
||||
let balance = (12 - q1[l][k]) as f32;
|
||||
out[0].noise[l][k] = level / (1.0 + exp2(balance));
|
||||
out[1].noise[l][k] = level / (1.0 + exp2(-balance));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
for ((lv, (e, q)), d) in out.iter_mut().zip(values).zip(&frame.channels) {
|
||||
let g = &d.grid;
|
||||
let amp = if g.amp_res { 1.0 } else { 0.5 };
|
||||
for l in 0..g.num_env {
|
||||
for k in 0..t.bands(g.freq_res[l]).len() - 1 {
|
||||
lv.energy[l][k] = 64.0 * exp2(amp * e[l][k] as f32);
|
||||
}
|
||||
}
|
||||
for l in 0..g.num_noise {
|
||||
for k in 0..t.n_q() {
|
||||
lv.noise[l][k] = exp2((NOISE_OFFSET - q[l][k]) as f32);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The SBR decoder of one channel element: a single channel (SCE, or an LFE, which has no SBR
|
||||
/// data) or a channel pair (CPE).
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Element {
|
||||
/// Output sampling rate.
|
||||
rate: u32,
|
||||
header: Option<Header>,
|
||||
tables: Option<FreqTables>,
|
||||
channels: Vec<Channel>,
|
||||
}
|
||||
|
||||
impl Element {
|
||||
pub(crate) fn new(channels: usize, rate: u32) -> Element {
|
||||
Element { rate, header: None, tables: None, channels: (0..channels).map(|_| Channel::new()).collect() }
|
||||
}
|
||||
|
||||
pub(crate) fn channels(&self) -> usize {
|
||||
self.channels.len()
|
||||
}
|
||||
|
||||
/// Forget the signal (a seek). The header is kept: it holds until the stream sends another.
|
||||
pub(crate) fn reset(&mut self) {
|
||||
for c in &mut self.channels {
|
||||
*c = Channel::new();
|
||||
}
|
||||
}
|
||||
|
||||
/// Read an element's SBR data: `None` without a header to read it by.
|
||||
fn read(&mut self, p: Payload) -> Option<Frame> {
|
||||
let mut br = BitReader::new(p.data);
|
||||
br.seek_bits(p.start);
|
||||
if p.crc {
|
||||
br.skip(10).ok()?;
|
||||
}
|
||||
if br.read_flag().ok()? {
|
||||
let h = Header::read(&mut br).ok()?;
|
||||
// a header that runs past its fill element is damaged: keep the one in use
|
||||
(br.position() <= p.end).then_some(())?;
|
||||
if self.header != Some(h) {
|
||||
if self.header.is_none_or(|o| !o.same_tables(&h)) {
|
||||
for c in &mut self.channels {
|
||||
c.history.reset_tables();
|
||||
}
|
||||
}
|
||||
self.tables = FreqTables::new(&h, self.rate);
|
||||
self.header = Some(h);
|
||||
}
|
||||
}
|
||||
let (h, t) = (self.header?, self.tables.as_ref()?);
|
||||
let frame = bitstream::read_data(&mut br, t, h.amp_res, self.channels.len()).ok()?;
|
||||
(br.position() <= p.end).then_some(frame)
|
||||
}
|
||||
|
||||
/// Decode one frame: 1024 core samples per channel in, 2048 out.
|
||||
pub(crate) fn process(&mut self, core: &[Vec<f32>], payload: Option<Payload>) -> Vec<Vec<f32>> {
|
||||
for (c, pcm) in self.channels.iter_mut().zip(core) {
|
||||
c.x_low.copy_within(SLOTS..X_SLOTS, 0);
|
||||
let scaled: Vec<f32> = pcm.iter().map(|s| s * PCM_SCALE).collect();
|
||||
c.analysis.process(&scaled, &mut c.x_low[T_HF_GEN..]);
|
||||
}
|
||||
let frame = payload.and_then(|p| self.read(p));
|
||||
let mut y: Vec<Box<HighBand>> = self.channels.iter().map(|_| Box::new([[Cx::default(); 64]; X_SLOTS])).collect();
|
||||
// per channel: kx, m, and the first and end slot of `y` this frame fills
|
||||
let mut current: Vec<Option<(usize, usize, usize, usize)>> = vec![None; self.channels.len()];
|
||||
if let (Some(frame), Some(h), Some(t)) = (&frame, self.header, self.tables.as_ref()) {
|
||||
let values: Vec<_> = self
|
||||
.channels
|
||||
.iter_mut()
|
||||
.zip(&frame.channels)
|
||||
.enumerate()
|
||||
.map(|(i, (c, d))| c.absolute(d, t, if frame.coupling && i == 1 { 2 } else { 1 }))
|
||||
.collect();
|
||||
let levels = dequantise(frame, &values, t);
|
||||
for (((c, d), (lv, y)), cur) in self.channels.iter_mut().zip(&frame.channels).zip(levels.iter().zip(&mut y)).zip(&mut current) {
|
||||
let g = &d.grid;
|
||||
let slots = (2 * g.t_env[0], 2 * g.t_env[g.num_env]);
|
||||
let mut x_high: Box<HighBand> = Box::new([[Cx::default(); 64]; X_SLOTS]);
|
||||
hf::generate(&c.x_low, t, &d.invf, &mut c.history, slots, &mut x_high);
|
||||
let f = hf::Frame { x: &x_high, t, hdr: &h, g, lv, harmonic: d.harmonic.as_ref() };
|
||||
hf::adjust(&f, &mut c.history, y);
|
||||
*cur = Some((t.kx, t.m, slots.0 + T_HF_ADJ, slots.1 + T_HF_ADJ));
|
||||
}
|
||||
}
|
||||
let mut out = vec![vec![0f32; 2 * 1024]; self.channels.len()];
|
||||
for (((c, y), out), current) in self.channels.iter_mut().zip(y).zip(&mut out).zip(current) {
|
||||
for (l, block) in out.as_chunks_mut::<64>().0.iter_mut().enumerate() {
|
||||
let i = l + T_HF_ADJ;
|
||||
let mut x = [Cx::default(); 64];
|
||||
// the high band of this slot: this frame's envelopes, the last frame's reaching
|
||||
// into this one, or none
|
||||
let high = match current {
|
||||
Some((kx, m, from, to)) if (from..to).contains(&i) => Some((&y[i], kx, m)),
|
||||
_ if i + SLOTS < c.y_prev_end => Some((&c.y_prev[i + SLOTS], c.y_prev_range.0, c.y_prev_range.1)),
|
||||
_ => None,
|
||||
};
|
||||
let low = high.map_or(current.map_or(32, |cur| cur.0), |h| h.1);
|
||||
x[..low].copy_from_slice(&c.x_low[i][..low]);
|
||||
if let Some((row, kx, m)) = high {
|
||||
x[kx..kx + m].copy_from_slice(&row[kx..kx + m]);
|
||||
}
|
||||
c.synthesis.process(&x, 64, block);
|
||||
}
|
||||
for s in out.iter_mut() {
|
||||
*s /= PCM_SCALE;
|
||||
}
|
||||
c.y_prev = y;
|
||||
(c.y_prev_range, c.y_prev_end) = current.map_or(((0, 0), 0), |cur| ((cur.0, cur.1), cur.3));
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// The header of [`test_payload`].
|
||||
#[cfg(test)]
|
||||
pub(crate) const TEST_HEADER: Header = Header {
|
||||
amp_res: true,
|
||||
start_freq: 5,
|
||||
stop_freq: 9,
|
||||
xover_band: 0,
|
||||
freq_scale: 2,
|
||||
alter_scale: true,
|
||||
noise_bands: 2,
|
||||
limiter_bands: 2,
|
||||
limiter_gains: 2,
|
||||
interpol_freq: true,
|
||||
smoothing_mode: true,
|
||||
};
|
||||
|
||||
/// An SBR single channel element for tests at output rate `rate`: [`TEST_HEADER`] (3 dB steps,
|
||||
/// start 5, stop 9, the default extra fields), then one FIXFIX envelope at high resolution with
|
||||
/// band `k` at `env(k)` (1.5 dB steps, as a single FIXFIX envelope has) and noise band `k` at
|
||||
/// `noise(k)`.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn test_payload(rate: u32, env: impl Fn(usize) -> i32, noise: impl Fn(usize) -> i32) -> Option<Vec<u8>> {
|
||||
use craft_media::bitstream::BitWriter;
|
||||
let t = FreqTables::new(&TEST_HEADER, rate)?;
|
||||
let (env, noise): (Vec<i32>, Vec<i32>) = ((0..t.n_high()).map(env).collect(), (0..t.n_q()).map(noise).collect());
|
||||
let mut bw = BitWriter::new();
|
||||
bw.write_bits(1, 1); // bs_header_flag
|
||||
for (v, n) in [(1, 1), (5, 4), (9, 4), (0, 3), (0, 2), (0, 1), (0, 1)] {
|
||||
bw.write_bits(v, n); // amp_res, start, stop, xover, reserved, no extra fields
|
||||
}
|
||||
bw.write_bits(0, 1); // bs_data_extra
|
||||
bw.write_bits(u32::from(bitstream::FIXFIX), 2);
|
||||
bw.write_bits(0, 2); // one envelope
|
||||
bw.write_bits(1, 1); // high resolution
|
||||
bw.write_bits(0, 2); // frequency-direction envelope and noise floor
|
||||
for _ in &noise {
|
||||
bw.write_bits(0, 2); // no inverse filtering
|
||||
}
|
||||
bw.write_bits(env[0] as u32, 7);
|
||||
for w in env.windows(2) {
|
||||
let (code, len) = tables::F_ENV_1_5DB[(w[1] - w[0] + 60) as usize];
|
||||
bw.write_bits(code, u32::from(len));
|
||||
}
|
||||
bw.write_bits(noise[0] as u32, 5);
|
||||
for w in noise.windows(2) {
|
||||
let (code, len) = tables::F_ENV_3_0DB[(w[1] - w[0] + 31) as usize];
|
||||
bw.write_bits(code, u32::from(len));
|
||||
}
|
||||
bw.write_bits(0, 2); // no sinusoids, no extended data
|
||||
Some(bw.finish())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::f64::consts::PI;
|
||||
|
||||
use super::*;
|
||||
|
||||
const RATE: u32 = 44_100;
|
||||
|
||||
fn tables() -> FreqTables {
|
||||
FreqTables::new(&TEST_HEADER, RATE).unwrap()
|
||||
}
|
||||
|
||||
/// Mean energy per DFT bin of `x` between `lo` and `hi` QMF channels (64 per half rate).
|
||||
fn energy(x: &[f32], lo: usize, hi: usize) -> f64 {
|
||||
let n = x.len();
|
||||
let (b0, b1) = (lo * n / 128, hi * n / 128);
|
||||
let mut e = 0f64;
|
||||
for b in b0 + 2..b1 - 2 {
|
||||
let (mut re, mut im) = (0f64, 0f64);
|
||||
for (i, &s) in x.iter().enumerate() {
|
||||
let w = 0.5 - 0.5 * (2.0 * PI * i as f64 / n as f64).cos();
|
||||
let ph = 2.0 * PI * (b * i) as f64 / n as f64;
|
||||
re += f64::from(s) * w * ph.cos();
|
||||
im += f64::from(s) * w * ph.sin();
|
||||
}
|
||||
e += re * re + im * im;
|
||||
}
|
||||
e / (b1 - b0 - 4) as f64
|
||||
}
|
||||
|
||||
fn run(e: &mut Element, data: &[u8], frames: usize) -> Vec<f32> {
|
||||
let core = vec![vec![0f32; 1024]];
|
||||
let mut out = Vec::new();
|
||||
for _ in 0..frames {
|
||||
let p = Payload { data, start: 0, end: 8 * data.len(), crc: false };
|
||||
let block = e.process(&core, Some(p));
|
||||
assert_eq!((block.len(), block[0].len()), (1, 2048));
|
||||
assert!(block[0].iter().all(|s| s.is_finite()));
|
||||
out.extend_from_slice(&block[0]);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Over a silent core the high band is the noise the envelope asks for: the upper bands,
|
||||
/// 12 steps of 1.5 dB above the lower ones, come out 18 dB louder, and nothing below `kx`.
|
||||
#[test]
|
||||
fn the_high_band_follows_the_envelope() {
|
||||
let t = tables();
|
||||
let mid = t.n_high() / 2;
|
||||
let data = test_payload(RATE, |k| if k < mid { 30 } else { 42 }, |_| 0).unwrap();
|
||||
let mut e = Element::new(1, RATE);
|
||||
let out = run(&mut e, &data, 6);
|
||||
let tail = &out[out.len() - 4096..];
|
||||
let (lower, upper) = (energy(tail, t.high[0], t.high[mid]), energy(tail, t.high[mid], t.high[t.n_high()]));
|
||||
let step = 10.0 * (upper / lower).log10();
|
||||
assert!((step - 18.06).abs() < 1.5, "{step:.2} dB");
|
||||
let below = energy(tail, 1, t.kx - 1);
|
||||
assert!(10.0 * (lower / below.max(1e-30)).log10() > 60.0);
|
||||
// the same envelope without its header is not read: an element that has seen none
|
||||
// passes the core through alone
|
||||
let mut fresh = Element::new(1, RATE);
|
||||
let mut no_header = data.clone();
|
||||
no_header[0] &= 0x7F;
|
||||
assert!(run(&mut fresh, &no_header, 3).iter().all(|&s| s == 0.0));
|
||||
}
|
||||
|
||||
/// Damaged SBR data (flipped bits, cut short, or noise) never panics and keeps the output
|
||||
/// finite and of full length.
|
||||
#[test]
|
||||
fn damaged_sbr_data_is_survived() {
|
||||
let good = test_payload(RATE, |k| 20 + k as i32, |_| 3).unwrap();
|
||||
let mut seed = 0x1234_5678u32;
|
||||
let mut next = || {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 17;
|
||||
seed ^= seed << 5;
|
||||
seed
|
||||
};
|
||||
let mut e = Element::new(1, RATE);
|
||||
let mut pair = Element::new(2, RATE);
|
||||
for i in 0..600 {
|
||||
let mut data = good.clone();
|
||||
for _ in 0..1 + next() % 6 {
|
||||
let bit = next() as usize % (8 * data.len());
|
||||
data[bit / 8] ^= 0x80 >> (bit % 8);
|
||||
}
|
||||
if i % 3 == 0 {
|
||||
data.truncate(1 + next() as usize % data.len());
|
||||
}
|
||||
if i % 5 == 0 {
|
||||
data = (0..1 + next() % 40).map(|_| next() as u8).collect();
|
||||
data[0] |= 0x80;
|
||||
}
|
||||
run(&mut e, &data, 1);
|
||||
let p = Payload { data: &data, start: 0, end: 8 * data.len(), crc: i % 2 == 0 };
|
||||
let out = pair.process(&[vec![0.1; 1024], vec![-0.1; 1024]], Some(p));
|
||||
assert!(out.iter().all(|c| c.len() == 2048 && c.iter().all(|s| s.is_finite())));
|
||||
}
|
||||
}
|
||||
|
||||
/// The largest envelope (127 steps of 1.5 dB) with the largest noise floor (each band 31
|
||||
/// steps below the last) keeps the output finite: the gains are formed so that no
|
||||
/// intermediate product of two clamped levels overflows.
|
||||
#[test]
|
||||
fn extreme_levels_stay_finite() {
|
||||
let rate =
|
||||
[22_050, 24_000, 32_000, 44_100, 48_000].into_iter().find(|&r| FreqTables::new(&TEST_HEADER, r).is_some_and(|t| t.n_q() >= 3)).unwrap();
|
||||
let data = test_payload(rate, |_| 127, |k| -31 * k as i32).unwrap();
|
||||
let mut e = Element::new(1, rate);
|
||||
run(&mut e, &data, 3);
|
||||
}
|
||||
|
||||
/// A header that runs past the end of its fill element is not taken: the one in use stays,
|
||||
/// and the frame is dropped.
|
||||
#[test]
|
||||
fn a_header_cut_short_is_not_taken() {
|
||||
let data = test_payload(RATE, |_| 30, |_| 0).unwrap();
|
||||
let mut e = Element::new(1, RATE);
|
||||
run(&mut e, &data, 1);
|
||||
assert_eq!(e.header, Some(TEST_HEADER));
|
||||
// another start frequency (bits 2..6), in an element that ends 6 bits in
|
||||
let mut cut = data.clone();
|
||||
cut[0] ^= 0x04;
|
||||
assert!(e.read(Payload { data: &cut, start: 0, end: 6, crc: false }).is_none());
|
||||
assert_eq!(e.header, Some(TEST_HEADER));
|
||||
assert!(e.read(Payload { data: &cut, start: 0, end: 8 * cut.len(), crc: false }).is_some());
|
||||
assert_ne!(e.header, Some(TEST_HEADER));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
//! The SBR filter banks (ISO/IEC 14496-3 §4.6.18.4): a 32-band complex QMF analysis bank at the
|
||||
//! core rate and a 64-band complex QMF synthesis bank at the output rate, sharing the window `c`.
|
||||
//!
|
||||
//! Each bank is a polyphase window followed by a complex modulation: analysis
|
||||
//! `2·exp(iπ(k + ½)(2n − ½)/64)`, synthesis `exp(iπ(k + ½)(2n − 251)/128)/64`. With the window's
|
||||
//! indexing, the two phase offsets put the banks' combined response on its centre tap, so the low
|
||||
//! band comes back unchanged (to about -60 dB, the window's own limit), 576 output samples late.
|
||||
//! The synthesis bank holds back its last two output samples, which makes it 578: with the SBR
|
||||
//! frame's lag, the 962 samples by which HE-AAC decoders (ffmpeg's among them) delay the core, and
|
||||
//! which the priming encoders signal counts on.
|
||||
|
||||
use std::f64::consts::PI;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use super::Cx;
|
||||
use super::tables::QMF_WINDOW;
|
||||
|
||||
/// Output samples by which analysis followed by synthesis delays the low band.
|
||||
pub(crate) const DELAY: usize = 578;
|
||||
|
||||
/// Modulation matrices, computed once.
|
||||
struct Kernels {
|
||||
/// Analysis, at `[k][n]`: 32 × 64.
|
||||
analysis: Vec<[Cx; 64]>,
|
||||
/// Synthesis, at `[n][k]`: 128 × 64.
|
||||
synthesis: Vec<[Cx; 64]>,
|
||||
}
|
||||
|
||||
fn kernels() -> &'static Kernels {
|
||||
static K: OnceLock<Kernels> = OnceLock::new();
|
||||
K.get_or_init(|| {
|
||||
let cx = |phase: f64, scale: f64| Cx { re: (phase.cos() * scale) as f32, im: (phase.sin() * scale) as f32 };
|
||||
let analysis = (0..32).map(|k| std::array::from_fn(|n| cx(PI * (k as f64 + 0.5) * (2.0 * n as f64 - 0.5) / 64.0, 2.0))).collect();
|
||||
let synthesis = (0..128).map(|n| std::array::from_fn(|k| cx(PI * (k as f64 + 0.5) * (2.0 * n as f64 - 251.0) / 128.0, 1.0 / 64.0))).collect();
|
||||
Kernels { analysis, synthesis }
|
||||
})
|
||||
}
|
||||
|
||||
/// The 32-band analysis bank of one channel.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Analysis {
|
||||
/// The last 320 input samples, newest first.
|
||||
x: Box<[f32; 320]>,
|
||||
}
|
||||
|
||||
impl Analysis {
|
||||
pub(crate) fn new() -> Analysis {
|
||||
Analysis { x: Box::new([0.0; 320]) }
|
||||
}
|
||||
|
||||
/// Split `32 · out.len()` input samples into `out.len()` slots of 32 subbands.
|
||||
pub(crate) fn process(&mut self, input: &[f32], out: &mut [[Cx; 32]]) {
|
||||
let k = kernels();
|
||||
for (block, slot) in input.as_chunks::<32>().0.iter().zip(out.iter_mut()) {
|
||||
self.x.copy_within(0..288, 32);
|
||||
for (n, &s) in block.iter().enumerate() {
|
||||
self.x[31 - n] = s;
|
||||
}
|
||||
let mut u = [0f32; 64];
|
||||
for (n, u) in u.iter_mut().enumerate() {
|
||||
*u = (0..5).map(|j| self.x[n + 64 * j] * QMF_WINDOW[2 * (n + 64 * j)]).sum();
|
||||
}
|
||||
for (sb, row) in slot.iter_mut().zip(&k.analysis) {
|
||||
let (mut re, mut im) = (0f32, 0f32);
|
||||
for (u, m) in u.iter().zip(row) {
|
||||
re += u * m.re;
|
||||
im += u * m.im;
|
||||
}
|
||||
*sb = Cx { re, im };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The 64-band synthesis bank of one channel.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Synthesis {
|
||||
v: Box<[f32; 1280]>,
|
||||
/// The last two output samples, which the next slot puts out first.
|
||||
held: [f32; 2],
|
||||
}
|
||||
|
||||
impl Synthesis {
|
||||
pub(crate) fn new() -> Synthesis {
|
||||
Synthesis { v: Box::new([0.0; 1280]), held: [0.0; 2] }
|
||||
}
|
||||
|
||||
/// Merge one slot of 64 subbands into 64 output samples (`out[..64]`, of which it needs 64).
|
||||
/// Bands from `bands` up are taken as zero.
|
||||
pub(crate) fn process(&mut self, x: &[Cx; 64], bands: usize, out: &mut [f32]) {
|
||||
let k = kernels();
|
||||
let bands = bands.min(64);
|
||||
self.v.copy_within(0..1152, 128);
|
||||
for (n, row) in k.synthesis.iter().enumerate() {
|
||||
self.v[n] = x[..bands].iter().zip(row).map(|(x, m)| x.re * m.re - x.im * m.im).sum();
|
||||
}
|
||||
let mut slot = [0f32; 66];
|
||||
slot[..2].copy_from_slice(&self.held);
|
||||
for (n, o) in slot[2..].iter_mut().enumerate() {
|
||||
let mut s = 0f32;
|
||||
for i in 0..5 {
|
||||
s += self.v[256 * i + n] * QMF_WINDOW[128 * i + n];
|
||||
s += self.v[256 * i + 192 + n] * QMF_WINDOW[128 * i + 64 + n];
|
||||
}
|
||||
*o = s;
|
||||
}
|
||||
self.held = [slot[64], slot[65]];
|
||||
for (o, s) in out.iter_mut().zip(&slot[..64]) {
|
||||
*o = *s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::SLOTS;
|
||||
use super::*;
|
||||
|
||||
fn tones(t: f64) -> f64 {
|
||||
[0.0371, 0.0913, 0.1527, 0.2211, 0.2903, 0.3519, 0.4127].iter().enumerate().map(|(j, f)| (2.0 * PI * f * t + j as f64).sin() * 0.1).sum()
|
||||
}
|
||||
|
||||
/// Tones across the core band through analysis and synthesis (low 32 bands only) come out at
|
||||
/// twice the rate, [`DELAY`] samples late, with the error 60 dB down.
|
||||
#[test]
|
||||
fn analysis_then_synthesis_reconstructs_the_input() {
|
||||
let input: Vec<f32> = (0..8 * 1024).map(|i| tones(i as f64) as f32).collect();
|
||||
let (mut a, mut s) = (Analysis::new(), Synthesis::new());
|
||||
let mut out = Vec::new();
|
||||
let mut slots = [[Cx::default(); 32]; SLOTS];
|
||||
for frame in input.as_chunks::<1024>().0 {
|
||||
a.process(frame, &mut slots);
|
||||
for slot in &slots {
|
||||
let mut x = [Cx::default(); 64];
|
||||
x[..32].copy_from_slice(slot);
|
||||
let mut o = [0f32; 64];
|
||||
s.process(&x, 32, &mut o);
|
||||
out.extend_from_slice(&o);
|
||||
}
|
||||
}
|
||||
assert_eq!(out.len(), 2 * input.len());
|
||||
let snr_at = |delay: usize| {
|
||||
let (mut err, mut sig) = (0f64, 0f64);
|
||||
for (i, &o) in out.iter().enumerate().skip(3000) {
|
||||
let want = tones((i as f64 - delay as f64) / 2.0);
|
||||
err += (o as f64 - want).powi(2);
|
||||
sig += want * want;
|
||||
}
|
||||
10.0 * (sig / err).log10()
|
||||
};
|
||||
let snr = snr_at(DELAY);
|
||||
assert!(snr > 60.0, "reconstruction SNR {snr:.1} dB");
|
||||
assert!(snr_at(DELAY - 1) < 40.0 && snr_at(DELAY + 1) < 40.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
//! SBR tables (ETSI ES 201 980 V1.2.2 Annex I, the SBR tables it shares with ISO/IEC 14496-3 §4.A.6).
|
||||
//!
|
||||
//! Transcribed from the specification's tables, which give the window to 14 digits (more than an
|
||||
//! `f32` holds). Annex I misprints the labels of `c[297]` and `c[305]`; the values are in order.
|
||||
//! Each Huffman table is a complete prefix code (see the tests in `sbr::bitstream`).
|
||||
|
||||
#![allow(clippy::excessive_precision)]
|
||||
|
||||
/// The QMF bank window `c` (640 coefficients).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static QMF_WINDOW: [f32; 640] = [
|
||||
-0.00055252865047, -0.00056176925738, -0.00049475180896, -0.00048752279712, -0.00048937912498, -0.00050407143497, -0.00052265642972, -0.00054665656337,
|
||||
-0.00056778025613, -0.00058709304852, -0.00061327473938, -0.00063124935319, -0.00065403333621, -0.00067776907764, -0.00069416146273, -0.00071577364744,
|
||||
-0.00072550431222, -0.00074409418541, -0.00074905980532, -0.00076813719270, -0.00077248485949, -0.00078343322877, -0.00077798694927, -0.00078036647100,
|
||||
-0.00078014496257, -0.00077579773310, -0.00076307935757, -0.00075300014201, -0.00073193571525, -0.00072153919876, -0.00069179375372, -0.00066504150893,
|
||||
-0.00063415949025, -0.00059461189330, -0.00055645763906, -0.00051455722108, -0.00046063254803, -0.00040951214522, -0.00035011758756, -0.00028969811748,
|
||||
-0.00020983373440, -0.00014463809349, -0.00006173344072, 0.00001349497418, 0.00010943831274, 0.00020430170688, 0.00029495311041, 0.00040265402160,
|
||||
0.00051073884952, 0.00062393761391, 0.00074580258865, 0.00086084433262, 0.00098859883015, 0.00112501551307, 0.00125778846475, 0.00139024948272,
|
||||
0.00154432198471, 0.00168680832531, 0.00183482654224, 0.00198411407369, 0.00214615835557, 0.00230172547746, 0.00246256169126, 0.00262017586902,
|
||||
0.00278704643465, 0.00294694477165, 0.00311254206525, 0.00327396134847, 0.00344188741828, 0.00360082681231, 0.00376039229104, 0.00392074323703,
|
||||
0.00408197531935, 0.00422642692270, 0.00437307196781, 0.00452098527825, 0.00466064606118, 0.00479325608498, 0.00491376035745, 0.00503930226013,
|
||||
0.00514073539032, 0.00524611661324, 0.00534716811982, 0.00541967759307, 0.00548760401507, 0.00554757145088, 0.00559380230045, 0.00562206432097,
|
||||
0.00564551969164, 0.00563891995151, 0.00562661141932, 0.00559171286630, 0.00554043639400, 0.00547537830770, 0.00538389758970, 0.00527157587272,
|
||||
0.00513822754514, 0.00498396877629, 0.00481094690600, 0.00460395301471, 0.00438018617447, 0.00412516423270, 0.00384564081246, 0.00354012465507,
|
||||
0.00320918858098, 0.00284467578623, 0.00245085400321, 0.00202741761850, 0.00157846825768, 0.00109023290512, 0.00058322642480, 0.00002760451905,
|
||||
-0.00054642808664, -0.00115681355227, -0.00180394725893, -0.00248267236449, -0.00319337783900, -0.00394011240522, -0.00472225962400, -0.00553372111088,
|
||||
-0.00637922932685, -0.00726158168517, -0.00817982333726, -0.00913253296085, -0.01011502154986, -0.01113155480321, -0.01218499959508, -0.01327182200351,
|
||||
0.01439046660792, 0.01554055533423, 0.01673247129989, 0.01794333813443, 0.01918724313698, 0.02045317933555, 0.02174675502535, 0.02306801692862,
|
||||
0.02441609920285, 0.02578758475467, 0.02718594296329, 0.02860721736385, 0.03005026574279, 0.03150176087389, 0.03297540810337, 0.03446209487686,
|
||||
0.03596975605542, 0.03748128504252, 0.03900536794745, 0.04053491705584, 0.04206490946367, 0.04360975421304, 0.04514884056413, 0.04668430272642,
|
||||
0.04821657200672, 0.04973857556014, 0.05125561555216, 0.05276307465207, 0.05424527683589, 0.05571736482138, 0.05716164501299, 0.05859156836260,
|
||||
0.05998374801761, 0.06134551717207, 0.06268578081172, 0.06397158980681, 0.06522471064380, 0.06643675122104, 0.06760759851228, 0.06870438283512,
|
||||
0.06976302447127, 0.07076287107266, 0.07170026731102, 0.07256825833083, 0.07336202550803, 0.07410036424342, 0.07474525581194, 0.07531373362019,
|
||||
0.07580083586584, 0.07619924793396, 0.07649921704119, 0.07670934904245, 0.07681739756964, 0.07682300113923, 0.07672049241746, 0.07650507183194,
|
||||
0.07617483218536, 0.07573057565061, 0.07515762552870, 0.07446643947564, 0.07364060057620, 0.07267746427299, 0.07158263647903, 0.07035330735093,
|
||||
0.06896640131951, 0.06745250215166, 0.06576906686508, 0.06394448059633, 0.06196027790387, 0.05981665708090, 0.05751526919867, 0.05504600343009,
|
||||
0.05240938217366, 0.04959786763445, 0.04663033051701, 0.04347687821958, 0.04014582784127, 0.03664181168133, 0.03295839306691, 0.02908240060125,
|
||||
0.02503075618909, 0.02079970728622, 0.01637012582228, 0.01176238327857, 0.00696368621617, 0.00197656014503, -0.00320868968304, -0.00857117491366,
|
||||
-0.01412888273558, -0.01988341292573, -0.02582272888064, -0.03195312745332, -0.03827765720822, -0.04478068215856, -0.05148041767934, -0.05837053268336,
|
||||
-0.06544098531359, -0.07269433008129, -0.08013729344279, -0.08775475365593, -0.09555333528914, -0.10353295311463, -0.11168269317730, -0.12000779846800,
|
||||
-0.12850028503878, -0.13715517611934, -0.14597664911870, -0.15496070710605, -0.16409588556669, -0.17338081721706, -0.18281725485142, -0.19239667457267,
|
||||
-0.20212501768103, -0.21197358538056, -0.22196526964149, -0.23206908706791, -0.24230168845974, -0.25264803095722, -0.26310532994603, -0.27366340405625,
|
||||
-0.28432141891085, -0.29507167170646, -0.30590985751916, -0.31682789136456, -0.32781137272105, -0.33887226938665, -0.34999141229310, -0.36115899031355,
|
||||
0.37237955463061, 0.38363500139043, 0.39492117615675, 0.40623176767625, 0.41756968968409, 0.42891199207373, 0.44025537543665, 0.45159965356824,
|
||||
0.46293080852757, 0.47424532146115, 0.48552530911099, 0.49677082545707, 0.50798175000434, 0.51912349702391, 0.53022408956855, 0.54125534487322,
|
||||
0.55220512585061, 0.56307891401370, 0.57385241316923, 0.58454032354679, 0.59511230862496, 0.60557835389180, 0.61591099320291, 0.62612426956055,
|
||||
0.63619801077286, 0.64612696959461, 0.65590163024671, 0.66551398801627, 0.67496631901712, 0.68423532934598, 0.69332823767032, 0.70223887193539,
|
||||
0.71094104263095, 0.71944626349561, 0.72774489002994, 0.73582117582769, 0.74368278636488, 0.75131374561237, 0.75870807608242, 0.76586748650939,
|
||||
0.77277808813327, 0.77942875190216, 0.78583531203920, 0.79197358416424, 0.79784664137700, 0.80344857518505, 0.80876950044491, 0.81381912706217,
|
||||
0.81857760046468, 0.82304198905409, 0.82722753473360, 0.83110384571520, 0.83469373618402, 0.83797173378865, 0.84095413924722, 0.84362382812005,
|
||||
0.84598184698206, 0.84803157770763, 0.84978051984268, 0.85119715249343, 0.85230470352147, 0.85310209497017, 0.85357205739107, 0.85373856005937,
|
||||
0.85357205739107, 0.85310209497017, 0.85230470352147, 0.85119715249343, 0.84978051984268, 0.84803157770763, 0.84598184698206, 0.84362382812005,
|
||||
0.84095413924722, 0.83797173378865, 0.83469373618402, 0.83110384571520, 0.82722753473360, 0.82304198905409, 0.81857760046468, 0.81381912706217,
|
||||
0.80876950044491, 0.80344857518505, 0.79784664137700, 0.79197358416424, 0.78583531203920, 0.77942875190216, 0.77277808813327, 0.76586748650939,
|
||||
0.75870807608242, 0.75131374561237, 0.74368278636488, 0.73582117582769, 0.72774489002994, 0.71944626349561, 0.71094104263095, 0.70223887193539,
|
||||
0.69332823767032, 0.68423532934598, 0.67496631901712, 0.66551398801627, 0.65590163024671, 0.64612696959461, 0.63619801077286, 0.62612426956055,
|
||||
0.61591099320291, 0.60557835389180, 0.59511230862496, 0.58454032354679, 0.57385241316923, 0.56307891401370, 0.55220512585061, 0.54125534487322,
|
||||
0.53022408956855, 0.51912349702391, 0.50798175000434, 0.49677082545707, 0.48552530911099, 0.47424532146115, 0.46293080852757, 0.45159965356824,
|
||||
0.44025537543665, 0.42891199207373, 0.41756968968409, 0.40623176767625, 0.39492117615675, 0.38363500139043, 0.37237955463061, 0.36115899031355,
|
||||
-0.34999141229310, -0.33887226938665, -0.32781137272105, -0.31682789136456, -0.30590985751916, -0.29507167170646, -0.28432141891085, -0.27366340405625,
|
||||
-0.26310532994603, -0.25264803095722, -0.24230168845974, -0.23206908706791, -0.22196526964149, -0.21197358538056, -0.20212501768103, -0.19239667457267,
|
||||
-0.18281725485142, -0.17338081721706, -0.16409588556669, -0.15496070710605, -0.14597664911870, -0.13715517611934, -0.12850028503878, -0.12000779846800,
|
||||
-0.11168269317730, -0.10353295311463, -0.09555333528914, -0.08775475365593, -0.08013729344279, -0.07269433008129, -0.06544098531359, -0.05837053268336,
|
||||
-0.05148041767934, -0.04478068215856, -0.03827765720822, -0.03195312745332, -0.02582272888064, -0.01988341292573, -0.01412888273558, -0.00857117491366,
|
||||
-0.00320868968304, 0.00197656014503, 0.00696368621617, 0.01176238327857, 0.01637012582228, 0.02079970728622, 0.02503075618909, 0.02908240060125,
|
||||
0.03295839306691, 0.03664181168133, 0.04014582784127, 0.04347687821958, 0.04663033051701, 0.04959786763445, 0.05240938217366, 0.05504600343009,
|
||||
0.05751526919867, 0.05981665708090, 0.06196027790387, 0.06394448059633, 0.06576906686508, 0.06745250215166, 0.06896640131951, 0.07035330735093,
|
||||
0.07158263647903, 0.07267746427299, 0.07364060057620, 0.07446643947564, 0.07515762552870, 0.07573057565061, 0.07617483218536, 0.07650507183194,
|
||||
0.07672049241746, 0.07682300113923, 0.07681739756964, 0.07670934904245, 0.07649921704119, 0.07619924793396, 0.07580083586584, 0.07531373362019,
|
||||
0.07474525581194, 0.07410036424342, 0.07336202550803, 0.07256825833083, 0.07170026731102, 0.07076287107266, 0.06976302447127, 0.06870438283512,
|
||||
0.06760759851228, 0.06643675122104, 0.06522471064380, 0.06397158980681, 0.06268578081172, 0.06134551717207, 0.05998374801761, 0.05859156836260,
|
||||
0.05716164501299, 0.05571736482138, 0.05424527683589, 0.05276307465207, 0.05125561555216, 0.04973857556014, 0.04821657200672, 0.04668430272642,
|
||||
0.04514884056413, 0.04360975421304, 0.04206490946367, 0.04053491705584, 0.03900536794745, 0.03748128504252, 0.03596975605542, 0.03446209487686,
|
||||
0.03297540810337, 0.03150176087389, 0.03005026574279, 0.02860721736385, 0.02718594296329, 0.02578758475467, 0.02441609920285, 0.02306801692862,
|
||||
0.02174675502535, 0.02045317933555, 0.01918724313698, 0.01794333813443, 0.01673247129989, 0.01554055533423, 0.01439046660792, 0.01327182200351,
|
||||
-0.01218499959508, -0.01113155480321, -0.01011502154986, -0.00913253296085, -0.00817982333726, -0.00726158168517, -0.00637922932685, -0.00553372111088,
|
||||
-0.00472225962400, -0.00394011240522, -0.00319337783900, -0.00248267236449, -0.00180394725893, -0.00115681355227, -0.00054642808664, 0.00002760451905,
|
||||
0.00058322642480, 0.00109023290512, 0.00157846825768, 0.00202741761850, 0.00245085400321, 0.00284467578623, 0.00320918858098, 0.00354012465507,
|
||||
0.00384564081246, 0.00412516423270, 0.00438018617447, 0.00460395301471, 0.00481094690600, 0.00498396877629, 0.00513822754514, 0.00527157587272,
|
||||
0.00538389758970, 0.00547537830770, 0.00554043639400, 0.00559171286630, 0.00562661141932, 0.00563891995151, 0.00564551969164, 0.00562206432097,
|
||||
0.00559380230045, 0.00554757145088, 0.00548760401507, 0.00541967759307, 0.00534716811982, 0.00524611661324, 0.00514073539032, 0.00503930226013,
|
||||
0.00491376035745, 0.00479325608498, 0.00466064606118, 0.00452098527825, 0.00437307196781, 0.00422642692270, 0.00408197531935, 0.00392074323703,
|
||||
0.00376039229104, 0.00360082681231, 0.00344188741828, 0.00327396134847, 0.00311254206525, 0.00294694477165, 0.00278704643465, 0.00262017586902,
|
||||
0.00246256169126, 0.00230172547746, 0.00214615835557, 0.00198411407369, 0.00183482654224, 0.00168680832531, 0.00154432198471, 0.00139024948272,
|
||||
0.00125778846475, 0.00112501551307, 0.00098859883015, 0.00086084433262, 0.00074580258865, 0.00062393761391, 0.00051073884952, 0.00040265402160,
|
||||
0.00029495311041, 0.00020430170688, 0.00010943831274, 0.00001349497418, -0.00006173344072, -0.00014463809349, -0.00020983373440, -0.00028969811748,
|
||||
-0.00035011758756, -0.00040951214522, -0.00046063254803, -0.00051455722108, -0.00055645763906, -0.00059461189330, -0.00063415949025, -0.00066504150893,
|
||||
-0.00069179375372, -0.00072153919876, -0.00073193571525, -0.00075300014201, -0.00076307935757, -0.00077579773310, -0.00078014496257, -0.00078036647100,
|
||||
-0.00077798694927, -0.00078343322877, -0.00077248485949, -0.00076813719270, -0.00074905980532, -0.00074409418541, -0.00072550431222, -0.00071577364744,
|
||||
-0.00069416146273, -0.00067776907764, -0.00065403333621, -0.00063124935319, -0.00061327473938, -0.00058709304852, -0.00056778025613, -0.00054665656337,
|
||||
-0.00052265642972, -0.00050407143497, -0.00048937912498, -0.00048752279712, -0.00049475180896, -0.00056176925738, -0.00055252865047, 0.00000000000000,
|
||||
];
|
||||
|
||||
/// The SBR noise table `V` (Table I.13): 512 unit-variance complex values (re, im).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static NOISE: [[f32; 2]; 512] = [
|
||||
[-0.99948153278296, -0.59483417516607], [0.97113454393991, -0.67528515225647], [0.14130051758487, -0.95090983575689], [-0.47005496701697, -0.37340549728647],
|
||||
[0.80705063769351, 0.29653668284408], [-0.38981478896926, 0.89572605717087], [-0.01053049862020, -0.66959058036166], [-0.91266367957293, -0.11522938140034],
|
||||
[0.54840422910309, 0.75221367176302], [0.40009252867955, -0.98929400334421], [-0.99867974711855, -0.88147068645358], [-0.95531076805040, 0.90908757154593],
|
||||
[-0.45725933317144, -0.56716323646760], [-0.72929675029275, -0.98008272727324], [0.75622801399036, 0.20950329995549], [0.07069442601050, -0.78247898470706],
|
||||
[0.74496252926055, -0.91169004445807], [-0.96440182703856, -0.94739918296622], [0.30424629369539, -0.49438267012479], [0.66565033746925, 0.64652935542491],
|
||||
[0.91697008020594, 0.17514097332009], [-0.70774918760427, 0.52548653416543], [-0.70051415345560, -0.45340028808763], [-0.99496513054797, -0.90071908066973],
|
||||
[0.98164490790123, -0.77463155528697], [-0.54671580548181, -0.02570928536004], [-0.01689629065389, 0.00287506445732], [-0.86110349531986, 0.42548583726477],
|
||||
[-0.98892980586032, -0.87881132267556], [0.51756627678691, 0.66926784710139], [-0.99635026409640, -0.58107730574765], [-0.99969370862163, 0.98369989360250],
|
||||
[0.55266258627194, 0.59449057465591], [0.34581177741673, 0.94879421061866], [0.62664209577999, -0.74402970906471], [-0.77149701404973, -0.33883658042801],
|
||||
[-0.91592244254432, 0.03687901376713], [-0.76285492357887, -0.91371867919124], [0.79788337195331, -0.93180971199849], [0.54473080610200, -0.11919206037186],
|
||||
[-0.85639281671058, 0.42429854760451], [-0.92882402971423, 0.27871809078609], [-0.11708371046774, -0.99800843444966], [0.21356749817493, -0.90716295627033],
|
||||
[-0.76191692573909, 0.99768118356265], [0.98111043100884, -0.95854459734407], [-0.85913269895572, 0.95766566168880], [-0.93307242253692, 0.49431757696466],
|
||||
[0.30485754879632, -0.70540034357529], [0.85289650925190, 0.46766131791044], [0.91328082618125, -0.99839597361769], [-0.05890199924154, 0.70741827819497],
|
||||
[0.28398686150148, 0.34633555702188], [0.95258164539612, -0.54893416026939], [-0.78566324168507, -0.75568541079691], [-0.95789495447877, -0.20423194696966],
|
||||
[0.82411158711197, 0.96654618432562], [-0.65185446735885, -0.88734990773289], [-0.93643603134666, 0.99870790442385], [0.91427159529618, -0.98290505544444],
|
||||
[-0.70395684036886, 0.58796798221039], [0.00563771969365, 0.61768196727244], [0.89065051931895, 0.52783352697585], [-0.68683707712762, 0.80806944710339],
|
||||
[0.72165342518718, -0.69259857349564], [-0.62928247730667, 0.13627037407335], [0.29938434065514, -0.46051329682246], [-0.91781958879280, -0.74012716684186],
|
||||
[0.99298717043688, 0.40816610075661], [0.82368298622748, -0.74036047190173], [-0.98512833386833, -0.99972330709594], [-0.95915368242257, -0.99237800466040],
|
||||
[-0.21411126572790, -0.93424819052545], [-0.68821476106884, -0.26892306315457], [0.91851997982317, 0.09358228901785], [-0.96062769559127, 0.36099095133739],
|
||||
[0.51646184922287, -0.71373332873917], [0.61130721139669, 0.46950141175917], [0.47336129371299, -0.27333178296162], [0.90998308703519, 0.96715662938132],
|
||||
[0.44844799194357, 0.99211574628306], [0.66614891079092, 0.96590176169121], [0.74922239129237, -0.89879858826087], [-0.99571588506485, 0.52785521494349],
|
||||
[0.97401082477563, -0.16855870075190], [0.72683747733879, -0.48060774432251], [0.95432193457128, 0.68849603408441], [-0.72962208425191, -0.76608443420917],
|
||||
[-0.85359479233537, 0.88738125901579], [-0.81412430338535, -0.97480768049637], [-0.87930772356786, 0.74748307690436], [-0.71573331064977, -0.98570608178923],
|
||||
[0.83524300028228, 0.83702537075163], [-0.48086065601423, -0.98848504923531], [0.97139128574778, 0.80093621198236], [0.51992825347895, 0.80247631400510],
|
||||
[-0.00848591195325, -0.76670128000486], [-0.70294374303036, 0.55359910445577], [-0.95894428168140, -0.43265504344783], [0.97079252950321, 0.09325857238682],
|
||||
[-0.92404293670797, 0.85507704027855], [-0.69506469500450, 0.98633412625459], [0.26559203620024, 0.73314307966524], [0.28038443336943, 0.14537913654427],
|
||||
[-0.74138124825523, 0.99310339807762], [-0.01752795995444, -0.82616635284178], [-0.55126773094930, -0.98898543862153], [0.97960898850996, -0.94021446752851],
|
||||
[-0.99196309146936, 0.67019017358456], [-0.67684928085260, 0.12631491649378], [0.09140039465500, -0.20537731453108], [-0.71658965751996, -0.97788200391224],
|
||||
[0.81014640078925, 0.53722648362443], [0.40616991671205, -0.26469008598449], [-0.67680188682972, 0.94502052337695], [0.86849774348749, -0.18333598647899],
|
||||
[-0.99500381284851, -0.02634122068550], [0.84329189340667, 0.10406957462213], [-0.09215968531446, 0.69540012101253], [0.99956173327206, -0.12358542001404],
|
||||
[-0.79732779473535, -0.91582524736159], [0.96349973642406, 0.96640458041000], [-0.79942778496547, 0.64323902822857], [-0.11566039853896, 0.28587846253726],
|
||||
[-0.39922954514662, 0.94129601616966], [0.99089197565987, -0.92062625581587], [0.28631285179909, -0.91035047143603], [-0.83302725605608, -0.67330410892084],
|
||||
[0.95404443402072, 0.49162765398743], [-0.06449863579434, 0.03250560813135], [-0.99575054486311, 0.42389784469507], [-0.65501142790847, 0.82546114655624],
|
||||
[-0.81254441908887, -0.51627234660629], [-0.99646369485481, 0.84490533520752], [0.00287840603348, 0.64768261158166], [0.70176989408455, -0.20453028573322],
|
||||
[0.96361882270190, 0.40706967140989], [-0.68883758192426, 0.91338958840772], [-0.34875585502238, 0.71472290693300], [0.91980081243087, 0.66507455644919],
|
||||
[-0.99009048343881, 0.85868021604848], [0.68865791458395, 0.55660316809678], [-0.99484402129368, -0.20052559254934], [0.94214511408023, -0.99696425367461],
|
||||
[-0.67414626793544, 0.49548221180078], [-0.47339353684664, -0.85904328834047], [0.14323651387360, -0.94145598222488], [-0.29268293575672, 0.05759224927952],
|
||||
[0.43793861458754, -0.78904969892724], [-0.36345126374441, 0.64874435357162], [-0.08750604656825, 0.97686944362527], [-0.96495267812511, -0.53960305946511],
|
||||
[0.55526940659947, 0.78891523734774], [0.73538215752630, 0.96452072373404], [-0.30889773919437, -0.80664389776860], [0.03574995626194, -0.97325616900959],
|
||||
[0.98720684660488, 0.48409133691962], [-0.81689296271203, -0.90827703628298], [0.67866860118215, 0.81284503870856], [-0.15808569732583, 0.85279555024382],
|
||||
[0.80723395114371, -0.24717418514605], [0.47788757329038, -0.46333147839295], [0.96367554763201, 0.38486749303242], [-0.99143875716818, -0.24945277239809],
|
||||
[0.83081876925833, -0.94780851414763], [-0.58753191905341, 0.01290772389163], [0.95538108220960, -0.85557052096538], [-0.96490920476211, -0.64020970923102],
|
||||
[-0.97327101028521, 0.12378128133110], [0.91400366022124, 0.57972471346930], [-0.99925837363824, 0.71084847864067], [-0.86875903507313, -0.20291699203564],
|
||||
[-0.26240034795124, -0.68264554369108], [-0.24664412953388, -0.87642273115183], [0.02416275806869, 0.27192914288905], [0.82068619590515, -0.85087787994476],
|
||||
[0.88547373760759, -0.89636802901469], [-0.18173078152226, -0.26152145156800], [0.09355476558534, 0.54845123045604], [-0.54668414224090, 0.95980774020221],
|
||||
[0.37050990604091, -0.59910140383171], [-0.70373594262891, 0.91227665827081], [-0.34600785879594, -0.99441426144200], [-0.68774481731008, -0.30238837956299],
|
||||
[-0.26843291251234, 0.83115668004362], [0.49072334613242, -0.45359708737775], [0.38975993093975, 0.95515358099121], [-0.97757125224150, 0.05305894580606],
|
||||
[-0.17325552859616, -0.92770672250494], [0.99948035025744, 0.58285545563426], [-0.64946246527458, 0.68645507104960], [-0.12016920576437, -0.57147322153312],
|
||||
[-0.58947456517751, -0.34847132454388], [-0.41815140454465, 0.16276422358861], [0.99885650204884, 0.11136095490444], [-0.56649614128386, -0.90494866361587],
|
||||
[0.94138021032330, 0.35281916733018], [-0.75725076534641, 0.53650549640587], [0.20541973692630, -0.94435144369918], [0.99980371023351, 0.79835913565599],
|
||||
[0.29078277605775, 0.35393777921520], [-0.62858772103030, 0.38765693387102], [0.43440904467688, -0.98546330463232], [-0.98298583762390, 0.21021524625209],
|
||||
[0.19513029146934, -0.94239832251867], [-0.95476662400101, 0.98364554179143], [0.93379635304810, -0.70881994583682], [-0.85235410573336, -0.08342347966410],
|
||||
[-0.86425093011245, -0.45795025029466], [0.38879779059045, 0.97274429344593], [0.92045124735495, -0.62433652524220], [0.89162532251878, 0.54950955570563],
|
||||
[-0.36834336949252, 0.96458298020975], [0.93891760988045, -0.89968353740388], [0.99267657565094, -0.03757034316958], [-0.94063471614176, 0.41332338538963],
|
||||
[0.99740224117019, -0.16830494996370], [-0.35899413170555, -0.46633226649613], [0.05237237274947, -0.25640361602661], [0.36703583957424, -0.38653265641875],
|
||||
[0.91653180367913, -0.30587628726597], [0.69000803499316, 0.90952171386132], [-0.38658751133527, 0.99501571208985], [-0.29250814029851, 0.37444994344615],
|
||||
[-0.60182204677608, 0.86779651036123], [-0.97418588163217, 0.96468523666475], [0.88461574003963, 0.57508405276414], [0.05198933055162, 0.21269661669964],
|
||||
[-0.53499621979720, 0.97241553731237], [-0.49429560226497, 0.98183865291903], [-0.98935142339139, -0.40249159006933], [-0.98081380091130, -0.72856895534041],
|
||||
[-0.27338148835532, 0.99950922447209], [0.06310802338302, -0.54539587529618], [-0.20461677199539, -0.14209977628489], [0.66223843141647, 0.72528579940326],
|
||||
[-0.84764345483665, 0.02372316801261], [-0.89039863483811, 0.88866581484602], [0.95903308477986, 0.76744927173873], [0.73504123909879, -0.03747203173192],
|
||||
[-0.31744434966056, -0.36834111883652], [-0.34110827591623, 0.40211222807691], [0.47803883714199, -0.39423219786288], [0.98299195879514, 0.01989791390047],
|
||||
[-0.30963073129751, -0.18076720599336], [0.99992588229018, -0.26281872094289], [-0.93149731080767, -0.98313162570490], [0.99923472302773, -0.80142993767554],
|
||||
[-0.26024169633417, -0.75999759855752], [-0.35712514743563, 0.19298963768574], [-0.99899084509530, 0.74645156992493], [0.86557171579452, 0.55593866696299],
|
||||
[0.33408042438752, 0.86185953874709], [0.99010736374716, 0.04602397576623], [-0.66694269691195, -0.91643611810148], [0.64016792079480, 0.15649530836856],
|
||||
[0.99570534804836, 0.45844586038111], [-0.63431466947340, 0.21079116459234], [-0.07706847005931, -0.89581437101329], [0.98590090577724, 0.88241721133981],
|
||||
[0.80099335254678, -0.36851896710853], [0.78368131392666, 0.45506999802597], [0.08707806671691, 0.80938994918745], [-0.86811883080712, 0.39347308654705],
|
||||
[-0.39466529740375, -0.66809432114456], [0.97875325649683, -0.72467840967746], [-0.95038560288864, 0.89563219587625], [0.17005239424212, 0.54683053962658],
|
||||
[-0.76910792026848, -0.96226617549298], [0.99743281016846, 0.42697157037567], [0.95437383549973, 0.97002324109952], [0.99578905365569, -0.54106826257356],
|
||||
[0.28058259829990, -0.85361420634036], [0.85256524470573, -0.64567607735589], [-0.50608540105128, -0.65846015480300], [-0.97210735183243, -0.23095213067791],
|
||||
[0.95424048234441, -0.99240147091219], [-0.96926570524023, 0.73775654896574], [0.30872163214726, 0.41514960556126], [-0.24523839572639, 0.63206633394807],
|
||||
[-0.33813265086024, -0.38661779441897], [-0.05826828420146, -0.06940774188029], [-0.22898461455054, 0.97054853316316], [-0.18509915019881, 0.47565762892084],
|
||||
[-0.10488238045009, -0.87769947402394], [-0.71886586182037, 0.78030982480538], [0.99793873738654, 0.90041310491497], [0.57563307626120, -0.91034337352097],
|
||||
[0.28909646383717, 0.96307783970534], [0.42188998312520, 0.48148651230437], [0.93335049681047, -0.43537023883588], [-0.97087374418267, 0.86636445711364],
|
||||
[0.36722871286923, 0.65291654172961], [-0.81093025665696, 0.08778370229363], [-0.26240603062237, -0.92774095379098], [0.83996497984604, 0.55839849139647],
|
||||
[-0.99909615720225, -0.96024605713970], [0.74649464155061, 0.12144893606462], [-0.74774595569805, -0.26898062008959], [0.95781667469567, -0.79047927052628],
|
||||
[0.95472308713099, -0.08588776019550], [0.48708332746299, 0.99999041579432], [0.46332038247497, 0.10964126185063], [-0.76497004940162, 0.89210929242238],
|
||||
[0.57397389364339, 0.35289703373760], [0.75374316974495, 0.96705214651335], [-0.59174397685714, -0.89405370422752], [0.75087906691890, -0.29612672982396],
|
||||
[-0.98607857336230, 0.25034911730023], [-0.40761056640505, -0.90045573444695], [0.66929266740477, 0.98629493401748], [-0.97463695257310, -0.00190223301301],
|
||||
[0.90145509409859, 0.99781390365446], [-0.87259289048043, 0.99233587353666], [-0.91529461447692, -0.15698707534206], [-0.03305738840705, -0.37205262859764],
|
||||
[0.07223051368337, -0.88805001733626], [0.99498012188353, 0.97094358113387], [-0.74904939500519, 0.99985483641521], [0.04585228574211, 0.99812337444082],
|
||||
[-0.89054954257993, -0.31791913188064], [-0.83782144651251, 0.97637632547466], [0.33454804933804, -0.86231516800408], [-0.99707579362824, 0.93237990079441],
|
||||
[-0.22827527843994, 0.18874759397997], [0.67248046289143, -0.03646211390569], [-0.05146538187944, -0.92599700120679], [0.99947295749905, 0.93625229707912],
|
||||
[0.66951124390363, 0.98905825623893], [-0.99602956559179, -0.44654715757688], [0.82104905483590, 0.99540741724928], [0.99186510988782, 0.72023001312947],
|
||||
[-0.65284592392918, 0.52186723253637], [0.93885443798188, -0.74895312615259], [0.96735248738388, 0.90891816978629], [-0.22225968841114, 0.57124029781228],
|
||||
[-0.44132783753414, -0.92688840659280], [-0.85694974219574, 0.88844532719844], [0.91783042091762, -0.46356892383970], [0.72556974415690, -0.99899555770747],
|
||||
[-0.99711581834508, 0.58211560180426], [0.77638976371966, 0.94321834873819], [0.07717324253925, 0.58638399856595], [-0.56049829194163, 0.82522301569036],
|
||||
[0.98398893639988, 0.39467440420569], [0.47546946844938, 0.68613044836811], [0.65675089314631, 0.18331637134880], [0.03273375457980, -0.74933109564108],
|
||||
[-0.38684144784738, 0.51337349030406], [-0.97346267944545, -0.96549364384098], [-0.53282156061942, -0.91423265091354], [0.99817310731176, 0.61133572482148],
|
||||
[-0.50254500772635, -0.88829338134294], [0.01995873238855, 0.85223515096765], [0.99930381973804, 0.94578896296649], [0.82907767600783, -0.06323442598128],
|
||||
[-0.58660709669728, 0.96840773806582], [-0.17573736667267, -0.48166920859485], [0.83434292401346, -0.13023450646997], [0.05946491307025, 0.20511047074866],
|
||||
[0.81505484574602, -0.94685947861369], [-0.44976380954860, 0.40894572671545], [-0.89746474625671, 0.99846578838537], [0.39677256130792, -0.74854668609359],
|
||||
[-0.07588948563079, 0.74096214084170], [0.76343198951445, 0.41746629422634], [-0.74490104699626, 0.94725911744610], [0.64880119792759, 0.41336660830571],
|
||||
[0.62319537462542, -0.93098313552599], [0.42215817594807, -0.07712787385208], [0.02704554141885, -0.05417518053666], [0.80001773566818, 0.91542195141039],
|
||||
[-0.79351832348816, -0.36208897989136], [0.63872359151636, 0.08128252493444], [0.52890520960295, 0.60048872455592], [0.74238552914587, 0.04491915291044],
|
||||
[0.99096131449250, -0.19451182854402], [-0.80412329643109, -0.88513818199457], [-0.64612616129736, 0.72198674804544], [0.11657770663191, -0.83662833815041],
|
||||
[-0.95053182488101, -0.96939905138082], [-0.62228872928622, 0.82767262846661], [0.03004475787316, -0.99738896333384], [-0.97987214341034, 0.36526129686425],
|
||||
[-0.99986980746200, -0.36021610299715], [0.89110648599879, -0.97894250343044], [0.10407960510582, 0.77357793811619], [0.95964737821728, -0.35435818285502],
|
||||
[0.50843233159162, 0.96107691266205], [0.17006334670615, -0.76854025314829], [0.25872675063360, 0.99893303933816], [-0.01115998681937, 0.98496019742444],
|
||||
[-0.79598702973261, 0.97138411318894], [-0.99264708948101, -0.99542822402536], [-0.99829663752818, 0.01877138824311], [-0.70801016548184, 0.33680685948117],
|
||||
[-0.70467057786826, 0.93272777501857], [0.99846021905254, -0.98725746254433], [-0.63364968534650, -0.16473594423746], [-0.16258217500792, -0.95939125400802],
|
||||
[-0.43645594360633, -0.94805030113284], [-0.99848471702976, 0.96245166923809], [-0.16796458968998, -0.98987511890470], [-0.87979225745213, -0.71725725041680],
|
||||
[0.44183099021786, -0.93568974498761], [0.93310180125532, -0.99913308068246], [-0.93941931782002, -0.56409379640356], [-0.88590003188677, 0.47624600491382],
|
||||
[0.99971463703691, -0.83889954253462], [-0.75376385639978, 0.00814643438625], [0.93887685615875, -0.11284528204636], [0.85126435782309, 0.52349251543547],
|
||||
[0.39701421446381, 0.81779634174316], [-0.37024464187437, -0.87071656222959], [-0.36024828242896, 0.34655735648287], [-0.93388812549209, -0.84476541096429],
|
||||
[-0.65298804552119, -0.18439575450921], [0.11960319006843, 0.99899346780168], [0.94292565553160, 0.83163906518293], [0.75081145286948, -0.35533223142265],
|
||||
[0.56721979748394, -0.24076836414499], [0.46857766746029, -0.30140233457198], [0.97312313923635, -0.99548191630031], [-0.38299976567017, 0.98516909715427],
|
||||
[0.41025800019463, 0.02116736935734], [0.09638062008048, 0.04411984381457], [-0.85283249275397, 0.91475563922421], [0.88866808958124, -0.99735267083226],
|
||||
[-0.48202429536989, -0.96805608884164], [0.27572582416567, 0.58634753335832], [-0.65889129659168, 0.58835634138583], [0.98838086953732, 0.99994349600236],
|
||||
[-0.20651349620689, 0.54593044066355], [-0.62126416356920, -0.59893681700392], [0.20320105410437, -0.86879180355289], [-0.97790548600584, 0.96290806999242],
|
||||
[0.11112534735126, 0.21484763313301], [-0.41368337314182, 0.28216837680365], [0.24133038992960, 0.51294362630238], [-0.66393410674885, -0.08249679629081],
|
||||
[-0.53697829178752, -0.97649903936228], [-0.97224737889348, 0.22081333579837], [0.87392477144549, -0.12796173740361], [0.19050361015753, 0.01602615387195],
|
||||
[-0.46353441212724, -0.95249041539006], [-0.07064096339021, -0.94479803205886], [-0.92444085484466, -0.10457590187436], [-0.83822593578728, -0.01695043208885],
|
||||
[0.75214681811150, -0.99955681042665], [-0.42102998829339, 0.99720941999394], [-0.72094786237696, -0.35008961934255], [0.78843311019251, 0.52851398958271],
|
||||
[0.97394027897442, -0.26695944086561], [0.99206463477946, -0.57010120849429], [0.76789609461795, -0.76519356730966], [-0.82002421836409, -0.73530179553767],
|
||||
[0.81924990025724, 0.99698425250579], [-0.26719850873357, 0.68903369776193], [-0.43311260380975, 0.85321815947490], [0.99194979673836, 0.91876249766422],
|
||||
[-0.80692001248487, -0.32627540663214], [0.43080003649976, -0.21919095636638], [0.67709491937357, -0.95478075822906], [0.56151770568316, -0.70693811747778],
|
||||
[0.10831862810749, -0.08628837174592], [0.91229417540436, -0.65987351408410], [-0.48972893932274, 0.56289246362686], [-0.89033658689697, -0.71656563987082],
|
||||
[0.65269447475094, 0.65916004833932], [0.67439478141121, -0.81684380846796], [-0.47770832416973, -0.16789556203025], [-0.99715979260878, -0.93565784007648],
|
||||
[-0.90889593602546, 0.62034397054380], [-0.06618622548177, -0.23812217221359], [0.99430266919728, 0.18812555317553], [0.97686402381843, -0.28664534366620],
|
||||
[0.94813650221268, -0.97506640027128], [-0.95434497492853, -0.79607978501983], [-0.49104783137150, 0.32895214359663], [0.99881175120751, 0.88993983831354],
|
||||
[0.50449166760303, -0.85995072408434], [0.47162891065108, -0.18680204049569], [-0.62081581361840, 0.75000676218956], [-0.43867015250812, 0.99998069244322],
|
||||
[0.98630563232075, -0.53578899600662], [-0.61510362277374, -0.89515019899997], [-0.03841517601843, -0.69888815681179], [-0.30102157304644, -0.07667808922205],
|
||||
[0.41881284182683, 0.02188098922282], [-0.86135454941237, 0.98947480909359], [0.67226861393788, -0.13494389011014], [-0.70737398842068, -0.76547349325992],
|
||||
[0.94044946687963, 0.09026201157416], [-0.82386352534327, 0.08924768823676], [-0.32070666698656, 0.50143421908753], [0.57593163224487, -0.98966422921509],
|
||||
[-0.36326018419965, 0.07440243123228], [0.99979044674350, -0.14130287347405], [-0.92366023326932, -0.97979298068180], [-0.44607178518598, -0.54233252016394],
|
||||
[0.44226800932956, 0.71326756742752], [0.03671907158312, 0.63606389366675], [0.52175424682195, -0.85396826735705], [-0.94701139690956, -0.01826348194255],
|
||||
[-0.98759606946049, 0.82288714303073], [0.87434794743625, 0.89399495655433], [-0.93412041758744, 0.41374052024363], [0.96063943315511, 0.93116709541280],
|
||||
[0.97534253457837, 0.86150930812689], [0.99642466504163, 0.70190043427512], [-0.94705089665984, -0.29580042814306], [0.91599807087376, -0.98147830385781],
|
||||
];
|
||||
|
||||
/// `t_huffman_env_1_5dB` (`(codeword, length)` by index): envelope, time direction, 1.5 dB steps (LAV 60).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static T_ENV_1_5DB: [(u32, u8); 121] = [
|
||||
(0x3ffd6, 18), (0x3ffd7, 18), (0x3ffd8, 18), (0x3ffd9, 18), (0x3ffda, 18), (0x3ffdb, 18), (0x7ffb8, 19), (0x7ffb9, 19),
|
||||
(0x7ffba, 19), (0x7ffbb, 19), (0x7ffbc, 19), (0x7ffbd, 19), (0x7ffbe, 19), (0x7ffbf, 19), (0x7ffc0, 19), (0x7ffc1, 19),
|
||||
(0x7ffc2, 19), (0x7ffc3, 19), (0x7ffc4, 19), (0x7ffc5, 19), (0x7ffc6, 19), (0x7ffc7, 19), (0x7ffc8, 19), (0x7ffc9, 19),
|
||||
(0x7ffca, 19), (0x7ffcb, 19), (0x7ffcc, 19), (0x7ffcd, 19), (0x7ffce, 19), (0x7ffcf, 19), (0x7ffd0, 19), (0x7ffd1, 19),
|
||||
(0x7ffd2, 19), (0x7ffd3, 19), (0x1ffe6, 17), (0x3ffd4, 18), (0xfff0, 16), (0x1ffe9, 17), (0x3ffd5, 18), (0x1ffe7, 17),
|
||||
(0xfff1, 16), (0xffec, 16), (0xffed, 16), (0xffee, 16), (0x7ff4, 15), (0x3ff9, 14), (0x3ff7, 14), (0x1ffa, 13),
|
||||
(0x1ff9, 13), (0xffb, 12), (0x7fc, 11), (0x3fc, 10), (0x1fd, 9), (0xfd, 8), (0x7d, 7), (0x3d, 6),
|
||||
(0x1d, 5), (0xd, 4), (0x5, 3), (0x1, 2), (0x0, 2), (0x4, 3), (0xc, 4), (0x1c, 5),
|
||||
(0x3c, 6), (0x7c, 7), (0xfc, 8), (0x1fc, 9), (0x3fd, 10), (0xffa, 12), (0x1ff8, 13), (0x3ff6, 14),
|
||||
(0x3ff8, 14), (0x7ff5, 15), (0xffef, 16), (0x1ffe8, 17), (0xfff2, 16), (0x7ffd4, 19), (0x7ffd5, 19), (0x7ffd6, 19),
|
||||
(0x7ffd7, 19), (0x7ffd8, 19), (0x7ffd9, 19), (0x7ffda, 19), (0x7ffdb, 19), (0x7ffdc, 19), (0x7ffdd, 19), (0x7ffde, 19),
|
||||
(0x7ffdf, 19), (0x7ffe0, 19), (0x7ffe1, 19), (0x7ffe2, 19), (0x7ffe3, 19), (0x7ffe4, 19), (0x7ffe5, 19), (0x7ffe6, 19),
|
||||
(0x7ffe7, 19), (0x7ffe8, 19), (0x7ffe9, 19), (0x7ffea, 19), (0x7ffeb, 19), (0x7ffec, 19), (0x7ffed, 19), (0x7ffee, 19),
|
||||
(0x7ffef, 19), (0x7fff0, 19), (0x7fff1, 19), (0x7fff2, 19), (0x7fff3, 19), (0x7fff4, 19), (0x7fff5, 19), (0x7fff6, 19),
|
||||
(0x7fff7, 19), (0x7fff8, 19), (0x7fff9, 19), (0x7fffa, 19), (0x7fffb, 19), (0x7fffc, 19), (0x7fffd, 19), (0x7fffe, 19),
|
||||
(0x7ffff, 19),
|
||||
];
|
||||
|
||||
/// `f_huffman_env_1_5dB` (`(codeword, length)` by index): envelope, frequency direction, 1.5 dB steps (LAV 60).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static F_ENV_1_5DB: [(u32, u8); 121] = [
|
||||
(0x7ffe7, 19), (0x7ffe8, 19), (0xfffd2, 20), (0xfffd3, 20), (0xfffd4, 20), (0xfffd5, 20), (0xfffd6, 20), (0xfffd7, 20),
|
||||
(0xfffd8, 20), (0x7ffda, 19), (0xfffd9, 20), (0xfffda, 20), (0xfffdb, 20), (0xfffdc, 20), (0x7ffdb, 19), (0xfffdd, 20),
|
||||
(0x7ffdc, 19), (0x7ffdd, 19), (0xfffde, 20), (0x3ffe4, 18), (0xfffdf, 20), (0xfffe0, 20), (0xfffe1, 20), (0x7ffde, 19),
|
||||
(0xfffe2, 20), (0xfffe3, 20), (0xfffe4, 20), (0x7ffdf, 19), (0xfffe5, 20), (0x7ffe0, 19), (0x3ffe8, 18), (0x7ffe1, 19),
|
||||
(0x3ffe0, 18), (0x3ffe9, 18), (0x1ffef, 17), (0x3ffe5, 18), (0x1ffec, 17), (0x1ffed, 17), (0x1ffee, 17), (0xfff4, 16),
|
||||
(0xfff3, 16), (0xfff0, 16), (0x7ff7, 15), (0x7ff6, 15), (0x3ffa, 14), (0x1ffa, 13), (0x1ff9, 13), (0xffa, 12),
|
||||
(0xff8, 12), (0x7f9, 11), (0x3fb, 10), (0x1fc, 9), (0x1fa, 9), (0xfb, 8), (0x7c, 7), (0x3c, 6),
|
||||
(0x1c, 5), (0xc, 4), (0x5, 3), (0x1, 2), (0x0, 2), (0x4, 3), (0xd, 4), (0x1d, 5),
|
||||
(0x3d, 6), (0xfa, 8), (0xfc, 8), (0x1fb, 9), (0x3fa, 10), (0x7f8, 11), (0x7fa, 11), (0x7fb, 11),
|
||||
(0xff9, 12), (0xffb, 12), (0x1ff8, 13), (0x1ffb, 13), (0x3ff8, 14), (0x3ff9, 14), (0xfff1, 16), (0xfff2, 16),
|
||||
(0x1ffea, 17), (0x1ffeb, 17), (0x3ffe1, 18), (0x3ffe2, 18), (0x3ffea, 18), (0x3ffe3, 18), (0x3ffe6, 18), (0x3ffe7, 18),
|
||||
(0x3ffeb, 18), (0xfffe6, 20), (0x7ffe2, 19), (0xfffe7, 20), (0xfffe8, 20), (0xfffe9, 20), (0xfffea, 20), (0xfffeb, 20),
|
||||
(0xfffec, 20), (0x7ffe3, 19), (0xfffed, 20), (0xfffee, 20), (0xfffef, 20), (0xffff0, 20), (0x7ffe4, 19), (0xffff1, 20),
|
||||
(0x3ffec, 18), (0xffff2, 20), (0xffff3, 20), (0x7ffe5, 19), (0x7ffe6, 19), (0xffff4, 20), (0xffff5, 20), (0xffff6, 20),
|
||||
(0xffff7, 20), (0xffff8, 20), (0xffff9, 20), (0xffffa, 20), (0xffffb, 20), (0xffffc, 20), (0xffffd, 20), (0xffffe, 20),
|
||||
(0xfffff, 20),
|
||||
];
|
||||
|
||||
/// `t_huffman_env_bal_1_5dB` (`(codeword, length)` by index): envelope balance, time direction, 1.5 dB steps (LAV 24).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static T_ENV_BAL_1_5DB: [(u32, u8); 49] = [
|
||||
(0xffe4, 16), (0xffe5, 16), (0xffe6, 16), (0xffe7, 16), (0xffe8, 16), (0xffe9, 16), (0xffea, 16), (0xffeb, 16),
|
||||
(0xffec, 16), (0xffed, 16), (0xffee, 16), (0xffef, 16), (0xfff0, 16), (0xfff1, 16), (0xfff2, 16), (0xfff3, 16),
|
||||
(0xfff4, 16), (0xffe2, 16), (0xffc, 12), (0x7fc, 11), (0x1fe, 9), (0x7e, 7), (0x1e, 5), (0x6, 3),
|
||||
(0x0, 1), (0x2, 2), (0xe, 4), (0x3e, 6), (0xfe, 8), (0x7fd, 11), (0xffd, 12), (0x7ff0, 15),
|
||||
(0xffe3, 16), (0xfff5, 16), (0xfff6, 16), (0xfff7, 16), (0xfff8, 16), (0xfff9, 16), (0xfffa, 16), (0x1fff6, 17),
|
||||
(0x1fff7, 17), (0x1fff8, 17), (0x1fff9, 17), (0x1fffa, 17), (0x1fffb, 17), (0x1fffc, 17), (0x1fffd, 17), (0x1fffe, 17),
|
||||
(0x1ffff, 17),
|
||||
];
|
||||
|
||||
/// `f_huffman_env_bal_1_5dB` (`(codeword, length)` by index): envelope balance, frequency direction, 1.5 dB steps (LAV 24).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static F_ENV_BAL_1_5DB: [(u32, u8); 49] = [
|
||||
(0x3ffe2, 18), (0x3ffe3, 18), (0x3ffe4, 18), (0x3ffe5, 18), (0x3ffe6, 18), (0x3ffe7, 18), (0x3ffe8, 18), (0x3ffe9, 18),
|
||||
(0x3ffea, 18), (0x3ffeb, 18), (0x3ffec, 18), (0x3ffed, 18), (0x3ffee, 18), (0x3ffef, 18), (0x3fff0, 18), (0xfff7, 16),
|
||||
(0x1fff0, 17), (0x3ffc, 14), (0x7fe, 11), (0x7fc, 11), (0xfe, 8), (0x7e, 7), (0xe, 4), (0x2, 2),
|
||||
(0x0, 1), (0x6, 3), (0x1e, 5), (0x3e, 6), (0x1fe, 9), (0x7fd, 11), (0xffe, 12), (0x7ffa, 15),
|
||||
(0xfff6, 16), (0x3fff1, 18), (0x3fff2, 18), (0x3fff3, 18), (0x3fff4, 18), (0x3fff5, 18), (0x3fff6, 18), (0x3fff7, 18),
|
||||
(0x3fff8, 18), (0x3fff9, 18), (0x3fffa, 18), (0x3fffb, 18), (0x3fffc, 18), (0x3fffd, 18), (0x3fffe, 18), (0x7fffe, 19),
|
||||
(0x7ffff, 19),
|
||||
];
|
||||
|
||||
/// `t_huffman_env_3_0dB` (`(codeword, length)` by index): envelope, time direction, 3 dB steps (LAV 31).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static T_ENV_3_0DB: [(u32, u8); 63] = [
|
||||
(0x3ffed, 18), (0x3ffee, 18), (0x7ffde, 19), (0x7ffdf, 19), (0x7ffe0, 19), (0x7ffe1, 19), (0x7ffe2, 19), (0x7ffe3, 19),
|
||||
(0x7ffe4, 19), (0x7ffe5, 19), (0x7ffe6, 19), (0x7ffe7, 19), (0x7ffe8, 19), (0x7ffe9, 19), (0x7ffea, 19), (0x7ffeb, 19),
|
||||
(0x7ffec, 19), (0x1fff4, 17), (0xfff7, 16), (0xfff9, 16), (0xfff8, 16), (0x3ffb, 14), (0x3ffa, 14), (0x3ff8, 14),
|
||||
(0x1ffa, 13), (0xffc, 12), (0x7fc, 11), (0xfe, 8), (0x3e, 6), (0xe, 4), (0x2, 2), (0x0, 1),
|
||||
(0x6, 3), (0x1e, 5), (0x7e, 7), (0x1fe, 9), (0x7fd, 11), (0x1ffb, 13), (0x3ff9, 14), (0x3ffc, 14),
|
||||
(0x7ffa, 15), (0xfff6, 16), (0x1fff5, 17), (0x3ffec, 18), (0x7ffed, 19), (0x7ffee, 19), (0x7ffef, 19), (0x7fff0, 19),
|
||||
(0x7fff1, 19), (0x7fff2, 19), (0x7fff3, 19), (0x7fff4, 19), (0x7fff5, 19), (0x7fff6, 19), (0x7fff7, 19), (0x7fff8, 19),
|
||||
(0x7fff9, 19), (0x7fffa, 19), (0x7fffb, 19), (0x7fffc, 19), (0x7fffd, 19), (0x7fffe, 19), (0x7ffff, 19),
|
||||
];
|
||||
|
||||
/// `f_huffman_env_3_0dB` (`(codeword, length)` by index): envelope and noise floor, frequency direction, 3 dB steps (LAV 31).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static F_ENV_3_0DB: [(u32, u8); 63] = [
|
||||
(0xffff0, 20), (0xffff1, 20), (0xffff2, 20), (0xffff3, 20), (0xffff4, 20), (0xffff5, 20), (0xffff6, 20), (0x3fff3, 18),
|
||||
(0x7fff5, 19), (0x7ffee, 19), (0x7ffef, 19), (0x7fff6, 19), (0x3fff4, 18), (0x3fff2, 18), (0xffff7, 20), (0x7fff0, 19),
|
||||
(0x1fff5, 17), (0x3fff0, 18), (0x1fff4, 17), (0xfff7, 16), (0xfff6, 16), (0x7ff8, 15), (0x3ffb, 14), (0xffd, 12),
|
||||
(0x7fd, 11), (0x3fd, 10), (0x1fd, 9), (0xfd, 8), (0x3e, 6), (0xe, 4), (0x2, 2), (0x0, 1),
|
||||
(0x6, 3), (0x1e, 5), (0xfc, 8), (0x1fc, 9), (0x3fc, 10), (0x7fc, 11), (0xffc, 12), (0x1ffc, 13),
|
||||
(0x3ffa, 14), (0x7ff9, 15), (0x7ffa, 15), (0xfff8, 16), (0xfff9, 16), (0x1fff6, 17), (0x1fff7, 17), (0x3fff5, 18),
|
||||
(0x3fff6, 18), (0x3fff1, 18), (0xffff8, 20), (0x7fff1, 19), (0x7fff2, 19), (0x7fff3, 19), (0xffff9, 20), (0x7fff7, 19),
|
||||
(0x7fff4, 19), (0xffffa, 20), (0xffffb, 20), (0xffffc, 20), (0xffffd, 20), (0xffffe, 20), (0xfffff, 20),
|
||||
];
|
||||
|
||||
/// `t_huffman_env_bal_3_0dB` (`(codeword, length)` by index): envelope balance, time direction, 3 dB steps (LAV 12).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static T_ENV_BAL_3_0DB: [(u32, u8); 25] = [
|
||||
(0x1ff2, 13), (0x1ff3, 13), (0x1ff4, 13), (0x1ff5, 13), (0x1ff6, 13), (0x1ff7, 13), (0x1ff8, 13), (0xff8, 12),
|
||||
(0xfe, 8), (0x7e, 7), (0xe, 4), (0x6, 3), (0x0, 1), (0x2, 2), (0x1e, 5), (0x3e, 6),
|
||||
(0x1fe, 9), (0x1ff9, 13), (0x1ffa, 13), (0x1ffb, 13), (0x1ffc, 13), (0x1ffd, 13), (0x1ffe, 13), (0x3ffe, 14),
|
||||
(0x3fff, 14),
|
||||
];
|
||||
|
||||
/// `f_huffman_env_bal_3_0dB` (`(codeword, length)` by index): envelope and noise floor balance, frequency direction, 3 dB steps (LAV 12).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static F_ENV_BAL_3_0DB: [(u32, u8); 25] = [
|
||||
(0x1ff7, 13), (0x1ff8, 13), (0x1ff9, 13), (0x1ffa, 13), (0x1ffb, 13), (0x3ff8, 14), (0x3ff9, 14), (0x7fc, 11),
|
||||
(0xfe, 8), (0x7e, 7), (0xe, 4), (0x2, 2), (0x0, 1), (0x6, 3), (0x1e, 5), (0x3e, 6),
|
||||
(0x1fe, 9), (0xffa, 12), (0x1ff6, 13), (0x3ffa, 14), (0x3ffb, 14), (0x3ffc, 14), (0x3ffd, 14), (0x3ffe, 14),
|
||||
(0x3fff, 14),
|
||||
];
|
||||
|
||||
/// `t_huffman_noise_3_0dB` (`(codeword, length)` by index): noise floor, time direction (LAV 31).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static T_NOISE_3_0DB: [(u32, u8); 63] = [
|
||||
(0x1fce, 13), (0x1fcf, 13), (0x1fd0, 13), (0x1fd1, 13), (0x1fd2, 13), (0x1fd3, 13), (0x1fd4, 13), (0x1fd5, 13),
|
||||
(0x1fd6, 13), (0x1fd7, 13), (0x1fd8, 13), (0x1fd9, 13), (0x1fda, 13), (0x1fdb, 13), (0x1fdc, 13), (0x1fdd, 13),
|
||||
(0x1fde, 13), (0x1fdf, 13), (0x1fe0, 13), (0x1fe1, 13), (0x1fe2, 13), (0x1fe3, 13), (0x1fe4, 13), (0x1fe5, 13),
|
||||
(0x1fe6, 13), (0x1fe7, 13), (0x7f2, 11), (0xfd, 8), (0x3e, 6), (0xe, 4), (0x6, 3), (0x0, 1),
|
||||
(0x2, 2), (0x1e, 5), (0xfc, 8), (0x3f8, 10), (0x1fcc, 13), (0x1fe8, 13), (0x1fe9, 13), (0x1fea, 13),
|
||||
(0x1feb, 13), (0x1fec, 13), (0x1fcd, 13), (0x1fed, 13), (0x1fee, 13), (0x1fef, 13), (0x1ff0, 13), (0x1ff1, 13),
|
||||
(0x1ff2, 13), (0x1ff3, 13), (0x1ff4, 13), (0x1ff5, 13), (0x1ff6, 13), (0x1ff7, 13), (0x1ff8, 13), (0x1ff9, 13),
|
||||
(0x1ffa, 13), (0x1ffb, 13), (0x1ffc, 13), (0x1ffd, 13), (0x1ffe, 13), (0x3ffe, 14), (0x3fff, 14),
|
||||
];
|
||||
|
||||
/// `t_huffman_noise_bal_3_0dB` (`(codeword, length)` by index): noise floor balance, time direction (LAV 12).
|
||||
#[rustfmt::skip]
|
||||
pub(crate) static T_NOISE_BAL_3_0DB: [(u32, u8); 25] = [
|
||||
(0xec, 8), (0xed, 8), (0xee, 8), (0xef, 8), (0xf0, 8), (0xf1, 8), (0xf2, 8), (0xf3, 8),
|
||||
(0xf4, 8), (0xf5, 8), (0x1c, 5), (0x2, 2), (0x0, 1), (0x6, 3), (0x3a, 6), (0xf6, 8),
|
||||
(0xf7, 8), (0xf8, 8), (0xf9, 8), (0xfa, 8), (0xfb, 8), (0xfc, 8), (0xfd, 8), (0xfe, 8),
|
||||
(0xff, 8),
|
||||
];
|
||||
@@ -61,7 +61,7 @@ pub enum Error {
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Data rate in kb/s by frmsizecod / 2 (Table 5.18).
|
||||
const BITRATES: [u32; 19] = [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640];
|
||||
pub(crate) const BITRATES: [u32; 19] = [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640];
|
||||
|
||||
/// Full-bandwidth channels per acmod (Table 5.8).
|
||||
const NFCHANS: [usize; 8] = [2, 1, 2, 3, 3, 4, 4, 5];
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
//! Compatibility WAVE and AIFF wrappers for embedded PCM essence in interchange files.
|
||||
//! PCM inside the model is interleaved little-endian signed integers (8-bit unsigned for WAVE).
|
||||
|
||||
pub use crate::{pcm_aiff_header as aiff_header, pcm_wav_file as wav_file, pcm_wav_header as wav_header};
|
||||
|
||||
/// PCM, channels, sample rate and bits of a WAVE file (integer PCM only).
|
||||
pub fn parse_wav(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
|
||||
if b.len() < 12 || &b[..4] != b"RIFF" || &b[8..12] != b"WAVE" {
|
||||
return None;
|
||||
}
|
||||
let mut at = 12;
|
||||
let mut fmt = None;
|
||||
while at + 8 <= b.len() {
|
||||
let id = &b[at..at + 4];
|
||||
let len = u32::from_le_bytes(b[at + 4..at + 8].try_into().ok()?) as usize;
|
||||
let body = &b[at + 8..(at + 8).saturating_add(len).min(b.len())];
|
||||
if id == b"fmt " && body.len() >= 16 {
|
||||
let tag = u16::from_le_bytes([body[0], body[1]]);
|
||||
let ch = u16::from_le_bytes([body[2], body[3]]);
|
||||
let sr = u32::from_le_bytes(body[4..8].try_into().ok()?);
|
||||
let bits = u16::from_le_bytes([body[14], body[15]]);
|
||||
if tag != 1 && tag != 0xFFFE {
|
||||
return None;
|
||||
}
|
||||
if tag == 0xFFFE {
|
||||
// The first two bytes of the sub-format GUID carry the real format code.
|
||||
let sub = body.get(24..26)?;
|
||||
if u16::from_le_bytes([sub[0], sub[1]]) != 1 {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
fmt = Some((ch, sr, bits));
|
||||
} else if id == b"data" {
|
||||
let (ch, sr, bits) = fmt?;
|
||||
return Some((body.to_vec(), ch, sr, bits));
|
||||
}
|
||||
at = at.checked_add(8usize.checked_add(len)?.checked_add(len & 1)?)?;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// A complete AIFF file (big-endian PCM) from little-endian PCM.
|
||||
pub fn aiff_file(pcm: &[u8], channels: u16, sample_rate: u32, bits: u16) -> Vec<u8> {
|
||||
let bps = bits.div_ceil(8) as usize;
|
||||
let frames = pcm.len() / (bps * channels.max(1) as usize).max(1);
|
||||
let mut v = aiff_header(frames as u32, channels, sample_rate, bits);
|
||||
for s in pcm.chunks_exact(bps.max(1)) {
|
||||
if bps == 1 {
|
||||
v.push(s[0] ^ 0x80); // WAVE 8-bit is unsigned, AIFF signed
|
||||
} else {
|
||||
v.extend(s.iter().rev());
|
||||
}
|
||||
}
|
||||
if (v.len() - 8) & 1 == 1 {
|
||||
v.push(0);
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// PCM (converted to little-endian), channels, sample rate and bits of an AIFF / AIFF-C (`NONE`
|
||||
/// / `twos` / `sowt`) file.
|
||||
pub fn parse_aiff(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
|
||||
if b.len() < 12 || &b[..4] != b"FORM" || !(&b[8..12] == b"AIFF" || &b[8..12] == b"AIFC") {
|
||||
return None;
|
||||
}
|
||||
let aifc = &b[8..12] == b"AIFC";
|
||||
let mut at = 12;
|
||||
let mut comm = None;
|
||||
while at + 8 <= b.len() {
|
||||
let id = &b[at..at + 4];
|
||||
let len = u32::from_be_bytes(b[at + 4..at + 8].try_into().ok()?) as usize;
|
||||
let body = &b[at + 8..(at + 8).saturating_add(len).min(b.len())];
|
||||
if id == b"COMM" && body.len() >= 18 {
|
||||
let ch = u16::from_be_bytes([body[0], body[1]]);
|
||||
let bits = u16::from_be_bytes([body[6], body[7]]);
|
||||
let sr = from_extended(body[8..18].try_into().ok()?).round() as u32;
|
||||
// AIFF has no compression code (trailing COMM bytes mean nothing); AIFF-C must name one
|
||||
// of the integer PCM codes, or the samples are not integer PCM at all
|
||||
let little = if aifc {
|
||||
match body.get(18..22)? {
|
||||
b"NONE" | b"twos" => false,
|
||||
b"sowt" => true,
|
||||
_ => return None,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
comm = Some((ch, sr, bits, little));
|
||||
} else if id == b"SSND" && body.len() >= 8 {
|
||||
let (ch, sr, bits, little) = comm?;
|
||||
let off = u32::from_be_bytes(body[..4].try_into().ok()?) as usize;
|
||||
let data = body.get(8usize.checked_add(off)?..)?;
|
||||
let bps = bits.div_ceil(8) as usize;
|
||||
let mut pcm = Vec::with_capacity(data.len());
|
||||
for s in data.chunks_exact(bps.max(1)) {
|
||||
if bps == 1 {
|
||||
pcm.push(s[0] ^ 0x80);
|
||||
} else if little {
|
||||
pcm.extend_from_slice(s);
|
||||
} else {
|
||||
pcm.extend(s.iter().rev());
|
||||
}
|
||||
}
|
||||
return Some((pcm, ch, sr, bits));
|
||||
}
|
||||
at = at.checked_add(8usize.checked_add(len)?.checked_add(len & 1)?)?;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn from_extended(b: [u8; 10]) -> f64 {
|
||||
let e = (u16::from_be_bytes([b[0], b[1]]) & 0x7FFF) as i32;
|
||||
let m = u64::from_be_bytes(b[2..10].try_into().unwrap_or([0; 8]));
|
||||
if e == 0 && m == 0 {
|
||||
return 0.0;
|
||||
}
|
||||
m as f64 / (1u64 << 63) as f64 * 2f64.powi(e - 16383)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
/// A mutated EDL whose clip ran for days produced an OMF descriptor for more than 4 GiB of
|
||||
/// PCM: the RIFF size field overflowed ("attempt to add with overflow").
|
||||
#[test]
|
||||
fn huge_wav_header_saturates() {
|
||||
let h = super::wav_header(u32::MAX, 2, u32::MAX, 24);
|
||||
assert_eq!(&h[4..8], &u32::MAX.to_le_bytes());
|
||||
}
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn accepts_truncated_essence_summaries() {
|
||||
let mut wav = wav_file(&[1, 2, 3, 4], 1, 48_000, 16);
|
||||
wav.truncate(46);
|
||||
assert_eq!(parse_wav(&wav), Some((vec![1, 2], 1, 48_000, 16)));
|
||||
let mut aiff = aiff_file(&[1, 2, 3, 4], 1, 48_000, 16);
|
||||
aiff.truncate(56);
|
||||
assert_eq!(parse_aiff(&aiff), Some((vec![1, 2], 1, 48_000, 16)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wav_and_aiff_round_trip() {
|
||||
let pcm: Vec<u8> = (0..48u8).collect(); // 8 stereo 24-bit frames
|
||||
let w = wav_file(&pcm, 2, 48_000, 24);
|
||||
assert_eq!(parse_wav(&w), Some((pcm.clone(), 2, 48_000, 24)));
|
||||
let a = aiff_file(&pcm, 2, 44_100, 24);
|
||||
assert_eq!(parse_aiff(&a), Some((pcm.clone(), 2, 44_100, 24)));
|
||||
let p16: Vec<u8> = (0..32u8).collect();
|
||||
assert_eq!(parse_aiff(&aiff_file(&p16, 1, 96_000, 16)), Some((p16, 1, 96_000, 16)));
|
||||
assert!(parse_wav(b"RIFF").is_none());
|
||||
assert!(parse_aiff(&a[..30]).is_none());
|
||||
for cut in 0..w.len() {
|
||||
let _ = parse_wav(&w[..cut]);
|
||||
let _ = parse_aiff(&a[..cut.min(a.len())]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aifc_needs_an_integer_pcm_compression_code() {
|
||||
let file = |kind: &[u8; 4], code: &[u8]| {
|
||||
let mut comm = vec![0, 1, 0, 0, 0, 1, 0, 16];
|
||||
comm.extend_from_slice(&[0x40, 0x0E, 0xBB, 0x80, 0, 0, 0, 0, 0, 0]);
|
||||
comm.extend_from_slice(code);
|
||||
let mut body = kind.to_vec();
|
||||
body.extend_from_slice(b"COMM");
|
||||
body.extend_from_slice(&(comm.len() as u32).to_be_bytes());
|
||||
body.extend_from_slice(&comm);
|
||||
if comm.len() & 1 == 1 {
|
||||
body.push(0);
|
||||
}
|
||||
body.extend_from_slice(b"SSND");
|
||||
body.extend_from_slice(&10u32.to_be_bytes());
|
||||
body.extend_from_slice(&[0; 8]);
|
||||
body.extend_from_slice(&[0x12, 0x34]);
|
||||
let mut v = b"FORM".to_vec();
|
||||
v.extend_from_slice(&(body.len() as u32).to_be_bytes());
|
||||
v.extend_from_slice(&body);
|
||||
v
|
||||
};
|
||||
assert_eq!(parse_aiff(&file(b"AIFC", b"NONE")).map(|p| p.0), Some(vec![0x34, 0x12]));
|
||||
assert_eq!(parse_aiff(&file(b"AIFC", b"twos")).map(|p| p.0), Some(vec![0x34, 0x12]));
|
||||
assert_eq!(parse_aiff(&file(b"AIFC", b"sowt")).map(|p| p.0), Some(vec![0x12, 0x34]));
|
||||
assert!(parse_aiff(&file(b"AIFC", b"fl32")).is_none(), "float samples are not integer PCM");
|
||||
assert!(parse_aiff(&file(b"AIFC", b"")).is_none(), "AIFF-C without a compression code");
|
||||
// plain AIFF ignores trailing COMM bytes
|
||||
assert_eq!(parse_aiff(&file(b"AIFF", b"sowt")).map(|p| p.0), Some(vec![0x34, 0x12]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,603 @@
|
||||
//! A native FLAC decoder (RFC 9639): [`decode`] for whole `.flac` files and [`Decoder`] for single
|
||||
//! frames (the packets of FLAC in MP4, Matroska or Ogg).
|
||||
//!
|
||||
//! Every frame is checked against its CRC-8 and CRC-16. Subframe types constant, verbatim, fixed
|
||||
//! and LPC; Rice partitions with 4- and 5-bit parameters and escaped partitions; wasted bits;
|
||||
//! left/side, side/right and mid/side stereo; fixed and variable block sizes; 4 to 32 bits per
|
||||
//! sample. The STREAMINFO MD5 is not verified.
|
||||
//!
|
||||
//! Samples come back as `i32`, one `Vec` per channel, in the FLAC channel order.
|
||||
|
||||
/// What went wrong decoding FLAC.
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum DecodeError {
|
||||
/// The data ends early.
|
||||
#[error("truncated FLAC data")]
|
||||
Truncated,
|
||||
/// The data is not valid FLAC.
|
||||
#[error("invalid FLAC data: {0}")]
|
||||
Invalid(&'static str),
|
||||
/// A frame or header failed its CRC.
|
||||
#[error("FLAC CRC mismatch in {0}")]
|
||||
Crc(&'static str),
|
||||
/// Valid FLAC that is not decoded.
|
||||
#[error("unsupported FLAC feature: {0}")]
|
||||
Unsupported(&'static str),
|
||||
/// The stream is longer than the sample limit.
|
||||
#[error("FLAC stream exceeds the sample limit")]
|
||||
TooLarge,
|
||||
}
|
||||
|
||||
/// `Result` of this module.
|
||||
pub type Result<T> = std::result::Result<T, DecodeError>;
|
||||
|
||||
/// The STREAMINFO metadata block (RFC 9639 section 8.2).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct StreamInfo {
|
||||
/// Smallest block size in samples.
|
||||
pub min_block_size: u16,
|
||||
/// Largest block size in samples.
|
||||
pub max_block_size: u16,
|
||||
/// Smallest frame in bytes, 0 if unknown.
|
||||
pub min_frame_size: u32,
|
||||
/// Largest frame in bytes, 0 if unknown.
|
||||
pub max_frame_size: u32,
|
||||
/// Sample rate in Hz.
|
||||
pub sample_rate: u32,
|
||||
/// Channel count, 1 to 8.
|
||||
pub channels: u8,
|
||||
/// Bits per sample, 4 to 32.
|
||||
pub bits_per_sample: u8,
|
||||
/// Samples per channel in the stream, 0 if unknown.
|
||||
pub total_samples: u64,
|
||||
/// MD5 of the unencoded audio, all zero if not set.
|
||||
pub md5: [u8; 16],
|
||||
}
|
||||
|
||||
impl StreamInfo {
|
||||
/// Parse the 34 bytes of a STREAMINFO block body.
|
||||
pub fn parse(b: &[u8]) -> Result<StreamInfo> {
|
||||
let b = b.get(..34).ok_or(DecodeError::Truncated)?;
|
||||
let packed = u64::from_be_bytes([b[10], b[11], b[12], b[13], b[14], b[15], b[16], b[17]]);
|
||||
let info = StreamInfo {
|
||||
min_block_size: u16::from_be_bytes([b[0], b[1]]),
|
||||
max_block_size: u16::from_be_bytes([b[2], b[3]]),
|
||||
min_frame_size: u32::from_be_bytes([0, b[4], b[5], b[6]]),
|
||||
max_frame_size: u32::from_be_bytes([0, b[7], b[8], b[9]]),
|
||||
sample_rate: (packed >> 44) as u32,
|
||||
channels: ((packed >> 41) & 7) as u8 + 1,
|
||||
bits_per_sample: ((packed >> 36) & 31) as u8 + 1,
|
||||
total_samples: packed & 0xF_FFFF_FFFF,
|
||||
md5: b[18..34].try_into().map_err(|_| DecodeError::Truncated)?,
|
||||
};
|
||||
if info.bits_per_sample < 4 {
|
||||
return Err(DecodeError::Invalid("bits per sample below 4"));
|
||||
}
|
||||
if info.min_block_size != 0
|
||||
&& (info.min_block_size < 16 && info.min_block_size != info.max_block_size || info.max_block_size < info.min_block_size)
|
||||
{
|
||||
return Err(DecodeError::Invalid("inconsistent block sizes"));
|
||||
}
|
||||
Ok(info)
|
||||
}
|
||||
}
|
||||
|
||||
/// One decoded frame.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Frame {
|
||||
/// Sample rate in Hz.
|
||||
pub sample_rate: u32,
|
||||
/// Bits per sample.
|
||||
pub bits_per_sample: u8,
|
||||
/// `true` when `number` is the first sample's index (variable block size), `false` when it is
|
||||
/// the frame's index (fixed block size).
|
||||
pub variable_block_size: bool,
|
||||
/// The coded number of the frame header.
|
||||
pub number: u64,
|
||||
/// Samples per channel in the frame.
|
||||
pub block_size: usize,
|
||||
/// One `Vec` per channel, after undoing any stereo decorrelation.
|
||||
pub channels: Vec<Vec<i32>>,
|
||||
/// Bytes the frame took, including the header and CRC footer.
|
||||
pub size: usize,
|
||||
}
|
||||
|
||||
// ---- checksums ----
|
||||
|
||||
fn crc8(data: &[u8]) -> u8 {
|
||||
let mut crc = 0u8;
|
||||
for &b in data {
|
||||
crc ^= b;
|
||||
for _ in 0..8 {
|
||||
crc = if crc & 0x80 != 0 { crc << 1 ^ 0x07 } else { crc << 1 };
|
||||
}
|
||||
}
|
||||
crc
|
||||
}
|
||||
|
||||
fn crc16(data: &[u8]) -> u16 {
|
||||
let mut crc = 0u16;
|
||||
for &b in data {
|
||||
crc ^= u16::from(b) << 8;
|
||||
for _ in 0..8 {
|
||||
crc = if crc & 0x8000 != 0 { crc << 1 ^ 0x8005 } else { crc << 1 };
|
||||
}
|
||||
}
|
||||
crc
|
||||
}
|
||||
|
||||
// ---- bits ----
|
||||
|
||||
struct Bits<'a> {
|
||||
data: &'a [u8],
|
||||
/// Position in bits.
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl<'a> Bits<'a> {
|
||||
fn new(data: &'a [u8]) -> Self {
|
||||
Bits { data, pos: 0 }
|
||||
}
|
||||
|
||||
fn left(&self) -> usize {
|
||||
self.data.len() * 8 - self.pos
|
||||
}
|
||||
|
||||
fn bit(&mut self) -> Result<u32> {
|
||||
let byte = *self.data.get(self.pos / 8).ok_or(DecodeError::Truncated)?;
|
||||
let v = (byte >> (7 - self.pos % 8)) & 1;
|
||||
self.pos += 1;
|
||||
Ok(u32::from(v))
|
||||
}
|
||||
|
||||
/// `n` bits (0 to 32) as an unsigned number.
|
||||
fn bits(&mut self, n: u32) -> Result<u64> {
|
||||
if n as usize > self.left() {
|
||||
return Err(DecodeError::Truncated);
|
||||
}
|
||||
let mut v = 0u64;
|
||||
let mut n = n as usize;
|
||||
while n > 0 {
|
||||
let byte = u64::from(self.data[self.pos / 8]);
|
||||
let avail = 8 - self.pos % 8;
|
||||
let take = avail.min(n);
|
||||
v = v << take | (byte >> (avail - take)) & ((1 << take) - 1);
|
||||
self.pos += take;
|
||||
n -= take;
|
||||
}
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// `n` bits (0 to 33) as a two's complement number.
|
||||
fn signed(&mut self, n: u32) -> Result<i64> {
|
||||
if n == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
let v = self.bits(n)? as i64;
|
||||
Ok(if v >> (n - 1) & 1 == 1 { v - (1 << n) } else { v })
|
||||
}
|
||||
|
||||
/// Zero bits up to the next one bit (which is consumed).
|
||||
fn unary(&mut self) -> Result<u64> {
|
||||
let mut n = 0u64;
|
||||
loop {
|
||||
// Skip whole zero bytes when aligned.
|
||||
if self.pos.is_multiple_of(8) {
|
||||
while self.data.get(self.pos / 8) == Some(&0) {
|
||||
self.pos += 8;
|
||||
n += 8;
|
||||
}
|
||||
}
|
||||
if self.bit()? == 1 {
|
||||
return Ok(n);
|
||||
}
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn align(&mut self) {
|
||||
self.pos = self.pos.div_ceil(8) * 8;
|
||||
}
|
||||
}
|
||||
|
||||
// ---- frame ----
|
||||
|
||||
const BLOCK_SIZES: [usize; 16] = [0, 192, 576, 1152, 2304, 4608, 0, 0, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768];
|
||||
|
||||
/// A frame decoder. `info` supplies what a frame header leaves to the STREAMINFO.
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct Decoder {
|
||||
info: Option<StreamInfo>,
|
||||
}
|
||||
|
||||
impl Decoder {
|
||||
/// A decoder with the stream's STREAMINFO (needed for frames whose header defers the sample
|
||||
/// rate or bit depth to it).
|
||||
pub fn new(info: StreamInfo) -> Decoder {
|
||||
Decoder { info: Some(info) }
|
||||
}
|
||||
|
||||
/// A decoder without STREAMINFO: frames that defer to it are an error.
|
||||
pub fn without_streaminfo() -> Decoder {
|
||||
Decoder { info: None }
|
||||
}
|
||||
|
||||
/// Decode the frame at the start of `data`.
|
||||
pub fn decode_frame(&self, data: &[u8]) -> Result<Frame> {
|
||||
// StreamInfo has public fields: container callers can construct it without
|
||||
// parse(), so validate before it can control shifts and sample reads.
|
||||
if self.info.is_some_and(|i| !(4..=32).contains(&i.bits_per_sample) || !(1..=8).contains(&i.channels) || i.sample_rate == 0) {
|
||||
return Err(DecodeError::Invalid("invalid decoder STREAMINFO"));
|
||||
}
|
||||
let mut r = Bits::new(data);
|
||||
if r.bits(15)? != 0x7FFC {
|
||||
return Err(DecodeError::Invalid("missing frame sync code"));
|
||||
}
|
||||
let variable = r.bit()? == 1;
|
||||
let (bs_code, sr_code) = (r.bits(4)? as usize, r.bits(4)? as usize);
|
||||
let (ch_code, bps_code) = (r.bits(4)? as usize, r.bits(3)? as usize);
|
||||
if r.bit()? != 0 {
|
||||
return Err(DecodeError::Invalid("reserved bit set"));
|
||||
}
|
||||
let number = coded_number(&mut r)?;
|
||||
let block_size = match bs_code {
|
||||
0 => return Err(DecodeError::Invalid("reserved block size")),
|
||||
6 => r.bits(8)? as usize + 1,
|
||||
7 => r.bits(16)? as usize + 1,
|
||||
c => BLOCK_SIZES[c],
|
||||
};
|
||||
let sample_rate = match sr_code {
|
||||
0 => self.info.map(|i| i.sample_rate).ok_or(DecodeError::Invalid("sample rate only in STREAMINFO"))?,
|
||||
1 => 88_200,
|
||||
2 => 176_400,
|
||||
3 => 192_000,
|
||||
4 => 8_000,
|
||||
5 => 16_000,
|
||||
6 => 22_050,
|
||||
7 => 24_000,
|
||||
8 => 32_000,
|
||||
9 => 44_100,
|
||||
10 => 48_000,
|
||||
11 => 96_000,
|
||||
12 => r.bits(8)? as u32 * 1000,
|
||||
13 => r.bits(16)? as u32,
|
||||
14 => r.bits(16)? as u32 * 10,
|
||||
_ => return Err(DecodeError::Invalid("forbidden sample rate code")),
|
||||
};
|
||||
let header_len = r.pos.div_ceil(8);
|
||||
let stored_crc = r.bits(8)? as u8;
|
||||
if crc8(&data[..header_len]) != stored_crc {
|
||||
return Err(DecodeError::Crc("a frame header"));
|
||||
}
|
||||
let bits = match bps_code {
|
||||
0 => self.info.map(|i| u32::from(i.bits_per_sample)).ok_or(DecodeError::Invalid("bit depth only in STREAMINFO"))?,
|
||||
1 => 8,
|
||||
2 => 12,
|
||||
4 => 16,
|
||||
5 => 20,
|
||||
6 => 24,
|
||||
7 => 32,
|
||||
_ => return Err(DecodeError::Invalid("reserved bit depth")),
|
||||
};
|
||||
let (channels, mode) = match ch_code {
|
||||
0..=7 => (ch_code + 1, Stereo::Independent),
|
||||
8 => (2, Stereo::LeftSide),
|
||||
9 => (2, Stereo::SideRight),
|
||||
10 => (2, Stereo::MidSide),
|
||||
_ => return Err(DecodeError::Invalid("reserved channel assignment")),
|
||||
};
|
||||
let mut out: Vec<Vec<i64>> = Vec::with_capacity(channels);
|
||||
for c in 0..channels {
|
||||
let extra = match (mode, c) {
|
||||
(Stereo::LeftSide, 1) | (Stereo::SideRight, 0) | (Stereo::MidSide, 1) => 1,
|
||||
_ => 0,
|
||||
};
|
||||
out.push(subframe(&mut r, block_size, bits + extra)?);
|
||||
}
|
||||
r.align();
|
||||
let size = r.pos / 8;
|
||||
let crc = r.bits(16)? as u16;
|
||||
if crc16(&data[..size]) != crc {
|
||||
return Err(DecodeError::Crc("a frame"));
|
||||
}
|
||||
let size = size + 2;
|
||||
if mode != Stereo::Independent {
|
||||
let (first, rest) = out.split_at_mut(1);
|
||||
let (a, b) = (&mut first[0], &mut rest[0]);
|
||||
for (x, y) in a.iter_mut().zip(b.iter_mut()) {
|
||||
match mode {
|
||||
Stereo::LeftSide => *y = x.wrapping_sub(*y),
|
||||
Stereo::SideRight => *x = x.wrapping_add(*y),
|
||||
_ => {
|
||||
let m = x.wrapping_shl(1) | (*y & 1);
|
||||
let side = *y;
|
||||
*x = m.wrapping_add(side) >> 1;
|
||||
*y = m.wrapping_sub(side) >> 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut chans = Vec::with_capacity(channels);
|
||||
for c in out {
|
||||
let v: Result<Vec<i32>> = c.into_iter().map(|s| checked_sample(s, bits)).collect();
|
||||
chans.push(v?);
|
||||
}
|
||||
Ok(Frame { sample_rate, bits_per_sample: bits as u8, variable_block_size: variable, number, block_size, channels: chans, size })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Stereo {
|
||||
Independent,
|
||||
LeftSide,
|
||||
SideRight,
|
||||
MidSide,
|
||||
}
|
||||
|
||||
/// The UTF-8-like coded number of a frame header (up to 36 bits, 7 bytes).
|
||||
fn coded_number(r: &mut Bits) -> Result<u64> {
|
||||
let first = r.bits(8)? as u8;
|
||||
let extra = match first {
|
||||
0x00..=0x7F => return Ok(u64::from(first)),
|
||||
0xC0..=0xDF => 1,
|
||||
0xE0..=0xEF => 2,
|
||||
0xF0..=0xF7 => 3,
|
||||
0xF8..=0xFB => 4,
|
||||
0xFC..=0xFD => 5,
|
||||
0xFE => 6,
|
||||
_ => return Err(DecodeError::Invalid("bad coded number")),
|
||||
};
|
||||
let mut v = if extra == 6 { 0 } else { u64::from(first & (0x3F >> extra)) };
|
||||
for _ in 0..extra {
|
||||
let b = r.bits(8)?;
|
||||
if b & 0xC0 != 0x80 {
|
||||
return Err(DecodeError::Invalid("bad coded number"));
|
||||
}
|
||||
v = v << 6 | (b & 0x3F);
|
||||
}
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// One subframe: `bits` is the frame bit depth (plus 1 for a side channel).
|
||||
fn subframe(r: &mut Bits, n: usize, bits: u32) -> Result<Vec<i64>> {
|
||||
if r.bit()? != 0 {
|
||||
return Err(DecodeError::Invalid("subframe header padding bit set"));
|
||||
}
|
||||
let kind = r.bits(6)? as u32;
|
||||
let wasted = if r.bit()? == 1 { r.unary()? as u32 + 1 } else { 0 };
|
||||
if wasted >= bits {
|
||||
return Err(DecodeError::Invalid("wasted bits leave no sample bits"));
|
||||
}
|
||||
let bps = bits - wasted;
|
||||
let mut s = vec![0i64; n];
|
||||
match kind {
|
||||
0 => {
|
||||
let v = r.signed(bps)?;
|
||||
s.fill(v);
|
||||
}
|
||||
1 => {
|
||||
for v in s.iter_mut() {
|
||||
*v = r.signed(bps)?;
|
||||
}
|
||||
}
|
||||
8..=12 => {
|
||||
let order = (kind - 8) as usize;
|
||||
warmup(r, &mut s, order, bps)?;
|
||||
residual(r, &mut s, order)?;
|
||||
for i in order..n {
|
||||
let at = |k: usize| s[i - k];
|
||||
// Wrapping: hostile residuals can overflow; such samples fail the range check later.
|
||||
let p = match order {
|
||||
0 => 0,
|
||||
1 => at(1),
|
||||
2 => at(1).wrapping_mul(2).wrapping_sub(at(2)),
|
||||
3 => at(1).wrapping_mul(3).wrapping_sub(at(2).wrapping_mul(3)).wrapping_add(at(3)),
|
||||
_ => at(1).wrapping_mul(4).wrapping_sub(at(2).wrapping_mul(6)).wrapping_add(at(3).wrapping_mul(4)).wrapping_sub(at(4)),
|
||||
};
|
||||
s[i] = s[i].wrapping_add(p);
|
||||
}
|
||||
}
|
||||
32..=63 => {
|
||||
let order = (kind - 31) as usize;
|
||||
warmup(r, &mut s, order, bps)?;
|
||||
let precision = r.bits(4)? as u32 + 1;
|
||||
if precision == 16 {
|
||||
return Err(DecodeError::Invalid("forbidden coefficient precision"));
|
||||
}
|
||||
let shift = r.signed(5)?;
|
||||
if shift < 0 {
|
||||
return Err(DecodeError::Invalid("negative prediction shift"));
|
||||
}
|
||||
let mut coefs = [0i64; 32];
|
||||
for c in coefs.iter_mut().take(order) {
|
||||
*c = r.signed(precision)?;
|
||||
}
|
||||
residual(r, &mut s, order)?;
|
||||
for i in order..n {
|
||||
let mut sum = 0i64;
|
||||
for (k, &c) in coefs.iter().take(order).enumerate() {
|
||||
sum = sum.wrapping_add(c.wrapping_mul(s[i - 1 - k]));
|
||||
}
|
||||
s[i] = s[i].wrapping_add(sum >> shift);
|
||||
}
|
||||
}
|
||||
_ => return Err(DecodeError::Invalid("reserved subframe type")),
|
||||
}
|
||||
if wasted > 0 {
|
||||
for v in s.iter_mut() {
|
||||
*v = v.wrapping_shl(wasted);
|
||||
}
|
||||
}
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
fn warmup(r: &mut Bits, s: &mut [i64], order: usize, bps: u32) -> Result<()> {
|
||||
if order > s.len() {
|
||||
return Err(DecodeError::Invalid("predictor order above the block size"));
|
||||
}
|
||||
for v in s.iter_mut().take(order) {
|
||||
*v = r.signed(bps)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The coded residual (RFC 9639 section 9.2.7), into `s[order..]`.
|
||||
fn residual(r: &mut Bits, s: &mut [i64], order: usize) -> Result<()> {
|
||||
let method = r.bits(2)?;
|
||||
let (param_bits, escape) = match method {
|
||||
0 => (4, 15),
|
||||
1 => (5, 31),
|
||||
_ => return Err(DecodeError::Invalid("reserved residual coding method")),
|
||||
};
|
||||
let part_order = r.bits(4)? as u32;
|
||||
let n = s.len();
|
||||
let parts = 1usize << part_order;
|
||||
if !n.is_multiple_of(parts) {
|
||||
return Err(DecodeError::Invalid("partition order does not divide the block size"));
|
||||
}
|
||||
let per = n >> part_order;
|
||||
if per < order {
|
||||
return Err(DecodeError::Invalid("partition shorter than the predictor order"));
|
||||
}
|
||||
let mut i = order;
|
||||
for p in 0..parts {
|
||||
let count = if p == 0 { per - order } else { per };
|
||||
let k = r.bits(param_bits)? as u32;
|
||||
if k == escape {
|
||||
let width = r.bits(5)? as u32;
|
||||
for v in s[i..i + count].iter_mut() {
|
||||
*v = r.signed(width)?;
|
||||
}
|
||||
} else {
|
||||
for v in s[i..i + count].iter_mut() {
|
||||
let q = r.unary()?;
|
||||
if q > (1 << 32) {
|
||||
return Err(DecodeError::Invalid("Rice quotient too large"));
|
||||
}
|
||||
let low = r.bits(k)?;
|
||||
let folded = q << k | low;
|
||||
*v = if folded & 1 == 0 { (folded >> 1) as i64 } else { -((folded >> 1) as i64) - 1 };
|
||||
}
|
||||
}
|
||||
i += count;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---- files ----
|
||||
|
||||
/// A decoded file.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Decoded {
|
||||
/// The STREAMINFO block.
|
||||
pub info: StreamInfo,
|
||||
/// One `Vec` of samples per channel.
|
||||
pub channels: Vec<Vec<i32>>,
|
||||
}
|
||||
|
||||
/// Most sample values (samples x channels) [`decode`] will produce.
|
||||
pub const DEFAULT_MAX_VALUES: u64 = 1 << 28;
|
||||
|
||||
/// Decode a whole `.flac` file (an ID3v2 tag in front is skipped).
|
||||
pub fn decode(bytes: &[u8]) -> Result<Decoded> {
|
||||
decode_with_limit(bytes, DEFAULT_MAX_VALUES)
|
||||
}
|
||||
|
||||
/// [`decode`] with a limit on the sample values (samples x channels) produced.
|
||||
pub fn decode_with_limit(bytes: &[u8], max_values: u64) -> Result<Decoded> {
|
||||
let mut pos = 0usize;
|
||||
if bytes.starts_with(b"ID3") {
|
||||
let size = bytes.get(6..10).ok_or(DecodeError::Truncated)?;
|
||||
if size.iter().any(|&b| b & 0x80 != 0) {
|
||||
return Err(DecodeError::Invalid("bad ID3v2 size"));
|
||||
}
|
||||
pos = 10 + size.iter().fold(0usize, |a, &b| a << 7 | usize::from(b));
|
||||
}
|
||||
if bytes.get(pos..pos + 4) != Some(b"fLaC") {
|
||||
return Err(DecodeError::Invalid("missing fLaC marker"));
|
||||
}
|
||||
pos += 4;
|
||||
let mut info: Option<StreamInfo> = None;
|
||||
loop {
|
||||
let head = bytes.get(pos..pos + 4).ok_or(DecodeError::Truncated)?;
|
||||
let last = head[0] & 0x80 != 0;
|
||||
let kind = head[0] & 0x7F;
|
||||
let len = usize::from(head[1]) << 16 | usize::from(head[2]) << 8 | usize::from(head[3]);
|
||||
let body = bytes.get(pos + 4..pos + 4 + len).ok_or(DecodeError::Truncated)?;
|
||||
match (kind, &info) {
|
||||
(0, None) => info = Some(StreamInfo::parse(body)?),
|
||||
(0, Some(_)) => return Err(DecodeError::Invalid("a second STREAMINFO block")),
|
||||
(_, None) => return Err(DecodeError::Invalid("STREAMINFO is not the first block")),
|
||||
(127, _) => return Err(DecodeError::Invalid("forbidden metadata block type")),
|
||||
_ => {}
|
||||
}
|
||||
pos += 4 + len;
|
||||
if last {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let info = info.ok_or(DecodeError::Invalid("no STREAMINFO"))?;
|
||||
let dec = Decoder::new(info);
|
||||
let mut channels: Vec<Vec<i32>> = vec![Vec::new(); usize::from(info.channels)];
|
||||
let mut total = 0u64;
|
||||
while pos + 2 <= bytes.len() && bytes[pos] == 0xFF && bytes[pos + 1] & 0xFE == 0xF8 {
|
||||
let frame = dec.decode_frame(&bytes[pos..])?;
|
||||
if frame.channels.len() != channels.len() {
|
||||
return Err(DecodeError::Invalid("the channel count changes inside the stream"));
|
||||
}
|
||||
// One file has one sample rate and depth. Otherwise callers would play
|
||||
// samples under incompatible STREAMINFO metadata.
|
||||
if frame.sample_rate != info.sample_rate || frame.bits_per_sample != info.bits_per_sample {
|
||||
return Err(DecodeError::Invalid("frame format differs from STREAMINFO"));
|
||||
}
|
||||
total += frame.block_size as u64;
|
||||
if total.saturating_mul(channels.len() as u64) > max_values {
|
||||
return Err(DecodeError::TooLarge);
|
||||
}
|
||||
for (dst, src) in channels.iter_mut().zip(frame.channels) {
|
||||
dst.extend_from_slice(&src);
|
||||
}
|
||||
pos += frame.size;
|
||||
}
|
||||
// Unknown total_samples must not turn a corrupt/truncated next frame into
|
||||
// a successfully decoded shorter recording.
|
||||
if pos != bytes.len() {
|
||||
return Err(DecodeError::Invalid("trailing data or missing frame sync"));
|
||||
}
|
||||
if info.total_samples != 0 && total != info.total_samples {
|
||||
return Err(DecodeError::Truncated);
|
||||
}
|
||||
Ok(Decoded { info, channels })
|
||||
}
|
||||
|
||||
// Validate the advertised precision, not just the storage type. Malformed
|
||||
// predictors may produce an i32 that is still invalid for 4–24-bit audio.
|
||||
fn checked_sample(sample: i64, bits: u32) -> Result<i32> {
|
||||
if !(4..=32).contains(&bits) {
|
||||
return Err(DecodeError::Invalid("invalid sample bit depth"));
|
||||
}
|
||||
let bound = 1i64 << (bits - 1);
|
||||
if !(-bound..bound).contains(&sample) {
|
||||
return Err(DecodeError::Invalid("sample outside the bit depth"));
|
||||
}
|
||||
i32::try_from(sample).map_err(|_| DecodeError::Invalid("sample outside the bit depth"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sample_bounds_tests {
|
||||
use super::checked_sample;
|
||||
|
||||
#[test]
|
||||
fn samples_must_fit_declared_precision() {
|
||||
for bits in 4..=32 {
|
||||
let bound = 1i64 << (bits - 1);
|
||||
assert_eq!(checked_sample(-bound, bits).map(i64::from), Ok(-bound));
|
||||
assert_eq!(checked_sample(bound - 1, bits).map(i64::from), Ok(bound - 1));
|
||||
assert!(checked_sample(-bound - 1, bits).is_err());
|
||||
assert!(checked_sample(bound, bits).is_err());
|
||||
}
|
||||
assert!(checked_sample(0, 0).is_err());
|
||||
assert!(checked_sample(0, 255).is_err());
|
||||
}
|
||||
}
|
||||
@@ -283,6 +283,40 @@ pub(crate) fn flac_assumed_mask(channels: usize) -> u32 {
|
||||
}
|
||||
}
|
||||
|
||||
/// The surround speaker a layout may name in place of `speaker`: formats with one surround pair
|
||||
/// do not tell side from back, so a pair of either kind feeds it.
|
||||
fn surround_twin(speaker: Speaker) -> Option<Speaker> {
|
||||
Some(match speaker {
|
||||
Speaker::SideLeft => Speaker::BackLeft,
|
||||
Speaker::SideRight => Speaker::BackRight,
|
||||
Speaker::BackLeft => Speaker::SideLeft,
|
||||
Speaker::BackRight => Speaker::SideRight,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// How audio with `layout` feeds a format whose layout for the channel count is `mask`: for each
|
||||
/// channel the format takes, the channel of the audio that feeds it. `order` is the format's
|
||||
/// channel order over `mask` (for each of its channels, the WAV-order channel of `mask`; `None`
|
||||
/// when it takes WAV order). `None` when `layout` is not the format's layout: it is unordered, or
|
||||
/// has a speaker the format has not.
|
||||
pub(crate) fn channel_map(mask: u32, order: Option<&[usize]>, layout: &ChannelLayout) -> Option<Vec<usize>> {
|
||||
let speakers = ChannelLayout::from_mask(mask).speakers();
|
||||
if speakers.len() != usize::from(layout.channels()) {
|
||||
return None;
|
||||
}
|
||||
let mut map = Vec::with_capacity(speakers.len());
|
||||
for j in 0..speakers.len() {
|
||||
let speaker = *speakers.get(order.and_then(|o| o.get(j).copied()).unwrap_or(j))?;
|
||||
let at = layout.index_of(speaker).or_else(|| surround_twin(speaker).and_then(|t| layout.index_of(t)))?;
|
||||
if map.contains(&at) {
|
||||
return None;
|
||||
}
|
||||
map.push(at);
|
||||
}
|
||||
Some(map)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
+301
-19
@@ -13,20 +13,24 @@
|
||||
//! 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.
|
||||
//! - [`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
|
||||
//! `f32` in WAV channel order.
|
||||
//! - [`encode`], [`FileEncoder`]: WAV/BWF/RF64 and AIFF/AIFF-C (feature `pcm-encode`, which also
|
||||
//! gives [`wav_float_header`]) and FLAC (feature `flac-encode`), whole-buffer or incremental into
|
||||
//! any `Write + Seek`. Feature `lossless-encode` turns on both; feature `encode` turns on
|
||||
//! `lossless-encode` and every codec's encoder.
|
||||
//! gives [`wav_float_header`]), FLAC (feature `flac-encode`), AAC-LC and ALAC in M4A
|
||||
//! (`aac-encode`, `alac-encode`) and MP3, AC-3 and E-AC-3 streams (`mp3-encode`, `ac3-encode`,
|
||||
//! `eac3-encode`), whole-buffer or incremental into any `Write + Seek`. Feature
|
||||
//! `lossless-encode` turns on WAV, AIFF and FLAC; feature `encode` turns on every format and encoder.
|
||||
//! - [`wav_pcm`], [`aiff_pcm`] (feature `decode-native`) and [`pcm_wav_file`], [`pcm_wav_header`],
|
||||
//! [`pcm_aiff_file`], [`pcm_aiff_header`] (feature `pcm-encode`): the integer PCM of a WAV or AIFF
|
||||
//! file bit for bit, and minimal files around such PCM, for essence embedded in other containers.
|
||||
//! - [`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`), [`ac3`]
|
||||
//! (`ac3-decode`), [`mpa`] (`mpa-decode`), [`mp3`] (`mp3-decode`, `mp3-encode`),
|
||||
//! [`flac`] (`flac-encode`: the packet encoder).
|
||||
//! `aac-encode`), [`alac`] (`alac-decode`, `alac-encode`), [`opus`] (`opus-decode`, and
|
||||
//! [`opus::encoder`] with `opus-encode`), [`ac3`] (`ac3-decode`), [`mpa`] (`mpa-decode`), [`mp3`]
|
||||
//! (`mp3-decode`, `mp3-encode`), [`flac`] (`flac-decode`: the frame and file decoder;
|
||||
//! `flac-encode`: the packet encoder), [`qoa`] (`qoa-decode`, `qoa-encode`: whole QOA files).
|
||||
//!
|
||||
//! The shared types below build with no features.
|
||||
|
||||
@@ -42,21 +46,48 @@ mod layout;
|
||||
pub mod mp3;
|
||||
#[cfg(feature = "mpa-decode")]
|
||||
pub mod mpa;
|
||||
#[cfg(feature = "opus-decode")]
|
||||
#[cfg(any(feature = "opus-decode", feature = "opus-encode"))]
|
||||
pub mod opus;
|
||||
#[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))]
|
||||
mod pcm;
|
||||
#[cfg(feature = "peaks")]
|
||||
pub mod peaks;
|
||||
#[cfg(any(feature = "qoa-decode", feature = "qoa-encode"))]
|
||||
pub mod qoa;
|
||||
#[cfg(feature = "decode-native")]
|
||||
mod read;
|
||||
#[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))]
|
||||
mod track;
|
||||
#[cfg(feature = "vorbis-encode")]
|
||||
pub mod vorbis;
|
||||
#[cfg(any(feature = "decode-native", feature = "pcm-encode"))]
|
||||
mod wav;
|
||||
#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))]
|
||||
/// Bounded WAV/RF64 cue, adtl and XMP marker metadata, without audio decoding.
|
||||
pub mod wav_markers;
|
||||
#[cfg(any(
|
||||
feature = "pcm-encode",
|
||||
feature = "flac-encode",
|
||||
feature = "aac-encode",
|
||||
feature = "alac-encode",
|
||||
feature = "mp3-encode",
|
||||
feature = "ac3-encode",
|
||||
feature = "eac3-encode",
|
||||
feature = "vorbis-encode"
|
||||
))]
|
||||
mod write;
|
||||
|
||||
/// PCM WAVE and AIFF wrappers for interchange essence.
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
pub mod essence_pcm;
|
||||
/// Streaming WAVE and AIFF headers and sample packing.
|
||||
pub mod pcm_export;
|
||||
/// Interleaved PCM packet decoding.
|
||||
pub mod pcm_interleaved;
|
||||
/// Range-backed PCM WAVE access and a 16-bit PCM writer.
|
||||
pub mod wav_range;
|
||||
#[cfg(feature = "vorbis-encode")]
|
||||
pub use write::{OGG_BITRATES, OggVorbisOptions, encode_ogg, encode_ogg_vorbis, quality_for_bitrate};
|
||||
|
||||
/// The FLAC packet encoder (RFC 9639), for a caller that muxes FLAC itself (into MP4, Matroska or
|
||||
/// its own `.flac` writer): planar integer samples in, frames out, and the STREAMINFO to patch in
|
||||
/// at the end. [`FileEncoder`] and [`encode`] write whole `.flac` files from `f32` instead.
|
||||
@@ -76,10 +107,15 @@ mod write;
|
||||
/// assert!(file.len() < 48_000 * 2 * 2 / 2); // well under half of 16-bit PCM
|
||||
/// # Ok::<(), craft_audio::flac::Error>(())
|
||||
/// ```
|
||||
#[cfg(feature = "flac-encode")]
|
||||
#[cfg(any(feature = "flac-encode", feature = "flac-decode"))]
|
||||
pub mod flac {
|
||||
#[cfg(feature = "flac-decode")]
|
||||
pub use crate::flac_decode::{DEFAULT_MAX_VALUES, DecodeError, Decoded, Decoder, Frame, StreamInfo, decode, decode_with_limit};
|
||||
#[cfg(feature = "flac-encode")]
|
||||
pub use crate::write::flac::encoder::{Encoder, EncoderConfig, Error, STREAMINFO_OFFSET};
|
||||
}
|
||||
#[cfg(feature = "flac-decode")]
|
||||
mod flac_decode;
|
||||
|
||||
pub use buffer::AudioBuffer;
|
||||
/// The craft-media this crate is built with, for its containers: using it rather than a separate
|
||||
@@ -98,10 +134,29 @@ pub use read::{
|
||||
pub use read::{RawPcm, aiff_pcm, wav_pcm};
|
||||
#[cfg(any(feature = "aac-decode", feature = "alac-decode", feature = "opus-decode", feature = "ac3-decode", feature = "mpa-decode"))]
|
||||
pub use track::{LOOKAHEAD_PACKETS, TrackDecoder};
|
||||
#[cfg(any(
|
||||
feature = "pcm-encode",
|
||||
feature = "flac-encode",
|
||||
feature = "aac-encode",
|
||||
feature = "alac-encode",
|
||||
feature = "mp3-encode",
|
||||
feature = "ac3-encode",
|
||||
feature = "eac3-encode"
|
||||
))]
|
||||
pub use write::FileEncoder;
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
pub use write::wav_float_header;
|
||||
#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))]
|
||||
pub use write::{ENCODE_EXTENSIONS, FileEncoder, encode, encode_format_for};
|
||||
#[cfg(any(
|
||||
feature = "pcm-encode",
|
||||
feature = "flac-encode",
|
||||
feature = "aac-encode",
|
||||
feature = "alac-encode",
|
||||
feature = "mp3-encode",
|
||||
feature = "ac3-encode",
|
||||
feature = "eac3-encode",
|
||||
feature = "vorbis-encode"
|
||||
))]
|
||||
pub use write::{ENCODE_EXTENSIONS, encode, encode_format_for};
|
||||
#[cfg(feature = "pcm-encode")]
|
||||
pub use write::{pcm_aiff_file, pcm_aiff_header, pcm_wav_file, pcm_wav_header};
|
||||
|
||||
@@ -207,8 +262,16 @@ impl From<alac::Error> for AudioError {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "opus-encode")]
|
||||
impl From<opus::encoder::Error> for AudioError {
|
||||
fn from(e: opus::encoder::Error) -> Self {
|
||||
AudioError::Encode(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// The file formats [`encode`] writes. Each needs its feature: WAV and AIFF `pcm-encode`, FLAC
|
||||
/// `flac-encode`; asking for one that is not built returns [`AudioError::Unsupported`].
|
||||
/// `flac-encode`, AAC `aac-encode`, ALAC `alac-encode`, MP3 `mp3-encode`, AC-3 `ac3-encode` and
|
||||
/// E-AC-3 `eac3-encode`; asking for one that is not built returns [`AudioError::Unsupported`].
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
@@ -219,20 +282,52 @@ pub enum EncodeFormat {
|
||||
Aiff,
|
||||
/// FLAC (integer depths).
|
||||
Flac,
|
||||
/// AAC-LC in an M4A (MP4) file, 1–8 channels at one of the 13 AAC sample rates. The edit list
|
||||
/// skips the encoder's priming and ends at the last frame, so the file decodes to the length
|
||||
/// that was written.
|
||||
Aac,
|
||||
/// Apple Lossless in an M4A (MP4) file (integer depths), 1–8 channels.
|
||||
Alac,
|
||||
/// An MP3 stream: mono or stereo at 32, 44.1 or 48 kHz, or 16, 22.05 or 24 kHz.
|
||||
Mp3,
|
||||
/// An AC-3 stream: up to 5.1 at 32, 44.1 or 48 kHz.
|
||||
Ac3,
|
||||
/// An E-AC-3 stream: up to 7.1 at 32, 44.1 or 48 kHz.
|
||||
Eac3,
|
||||
/// Ogg Vorbis, 1–8 channels at any sample rate, whole-buffer encoding only. Variable bit
|
||||
/// rate: a [`RateControl::Bitrate`] picks the quality whose nominal rate it is
|
||||
/// ([`quality_for_bitrate`]), [`RateControl::Quality`] sets it directly.
|
||||
Ogg,
|
||||
}
|
||||
|
||||
impl EncodeFormat {
|
||||
/// Every format, in the order menus list them. Whether [`encode`] writes one depends on its
|
||||
/// feature.
|
||||
pub const ALL: &[EncodeFormat] = &[EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac];
|
||||
/// feature ([`EncodeFormat::is_built`]).
|
||||
pub const ALL: &[EncodeFormat] = &[
|
||||
EncodeFormat::Wav,
|
||||
EncodeFormat::Aiff,
|
||||
EncodeFormat::Flac,
|
||||
EncodeFormat::Alac,
|
||||
EncodeFormat::Aac,
|
||||
EncodeFormat::Mp3,
|
||||
EncodeFormat::Ac3,
|
||||
EncodeFormat::Eac3,
|
||||
EncodeFormat::Ogg,
|
||||
];
|
||||
|
||||
/// The extension a new file of this format gets, lower case and without the dot: `"wav"`,
|
||||
/// `"aif"`, `"flac"`. [`encode_format_for`] maps it back.
|
||||
/// `"aif"`, `"flac"`, `"m4a"` (AAC and ALAC), `"mp3"`, `"ac3"`, `"eac3"`.
|
||||
/// [`encode_format_for`] maps it back, `"m4a"` to AAC.
|
||||
pub fn extension(self) -> &'static str {
|
||||
match self {
|
||||
EncodeFormat::Wav => "wav",
|
||||
EncodeFormat::Aiff => "aif",
|
||||
EncodeFormat::Flac => "flac",
|
||||
EncodeFormat::Aac | EncodeFormat::Alac => "m4a",
|
||||
EncodeFormat::Mp3 => "mp3",
|
||||
EncodeFormat::Ac3 => "ac3",
|
||||
EncodeFormat::Eac3 => "eac3",
|
||||
EncodeFormat::Ogg => "ogg",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,6 +337,11 @@ impl EncodeFormat {
|
||||
EncodeFormat::Wav => "audio/wav",
|
||||
EncodeFormat::Aiff => "audio/aiff",
|
||||
EncodeFormat::Flac => "audio/flac",
|
||||
EncodeFormat::Aac | EncodeFormat::Alac => "audio/mp4",
|
||||
EncodeFormat::Mp3 => "audio/mpeg",
|
||||
EncodeFormat::Ac3 => "audio/ac3",
|
||||
EncodeFormat::Eac3 => "audio/eac3",
|
||||
EncodeFormat::Ogg => "audio/ogg",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,8 +351,163 @@ impl EncodeFormat {
|
||||
EncodeFormat::Wav => Container::Wav,
|
||||
EncodeFormat::Aiff => Container::Aiff,
|
||||
EncodeFormat::Flac => Container::Flac,
|
||||
EncodeFormat::Aac | EncodeFormat::Alac => Container::Mp4,
|
||||
EncodeFormat::Mp3 => Container::MpegAudio,
|
||||
EncodeFormat::Ac3 | EncodeFormat::Eac3 => Container::Ac3,
|
||||
EncodeFormat::Ogg => Container::Ogg,
|
||||
}
|
||||
}
|
||||
|
||||
/// The codec of the audio this format writes.
|
||||
pub fn codec(self) -> CodecId {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff => CodecId::Pcm,
|
||||
EncodeFormat::Flac => CodecId::Flac,
|
||||
EncodeFormat::Aac => CodecId::Aac,
|
||||
EncodeFormat::Alac => CodecId::Alac,
|
||||
EncodeFormat::Mp3 => CodecId::Mp3,
|
||||
EncodeFormat::Ac3 => CodecId::Ac3,
|
||||
EncodeFormat::Eac3 => CodecId::Eac3,
|
||||
EncodeFormat::Ogg => CodecId::Vorbis,
|
||||
}
|
||||
}
|
||||
|
||||
/// The feature that builds this format's writer.
|
||||
pub fn feature(self) -> &'static str {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff => "pcm-encode",
|
||||
EncodeFormat::Flac => "flac-encode",
|
||||
EncodeFormat::Aac => "aac-encode",
|
||||
EncodeFormat::Alac => "alac-encode",
|
||||
EncodeFormat::Mp3 => "mp3-encode",
|
||||
EncodeFormat::Ac3 => "ac3-encode",
|
||||
EncodeFormat::Eac3 => "eac3-encode",
|
||||
EncodeFormat::Ogg => "vorbis-encode",
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether [`encode`] writes this format in this build (its
|
||||
/// [`feature`](Self::feature) is on): what to filter [`EncodeFormat::ALL`] by for a menu.
|
||||
pub fn is_built(self) -> bool {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff => cfg!(feature = "pcm-encode"),
|
||||
EncodeFormat::Flac => cfg!(feature = "flac-encode"),
|
||||
EncodeFormat::Aac => cfg!(feature = "aac-encode"),
|
||||
EncodeFormat::Alac => cfg!(feature = "alac-encode"),
|
||||
EncodeFormat::Mp3 => cfg!(feature = "mp3-encode"),
|
||||
EncodeFormat::Ac3 => cfg!(feature = "ac3-encode"),
|
||||
EncodeFormat::Eac3 => cfg!(feature = "eac3-encode"),
|
||||
EncodeFormat::Ogg => cfg!(feature = "vorbis-encode"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The sample rates the format takes, ascending, or `None` when it has no fixed list (WAV,
|
||||
/// AIFF, FLAC and ALAC take any rate their headers can hold).
|
||||
pub fn sample_rates(self) -> Option<&'static [u32]> {
|
||||
match self {
|
||||
EncodeFormat::Aac => Some(&[7350, 8000, 11_025, 12_000, 16_000, 22_050, 24_000, 32_000, 44_100, 48_000, 64_000, 88_200, 96_000]),
|
||||
EncodeFormat::Mp3 => Some(&[16_000, 22_050, 24_000, 32_000, 44_100, 48_000]),
|
||||
EncodeFormat::Ac3 | EncodeFormat::Eac3 => Some(&[32_000, 44_100, 48_000]),
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac | EncodeFormat::Alac | EncodeFormat::Ogg => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The most channels a file of this format holds. A WAV's frame size is a 16-bit field, so it
|
||||
/// holds 65535 / bytes per sample: 32767 at 16 bits (the count given here), 21845 at 24 and
|
||||
/// 16383 at 32; [`EncodeOptions::check`] answers for the depth chosen.
|
||||
pub fn max_channels(self) -> u16 {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff => 32_767,
|
||||
EncodeFormat::Flac | EncodeFormat::Aac | EncodeFormat::Alac | EncodeFormat::Eac3 | EncodeFormat::Ogg => 8,
|
||||
EncodeFormat::Ac3 => 6,
|
||||
EncodeFormat::Mp3 => 2,
|
||||
}
|
||||
}
|
||||
|
||||
/// The speaker layouts of a format that has a fixed set of them, as WAV masks, for a channel
|
||||
/// count: one each for AAC, ALAC and MP3, one per coding mode for AC-3 and E-AC-3. Empty for a
|
||||
/// count the format has not, and for WAV, AIFF and FLAC, which have no such set.
|
||||
pub(crate) fn layout_masks(self, channels: u16) -> &'static [u32] {
|
||||
let per_count: &[&[u32]] = match self {
|
||||
// Channel configurations 1–6, the 6.1 program config element, and configuration 7
|
||||
// (7.1 with a front pair left and right of centre).
|
||||
EncodeFormat::Aac => &[&[0x4], &[0x3], &[0x7], &[0x107], &[0x607], &[0x60F], &[0x70F], &[0xFF]],
|
||||
// Apple's layout tags for 1–8 channels, surrounds as back speakers.
|
||||
EncodeFormat::Alac => &[&[0x4], &[0x3], &[0x7], &[0x107], &[0x37], &[0x3F], &[0x13F], &[0xFF]],
|
||||
EncodeFormat::Mp3 => &[&[0x4], &[0x3]],
|
||||
// The Vorbis I §4.3.9 layouts for 1–8 channels (6.1 with a back centre and sides, 7.1
|
||||
// with sides and backs), written in Vorbis order.
|
||||
EncodeFormat::Ogg => &[&[0x4], &[0x3], &[0x7], &[0x33], &[0x37], &[0x3F], &[0x70F], &[0x63F]],
|
||||
// The coding modes 1/0 to 3/2 with and without an LFE, by channel count.
|
||||
EncodeFormat::Ac3 => &[&[0x4], &[0x3, 0xC], &[0x7, 0x103, 0xB], &[0x107, 0x603, 0xF, 0x10B], &[0x607, 0x10F, 0x60B], &[0x60F]],
|
||||
// The same, and 7.1 with backs and sides.
|
||||
EncodeFormat::Eac3 => {
|
||||
&[&[0x4], &[0x3, 0xC], &[0x7, 0x103, 0xB], &[0x107, 0x603, 0xF, 0x10B], &[0x607, 0x10F, 0x60B], &[0x60F], &[], &[0x63F]]
|
||||
}
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac => &[],
|
||||
};
|
||||
usize::from(channels).checked_sub(1).and_then(|i| per_count.get(i)).copied().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The speaker layouts a format with a fixed set of them has for `channels` channels: one for
|
||||
/// AAC, ALAC and MP3, one per coding mode for AC-3 and E-AC-3; empty when the format cannot
|
||||
/// hold that many channels. `None` for WAV, AIFF and FLAC, which take any layout. Audio in
|
||||
/// another layout has to be rearranged into one of these before it is written.
|
||||
///
|
||||
/// ```
|
||||
/// use craft_audio::{ChannelLayout, EncodeFormat};
|
||||
///
|
||||
/// assert_eq!(EncodeFormat::Aac.fixed_layouts(6), Some(vec![ChannelLayout::SURROUND_5_1_SIDE]));
|
||||
/// assert_eq!(EncodeFormat::Mp3.fixed_layouts(6), Some(vec![]));
|
||||
/// assert_eq!(EncodeFormat::Wav.fixed_layouts(6), None);
|
||||
/// ```
|
||||
pub fn fixed_layouts(self, channels: u16) -> Option<Vec<ChannelLayout>> {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac => None,
|
||||
_ => Some(self.layout_masks(channels).iter().map(|&mask| ChannelLayout::from_mask(mask)).collect()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the format holds audio with `layout`: for WAV, AIFF and FLAC any layout of up to
|
||||
/// [`max_channels`](Self::max_channels) channels, for the others one of
|
||||
/// [`fixed_layouts`](Self::fixed_layouts), where side and back surrounds stand for each other
|
||||
/// in a layout with one surround pair.
|
||||
pub fn supports_layout(self, layout: &ChannelLayout) -> bool {
|
||||
match self {
|
||||
EncodeFormat::Wav | EncodeFormat::Aiff | EncodeFormat::Flac => (1..=self.max_channels()).contains(&layout.channels()),
|
||||
_ => self.layout_masks(layout.channels()).iter().any(|&mask| layout::channel_map(mask, None, layout).is_some()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the format takes [`RateControl::Quality`]: AAC and Ogg Vorbis. The other lossy
|
||||
/// formats are written at a constant bit rate only.
|
||||
pub fn supports_quality(self) -> bool {
|
||||
matches!(self, EncodeFormat::Aac | EncodeFormat::Ogg)
|
||||
}
|
||||
|
||||
/// Whether the format loses audio: AAC, MP3, AC-3 and E-AC-3. These take
|
||||
/// [`EncodeOptions::rate_control`] and ignore the bit depth and dither.
|
||||
pub fn is_lossy(self) -> bool {
|
||||
matches!(self, EncodeFormat::Aac | EncodeFormat::Mp3 | EncodeFormat::Ac3 | EncodeFormat::Eac3 | EncodeFormat::Ogg)
|
||||
}
|
||||
}
|
||||
|
||||
/// How a lossy format spends bits ([`EncodeOptions::rate_control`]).
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum RateControl {
|
||||
/// A constant bit rate in bits per second, for all channels together. A format with a fixed
|
||||
/// set of rates (MP3, AC-3) uses the highest one not above it; a rate outside what the
|
||||
/// stream can carry is moved to the nearest one inside. AAC is written at 6 kbit/s or a
|
||||
/// quarter of the sample rate per channel at least: from about half of that down its encoder
|
||||
/// codes silence. Vorbis takes 64–500 kbit/s and writes the quality whose nominal rate that is
|
||||
/// ([`quality_for_bitrate`]); its rate varies with the audio.
|
||||
Bitrate(u32),
|
||||
/// A constant quality from 0.0 (smallest) to 1.0 (best), at whatever bit rate each frame
|
||||
/// needs. AAC and Vorbis (0.0 is Vorbis quality -1, 1.0 quality 10); values outside the range
|
||||
/// are clamped, and NaN is an error.
|
||||
Quality(f32),
|
||||
}
|
||||
|
||||
/// Stored sample representation.
|
||||
@@ -411,9 +666,9 @@ pub enum BitDepth {
|
||||
pub struct EncodeOptions {
|
||||
/// Output file format.
|
||||
pub format: EncodeFormat,
|
||||
/// Sample depth to write.
|
||||
/// Sample depth to write (ignored by the lossy formats).
|
||||
pub bit_depth: BitDepth,
|
||||
/// Add TPDF dither when quantizing to an integer depth.
|
||||
/// Add TPDF dither when quantizing to an integer depth (ignored by the lossy formats).
|
||||
pub dither: bool,
|
||||
/// Write a BWF `bext` chunk (WAV only; ignored for other formats).
|
||||
pub bwf: Option<BwfInfo>,
|
||||
@@ -424,15 +679,27 @@ pub struct EncodeOptions {
|
||||
/// the count (they read 5 and 6 channels with side surrounds and 7 as 6.1 with sides, so the
|
||||
/// usual layouts for those counts get one; see the README's limits); AIFF has a fixed channel
|
||||
/// order and ignores it. It must have as many channels as the audio.
|
||||
///
|
||||
/// AAC, ALAC, MP3, AC-3 and E-AC-3 store channels in their own order, so here the layout says
|
||||
/// which speaker each channel of the audio (in WAV order) is, and the channels are reordered
|
||||
/// to match. Each of these formats has a fixed set of layouts (AAC and ALAC one per channel
|
||||
/// count, AC-3 one per coding mode); a layout outside it is [`AudioError::Unsupported`]. Side
|
||||
/// and back surrounds are taken as the same pair where the format has only one.
|
||||
pub layout: Option<ChannelLayout>,
|
||||
/// Write WAV in the RF64 layout even below 4 GiB (WAV only; ignored for other formats).
|
||||
pub force_rf64: bool,
|
||||
/// The bit rate or quality of a lossy format (ignored by the others). `None` is a constant
|
||||
/// 64 kbit/s per channel for AAC, 96 kbit/s per channel for MP3, and 96 kbit/s per
|
||||
/// full-bandwidth channel (the LFE not counted) for AC-3 and E-AC-3, moved to the nearest rate
|
||||
/// the stream can carry.
|
||||
pub rate_control: Option<RateControl>,
|
||||
}
|
||||
|
||||
impl EncodeOptions {
|
||||
/// Options for `format`: 24-bit, no dither, no BWF chunk, the usual layout.
|
||||
/// Options for `format`: 24-bit, no dither, no BWF chunk, the usual layout, the format's
|
||||
/// default bit rate.
|
||||
pub fn new(format: EncodeFormat) -> Self {
|
||||
Self { format, bit_depth: BitDepth::Int24, dither: false, bwf: None, layout: None, force_rf64: false }
|
||||
Self { format, bit_depth: BitDepth::Int24, dither: false, bwf: None, layout: None, force_rf64: false, rate_control: None }
|
||||
}
|
||||
|
||||
/// Sets `format`.
|
||||
@@ -470,6 +737,18 @@ impl EncodeOptions {
|
||||
self.force_rf64 = force_rf64;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets `rate_control` to a constant bit rate in bits per second ([`RateControl::Bitrate`]).
|
||||
pub fn with_bitrate(mut self, bits_per_second: u32) -> Self {
|
||||
self.rate_control = Some(RateControl::Bitrate(bits_per_second));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets `rate_control` to a constant quality from 0.0 to 1.0 ([`RateControl::Quality`]).
|
||||
pub fn with_quality(mut self, quality: f32) -> Self {
|
||||
self.rate_control = Some(RateControl::Quality(quality));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for EncodeOptions {
|
||||
@@ -493,3 +772,6 @@ mod tests {
|
||||
assert!(matches!(e, AudioError::Encode(_)), "{e}");
|
||||
}
|
||||
}
|
||||
|
||||
/// DVD and Blu-ray LPCM packet decoding.
|
||||
pub mod lpcm;
|
||||
@@ -0,0 +1,118 @@
|
||||
//! DVD-Video and Blu-ray LPCM packet grammar and sample decoding.
|
||||
|
||||
/// Blu-ray / AVCHD LPCM packet header (4 bytes before the samples).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct BlurayLpcm {
|
||||
/// Sample rate in hertz.
|
||||
pub sample_rate: u32,
|
||||
/// Number of decoded channels.
|
||||
pub channels: usize,
|
||||
/// Significant bits per sample.
|
||||
pub bits: u32,
|
||||
}
|
||||
|
||||
/// Channels of each channel_assignment code (mono, stereo, 3/0, 2/1, 3/1, 2/2, 3/2, 3/2+LFE,
|
||||
/// 3/4, 3/4+LFE).
|
||||
const BD_LPCM_CHANNELS: [usize; 12] = [0, 1, 0, 2, 3, 3, 4, 4, 5, 6, 7, 8];
|
||||
|
||||
/// Parse and validate the four-byte Blu-ray LPCM header.
|
||||
pub fn bluray_lpcm_header(p: &[u8]) -> Option<BlurayLpcm> {
|
||||
if p.len() < 4 {
|
||||
return None;
|
||||
}
|
||||
let channels = *BD_LPCM_CHANNELS.get((p[2] >> 4) as usize).filter(|&&c| c > 0)?;
|
||||
let sample_rate = match p[2] & 15 {
|
||||
1 => 48_000,
|
||||
4 => 96_000,
|
||||
5 => 192_000,
|
||||
_ => return None,
|
||||
};
|
||||
let bits = match p[3] >> 6 {
|
||||
1 => 16,
|
||||
2 => 20,
|
||||
3 => 24,
|
||||
_ => return None,
|
||||
};
|
||||
Some(BlurayLpcm { sample_rate, channels, bits })
|
||||
}
|
||||
|
||||
/// Decode one Blu-ray LPCM packet (big-endian; channels padded to an even count; 20-bit
|
||||
/// samples are stored in 24 bits).
|
||||
pub fn decode_bluray_lpcm(p: &[u8]) -> Option<Vec<Vec<f32>>> {
|
||||
let h = bluray_lpcm_header(p)?;
|
||||
let coded = (h.channels + 1) & !1;
|
||||
let bps = if h.bits == 16 { 2 } else { 3 };
|
||||
let data = &p[4..];
|
||||
let n = data.len() / (coded * bps);
|
||||
let mut out = vec![Vec::with_capacity(n); h.channels];
|
||||
for k in 0..n {
|
||||
for (c, o) in out.iter_mut().enumerate() {
|
||||
let at = (k * coded + c) * bps;
|
||||
let v = if bps == 2 {
|
||||
i16::from_be_bytes([data[at], data[at + 1]]) as f32 / 32768.0
|
||||
} else {
|
||||
(i32::from_be_bytes([data[at], data[at + 1], data[at + 2], 0]) >> 8) as f32 / 8_388_608.0
|
||||
};
|
||||
o.push(v);
|
||||
}
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Bytes per two sample frames of DVD LPCM. The caller must bound `channels` to the DVD channel range.
|
||||
pub fn dvd_lpcm_group_bytes(channels: usize, bits: u32) -> usize {
|
||||
match bits {
|
||||
16 => 4 * channels,
|
||||
20 => 5 * channels,
|
||||
_ => 6 * channels,
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode DVD-Video LPCM (big-endian). 16-bit samples are interleaved; 20- and 24-bit samples
|
||||
/// come in groups of two frames: the upper 16 bits of all the group's samples, then their low
|
||||
/// 4 / 8 bits. The caller must bound `channels` to the DVD channel range.
|
||||
pub fn decode_dvd_lpcm(data: &[u8], channels: usize, bits: u32) -> Vec<Vec<f32>> {
|
||||
let ch = channels.max(1);
|
||||
let group = dvd_lpcm_group_bytes(ch, bits);
|
||||
let groups = data.len() / group;
|
||||
let mut out = vec![Vec::with_capacity(groups * 2); ch];
|
||||
for g in 0..groups {
|
||||
let b = &data[g * group..(g + 1) * group];
|
||||
for f in 0..2 {
|
||||
for (c, o) in out.iter_mut().enumerate() {
|
||||
let k = f * ch + c;
|
||||
let hi = i16::from_be_bytes([b[2 * k], b[2 * k + 1]]) as i32;
|
||||
let v = match bits {
|
||||
16 => hi as f32 / 32768.0,
|
||||
20 => {
|
||||
let nib = b[4 * ch + k / 2];
|
||||
let lo = if k.is_multiple_of(2) { nib >> 4 } else { nib & 15 } as i32;
|
||||
((hi << 4) | lo) as f32 / 524_288.0
|
||||
}
|
||||
_ => ((hi << 8) | b[4 * ch + k] as i32) as f32 / 8_388_608.0,
|
||||
};
|
||||
o.push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn bluray_header_and_even_channel_padding() {
|
||||
let packet = [0, 0, 0x31, 0x40, 0x40, 0, 0xc0, 0];
|
||||
assert_eq!(bluray_lpcm_header(&packet), Some(BlurayLpcm { sample_rate: 48_000, channels: 2, bits: 16 }));
|
||||
assert_eq!(decode_bluray_lpcm(&packet), Some(vec![vec![0.5], vec![-0.5]]));
|
||||
assert_eq!(decode_bluray_lpcm(&packet[..3]), None);
|
||||
}
|
||||
#[test]
|
||||
fn dvd_lpcm_16_20_and_24_bit_groups() {
|
||||
assert_eq!(decode_dvd_lpcm(&[0x40, 0, 0xc0, 0], 1, 16), vec![vec![0.5, -0.5]]);
|
||||
assert_eq!(decode_dvd_lpcm(&[0x40, 0, 0xc0, 0, 0], 1, 20), vec![vec![0.5, -0.5]]);
|
||||
assert_eq!(decode_dvd_lpcm(&[0x40, 0, 0xc0, 0, 0, 0], 1, 24), vec![vec![0.5, -0.5]]);
|
||||
assert_eq!(decode_dvd_lpcm(&[1, 2, 3], 1, 16), vec![Vec::<f32>::new()]);
|
||||
}
|
||||
}
|
||||
@@ -4,141 +4,7 @@
|
||||
//! producing the N/2 "folded" time samples; the window overlap region is then unfolded against the
|
||||
//! folded tail of the previous block, which the caller keeps in place in the output buffer.
|
||||
|
||||
#[derive(Clone, Copy, Default, Debug)]
|
||||
pub struct Cpx {
|
||||
pub re: f32,
|
||||
pub im: f32,
|
||||
}
|
||||
|
||||
impl Cpx {
|
||||
#[inline]
|
||||
fn mul(self, o: Cpx) -> Cpx {
|
||||
Cpx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re }
|
||||
}
|
||||
#[inline]
|
||||
fn add(self, o: Cpx) -> Cpx {
|
||||
Cpx { re: self.re + o.re, im: self.im + o.im }
|
||||
}
|
||||
#[inline]
|
||||
fn sub(self, o: Cpx) -> Cpx {
|
||||
Cpx { re: self.re - o.re, im: self.im - o.im }
|
||||
}
|
||||
}
|
||||
|
||||
/// Mixed-radix (2, 3, 4, 5) forward complex FFT, unscaled. Radix 2, 3 and 4 use dedicated
|
||||
/// butterflies; radix 5 uses a generic one.
|
||||
pub struct Fft {
|
||||
n: usize,
|
||||
factors: Vec<usize>,
|
||||
twiddle: Vec<Cpx>,
|
||||
}
|
||||
|
||||
impl Fft {
|
||||
pub fn new(n: usize) -> Fft {
|
||||
let mut factors = Vec::new();
|
||||
let mut m = n;
|
||||
for p in [4usize, 2, 3, 5] {
|
||||
while m.is_multiple_of(p) {
|
||||
factors.push(p);
|
||||
m /= p;
|
||||
}
|
||||
}
|
||||
assert_eq!(m, 1, "unsupported FFT size {n}");
|
||||
let twiddle = (0..n)
|
||||
.map(|k| {
|
||||
let ph = -2.0 * std::f64::consts::PI * k as f64 / n as f64;
|
||||
Cpx { re: ph.cos() as f32, im: ph.sin() as f32 }
|
||||
})
|
||||
.collect();
|
||||
Fft { n, factors, twiddle }
|
||||
}
|
||||
|
||||
pub fn process(&self, data: &mut [Cpx], scratch: &mut Vec<Cpx>) {
|
||||
scratch.clear();
|
||||
scratch.extend_from_slice(&data[..self.n]);
|
||||
self.rec(&mut data[..self.n], scratch, 1, self.n, 0, 1);
|
||||
}
|
||||
|
||||
fn rec(&self, out: &mut [Cpx], inp: &[Cpx], in_stride: usize, n: usize, fi: usize, tw_stride: usize) {
|
||||
let p = self.factors[fi];
|
||||
let m = n / p;
|
||||
if m == 1 {
|
||||
for (i, o) in out.iter_mut().enumerate().take(p) {
|
||||
*o = inp[i * in_stride];
|
||||
}
|
||||
} else {
|
||||
for q in 0..p {
|
||||
self.rec(&mut out[q * m..(q + 1) * m], &inp[q * in_stride..], in_stride * p, m, fi + 1, tw_stride * p);
|
||||
}
|
||||
}
|
||||
let tw = &self.twiddle;
|
||||
match p {
|
||||
2 => {
|
||||
for k in 0..m {
|
||||
let a0 = out[k];
|
||||
let a1 = out[m + k].mul(tw[k * tw_stride]);
|
||||
out[k] = a0.add(a1);
|
||||
out[m + k] = a0.sub(a1);
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
for k in 0..m {
|
||||
let t0 = out[k];
|
||||
let t1 = out[m + k].mul(tw[k * tw_stride]);
|
||||
let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]);
|
||||
let t3 = out[3 * m + k].mul(tw[3 * k * tw_stride]);
|
||||
let s0 = t0.add(t2);
|
||||
let d0 = t0.sub(t2);
|
||||
let s1 = t1.add(t3);
|
||||
let d1 = t1.sub(t3);
|
||||
out[k] = s0.add(s1);
|
||||
out[2 * m + k] = s0.sub(s1);
|
||||
// -j * d1 = (d1.im, -d1.re)
|
||||
out[m + k] = Cpx { re: d0.re + d1.im, im: d0.im - d1.re };
|
||||
out[3 * m + k] = Cpx { re: d0.re - d1.im, im: d0.im + d1.re };
|
||||
}
|
||||
}
|
||||
3 => {
|
||||
// sin(-2*pi/3) = -sqrt(3)/2
|
||||
let h = tw[self.n / 3].im;
|
||||
for k in 0..m {
|
||||
let t0 = out[k];
|
||||
let t1 = out[m + k].mul(tw[k * tw_stride]);
|
||||
let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]);
|
||||
let s = t1.add(t2);
|
||||
let d = t1.sub(t2);
|
||||
let a = Cpx { re: t0.re - 0.5 * s.re, im: t0.im - 0.5 * s.im };
|
||||
out[k] = t0.add(s);
|
||||
// j * h * d = (-h * d.im, h * d.re)
|
||||
out[m + k] = Cpx { re: a.re - h * d.im, im: a.im + h * d.re };
|
||||
out[2 * m + k] = Cpx { re: a.re + h * d.im, im: a.im - h * d.re };
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Radix 5 (at most once per transform): generic butterfly with the five roots of
|
||||
// unity looked up once.
|
||||
let mut w5 = [Cpx::default(); 5];
|
||||
for (q, w) in w5.iter_mut().enumerate() {
|
||||
*w = tw[q * (self.n / p)];
|
||||
}
|
||||
let mut t = [Cpx::default(); 5];
|
||||
for k in 0..m {
|
||||
t[0] = out[k];
|
||||
for q in 1..p {
|
||||
t[q] = out[q * m + k].mul(tw[q * k * tw_stride]);
|
||||
}
|
||||
for q2 in 0..p {
|
||||
let mut acc = t[0];
|
||||
for q in 1..p {
|
||||
acc = acc.add(t[q].mul(w5[(q * q2) % p]));
|
||||
}
|
||||
out[q2 * m + k] = acc;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
use crate::opus::fft::{Cpx, Fft};
|
||||
|
||||
pub struct Imdct {
|
||||
/// Full MDCT length (2x the number of coefficients).
|
||||
|
||||
@@ -1,272 +1,45 @@
|
||||
//! Pulse cache and bit allocation (RFC 6716 §4.3.3, §4.3.4.1).
|
||||
|
||||
use super::tables::{ALLOC, EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS, OVERLAP};
|
||||
use super::tables::{EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS, OVERLAP};
|
||||
use crate::opus::decode::range::{BITRES, RangeDecoder};
|
||||
|
||||
const MAX_PSEUDO: i32 = 40;
|
||||
const LOG_MAX_PSEUDO: i32 = 6;
|
||||
pub use crate::opus::rate_common::get_pulses;
|
||||
#[cfg(test)]
|
||||
use crate::opus::rate_common::log2_frac;
|
||||
#[cfg(test)]
|
||||
pub use crate::opus::rate_common::pvq_v;
|
||||
pub const MAX_FINE_BITS: i32 = 8;
|
||||
const FINE_OFFSET: i32 = 21;
|
||||
pub const QTHETA_OFFSET: i32 = 4;
|
||||
pub const QTHETA_OFFSET_TWOPHASE: i32 = 16;
|
||||
const ALLOC_STEPS: i32 = 6;
|
||||
|
||||
/// Number of pulses represented by pseudo-pulse index `i`.
|
||||
pub fn get_pulses(i: i32) -> i32 {
|
||||
if i < 8 { i } else { (8 + (i & 7)) << ((i >> 3) - 1) }
|
||||
}
|
||||
|
||||
/// Conservative (rounded up) log2 of `val` with `frac` fractional bits.
|
||||
pub fn log2_frac(mut val: u32, mut frac: u32) -> i32 {
|
||||
let mut l = crate::opus::decode::range::ilog(val);
|
||||
if val & val.wrapping_sub(1) != 0 {
|
||||
if l > 16 {
|
||||
val = ((val - 1) >> (l - 16)) + 1;
|
||||
} else {
|
||||
val <<= 16 - l;
|
||||
}
|
||||
let mut l = (l - 1) << frac;
|
||||
loop {
|
||||
let b = (val >> 16) as i32;
|
||||
l += b << frac;
|
||||
val = (val + b as u32) >> b;
|
||||
val = (val * val + 0x7FFF) >> 15;
|
||||
if frac == 0 {
|
||||
break;
|
||||
}
|
||||
frac -= 1;
|
||||
}
|
||||
l + (val > 0x8000) as i32
|
||||
} else {
|
||||
l -= 1;
|
||||
l << frac
|
||||
}
|
||||
}
|
||||
|
||||
/// PVQ codebook size V(N, K) as u128 (saturating far above 2^32).
|
||||
pub fn pvq_v(n: usize, k: usize) -> u128 {
|
||||
// V(N,K) = V(N-1,K) + V(N,K-1) + V(N-1,K-1); computed row by row.
|
||||
if k == 0 {
|
||||
return 1;
|
||||
}
|
||||
if n == 0 {
|
||||
return 0;
|
||||
}
|
||||
let mut row = vec![0u128; k + 1]; // V(0, k)
|
||||
row[0] = 1;
|
||||
for _ in 0..n {
|
||||
let mut prev_diag = row[0]; // V(n-1, 0)
|
||||
// V(n, 0) = 1
|
||||
for kk in 1..=k {
|
||||
let up = row[kk]; // V(n-1, kk)
|
||||
let v = up.saturating_add(row[kk - 1]).saturating_add(prev_diag);
|
||||
prev_diag = up;
|
||||
row[kk] = v.min(u128::MAX >> 8);
|
||||
}
|
||||
row[0] = 1;
|
||||
}
|
||||
row[k]
|
||||
}
|
||||
|
||||
fn fits_in32(n: usize, k: i32) -> bool {
|
||||
pvq_v(n, k as usize) <= u32::MAX as u128
|
||||
}
|
||||
|
||||
/// Precomputed per-mode data shared by all CELT decoders.
|
||||
/// Decoder mode: shared allocation tables and decoder-specific inverse transforms.
|
||||
pub struct Mode {
|
||||
/// log2 of band width in 1/8 bits.
|
||||
pub log_n: [i32; NB_EBANDS],
|
||||
/// Index into `cache_bits` for `(LM+1)*NB_EBANDS + band` (-1 when unused).
|
||||
pub cache_index: Vec<i16>,
|
||||
/// `cache[0]` = max pseudo-pulses; `cache[k]` = bits(k) - 1 in 1/8 bits.
|
||||
pub cache_bits: Vec<u8>,
|
||||
/// Per-band maximum allocation: `[(2*LM + C-1)*NB_EBANDS + band]`.
|
||||
pub caps: Vec<u8>,
|
||||
core: crate::opus::rate_common::Mode,
|
||||
pub window: [f32; OVERLAP],
|
||||
pub mdct: [super::mdct::Imdct; MAX_LM + 1],
|
||||
}
|
||||
|
||||
impl Mode {
|
||||
pub fn new() -> Mode {
|
||||
let mut log_n = [0i32; NB_EBANDS];
|
||||
for (i, l) in log_n.iter_mut().enumerate() {
|
||||
*l = log2_frac((EBANDS[i + 1] - EBANDS[i]) as u32, BITRES);
|
||||
}
|
||||
let (cache_index, cache_bits) = compute_pulse_cache(MAX_LM as i32);
|
||||
let mut m = Mode {
|
||||
log_n,
|
||||
cache_index,
|
||||
cache_bits,
|
||||
caps: Vec::new(),
|
||||
pub fn new() -> Self {
|
||||
let mut mode = Self {
|
||||
core: crate::opus::rate_common::Mode::new(),
|
||||
window: [0.0; OVERLAP],
|
||||
mdct: [super::mdct::Imdct::new(1920), super::mdct::Imdct::new(960), super::mdct::Imdct::new(480), super::mdct::Imdct::new(240)],
|
||||
};
|
||||
m.caps = compute_caps(&m);
|
||||
for i in 0..OVERLAP {
|
||||
let x = std::f64::consts::FRAC_PI_2 * (i as f64 + 0.5) / OVERLAP as f64;
|
||||
let s = x.sin();
|
||||
m.window[i] = (std::f64::consts::FRAC_PI_2 * s * s).sin() as f32;
|
||||
mode.window[i] = (std::f64::consts::FRAC_PI_2 * s * s).sin() as f32;
|
||||
}
|
||||
m
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn cache(&self, lm: i32, band: usize) -> &[u8] {
|
||||
let idx = self.cache_index[((lm + 1) as usize) * NB_EBANDS + band];
|
||||
&self.cache_bits[idx.max(0) as usize..]
|
||||
}
|
||||
|
||||
pub fn bits2pulses(&self, band: usize, lm: i32, bits: i32) -> i32 {
|
||||
let cache = self.cache(lm, band);
|
||||
let mut lo = 0i32;
|
||||
let mut hi = cache[0] as i32;
|
||||
let bits = bits - 1;
|
||||
for _ in 0..LOG_MAX_PSEUDO {
|
||||
let mid = (lo + hi + 1) >> 1;
|
||||
if cache[mid as usize] as i32 >= bits {
|
||||
hi = mid;
|
||||
} else {
|
||||
lo = mid;
|
||||
}
|
||||
}
|
||||
let lo_bits = if lo == 0 { -1 } else { cache[lo as usize] as i32 };
|
||||
if bits - lo_bits <= cache[hi as usize] as i32 - bits { lo } else { hi }
|
||||
}
|
||||
|
||||
pub fn pulses2bits(&self, band: usize, lm: i32, pulses: i32) -> i32 {
|
||||
if pulses == 0 { 0 } else { self.cache(lm, band)[pulses as usize] as i32 + 1 }
|
||||
}
|
||||
|
||||
/// Per-band allocation caps for this frame (`init_caps`).
|
||||
pub fn init_caps(&self, lm: usize, c: usize) -> [i32; NB_EBANDS] {
|
||||
let mut cap = [0i32; NB_EBANDS];
|
||||
for (i, cp) in cap.iter_mut().enumerate() {
|
||||
let n = (EBANDS[i + 1] - EBANDS[i]) << lm;
|
||||
*cp = ((self.caps[NB_EBANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * n) >> 2;
|
||||
}
|
||||
cap
|
||||
mode
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_pulse_cache(lm_max: i32) -> (Vec<i16>, Vec<u8>) {
|
||||
let nb = NB_EBANDS;
|
||||
let rows = (lm_max + 2) as usize;
|
||||
let mut index = vec![-1i16; nb * rows];
|
||||
let mut entries: Vec<(usize, i32, usize)> = Vec::new(); // (N, K, offset)
|
||||
let mut curr = 0usize;
|
||||
for i in 0..rows {
|
||||
for j in 0..nb {
|
||||
let n = (((EBANDS[j + 1] - EBANDS[j]) << i) >> 1) as usize;
|
||||
index[i * nb + j] = -1;
|
||||
// Reuse the entry of an earlier band with the same size.
|
||||
'search: for k in 0..=i {
|
||||
for nn in 0..nb {
|
||||
if k == i && nn >= j {
|
||||
break;
|
||||
}
|
||||
if n == (((EBANDS[nn + 1] - EBANDS[nn]) << k) >> 1) as usize {
|
||||
index[i * nb + j] = index[k * nb + nn];
|
||||
break 'search;
|
||||
}
|
||||
}
|
||||
}
|
||||
if index[i * nb + j] == -1 && n != 0 {
|
||||
let mut k = 0;
|
||||
while k < MAX_PSEUDO && fits_in32(n, get_pulses(k + 1)) {
|
||||
k += 1;
|
||||
}
|
||||
index[i * nb + j] = curr as i16;
|
||||
entries.push((n, k, curr));
|
||||
curr += (k + 1) as usize;
|
||||
}
|
||||
}
|
||||
impl std::ops::Deref for Mode {
|
||||
type Target = crate::opus::rate_common::Mode;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.core
|
||||
}
|
||||
let mut bits = vec![0u8; curr];
|
||||
for &(n, k, off) in &entries {
|
||||
let maxk = get_pulses(k) as usize;
|
||||
let tmp = required_bits(n, maxk, BITRES);
|
||||
for j in 1..=k {
|
||||
bits[off + j as usize] = (tmp[get_pulses(j) as usize] - 1) as u8;
|
||||
}
|
||||
bits[off] = k as u8;
|
||||
}
|
||||
(index, bits)
|
||||
}
|
||||
|
||||
/// Bits (1/2^frac units, rounded up) needed for each K in `0..=maxk` for dimension `n`.
|
||||
fn required_bits(n: usize, maxk: usize, frac: u32) -> Vec<i32> {
|
||||
let mut out = vec![0i32; maxk + 1];
|
||||
if n == 1 {
|
||||
for o in out.iter_mut().skip(1) {
|
||||
*o = 1 << frac;
|
||||
}
|
||||
} else {
|
||||
for (k, o) in out.iter_mut().enumerate().skip(1) {
|
||||
let v = pvq_v(n, k);
|
||||
*o = log2_frac(v as u32, frac);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn compute_caps(m: &Mode) -> Vec<u8> {
|
||||
let nb = NB_EBANDS;
|
||||
let mut caps = vec![0u8; (MAX_LM + 1) * 2 * nb];
|
||||
for i in 0..=MAX_LM as i32 {
|
||||
for c in 1..=2i32 {
|
||||
for j in 0..nb {
|
||||
let mut n0 = EBANDS[j + 1] - EBANDS[j];
|
||||
let max_bits = if n0 << i == 1 {
|
||||
(c * (1 + MAX_FINE_BITS)) << BITRES
|
||||
} else {
|
||||
let mut lm0 = 0;
|
||||
if n0 > 2 {
|
||||
n0 >>= 1;
|
||||
lm0 -= 1;
|
||||
} else if n0 <= 1 {
|
||||
lm0 = i.min(1);
|
||||
n0 <<= lm0;
|
||||
}
|
||||
let pcache = m.cache(lm0, j);
|
||||
let mut mb = pcache[pcache[0] as usize] as i32 + 1;
|
||||
let mut n = n0;
|
||||
for k in 0..(i - lm0) {
|
||||
mb <<= 1;
|
||||
let offset = ((m.log_n[j] + ((lm0 + k) << BITRES)) >> 1) - QTHETA_OFFSET;
|
||||
let num = 459 * ((2 * n - 1) * offset + mb);
|
||||
let den = ((2 * n - 1) << 9) - 459;
|
||||
let qb = ((num + (den >> 1)) / den).min(57);
|
||||
mb += qb;
|
||||
n <<= 1;
|
||||
}
|
||||
if c == 2 {
|
||||
mb <<= 1;
|
||||
let offset = ((m.log_n[j] + (i << BITRES)) >> 1) - if n == 2 { QTHETA_OFFSET_TWOPHASE } else { QTHETA_OFFSET };
|
||||
let ndof = 2 * n - 1 - (n == 2) as i32;
|
||||
let p = if n == 2 { 512 } else { 487 };
|
||||
let num = p * (mb + ndof * offset);
|
||||
let den = (ndof << 9) - p;
|
||||
let qb = ((num + (den >> 1)) / den).min(if n == 2 { 64 } else { 61 });
|
||||
mb += qb;
|
||||
}
|
||||
let ndof = c * n + if c == 2 && n > 2 { 1 } else { 0 };
|
||||
let mut offset = ((m.log_n[j] + (i << BITRES)) >> 1) - FINE_OFFSET;
|
||||
if n == 2 {
|
||||
offset += (1 << BITRES) >> 2;
|
||||
}
|
||||
let num = mb + ndof * offset;
|
||||
let den = (ndof - 1) << BITRES;
|
||||
let qb = ((num + (den >> 1)) / den).min(MAX_FINE_BITS);
|
||||
mb += (c * qb) << BITRES;
|
||||
mb
|
||||
};
|
||||
let w = c * ((EBANDS[j + 1] - EBANDS[j]) << i);
|
||||
let v = (4 * max_bits / w) - 64;
|
||||
caps[((i * 2 + c - 1) as usize) * nb + j] = v.clamp(0, 255) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
caps
|
||||
}
|
||||
|
||||
/// Result of the allocation process.
|
||||
@@ -296,89 +69,8 @@ pub struct AllocationInput<'a, 'd> {
|
||||
|
||||
pub fn compute_allocation(input: AllocationInput<'_, '_>) -> Allocation {
|
||||
let AllocationInput { mode: m, start, end, offsets, cap, trim: alloc_trim, total, channels: c, lm, decoder: dec } = input;
|
||||
let ci = c as i32;
|
||||
let lmi = lm as i32;
|
||||
let mut total = total.max(0);
|
||||
let mut skip_start = start;
|
||||
let skip_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 };
|
||||
total -= skip_rsv;
|
||||
let mut intensity_rsv = 0;
|
||||
let mut dual_stereo_rsv = 0;
|
||||
if c == 2 {
|
||||
intensity_rsv = LOG2_FRAC_TABLE[end - start];
|
||||
if intensity_rsv > total {
|
||||
intensity_rsv = 0;
|
||||
} else {
|
||||
total -= intensity_rsv;
|
||||
dual_stereo_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 };
|
||||
total -= dual_stereo_rsv;
|
||||
}
|
||||
}
|
||||
let mut bits1 = [0i32; NB_EBANDS];
|
||||
let mut bits2 = [0i32; NB_EBANDS];
|
||||
let mut thresh = [0i32; NB_EBANDS];
|
||||
let mut trim_offset = [0i32; NB_EBANDS];
|
||||
for j in start..end {
|
||||
let w = EBANDS[j + 1] - EBANDS[j];
|
||||
thresh[j] = (ci << BITRES).max(((3 * w) << lmi << BITRES) >> 4);
|
||||
trim_offset[j] = (ci * w * (alloc_trim - 5 - lmi) * (end as i32 - j as i32 - 1) * (1 << (lmi + BITRES as i32))) >> 6;
|
||||
if w << lmi == 1 {
|
||||
trim_offset[j] -= ci << BITRES;
|
||||
}
|
||||
}
|
||||
let nb_alloc = ALLOC.len() as i32;
|
||||
let mut lo = 1;
|
||||
let mut hi = nb_alloc - 1;
|
||||
loop {
|
||||
let mut done = false;
|
||||
let mut psum = 0;
|
||||
let mid = (lo + hi) >> 1;
|
||||
for j in (start..end).rev() {
|
||||
let n = EBANDS[j + 1] - EBANDS[j];
|
||||
let mut bitsj = (ci * n * (ALLOC[mid as usize][j] as i32)) << lmi >> 2;
|
||||
if bitsj > 0 {
|
||||
bitsj = (bitsj + trim_offset[j]).max(0);
|
||||
}
|
||||
bitsj += offsets[j];
|
||||
if bitsj >= thresh[j] || done {
|
||||
done = true;
|
||||
psum += bitsj.min(cap[j]);
|
||||
} else if bitsj >= ci << BITRES {
|
||||
psum += ci << BITRES;
|
||||
}
|
||||
}
|
||||
if psum > total {
|
||||
hi = mid - 1;
|
||||
} else {
|
||||
lo = mid + 1;
|
||||
}
|
||||
if lo > hi {
|
||||
break;
|
||||
}
|
||||
}
|
||||
hi = lo;
|
||||
lo -= 1;
|
||||
for j in start..end {
|
||||
let n = EBANDS[j + 1] - EBANDS[j];
|
||||
let mut bits1j = (ci * n * (ALLOC[lo as usize][j] as i32)) << lmi >> 2;
|
||||
let mut bits2j = if hi >= nb_alloc { cap[j] } else { (ci * n * (ALLOC[hi as usize][j] as i32)) << lmi >> 2 };
|
||||
if bits1j > 0 {
|
||||
bits1j = (bits1j + trim_offset[j]).max(0);
|
||||
}
|
||||
if bits2j > 0 {
|
||||
bits2j = (bits2j + trim_offset[j]).max(0);
|
||||
}
|
||||
if lo > 0 {
|
||||
bits1j += offsets[j];
|
||||
}
|
||||
bits2j += offsets[j];
|
||||
if offsets[j] > 0 {
|
||||
skip_start = j;
|
||||
}
|
||||
bits2j = (bits2j - bits1j).max(0);
|
||||
bits1[j] = bits1j;
|
||||
bits2[j] = bits2j;
|
||||
}
|
||||
let crate::opus::rate_common::Bracket { skip_start, bits1, bits2, thresh, total, skip_rsv, intensity_rsv, dual_stereo_rsv } =
|
||||
crate::opus::rate_common::bracket(start, end, offsets, cap, alloc_trim, total, c, lm);
|
||||
interp_bits2pulses(InterpPulsesInput {
|
||||
mode: m,
|
||||
start,
|
||||
|
||||
@@ -14,6 +14,7 @@ pub const MAX_LM: usize = 3;
|
||||
pub const PREEMPH: f32 = 0.850_006_1;
|
||||
|
||||
/// Static allocation table (Table 57), indexed `[quality][band]`, in 1/32 bit per MDCT bin.
|
||||
#[cfg(not(feature = "opus-encode"))]
|
||||
pub const ALLOC: [[u8; NB_EBANDS]; 11] = transpose([
|
||||
[0, 90, 110, 118, 126, 134, 144, 152, 162, 172, 200],
|
||||
[0, 80, 100, 110, 119, 127, 137, 145, 155, 165, 200],
|
||||
@@ -38,6 +39,7 @@ pub const ALLOC: [[u8; NB_EBANDS]; 11] = transpose([
|
||||
[0, 0, 0, 0, 0, 1, 1, 1, 1, 20, 104],
|
||||
]);
|
||||
|
||||
#[cfg(not(feature = "opus-encode"))]
|
||||
const fn transpose(t: [[u8; 11]; NB_EBANDS]) -> [[u8; NB_EBANDS]; 11] {
|
||||
let mut out = [[0u8; NB_EBANDS]; 11];
|
||||
let mut b = 0;
|
||||
|
||||
@@ -4,25 +4,7 @@
|
||||
//! RFC 6716 Table 54 (and the integer delays used when no rate conversion is needed), so that the
|
||||
//! SILK output lines up with the CELT layer in Hybrid mode.
|
||||
|
||||
/// Dot product with eight independent partial sums, so the additions pipeline and the loop
|
||||
/// vectorises instead of waiting on one 4-cycle add chain. Rounds differently from a
|
||||
/// sequential sum (the resampler is not normative).
|
||||
#[inline]
|
||||
fn dot8(a: &[f32], b: &[f32]) -> f32 {
|
||||
let mut s = [0f32; 8];
|
||||
let (ca, ra) = a.as_chunks::<8>();
|
||||
let (cb, rb) = b.as_chunks::<8>();
|
||||
for (x, y) in ca.iter().zip(cb) {
|
||||
for l in 0..8 {
|
||||
s[l] += x[l] * y[l];
|
||||
}
|
||||
}
|
||||
let mut acc = ((s[0] + s[4]) + (s[1] + s[5])) + ((s[2] + s[6]) + (s[3] + s[7]));
|
||||
for (x, y) in ra.iter().zip(rb) {
|
||||
acc += x * y;
|
||||
}
|
||||
acc
|
||||
}
|
||||
use crate::opus::dot::dot8;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Resampler {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
//! Shared fixed-order FIR dot product.
|
||||
|
||||
/// Dot product with eight independent partial sums, which the compiler can vectorise (a single
|
||||
/// running sum is bound by the latency of the float addition). The sums are combined pairwise.
|
||||
#[inline]
|
||||
pub(super) fn dot8(a: &[f32], b: &[f32]) -> f32 {
|
||||
let mut s = [0f32; 8];
|
||||
let (ca, ra) = a.as_chunks::<8>();
|
||||
let (cb, rb) = b.as_chunks::<8>();
|
||||
for (x, y) in ca.iter().zip(cb) {
|
||||
for l in 0..8 {
|
||||
s[l] += x[l] * y[l];
|
||||
}
|
||||
}
|
||||
let mut acc = ((s[0] + s[4]) + (s[1] + s[5])) + ((s[2] + s[6]) + (s[3] + s[7]));
|
||||
for (x, y) in ra.iter().zip(rb) {
|
||||
acc += x * y;
|
||||
}
|
||||
acc
|
||||
}
|
||||
@@ -0,0 +1,720 @@
|
||||
//! Band shape coding: PVQ search and codeword indexing, spreading, recursive band splitting
|
||||
//! (RFC 6716 §4.3.4, encoder direction).
|
||||
//!
|
||||
//! The encoder codes 20 ms long-block frames only (no transients, no TF changes), and stereo
|
||||
//! as dual (independent L/R) stereo, so a band is always coded by `quant_partition` with a single
|
||||
//! block. Every bit-budget computation mirrors the decoder so both sides take the same
|
||||
//! split/pulse decisions.
|
||||
|
||||
use crate::opus::encoder::range::{BITRES, RangeEncoder, ilog};
|
||||
use crate::opus::encoder::rate::{Mode, QTHETA_OFFSET, get_pulses};
|
||||
use crate::opus::encoder::tables::EBANDS;
|
||||
use crate::opus::encoder::{CeltFrame, rate::Allocation};
|
||||
|
||||
pub const SPREAD_NORMAL: usize = 2;
|
||||
|
||||
#[inline]
|
||||
fn frac_mul16(a: i32, b: i32) -> i32 {
|
||||
(16384 + (a as i16 as i32) * (b as i16 as i32)) >> 15
|
||||
}
|
||||
|
||||
fn bitexact_cos(x: i32) -> i32 {
|
||||
let tmp = (4096 + x * x) >> 13;
|
||||
let x2 = tmp;
|
||||
let x2 = (32767 - x2) + frac_mul16(x2, -7651 + frac_mul16(x2, 8277 + frac_mul16(-626, x2)));
|
||||
1 + x2
|
||||
}
|
||||
|
||||
fn bitexact_log2tan(isin: i32, icos: i32) -> i32 {
|
||||
let lc = ilog(icos as u32);
|
||||
let ls = ilog(isin as u32);
|
||||
let icos = icos << (15 - lc);
|
||||
let isin = isin << (15 - ls);
|
||||
(ls - lc) * (1 << 11) + frac_mul16(isin, frac_mul16(isin, -2597) + 7932) - frac_mul16(icos, frac_mul16(icos, -2597) + 7932)
|
||||
}
|
||||
|
||||
fn compute_qn(n: i32, b: i32, offset: i32, pulse_cap: i32, stereo: bool) -> i32 {
|
||||
const EXP2_TABLE8: [i32; 8] = [16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048];
|
||||
let mut n2 = 2 * n - 1;
|
||||
if stereo && n == 2 {
|
||||
n2 -= 1;
|
||||
}
|
||||
let mut qb = (b + n2 * offset) / n2;
|
||||
qb = qb.min(b - pulse_cap - (4 << BITRES));
|
||||
qb = qb.min(8 << BITRES);
|
||||
if qb < (1 << BITRES >> 1) {
|
||||
1
|
||||
} else {
|
||||
let qn = EXP2_TABLE8[(qb & 0x7) as usize] >> (14 - (qb >> BITRES));
|
||||
(qn + 1) >> 1 << 1
|
||||
}
|
||||
}
|
||||
|
||||
/// One elementary pass of the spreading rotation, inverted (the decoder applies the forward
|
||||
/// pass; the encoder undoes it before the pulse search).
|
||||
fn exp_rotation1_inv(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) {
|
||||
if len <= stride {
|
||||
return;
|
||||
}
|
||||
if len > 2 * stride {
|
||||
for i in 0..=len - 2 * stride - 1 {
|
||||
let x1 = x[i];
|
||||
let x2 = x[i + stride];
|
||||
x[i + stride] = c * x2 - s * x1;
|
||||
x[i] = c * x1 + s * x2;
|
||||
}
|
||||
}
|
||||
for i in (0..len - stride).rev() {
|
||||
let x1 = x[i];
|
||||
let x2 = x[i + stride];
|
||||
x[i + stride] = c * x2 - s * x1;
|
||||
x[i] = c * x1 + s * x2;
|
||||
}
|
||||
}
|
||||
|
||||
/// Inverse of the decoder's spreading rotation (§4.3.4.3) for one block of `len` samples.
|
||||
pub fn exp_rotation_inv(x: &mut [f32], len: usize, k: usize, spread: usize) {
|
||||
const SPREAD_FACTOR: [usize; 3] = [15, 10, 5];
|
||||
if 2 * k >= len || spread == 0 {
|
||||
return;
|
||||
}
|
||||
let factor = SPREAD_FACTOR[spread - 1];
|
||||
let gain = len as f32 / (len + factor * k) as f32;
|
||||
let theta = 0.5 * gain * gain;
|
||||
let c = (0.5 * std::f32::consts::PI * theta).cos();
|
||||
let s = (0.5 * std::f32::consts::PI * (1.0 - theta)).cos();
|
||||
let mut stride2 = 0;
|
||||
if len >= 8 {
|
||||
stride2 = 1;
|
||||
while stride2 * stride2 + stride2 < len {
|
||||
stride2 += 1;
|
||||
}
|
||||
}
|
||||
// The decoder applies (stride2: s, c) then (1: c, s); undo in reverse order.
|
||||
exp_rotation1_inv(x, len, 1, c, s);
|
||||
if stride2 != 0 {
|
||||
exp_rotation1_inv(x, len, stride2, s, c);
|
||||
}
|
||||
}
|
||||
|
||||
/// Relative slack for the pulse-candidate filter in [`pvq_search`]: far above the f32 rounding of
|
||||
/// the filter score (a few 1e-7), far below the spread of real scores.
|
||||
const PVQ_SCORE_SLACK: f32 = 1e-5;
|
||||
|
||||
/// Greedy PVQ search: the integer vector `y` with `sum |y| = k` closest in direction to `x`.
|
||||
pub fn pvq_search(x: &[f32], k: usize, y: &mut [i32]) {
|
||||
let n = x.len();
|
||||
y[..n].fill(0);
|
||||
// `abs`, `dy` (2 y + 1, infinite in the padding) and the per-pass scores `score`, each padded to
|
||||
// whole 8-lane chunks.
|
||||
let np = n.next_multiple_of(8);
|
||||
let mut buf = vec![0f32; 3 * np];
|
||||
let (abs, rest) = buf.split_at_mut(np);
|
||||
let (dy, score) = rest.split_at_mut(np);
|
||||
for (a, v) in abs.iter_mut().zip(x) {
|
||||
*a = v.abs();
|
||||
}
|
||||
let sum: f32 = abs[..n].iter().sum();
|
||||
if !sum.is_finite() || sum <= 1e-15 {
|
||||
y[0] = k as i32;
|
||||
return;
|
||||
}
|
||||
let mut placed = 0usize;
|
||||
let mut xy = 0f32;
|
||||
let mut yy = 0f32;
|
||||
if k > n / 2 {
|
||||
// Pre-project onto the pyramid (rounding down) and place the rest greedily.
|
||||
let r = (k as f32 - 1.0) / sum;
|
||||
for j in 0..n {
|
||||
// Non-negative and at most k, so the truncating cast is the floor.
|
||||
let p = (abs[j] * r) as i32;
|
||||
y[j] = p;
|
||||
placed += p as usize;
|
||||
xy += abs[j] * p as f32;
|
||||
yy += (p * p) as f32;
|
||||
}
|
||||
}
|
||||
for j in 0..np {
|
||||
dy[j] = if j < n { 2.0 * y[j] as f32 + 1.0 } else { f32::INFINITY };
|
||||
}
|
||||
while placed < k {
|
||||
// Vectorisable scores (xy + |x_j|)^2 / (yy + 2 y_j + 1) and their maximum.
|
||||
for ((sc, &a), &d) in score.iter_mut().zip(abs.iter()).zip(dy.iter()) {
|
||||
let t = xy + a;
|
||||
*sc = t * t / (yy + d);
|
||||
}
|
||||
let mut lane_max = [0f32; 8];
|
||||
for c in score.as_chunks::<8>().0 {
|
||||
for i in 0..8 {
|
||||
lane_max[i] = if c[i] > lane_max[i] { c[i] } else { lane_max[i] };
|
||||
}
|
||||
}
|
||||
let top = lane_max.iter().fold(0f32, |m, &v| if v > m { v } else { m });
|
||||
// The exact cross-multiplied test below, in index order with the first maximum winning, only
|
||||
// matters for entries within rounding distance of the top score: every other entry is beaten
|
||||
// by the top one whenever it is reached and never beats it afterwards. Running the test on
|
||||
// those alone therefore picks the same pulse as running it on all of them.
|
||||
let filter = top > 1e-30 && top.is_finite();
|
||||
let lo = top * (1.0 - PVQ_SCORE_SLACK);
|
||||
// Usually exactly one entry qualifies; its index falls out of a lane-wise count.
|
||||
let mut count = [0u32; 8];
|
||||
let mut index = [0u32; 8];
|
||||
for (ci, c) in score.as_chunks::<8>().0.iter().enumerate() {
|
||||
for i in 0..8 {
|
||||
let hit = u32::from(c[i] >= lo);
|
||||
count[i] += hit;
|
||||
index[i] += hit * (ci * 8 + i) as u32;
|
||||
}
|
||||
}
|
||||
let mut best = 0usize;
|
||||
if filter && count.iter().sum::<u32>() == 1 {
|
||||
best = index.iter().sum::<u32>() as usize;
|
||||
} else {
|
||||
let mut best_num = -1f32;
|
||||
let mut best_den = 1f32;
|
||||
for j in 0..n {
|
||||
if !filter || score[j] >= lo {
|
||||
let num = xy + abs[j];
|
||||
let num = num * num;
|
||||
let den = yy + 2.0 * y[j] as f32 + 1.0;
|
||||
if num * best_den > best_num * den {
|
||||
best_num = num;
|
||||
best_den = den;
|
||||
best = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
xy += abs[best];
|
||||
yy += 2.0 * y[best] as f32 + 1.0;
|
||||
y[best] += 1;
|
||||
dy[best] = 2.0 * y[best] as f32 + 1.0;
|
||||
placed += 1;
|
||||
}
|
||||
for j in 0..n {
|
||||
if x[j] < 0.0 {
|
||||
y[j] = -y[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Largest band width and pulse count the encoder codes (176 coefficients, `get_pulses(40)` = 128).
|
||||
const PVQ_MAX_N: usize = 176;
|
||||
const PVQ_MAX_K: usize = 128;
|
||||
|
||||
/// Row-major `V(n, k)` for `n <= max_n`, `k <= max_k` with row stride `max_k + 1`, saturated at
|
||||
/// `u32::MAX` (a codebook that large is never coded, see `rate::fits_in32`).
|
||||
fn build_pvq_table(max_n: usize, max_k: usize) -> Vec<u32> {
|
||||
let w = max_k + 1;
|
||||
let mut v = vec![0u32; (max_n + 1) * w];
|
||||
v[0] = 1;
|
||||
for nn in 1..=max_n {
|
||||
v[nn * w] = 1;
|
||||
for kk in 1..=max_k {
|
||||
let sum = v[(nn - 1) * w + kk] as u64 + v[nn * w + kk - 1] as u64 + v[(nn - 1) * w + kk - 1] as u64;
|
||||
v[nn * w + kk] = sum.min(u32::MAX as u64) as u32;
|
||||
}
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
fn pvq_table() -> &'static [u32] {
|
||||
static TABLE: std::sync::OnceLock<Vec<u32>> = std::sync::OnceLock::new();
|
||||
TABLE.get_or_init(|| build_pvq_table(PVQ_MAX_N, PVQ_MAX_K))
|
||||
}
|
||||
|
||||
/// Encodes the PVQ codeword `y` (`sum |y| = k`) as the index the RFC 6716 §4.3.4.2 decoder maps
|
||||
/// back to `y`.
|
||||
pub fn encode_pulses(y: &[i32], k: usize, enc: &mut RangeEncoder) {
|
||||
let n = y.len();
|
||||
// Shapes outside the coded range (never produced by the band layout) get a local table.
|
||||
let local;
|
||||
let (v, w) = if n <= PVQ_MAX_N && k <= PVQ_MAX_K {
|
||||
(pvq_table(), PVQ_MAX_K + 1)
|
||||
} else {
|
||||
local = build_pvq_table(n, k);
|
||||
(&local[..], k + 1)
|
||||
};
|
||||
let row = |nn: usize| &v[nn * w..(nn + 1) * w];
|
||||
let total = row(n)[k] as u64;
|
||||
let mut index = 0u64;
|
||||
let mut kk = k;
|
||||
for j in 0..n {
|
||||
if kk == 0 {
|
||||
break;
|
||||
}
|
||||
let rem = n - j;
|
||||
let t = y[j].unsigned_abs() as usize;
|
||||
let below = row(rem - 1);
|
||||
// Codewords with |y_j| = t start at sum_{m < kk - t} V(rem-1, m) within their sign half.
|
||||
let mut base: u64 = below[..kk - t].iter().map(|&x| x as u64).sum();
|
||||
if y[j] < 0 {
|
||||
base += (below[kk] as u64 + row(rem)[kk] as u64) / 2;
|
||||
}
|
||||
index += base;
|
||||
kk -= t;
|
||||
}
|
||||
debug_assert!(total <= u32::MAX as u64);
|
||||
if total >= 2 {
|
||||
enc.uint(index as u32, total as u32);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BandEnc<'m, 'e> {
|
||||
pub m: &'m Mode,
|
||||
pub enc: &'e mut RangeEncoder,
|
||||
pub i: usize,
|
||||
pub spread: usize,
|
||||
pub remaining_bits: i32,
|
||||
iy: Vec<i32>,
|
||||
tmp: Vec<f32>,
|
||||
}
|
||||
|
||||
impl BandEnc<'_, '_> {
|
||||
/// `compute_theta` for a mono split (single block): measures the energy split between the
|
||||
/// two halves and codes the quantised angle with the triangular pdf.
|
||||
fn compute_theta_split(&mut self, xa: &[f32], xb: &[f32], b: &mut i32, lm: i32) -> (i32, i32, i32) {
|
||||
let n = xa.len() as i32;
|
||||
let i = self.i;
|
||||
let pulse_cap = self.m.log_n[i] + lm * (1 << BITRES);
|
||||
let offset = (pulse_cap >> 1) - QTHETA_OFFSET;
|
||||
let qn = compute_qn(n, *b, offset, pulse_cap, false);
|
||||
let tell = self.enc.tell_frac();
|
||||
let mut itheta = 0i32;
|
||||
if qn != 1 {
|
||||
let mid = xa.iter().map(|v| v * v).sum::<f32>().sqrt();
|
||||
let side = xb.iter().map(|v| v * v).sum::<f32>().sqrt();
|
||||
let ang = (side as f64).atan2(mid as f64) * std::f64::consts::FRAC_2_PI;
|
||||
let it = ((ang * qn as f64 + 0.5).floor() as i32).clamp(0, qn) as u32;
|
||||
let q = qn as u32;
|
||||
let half = q >> 1;
|
||||
let ft = (half + 1) * (half + 1);
|
||||
let (fl, fs) = if it <= half { ((it * (it + 1)) >> 1, it + 1) } else { (ft - (((q + 1 - it) * (q + 2 - it)) >> 1), q + 1 - it) };
|
||||
self.enc.encode(fl, fl + fs, ft);
|
||||
itheta = ((it * 16384) / q) as i32;
|
||||
}
|
||||
let qalloc = self.enc.tell_frac() - tell;
|
||||
*b -= qalloc;
|
||||
let delta = if itheta == 0 {
|
||||
-16384
|
||||
} else if itheta == 16384 {
|
||||
16384
|
||||
} else {
|
||||
let imid = bitexact_cos(itheta);
|
||||
let iside = bitexact_cos(16384 - itheta);
|
||||
frac_mul16((n - 1) << 7, bitexact_log2tan(iside, imid))
|
||||
};
|
||||
(itheta, delta, qalloc)
|
||||
}
|
||||
|
||||
/// `quant_partition` (single block, encoder side).
|
||||
pub fn quant_partition(&mut self, x: &[f32], mut b: i32, mut lm: i32) {
|
||||
let n = x.len();
|
||||
let i = self.i;
|
||||
let cache = self.m.cache(lm, i);
|
||||
let cache_max = cache[cache[0] as usize] as i32;
|
||||
if lm != -1 && b > cache_max + 12 && n > 2 {
|
||||
let half = n >> 1;
|
||||
let (xa, xb) = x.split_at(half);
|
||||
lm -= 1;
|
||||
let (itheta, delta, qalloc) = self.compute_theta_split(xa, xb, &mut b, lm);
|
||||
let mut mbits = 0.max(b.min((b - delta) / 2));
|
||||
let mut sbits = b - mbits;
|
||||
self.remaining_bits -= qalloc;
|
||||
let mut rebalance = self.remaining_bits;
|
||||
if mbits >= sbits {
|
||||
self.quant_partition(xa, mbits, lm);
|
||||
rebalance = mbits - (rebalance - self.remaining_bits);
|
||||
if rebalance > 3 << BITRES && itheta != 0 {
|
||||
sbits += rebalance - (3 << BITRES);
|
||||
}
|
||||
self.quant_partition(xb, sbits, lm);
|
||||
} else {
|
||||
self.quant_partition(xb, sbits, lm);
|
||||
rebalance = sbits - (rebalance - self.remaining_bits);
|
||||
if rebalance > 3 << BITRES && itheta != 16384 {
|
||||
mbits += rebalance - (3 << BITRES);
|
||||
}
|
||||
self.quant_partition(xa, mbits, lm);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let m = self.m;
|
||||
let mut q = m.bits2pulses(i, lm, b);
|
||||
let mut curr_bits = m.pulses2bits(i, lm, q);
|
||||
self.remaining_bits -= curr_bits;
|
||||
while self.remaining_bits < 0 && q > 0 {
|
||||
self.remaining_bits += curr_bits;
|
||||
q -= 1;
|
||||
curr_bits = m.pulses2bits(i, lm, q);
|
||||
self.remaining_bits -= curr_bits;
|
||||
}
|
||||
if q != 0 {
|
||||
let k = get_pulses(q) as usize;
|
||||
let mut tmp = std::mem::take(&mut self.tmp);
|
||||
tmp.clear();
|
||||
tmp.extend_from_slice(x);
|
||||
exp_rotation_inv(&mut tmp, n, k, self.spread);
|
||||
let mut iy = std::mem::take(&mut self.iy);
|
||||
iy.clear();
|
||||
iy.resize(n, 0);
|
||||
pvq_search(&tmp, k, &mut iy);
|
||||
encode_pulses(&iy, k, self.enc);
|
||||
self.iy = iy;
|
||||
self.tmp = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Shape-coding controls, independent of the selected band allocation.
|
||||
pub struct BandCoding {
|
||||
pub spread: usize,
|
||||
pub total_bits: i32,
|
||||
}
|
||||
|
||||
/// Codes all band shapes (`quant_all_bands`, encoder side, long blocks). `x` holds
|
||||
/// `frame.channels` channels of `n_total` normalised MDCT coefficients each.
|
||||
pub fn quant_all_bands(m: &Mode, frame: CeltFrame, x: &[f32], n_total: usize, allocation: &Allocation, coding: BandCoding, enc: &mut RangeEncoder) {
|
||||
let CeltFrame { start, end, channels: c, lm } = frame;
|
||||
let Allocation { pulses, dual_stereo, intensity, coded_bands, balance, .. } = allocation;
|
||||
let (dual_stereo, intensity, coded_bands, mut balance) = (*dual_stereo, *intensity, *coded_bands, *balance);
|
||||
let BandCoding { spread, total_bits } = coding;
|
||||
let mm = 1usize << lm;
|
||||
debug_assert!(c == 1 || dual_stereo || intensity <= start, "stereo frames are coded as dual stereo");
|
||||
let mut ctx = BandEnc { m, enc, i: 0, spread, remaining_bits: 0, iy: Vec::new(), tmp: Vec::new() };
|
||||
for i in start..end {
|
||||
ctx.i = i;
|
||||
let xo = mm * EBANDS[i] as usize;
|
||||
let n = mm * EBANDS[i + 1] as usize - xo;
|
||||
let tell = ctx.enc.tell_frac();
|
||||
if i != start {
|
||||
balance -= tell;
|
||||
}
|
||||
let remaining_bits = total_bits - tell - 1;
|
||||
ctx.remaining_bits = remaining_bits;
|
||||
let b = if i < coded_bands {
|
||||
let curr_balance = balance / 3.min(coded_bands as i32 - i as i32);
|
||||
0.max(16383.min((remaining_bits + 1).min(pulses[i] + curr_balance)))
|
||||
} else {
|
||||
0
|
||||
};
|
||||
// Bands at and above `intensity` (= coded_bands) carry no bits, so they emit no symbols.
|
||||
if i < coded_bands || c == 1 {
|
||||
if c == 2 && dual_stereo && i < intensity {
|
||||
ctx.quant_partition(&x[xo..xo + n], b / 2, lm as i32);
|
||||
ctx.quant_partition(&x[n_total + xo..n_total + xo + n], b / 2, lm as i32);
|
||||
} else {
|
||||
ctx.quant_partition(&x[xo..xo + n], b, lm as i32);
|
||||
}
|
||||
}
|
||||
balance += pulses[i] + tell;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::opus::encoder::range::dec::RangeDecoder;
|
||||
|
||||
fn exp_rotation1(x: &mut [f32], len: usize, stride: usize, c: f32, s: f32) {
|
||||
let ms = -s;
|
||||
if len <= stride {
|
||||
return;
|
||||
}
|
||||
for i in 0..len - stride {
|
||||
let x1 = x[i];
|
||||
let x2 = x[i + stride];
|
||||
x[i + stride] = c * x2 + s * x1;
|
||||
x[i] = c * x1 + ms * x2;
|
||||
}
|
||||
if len > 2 * stride {
|
||||
for i in (0..=len - 2 * stride - 1).rev() {
|
||||
let x1 = x[i];
|
||||
let x2 = x[i + stride];
|
||||
x[i + stride] = c * x2 + s * x1;
|
||||
x[i] = c * x1 + ms * x2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The decoder's spreading rotation (single block).
|
||||
fn exp_rotation(x: &mut [f32], len: usize, k: usize, spread: usize) {
|
||||
const SPREAD_FACTOR: [usize; 3] = [15, 10, 5];
|
||||
if 2 * k >= len || spread == 0 {
|
||||
return;
|
||||
}
|
||||
let factor = SPREAD_FACTOR[spread - 1];
|
||||
let gain = len as f32 / (len + factor * k) as f32;
|
||||
let theta = 0.5 * gain * gain;
|
||||
let c = (0.5 * std::f32::consts::PI * theta).cos();
|
||||
let s = (0.5 * std::f32::consts::PI * (1.0 - theta)).cos();
|
||||
let mut stride2 = 0;
|
||||
if len >= 8 {
|
||||
stride2 = 1;
|
||||
while stride2 * stride2 + stride2 < len {
|
||||
stride2 += 1;
|
||||
}
|
||||
}
|
||||
if stride2 != 0 {
|
||||
exp_rotation1(x, len, stride2, s, c);
|
||||
}
|
||||
exp_rotation1(x, len, 1, c, s);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spreading_inverse_is_exact() {
|
||||
for &len in &[8usize, 16, 24, 48, 96, 176] {
|
||||
for k in [1usize, 2, 3, 5] {
|
||||
let orig: Vec<f32> = (0..len).map(|i| ((i * 31 % 17) as f32 - 8.0) / 8.0).collect();
|
||||
let mut x = orig.clone();
|
||||
exp_rotation_inv(&mut x, len, k, SPREAD_NORMAL);
|
||||
exp_rotation(&mut x, len, k, SPREAD_NORMAL);
|
||||
for (a, b) in x.iter().zip(&orig) {
|
||||
assert!((a - b).abs() < 1e-4, "len={len} k={k}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// PVQ decoder of RFC 6716 §4.3.4.2 (as in FilmCraft's decoder).
|
||||
fn decode_pulses(n: usize, k: usize, dec: &mut RangeDecoder) -> Vec<i32> {
|
||||
let w = k + 1;
|
||||
let mut v = vec![0u64; (n + 1) * w];
|
||||
v[0] = 1;
|
||||
for nn in 1..=n {
|
||||
v[nn * w] = 1;
|
||||
for kk in 1..=k {
|
||||
v[nn * w + kk] = v[(nn - 1) * w + kk].saturating_add(v[nn * w + kk - 1]).saturating_add(v[(nn - 1) * w + kk - 1]);
|
||||
}
|
||||
}
|
||||
let total = v[n * w + k];
|
||||
let mut i = if total >= 2 { dec.uint(total as u32) as u64 } else { 0 };
|
||||
let mut y = vec![0i32; n];
|
||||
let mut kk = k;
|
||||
for j in 0..n {
|
||||
let rem = n - j;
|
||||
if kk == 0 {
|
||||
break;
|
||||
}
|
||||
let vv = |nn: usize, kx: usize| v[nn * w + kx];
|
||||
let mut p = (vv(rem - 1, kk) + vv(rem, kk)) / 2;
|
||||
let neg = i >= p;
|
||||
if neg {
|
||||
i -= p;
|
||||
}
|
||||
let k0 = kk;
|
||||
p -= vv(rem - 1, kk);
|
||||
while p > i && kk > 0 {
|
||||
kk -= 1;
|
||||
p -= vv(rem - 1, kk);
|
||||
}
|
||||
let mag = (k0 - kk) as i32;
|
||||
y[j] = if neg { -mag } else { mag };
|
||||
i -= p.min(i);
|
||||
}
|
||||
y
|
||||
}
|
||||
|
||||
fn all_codewords(n: usize, k: usize) -> Vec<Vec<i32>> {
|
||||
if n == 0 {
|
||||
return if k == 0 { vec![vec![]] } else { vec![] };
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
for t in -(k as i32)..=(k as i32) {
|
||||
for mut rest in all_codewords(n - 1, k - t.unsigned_abs() as usize) {
|
||||
rest.insert(0, t);
|
||||
out.push(rest);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pvq_index_roundtrip_exhaustive() {
|
||||
for n in 1..6usize {
|
||||
for k in 1..6usize {
|
||||
for y in all_codewords(n, k) {
|
||||
let mut e = RangeEncoder::new();
|
||||
encode_pulses(&y, k, &mut e);
|
||||
let buf = e.done(16);
|
||||
let mut d = RangeDecoder::new(&buf);
|
||||
assert_eq!(decode_pulses(n, k, &mut d), y, "n={n} k={k}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The per-call table encoder this module used before the shared table.
|
||||
fn encode_pulses_reference(y: &[i32], k: usize, enc: &mut RangeEncoder) {
|
||||
let n = y.len();
|
||||
let w = k + 1;
|
||||
let mut v = vec![0u64; (n + 1) * w];
|
||||
v[0] = 1;
|
||||
for nn in 1..=n {
|
||||
v[nn * w] = 1;
|
||||
for kk in 1..=k {
|
||||
let a = v[(nn - 1) * w + kk];
|
||||
let b = v[nn * w + kk - 1];
|
||||
let c = v[(nn - 1) * w + kk - 1];
|
||||
v[nn * w + kk] = a.saturating_add(b).saturating_add(c);
|
||||
}
|
||||
}
|
||||
let total = v[n * w + k];
|
||||
let vv = |nn: usize, kx: usize| v[nn * w + kx];
|
||||
let mut index = 0u64;
|
||||
let mut kk = k;
|
||||
for j in 0..n {
|
||||
if kk == 0 {
|
||||
break;
|
||||
}
|
||||
let rem = n - j;
|
||||
let t = y[j].unsigned_abs() as usize;
|
||||
let mut base: u64 = (0..kk - t).map(|m| vv(rem - 1, m)).sum();
|
||||
if y[j] < 0 {
|
||||
base += (vv(rem - 1, kk) + vv(rem, kk)) / 2;
|
||||
}
|
||||
index += base;
|
||||
kk -= t;
|
||||
}
|
||||
if total >= 2 {
|
||||
enc.uint(index as u32, total as u32);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pvq_table_encode_matches_per_call_table() {
|
||||
let mut seed = 0x9E37_79B9_7F4A_7C15u64;
|
||||
let mut next = move |m: u64| {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 7;
|
||||
seed ^= seed << 17;
|
||||
seed % m
|
||||
};
|
||||
let mut checked = 0;
|
||||
for n in (1..=PVQ_MAX_N).step_by(5).chain([PVQ_MAX_N]) {
|
||||
for k in (1..=PVQ_MAX_K).step_by(3).chain([PVQ_MAX_K]) {
|
||||
if crate::opus::encoder::rate::pvq_v(n, k) > u32::MAX as u128 {
|
||||
continue;
|
||||
}
|
||||
for _ in 0..4 {
|
||||
let mut y = vec![0i32; n];
|
||||
for _ in 0..k {
|
||||
let at = next(n as u64) as usize;
|
||||
y[at] += if next(2) == 0 { 1 } else { -1 };
|
||||
}
|
||||
// Opposite pulses cancel; top up so that sum |y| = k again.
|
||||
let mut have: usize = y.iter().map(|v| v.unsigned_abs() as usize).sum();
|
||||
while have < k {
|
||||
let at = next(n as u64) as usize;
|
||||
y[at] += if y[at] < 0 { -1 } else { 1 };
|
||||
have += 1;
|
||||
}
|
||||
let (mut a, mut b) = (RangeEncoder::new(), RangeEncoder::new());
|
||||
encode_pulses(&y, k, &mut a);
|
||||
encode_pulses_reference(&y, k, &mut b);
|
||||
assert_eq!(a.done(16), b.done(16), "n={n} k={k}");
|
||||
checked += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(checked > 100, "{checked}");
|
||||
}
|
||||
|
||||
/// The scalar greedy search the vectorised one replaced.
|
||||
fn pvq_search_ref(x: &[f32], k: usize, y: &mut [i32]) {
|
||||
let n = x.len();
|
||||
y[..n].fill(0);
|
||||
let abs: Vec<f32> = x.iter().map(|v| v.abs()).collect();
|
||||
let sum: f32 = abs.iter().sum();
|
||||
if !sum.is_finite() || sum <= 1e-15 {
|
||||
y[0] = k as i32;
|
||||
return;
|
||||
}
|
||||
let mut placed = 0usize;
|
||||
let mut xy = 0f32;
|
||||
let mut yy = 0f32;
|
||||
if k > n / 2 {
|
||||
let r = (k as f32 - 1.0) / sum;
|
||||
for j in 0..n {
|
||||
let p = (abs[j] * r).floor() as i32;
|
||||
y[j] = p;
|
||||
placed += p as usize;
|
||||
xy += abs[j] * p as f32;
|
||||
yy += (p * p) as f32;
|
||||
}
|
||||
}
|
||||
while placed < k {
|
||||
let mut best = 0usize;
|
||||
let mut best_num = -1f32;
|
||||
let mut best_den = 1f32;
|
||||
for j in 0..n {
|
||||
let num = xy + abs[j];
|
||||
let num = num * num;
|
||||
let den = yy + 2.0 * y[j] as f32 + 1.0;
|
||||
if num * best_den > best_num * den {
|
||||
best_num = num;
|
||||
best_den = den;
|
||||
best = j;
|
||||
}
|
||||
}
|
||||
xy += abs[best];
|
||||
yy += 2.0 * y[best] as f32 + 1.0;
|
||||
y[best] += 1;
|
||||
placed += 1;
|
||||
}
|
||||
for j in 0..n {
|
||||
if x[j] < 0.0 {
|
||||
y[j] = -y[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pvq_search_matches_scalar_reference() {
|
||||
let mut state = 0x9e3779b97f4a7c15u64;
|
||||
let mut rnd = move || {
|
||||
state ^= state << 13;
|
||||
state ^= state >> 7;
|
||||
state ^= state << 17;
|
||||
(state >> 40) as f32 / (1u64 << 24) as f32 * 2.0 - 1.0
|
||||
};
|
||||
let mut a = vec![0i32; 200];
|
||||
let mut b = vec![0i32; 200];
|
||||
let mut cases = 0;
|
||||
for round in 0..6000 {
|
||||
let n = 1 + (round * 37 + (rnd().abs() * 176.0) as usize) % 176;
|
||||
let k = 1 + (rnd().abs() * 127.0) as usize;
|
||||
let scale = [1.0f32, 1e-9, 1e6, 3e-3][round % 4];
|
||||
let x: Vec<f32> = (0..n)
|
||||
.map(|_| match round % 6 {
|
||||
0 => rnd(),
|
||||
1 => if rnd() > 0.8 { rnd() } else { 0.0 },
|
||||
2 => (rnd() * 4.0).round() / 4.0,
|
||||
3 => if rnd() > 0.0 { 1.0 } else { -1.0 },
|
||||
4 => rnd().powi(5),
|
||||
_ => if rnd() > 0.95 { 3.0 * rnd() } else { 1e-4 * rnd() },
|
||||
} * scale)
|
||||
.collect();
|
||||
pvq_search(&x, k, &mut a);
|
||||
pvq_search_ref(&x, k, &mut b);
|
||||
assert_eq!(a[..n], b[..n], "n={n} k={k} round={round}");
|
||||
cases += 1;
|
||||
}
|
||||
assert_eq!(cases, 6000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pvq_search_finds_good_direction() {
|
||||
let x: Vec<f32> = (0..32).map(|i| ((i as f32) * 0.7).sin()).collect();
|
||||
let mut y = vec![0i32; 32];
|
||||
for k in [1usize, 4, 16, 64] {
|
||||
pvq_search(&x, k, &mut y);
|
||||
assert_eq!(y.iter().map(|v| v.unsigned_abs() as usize).sum::<usize>(), k);
|
||||
let xy: f32 = x.iter().zip(&y).map(|(a, &b)| a * b as f32).sum();
|
||||
let yy: f32 = y.iter().map(|&b| (b * b) as f32).sum();
|
||||
let xx: f32 = x.iter().map(|a| a * a).sum();
|
||||
let corr = xy / (xx * yy).sqrt();
|
||||
assert!(corr > if k >= 16 { 0.9 } else { 0.2 }, "k={k} corr={corr}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
//! Band energy quantisation: Laplace-coded coarse energy with inter/intra prediction, fine
|
||||
//! energy and the final refinement bits (RFC 6716 §4.3.2, encoder direction).
|
||||
//!
|
||||
//! The quantised energies are updated with exactly the decoder's arithmetic so the prediction
|
||||
//! state never drifts between encoder and decoder.
|
||||
|
||||
use crate::opus::encoder::CeltFrame;
|
||||
use crate::opus::encoder::range::RangeEncoder;
|
||||
use crate::opus::encoder::rate::{Allocation, MAX_FINE_BITS};
|
||||
use crate::opus::encoder::tables::{BETA_COEF, BETA_INTRA, E_PROB_MODEL, NB_EBANDS, PRED_COEF, SMALL_ENERGY_ICDF};
|
||||
|
||||
const LAPLACE_MINP: u32 = 1;
|
||||
const LAPLACE_NMIN: u32 = 16;
|
||||
|
||||
fn laplace_get_freq1(fs0: u32, decay: u32) -> u32 {
|
||||
let ft = 32768 - LAPLACE_MINP * (2 * LAPLACE_NMIN) - fs0;
|
||||
(ft * (16384 - decay)) >> 15
|
||||
}
|
||||
|
||||
/// Encodes a Laplace-distributed integer (`fs` = probability of 0 in Q15, `decay` in Q14).
|
||||
/// Returns the value actually coded (very large magnitudes are clamped to the representable
|
||||
/// range).
|
||||
pub fn laplace_encode(enc: &mut RangeEncoder, value: i32, mut fs: u32, decay: u32) -> i32 {
|
||||
let mut fl = 0u32;
|
||||
let mut coded = value;
|
||||
if value != 0 {
|
||||
let s: i32 = if value < 0 { -1 } else { 0 };
|
||||
let val = value.abs();
|
||||
fl = fs;
|
||||
fs = laplace_get_freq1(fs, decay);
|
||||
let mut i = 1;
|
||||
while fs > 0 && i < val {
|
||||
fs *= 2;
|
||||
fl += fs + 2 * LAPLACE_MINP;
|
||||
fs = (fs * decay) >> 15;
|
||||
i += 1;
|
||||
}
|
||||
if fs == 0 {
|
||||
let ndi_max = (32768 - fl + LAPLACE_MINP - 1) as i32;
|
||||
let ndi_max = (ndi_max - s) >> 1;
|
||||
let di = (val - i).min(ndi_max - 1);
|
||||
fl = (fl as i32 + (2 * di + 1 + s) * LAPLACE_MINP as i32) as u32;
|
||||
fs = LAPLACE_MINP.min(32768 - fl);
|
||||
let mag = i + di;
|
||||
coded = if s < 0 { -mag } else { mag };
|
||||
} else {
|
||||
fs += LAPLACE_MINP;
|
||||
if s == 0 {
|
||||
fl += fs;
|
||||
}
|
||||
}
|
||||
}
|
||||
enc.encode_bin(fl, fl + fs, 15);
|
||||
coded
|
||||
}
|
||||
|
||||
/// `quant_coarse_energy` for one prediction mode. `e` holds the target log2 energies (relative
|
||||
/// to `E_MEANS`), `old_e` the quantised energies, updated in place. `budget` is the frame size in
|
||||
/// bits.
|
||||
pub fn quant_coarse_energy(
|
||||
frame: CeltFrame,
|
||||
e: &[f32; 2 * NB_EBANDS],
|
||||
old_e: &mut [f32; 2 * NB_EBANDS],
|
||||
intra: bool,
|
||||
enc: &mut RangeEncoder,
|
||||
budget: i32,
|
||||
) {
|
||||
let CeltFrame { start, end, channels: c, lm } = frame;
|
||||
let prob = &E_PROB_MODEL[lm][intra as usize];
|
||||
let (coef, beta) = if intra { (0.0, BETA_INTRA) } else { (PRED_COEF[lm], BETA_COEF[lm]) };
|
||||
let mut prev = [0f32; 2];
|
||||
for i in start..end {
|
||||
for ch in 0..c {
|
||||
let idx = i + ch * NB_EBANDS;
|
||||
let oldc = old_e[idx].max(-9.0);
|
||||
let x = e[idx];
|
||||
let f = x - coef * oldc - prev[ch];
|
||||
let mut qi = (f + 0.5).floor().clamp(-64.0, 64.0) as i32;
|
||||
let tell = enc.tell();
|
||||
// Keep a few bits per remaining band in reserve when the budget runs out.
|
||||
let bits_left = budget - tell - 3 * c as i32 * (end - i) as i32;
|
||||
if i != start && bits_left < 30 {
|
||||
if bits_left < 24 {
|
||||
qi = qi.min(1);
|
||||
}
|
||||
if bits_left < 16 {
|
||||
qi = qi.max(-1);
|
||||
}
|
||||
}
|
||||
if budget - tell >= 15 {
|
||||
let pi = 2 * i.min(20);
|
||||
qi = laplace_encode(enc, qi, (prob[pi] as u32) << 7, (prob[pi + 1] as u32) << 6);
|
||||
} else if budget - tell >= 2 {
|
||||
qi = qi.clamp(-1, 1);
|
||||
enc.icdf(if qi < 0 { (-2 * qi - 1) as usize } else { (2 * qi) as usize }, &SMALL_ENERGY_ICDF, 2);
|
||||
} else if budget - tell >= 1 {
|
||||
qi = qi.clamp(-1, 0);
|
||||
enc.bit_logp(qi == -1, 1);
|
||||
} else {
|
||||
qi = -1;
|
||||
}
|
||||
let q = qi as f32;
|
||||
old_e[idx] = oldc;
|
||||
let tmp = coef * old_e[idx] + prev[ch] + q;
|
||||
old_e[idx] = tmp;
|
||||
prev[ch] = prev[ch] + q - beta * q;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `quant_fine_energy`: raw bits refining each band's quantised energy.
|
||||
pub fn quant_fine_energy(
|
||||
frame: CeltFrame,
|
||||
e: &[f32; 2 * NB_EBANDS],
|
||||
old_e: &mut [f32; 2 * NB_EBANDS],
|
||||
fine_quant: &[i32; NB_EBANDS],
|
||||
enc: &mut RangeEncoder,
|
||||
) {
|
||||
let CeltFrame { start, end, channels: c, .. } = frame;
|
||||
for i in start..end {
|
||||
let fq = fine_quant[i];
|
||||
if fq <= 0 {
|
||||
continue;
|
||||
}
|
||||
for ch in 0..c {
|
||||
let idx = i + ch * NB_EBANDS;
|
||||
let err = e[idx] - old_e[idx];
|
||||
let levels = 1i32 << fq;
|
||||
let q2 = (((err + 0.5) * levels as f32).floor() as i32).clamp(0, levels - 1);
|
||||
enc.bits(q2 as u32, fq as u32);
|
||||
let offset = (q2 as f32 + 0.5) * (1 << (14 - fq)) as f32 / 16384.0 - 0.5;
|
||||
old_e[idx] += offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `quant_energy_finalise`: spends the bits left at the end of the frame on one more bit of
|
||||
/// energy resolution per band, in priority order.
|
||||
pub fn quant_energy_finalise(
|
||||
frame: CeltFrame,
|
||||
e: &[f32; 2 * NB_EBANDS],
|
||||
old_e: &mut [f32; 2 * NB_EBANDS],
|
||||
allocation: &Allocation,
|
||||
mut bits_left: i32,
|
||||
enc: &mut RangeEncoder,
|
||||
) {
|
||||
let CeltFrame { start, end, channels: c, .. } = frame;
|
||||
let Allocation { fine_quant, fine_priority, .. } = allocation;
|
||||
for prio in 0..2 {
|
||||
let mut i = start;
|
||||
while i < end && bits_left >= c as i32 {
|
||||
if fine_quant[i] >= MAX_FINE_BITS || fine_priority[i] != prio {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
for ch in 0..c {
|
||||
let idx = i + ch * NB_EBANDS;
|
||||
let q2 = (e[idx] - old_e[idx] >= 0.0) as u32;
|
||||
enc.bits(q2, 1);
|
||||
let offset = (q2 as f32 - 0.5) * (1 << (14 - fine_quant[i] - 1)) as f32 / 16384.0;
|
||||
old_e[idx] += offset;
|
||||
bits_left -= 1;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::opus::encoder::range::dec::RangeDecoder;
|
||||
|
||||
/// Laplace decoder of RFC 6716 §4.3.2.1 (as in FilmCraft's decoder).
|
||||
fn laplace_decode(dec: &mut RangeDecoder, mut fs: u32, decay: u32) -> i32 {
|
||||
let mut val = 0i32;
|
||||
let fm = dec.decode(32768);
|
||||
let mut fl = 0u32;
|
||||
if fm >= fs {
|
||||
val += 1;
|
||||
fl = fs;
|
||||
fs = laplace_get_freq1(fs, decay) + LAPLACE_MINP;
|
||||
while fs > LAPLACE_MINP && fm >= fl + 2 * fs {
|
||||
fs *= 2;
|
||||
fl += fs;
|
||||
fs = (((fs - 2 * LAPLACE_MINP) * decay) >> 15) + LAPLACE_MINP;
|
||||
val += 1;
|
||||
}
|
||||
if fs <= LAPLACE_MINP {
|
||||
let di = (fm - fl) >> 1;
|
||||
val += di as i32;
|
||||
fl += 2 * di * LAPLACE_MINP;
|
||||
}
|
||||
if fm < fl + fs {
|
||||
val = -val;
|
||||
} else {
|
||||
fl += fs;
|
||||
}
|
||||
}
|
||||
dec.update(fl, (fl + fs).min(32768), 32768);
|
||||
val
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn laplace_roundtrip_all_models() {
|
||||
for lm in 0..4 {
|
||||
for intra in 0..2 {
|
||||
for band in 0..21 {
|
||||
let prob = &E_PROB_MODEL[lm][intra];
|
||||
let (fs, decay) = ((prob[2 * band] as u32) << 7, (prob[2 * band + 1] as u32) << 6);
|
||||
let vals: Vec<i32> = (-64..=64).collect();
|
||||
let mut e = RangeEncoder::new();
|
||||
let coded: Vec<i32> = vals.iter().map(|&v| laplace_encode(&mut e, v, fs, decay)).collect();
|
||||
let size = (e.tell() as usize).div_ceil(8);
|
||||
let buf = e.done(size);
|
||||
let mut d = RangeDecoder::new(&buf);
|
||||
for (&v, &c) in vals.iter().zip(&coded) {
|
||||
assert!(c == v || (c.signum() == v.signum() && c.abs() < v.abs()), "clamp {v} -> {c}");
|
||||
assert_eq!(laplace_decode(&mut d, fs, decay), c, "lm={lm} intra={intra} band={band} v={v}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
//! Forward MDCT with the CELT low-overlap window (RFC 6716 §4.3.7, run in the analysis
|
||||
//! direction).
|
||||
//!
|
||||
//! A 20 ms frame is a 1920-point MDCT (960 coefficients) whose window is zero for 420 samples,
|
||||
//! rises over the 120-sample overlap, is flat for 840 samples, falls over the next 120 samples
|
||||
//! and is zero again for the last 420. The input block is therefore the 120 overlap samples kept
|
||||
//! from the previous frame followed by the 960 new samples. The MDCT is folded into a DCT-IV,
|
||||
//! which is computed with an N/2-point complex FFT.
|
||||
|
||||
use crate::opus::encoder::tables::OVERLAP;
|
||||
|
||||
use crate::opus::fft::{Cpx, Fft};
|
||||
|
||||
/// Forward MDCT producing `n` coefficients from a block of `n + OVERLAP` samples.
|
||||
pub struct Mdct {
|
||||
/// Number of coefficients (half the MDCT length).
|
||||
n: usize,
|
||||
fft: Fft,
|
||||
pre: Vec<Cpx>,
|
||||
post: Vec<Cpx>,
|
||||
/// Rising half of the low-overlap window (RFC 6716 §4.3.7).
|
||||
pub window: [f32; OVERLAP],
|
||||
scale: f32,
|
||||
z: Vec<Cpx>,
|
||||
scratch: Vec<Cpx>,
|
||||
v: Vec<f32>,
|
||||
}
|
||||
|
||||
impl Mdct {
|
||||
pub fn new(n: usize) -> Mdct {
|
||||
let n2 = n / 2;
|
||||
let pi = std::f64::consts::PI;
|
||||
let pre = (0..n2)
|
||||
.map(|m| {
|
||||
let ph = -pi * m as f64 / n as f64;
|
||||
Cpx { re: ph.cos() as f32, im: ph.sin() as f32 }
|
||||
})
|
||||
.collect();
|
||||
let post = (0..n2)
|
||||
.map(|p| {
|
||||
let ph = -pi * (p as f64 + 0.25) / n as f64;
|
||||
Cpx { re: ph.cos() as f32, im: ph.sin() as f32 }
|
||||
})
|
||||
.collect();
|
||||
let mut window = [0f32; OVERLAP];
|
||||
for (i, w) in window.iter_mut().enumerate() {
|
||||
let x = std::f64::consts::FRAC_PI_2 * (i as f64 + 0.5) / OVERLAP as f64;
|
||||
let s = x.sin();
|
||||
*w = (std::f64::consts::FRAC_PI_2 * s * s).sin() as f32;
|
||||
}
|
||||
Mdct { n, fft: Fft::new(n2), pre, post, window, scale: 2.0 / n as f32, z: Vec::new(), scratch: Vec::new(), v: vec![0.0; n] }
|
||||
}
|
||||
|
||||
/// Transforms `block` (`n + OVERLAP` samples: the previous frame's last `OVERLAP` samples, then
|
||||
/// `n` new ones) into `out[..n]`, scaled so that the RFC 6716 decoder's inverse MDCT and
|
||||
/// overlap-add reproduce the input.
|
||||
pub fn forward(&mut self, block: &[f32], out: &mut [f32]) {
|
||||
let n = self.n;
|
||||
let n2 = n / 2;
|
||||
debug_assert_eq!(block.len(), n + OVERLAP);
|
||||
// Place the windowed block into the (virtual) 2n-sample MDCT input u at offset `pad`.
|
||||
let pad = (n - OVERLAP) / 2;
|
||||
let w = &self.window;
|
||||
let u = |t: usize| -> f32 {
|
||||
if t < pad || t >= pad + n + OVERLAP {
|
||||
return 0.0;
|
||||
}
|
||||
let j = t - pad;
|
||||
let g = if j < OVERLAP {
|
||||
w[j]
|
||||
} else if j >= n {
|
||||
w[OVERLAP - 1 - (j - n)]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
block[j] * g
|
||||
};
|
||||
// Fold to a DCT-IV input: v = (-c_r - d, a - b_r) over the four quarters of u.
|
||||
for m in 0..n2 {
|
||||
self.v[m] = -u(n + n2 - 1 - m) - u(n + n2 + m);
|
||||
self.v[n2 + m] = u(m) - u(n2 + n2 - 1 - m);
|
||||
}
|
||||
// DCT-IV via an n/2-point complex FFT.
|
||||
self.z.clear();
|
||||
for m in 0..n2 {
|
||||
let wv = Cpx { re: self.v[2 * m], im: self.v[n - 1 - 2 * m] };
|
||||
self.z.push(wv.mul(self.pre[m]));
|
||||
}
|
||||
self.fft.process(&mut self.z, &mut self.scratch);
|
||||
for p in 0..n2 {
|
||||
let y = self.z[p].mul(self.post[p]);
|
||||
out[2 * p] = y.re * self.scale;
|
||||
out[n - 1 - 2 * p] = -y.im * self.scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Direct MDCT definition X[k] = sum u[t] cos(pi/N (t + 1/2 + N/2)(k + 1/2)).
|
||||
fn direct(block: &[f32], n: usize, window: &[f32; OVERLAP]) -> Vec<f64> {
|
||||
let pad = (n - OVERLAP) / 2;
|
||||
let mut u = vec![0f64; 2 * n];
|
||||
for (j, &b) in block.iter().enumerate() {
|
||||
let g = if j < OVERLAP {
|
||||
window[j]
|
||||
} else if j >= n {
|
||||
window[OVERLAP - 1 - (j - n)]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
u[pad + j] = (b * g) as f64;
|
||||
}
|
||||
(0..n)
|
||||
.map(|k| {
|
||||
(0..2 * n).map(|t| u[t] * (std::f64::consts::PI / n as f64 * (t as f64 + 0.5 + n as f64 / 2.0) * (k as f64 + 0.5)).cos()).sum::<f64>()
|
||||
* 2.0
|
||||
/ n as f64
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fft_matches_dft() {
|
||||
// Every radix combination, including the 480-point size of the 20 ms frame.
|
||||
for &n in &[2usize, 3, 4, 5, 6, 8, 12, 15, 16, 20, 24, 30, 48, 60, 120, 240, 480] {
|
||||
let fft = Fft::new(n);
|
||||
let x: Vec<Cpx> = (0..n).map(|i| Cpx { re: ((i * 37 % 101) as f32 - 50.0) / 7.0, im: ((i * 53 % 89) as f32 - 44.0) / 9.0 }).collect();
|
||||
let mut y = x.clone();
|
||||
fft.process(&mut y, &mut Vec::new());
|
||||
for k in 0..n {
|
||||
let (mut re, mut im) = (0f64, 0f64);
|
||||
for (j, v) in x.iter().enumerate() {
|
||||
let ph = -2.0 * std::f64::consts::PI * ((j * k) % n) as f64 / n as f64;
|
||||
re += v.re as f64 * ph.cos() - v.im as f64 * ph.sin();
|
||||
im += v.re as f64 * ph.sin() + v.im as f64 * ph.cos();
|
||||
}
|
||||
assert!(
|
||||
(y[k].re as f64 - re).abs() < 2e-3 && (y[k].im as f64 - im).abs() < 2e-3,
|
||||
"n={n} k={k}: ({}, {}) vs ({re}, {im})",
|
||||
y[k].re,
|
||||
y[k].im
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matches_direct_formula() {
|
||||
for &n in &[240usize, 960] {
|
||||
let block: Vec<f32> = (0..n + OVERLAP).map(|i| ((i * 37 % 101) as f32 - 50.0) / 7.0).collect();
|
||||
let mut m = Mdct::new(n);
|
||||
let mut out = vec![0f32; n];
|
||||
m.forward(&block, &mut out);
|
||||
let d = direct(&block, n, &m.window);
|
||||
for k in 0..n {
|
||||
assert!((out[k] as f64 - d[k]).abs() < 1e-2, "n={n} k={k}: {} vs {}", out[k], d[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Replica of the RFC 6716 decoder inverse MDCT (as in FilmCraft's decoder) ----
|
||||
|
||||
fn imdct_backward(n_full: usize, input: &[f32], out: &mut [f32], window: &[f32]) {
|
||||
let n2 = n_full >> 1;
|
||||
let n4 = n_full >> 2;
|
||||
let t: Vec<f32> = (0..n2).map(|i| (2.0 * std::f64::consts::PI * (i as f64 + 0.125) / n_full as f64).cos() as f32).collect();
|
||||
let fft = Fft::new(n4);
|
||||
let mut z = Vec::new();
|
||||
for i in 0..n4 {
|
||||
let x1 = input[2 * i];
|
||||
let x2 = input[n2 - 1 - 2 * i];
|
||||
let yr = x2 * t[i] + x1 * t[n4 + i];
|
||||
let yi = x1 * t[i] - x2 * t[n4 + i];
|
||||
z.push(Cpx { re: yi, im: yr });
|
||||
}
|
||||
fft.process(&mut z, &mut Vec::new());
|
||||
let overlap = OVERLAP;
|
||||
let base = overlap >> 1;
|
||||
let y = &mut out[base..base + n2];
|
||||
for i in 0..n4.div_ceil(2) {
|
||||
let front = z[i];
|
||||
let back = z[n4 - 1 - i];
|
||||
let (re, im) = (front.im, front.re);
|
||||
let (t0, t1) = (t[i], t[n4 + i]);
|
||||
let yr0 = re * t0 + im * t1;
|
||||
let yi0 = re * t1 - im * t0;
|
||||
let (re, im) = (back.im, back.re);
|
||||
let (t0, t1) = (t[n4 - i - 1], t[n2 - i - 1]);
|
||||
let yr1 = re * t0 + im * t1;
|
||||
let yi1 = re * t1 - im * t0;
|
||||
y[2 * i] = yr0;
|
||||
y[n2 - 1 - 2 * i] = yi0;
|
||||
y[n2 - 2 - 2 * i] = yr1;
|
||||
y[2 * i + 1] = yi1;
|
||||
}
|
||||
for i in 0..overlap / 2 {
|
||||
let x1 = out[overlap - 1 - i];
|
||||
let x2 = out[i];
|
||||
let w1 = window[i];
|
||||
let w2 = window[overlap - 1 - i];
|
||||
out[i] = x2 * w2 - x1 * w1;
|
||||
out[overlap - 1 - i] = x2 * w1 + x1 * w2;
|
||||
}
|
||||
}
|
||||
|
||||
/// MDCT analysis followed by the decoder's IMDCT + TDAC overlap-add reproduces the input
|
||||
/// delayed by exactly `OVERLAP` samples.
|
||||
#[test]
|
||||
fn perfect_reconstruction_through_decoder_imdct() {
|
||||
let n = 960;
|
||||
let frames = 5;
|
||||
let sig: Vec<f32> = (0..n * frames).map(|i| (i as f32 * 0.013).sin() * 1000.0 + ((i * 7919) % 211) as f32 - 105.0).collect();
|
||||
let mut m = Mdct::new(n);
|
||||
let mut hist = vec![0f32; OVERLAP];
|
||||
// Decoder memory: [frame output n][folded tail OVERLAP/2] laid out like decode_mem.
|
||||
let mut mem = vec![0f32; n + OVERLAP];
|
||||
let mut decoded = Vec::new();
|
||||
for f in 0..frames {
|
||||
let mut block = hist.clone();
|
||||
block.extend_from_slice(&sig[f * n..(f + 1) * n]);
|
||||
hist.copy_from_slice(&block[n..]);
|
||||
let mut coefs = vec![0f32; n];
|
||||
m.forward(&block, &mut coefs);
|
||||
// Shift: the previous folded tail (at n..n+OVERLAP/2) moves to 0..OVERLAP/2.
|
||||
mem.copy_within(n..n + OVERLAP / 2, 0);
|
||||
imdct_backward(2 * n, &coefs, &mut mem, &m.window);
|
||||
decoded.extend_from_slice(&mem[..n]);
|
||||
}
|
||||
let mut err = 0f64;
|
||||
let mut sum = 0f64;
|
||||
for t in OVERLAP..n * frames {
|
||||
let d = decoded[t] as f64 - sig[t - OVERLAP] as f64;
|
||||
err += d * d;
|
||||
sum += (sig[t - OVERLAP] as f64).powi(2);
|
||||
}
|
||||
let snr = 10.0 * (sum / err).log10();
|
||||
assert!(snr > 80.0, "reconstruction SNR {snr:.1} dB");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,596 @@
|
||||
//! Clean-room pure-Rust Opus encoder (RFC 6716 SILK, hybrid and CELT modes; RFC 7845 `OpusHead`).
|
||||
//!
|
||||
//! Produces 20 ms Opus packets from 48 kHz float input, mono or stereo. The coding mode and
|
||||
//! audio bandwidth are chosen once per stream from the per-channel bitrate and the
|
||||
//! [`Application`] (see the crate README for the table):
|
||||
//!
|
||||
//! - **SILK** (narrowband/mediumband/wideband): linear-predictive speech coding at an internal
|
||||
//! rate of 8/12/16 kHz; packet sizes vary around the target bitrate.
|
||||
//! - **Hybrid** (super-wideband/fullband): SILK wideband codes 0–8 kHz and CELT codes the bands
|
||||
//! above in the same range-coded frame; constant packet size.
|
||||
//! - **CELT** (fullband): MDCT transform coding; constant packet size.
|
||||
//!
|
||||
//! From EffectCraft's `effectcraft-opusenc` (storytold/effectcraft@c618429), with its later speed-ups
|
||||
//! and CELT stage refactor up to storytold/effectcraft@d6c9ad66. Public items keep their names, so
|
||||
//! EffectCraft can re-export this module.
|
||||
//!
|
||||
//! ```
|
||||
//! # #[cfg(feature = "opus-encode")] {
|
||||
//! use craft_audio::opus::encoder::{Application, Mode, Encoder};
|
||||
//! let mut enc = Encoder::new(2, 128_000).unwrap();
|
||||
//! let pcm = vec![0.0f32; Encoder::FRAME_SIZE * 2];
|
||||
//! let packet = enc.encode_float(&pcm).unwrap();
|
||||
//! assert_eq!(packet[0] >> 3, 31); // CELT-only, fullband, 20 ms
|
||||
//! assert_eq!(enc.opus_head().len(), 19);
|
||||
//! let voip = Encoder::with_application(1, 16_000, Application::Voip).unwrap();
|
||||
//! assert_eq!(voip.mode(), Mode::Silk);
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
// The bit-allocation code mirrors the normative integer arithmetic; keep C-like shift expressions.
|
||||
#![allow(clippy::precedence, clippy::int_plus_one)]
|
||||
|
||||
mod bands;
|
||||
mod energy;
|
||||
mod mdct;
|
||||
mod range;
|
||||
mod rate;
|
||||
mod resample;
|
||||
mod silk;
|
||||
pub(super) mod tables;
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use bands::{BandCoding, SPREAD_NORMAL, quant_all_bands};
|
||||
use energy::{quant_coarse_energy, quant_energy_finalise, quant_fine_energy};
|
||||
use mdct::Mdct;
|
||||
use range::{BITRES, RangeEncoder};
|
||||
use rate::{AllocationInput, Mode as CeltMode, compute_allocation};
|
||||
use resample::Downsampler;
|
||||
use silk::SilkEncoder;
|
||||
use tables::{E_MEANS, EBANDS, NB_EBANDS, OVERLAP, PREEMPH, SPREAD_ICDF, TF_SELECT_TABLE, TRIM_ICDF};
|
||||
|
||||
/// Band range, channel count and transform size shared by CELT coding stages.
|
||||
#[derive(Clone, Copy)]
|
||||
struct CeltFrame {
|
||||
start: usize,
|
||||
end: usize,
|
||||
channels: usize,
|
||||
lm: usize,
|
||||
}
|
||||
|
||||
/// Samples per channel in one 20 ms frame at 48 kHz.
|
||||
const N: usize = 960;
|
||||
/// LM of a 20 ms frame (8 short blocks).
|
||||
const LM: usize = 3;
|
||||
/// Smallest CELT payload the encoder produces (bytes).
|
||||
const MIN_FRAME_BYTES: usize = 16;
|
||||
/// Largest payload allowed in one frame (RFC 6716 §3.4, R2).
|
||||
const MAX_FRAME_BYTES: usize = 1275;
|
||||
/// First CELT band coded in hybrid frames (8 kHz).
|
||||
const HYBRID_START_BAND: usize = 17;
|
||||
|
||||
fn celt_mode() -> &'static CeltMode {
|
||||
static MODE: OnceLock<CeltMode> = OnceLock::new();
|
||||
MODE.get_or_init(CeltMode::new)
|
||||
}
|
||||
|
||||
/// Why an encoder could not be created or a frame could not be encoded.
|
||||
#[non_exhaustive]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
/// Opus family-0 streams have 1 or 2 channels.
|
||||
#[error("Opus family-0 streams have 1 or 2 channels, not {0}")]
|
||||
Channels(u8),
|
||||
/// The mode cannot code that bandwidth.
|
||||
#[error("unsupported mode/bandwidth combination {0:?}/{1:?}")]
|
||||
ModeBandwidth(Mode, Bandwidth),
|
||||
/// `encode_float` takes exactly `FRAME_SIZE * channels` samples.
|
||||
#[error("a frame is {expected} interleaved samples, not {got}")]
|
||||
FrameLength {
|
||||
/// Interleaved samples a frame takes (`FRAME_SIZE * channels`).
|
||||
expected: usize,
|
||||
/// Interleaved samples passed.
|
||||
got: usize,
|
||||
},
|
||||
}
|
||||
|
||||
/// Intended use of the stream; selects the mode/bandwidth table.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub enum Application {
|
||||
/// General audio (music, mixed content): CELT unless the bitrate is low.
|
||||
#[default]
|
||||
Audio,
|
||||
/// Speech: SILK and hybrid over a wider bitrate range, narrower bandwidths at low rates.
|
||||
Voip,
|
||||
}
|
||||
|
||||
/// Opus coding mode (RFC 6716 §2).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum Mode {
|
||||
/// MDCT transform coding only.
|
||||
Celt,
|
||||
/// Linear-prediction coding only.
|
||||
Silk,
|
||||
/// SILK below 8 kHz plus CELT above.
|
||||
Hybrid,
|
||||
}
|
||||
|
||||
pub use crate::opus::Bandwidth;
|
||||
|
||||
/// Mode and bandwidth for a per-channel bitrate (bits per second per channel).
|
||||
fn choose_mode(app: Application, per_channel: u32) -> (Mode, Bandwidth) {
|
||||
match app {
|
||||
Application::Voip => match per_channel {
|
||||
0..10_000 => (Mode::Silk, Bandwidth::Narrowband),
|
||||
10_000..12_000 => (Mode::Silk, Bandwidth::Mediumband),
|
||||
12_000..18_000 => (Mode::Silk, Bandwidth::Wideband),
|
||||
18_000..28_000 => (Mode::Hybrid, Bandwidth::SuperWideband),
|
||||
28_000..40_000 => (Mode::Hybrid, Bandwidth::Fullband),
|
||||
_ => (Mode::Celt, Bandwidth::Fullband),
|
||||
},
|
||||
Application::Audio => match per_channel {
|
||||
0..16_000 => (Mode::Silk, Bandwidth::Wideband),
|
||||
16_000..36_000 => (Mode::Hybrid, Bandwidth::Fullband),
|
||||
_ => (Mode::Celt, Bandwidth::Fullband),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// SILK side of a SILK-only or hybrid stream.
|
||||
struct SilkPath {
|
||||
enc: SilkEncoder,
|
||||
down: Vec<Downsampler>,
|
||||
/// Bits per frame aimed at (excluding the TOC byte in SILK-only mode).
|
||||
target_bits: f64,
|
||||
/// Bits saved (positive) or overspent by previous frames (SILK-only mode).
|
||||
reservoir: f64,
|
||||
}
|
||||
|
||||
/// The name EffectCraft's `effectcraft-opusenc` used, kept so it can re-export this module.
|
||||
#[doc(hidden)]
|
||||
pub type OpusEncoder = Encoder;
|
||||
|
||||
/// Opus encoder for one mono or stereo stream at 48 kHz.
|
||||
pub struct Encoder {
|
||||
channels: usize,
|
||||
application: Application,
|
||||
mode: Mode,
|
||||
bandwidth: Bandwidth,
|
||||
/// Requested bitrate (clamped), bits per second.
|
||||
bitrate_bps: u32,
|
||||
/// Payload bytes per packet in CELT and hybrid modes (the packet is one byte longer: the
|
||||
/// TOC).
|
||||
frame_bytes: usize,
|
||||
mdct: Mdct,
|
||||
/// Last input sample of each channel (pre-emphasis filter memory, ×32768).
|
||||
preemph_mem: [f32; 2],
|
||||
/// Last `OVERLAP` pre-emphasised samples of each channel.
|
||||
hist: Vec<f32>,
|
||||
/// Quantised band energies, exactly as the decoder holds them.
|
||||
old_band_e: [f32; 2 * NB_EBANDS],
|
||||
block: Vec<f32>,
|
||||
freq: Vec<f32>,
|
||||
silk: Option<SilkPath>,
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
/// Samples per channel per packet (20 ms at 48 kHz).
|
||||
pub const FRAME_SIZE: usize = N;
|
||||
|
||||
/// Creates a general-audio encoder ([`Application::Audio`]) for `channels` (1 or 2)
|
||||
/// channels of 48 kHz audio at `bitrate_bps` (total for all channels).
|
||||
pub fn new(channels: u8, bitrate_bps: u32) -> Result<Self, Error> {
|
||||
Self::with_application(channels, bitrate_bps, Application::Audio)
|
||||
}
|
||||
|
||||
/// Creates an encoder for `channels` (1 or 2) channels of 48 kHz audio at `bitrate_bps`
|
||||
/// (total for all channels; clamped to 6 kb/s per channel – 510 kb/s), choosing the mode
|
||||
/// and bandwidth for `application`.
|
||||
pub fn with_application(channels: u8, bitrate_bps: u32, application: Application) -> Result<Self, Error> {
|
||||
// Checked before the clamp below, whose lower bound passes its upper one from 86 channels.
|
||||
if channels != 1 && channels != 2 {
|
||||
return Err(Error::Channels(channels));
|
||||
}
|
||||
let c = channels as u32;
|
||||
let (mode, bandwidth) = choose_mode(application, bitrate_bps.clamp(6_000 * c, 510_000) / c);
|
||||
Self::build(channels, bitrate_bps, application, mode, bandwidth)
|
||||
}
|
||||
|
||||
/// Creates an encoder with an explicit mode and bandwidth instead of the application's
|
||||
/// table. Valid combinations: SILK with narrowband, mediumband or wideband; hybrid with
|
||||
/// super-wideband or fullband; CELT with fullband.
|
||||
///
|
||||
/// Any other combination is [`Error::ModeBandwidth`]; `channels` other than 1 or 2 is
|
||||
/// [`Error::Channels`].
|
||||
pub fn with_mode(channels: u8, bitrate_bps: u32, mode: Mode, bandwidth: Bandwidth) -> Result<Self, Error> {
|
||||
let ok = match mode {
|
||||
Mode::Silk => bandwidth <= Bandwidth::Wideband,
|
||||
Mode::Hybrid => bandwidth >= Bandwidth::SuperWideband,
|
||||
Mode::Celt => bandwidth == Bandwidth::Fullband,
|
||||
};
|
||||
if !ok {
|
||||
return Err(Error::ModeBandwidth(mode, bandwidth));
|
||||
}
|
||||
Self::build(channels, bitrate_bps, Application::Audio, mode, bandwidth)
|
||||
}
|
||||
|
||||
fn build(channels: u8, bitrate_bps: u32, application: Application, mode: Mode, bandwidth: Bandwidth) -> Result<Self, Error> {
|
||||
if channels != 1 && channels != 2 {
|
||||
return Err(Error::Channels(channels));
|
||||
}
|
||||
let c = channels as usize;
|
||||
let bitrate_bps = bitrate_bps.clamp(6_000 * c as u32, 510_000);
|
||||
let packet_bytes = (bitrate_bps as usize).div_ceil(400);
|
||||
let frame_bytes = packet_bytes.saturating_sub(1).clamp(MIN_FRAME_BYTES, MAX_FRAME_BYTES);
|
||||
let silk = match mode {
|
||||
Mode::Celt => None,
|
||||
Mode::Silk | Mode::Hybrid => {
|
||||
let fs_khz = match bandwidth {
|
||||
Bandwidth::Narrowband => 8,
|
||||
Bandwidth::Mediumband => 12,
|
||||
_ => 16,
|
||||
};
|
||||
let target_bits = if mode == Mode::Silk {
|
||||
bitrate_bps as f64 / 50.0 - 8.0
|
||||
} else {
|
||||
// SILK gets most of a hybrid packet; CELT codes 8 kHz and up with the rest.
|
||||
let per = (bitrate_bps / c as u32) as f64;
|
||||
let share = if bandwidth == Bandwidth::SuperWideband { 0.72 } else { 0.62 };
|
||||
c as f64 * (per * share).min(per - 6_000.0).max(per * 0.5) / 50.0
|
||||
};
|
||||
Some(SilkPath {
|
||||
enc: SilkEncoder::new(fs_khz, c),
|
||||
down: (0..c).map(|_| Downsampler::new(fs_khz as u32 * 1000)).collect(),
|
||||
target_bits,
|
||||
reservoir: 0.0,
|
||||
})
|
||||
}
|
||||
};
|
||||
Ok(Encoder {
|
||||
channels: c,
|
||||
application,
|
||||
mode,
|
||||
bandwidth,
|
||||
bitrate_bps,
|
||||
frame_bytes,
|
||||
mdct: Mdct::new(N),
|
||||
preemph_mem: [0.0; 2],
|
||||
hist: vec![0.0; c * OVERLAP],
|
||||
old_band_e: [0.0; 2 * NB_EBANDS],
|
||||
block: vec![0.0; N + OVERLAP],
|
||||
freq: vec![0.0; c * N],
|
||||
silk,
|
||||
})
|
||||
}
|
||||
|
||||
/// Number of channels.
|
||||
pub fn channels(&self) -> u8 {
|
||||
self.channels as u8
|
||||
}
|
||||
|
||||
/// The application the encoder was created for.
|
||||
pub fn application(&self) -> Application {
|
||||
self.application
|
||||
}
|
||||
|
||||
/// Coding mode of every packet of the stream.
|
||||
pub fn mode(&self) -> Mode {
|
||||
self.mode
|
||||
}
|
||||
|
||||
/// Coded audio bandwidth of every packet of the stream.
|
||||
pub fn bandwidth(&self) -> Bandwidth {
|
||||
self.bandwidth
|
||||
}
|
||||
|
||||
/// Bitrate in bits per second: the constant packet size × 50 packets per second in CELT
|
||||
/// and hybrid modes, the (average) target in SILK mode.
|
||||
pub fn bitrate(&self) -> u32 {
|
||||
match self.mode {
|
||||
Mode::Silk => self.bitrate_bps,
|
||||
_ => ((self.frame_bytes + 1) * 8 * 50) as u32,
|
||||
}
|
||||
}
|
||||
|
||||
/// Samples at 48 kHz the decoder must discard from the start of the stream (the `OpusHead`
|
||||
/// pre-skip): the encoder's total delay. CELT: the 120-sample MDCT overlap. SILK/hybrid:
|
||||
/// the decimator (83 samples, 88 for narrowband) plus the decoder's SILK delay (one
|
||||
/// internal-rate sample and the resampler delay of RFC 6716 Table 54), which also rounds
|
||||
/// to 120 samples.
|
||||
pub fn pre_skip(&self) -> u16 {
|
||||
let (fs, table54_ms) = match (self.mode, self.bandwidth) {
|
||||
(Mode::Celt, _) => return OVERLAP as u16,
|
||||
(_, Bandwidth::Narrowband) => (8_000, 0.538),
|
||||
(_, Bandwidth::Mediumband) => (12_000, 0.692),
|
||||
_ => (16_000, 0.706),
|
||||
};
|
||||
(resample::delay(fs) as f64 + 48_000.0 / fs as f64 + table54_ms * 48.0).round() as u16
|
||||
}
|
||||
|
||||
/// The 19-byte `OpusHead` identification header (RFC 7845 §5.1): version 1, channel count,
|
||||
/// pre-skip, input sample rate 48000, output gain 0, channel mapping family 0.
|
||||
pub fn opus_head(&self) -> Vec<u8> {
|
||||
let mut h = Vec::with_capacity(19);
|
||||
h.extend_from_slice(b"OpusHead");
|
||||
h.push(1);
|
||||
h.push(self.channels as u8);
|
||||
h.extend_from_slice(&self.pre_skip().to_le_bytes());
|
||||
h.extend_from_slice(&48_000u32.to_le_bytes());
|
||||
h.extend_from_slice(&0i16.to_le_bytes());
|
||||
h.push(0);
|
||||
h
|
||||
}
|
||||
|
||||
/// TOC byte (RFC 6716 §3.1, Table 2) of a 20 ms single-frame packet.
|
||||
fn toc(&self) -> u8 {
|
||||
let config: u8 = match (self.mode, self.bandwidth) {
|
||||
(Mode::Silk, Bandwidth::Narrowband) => 1,
|
||||
(Mode::Silk, Bandwidth::Mediumband) => 5,
|
||||
(Mode::Silk, _) => 9,
|
||||
(Mode::Hybrid, Bandwidth::SuperWideband) => 13,
|
||||
(Mode::Hybrid, _) => 15,
|
||||
(Mode::Celt, _) => 31,
|
||||
};
|
||||
(config << 3) | if self.channels == 2 { 4 } else { 0 }
|
||||
}
|
||||
|
||||
/// Encodes one 20 ms frame of interleaved f32 samples in [-1, 1] (exactly
|
||||
/// `FRAME_SIZE * channels` values) and returns one Opus packet, or
|
||||
/// [`Error::FrameLength`] for any other length.
|
||||
pub fn encode_float(&mut self, pcm: &[f32]) -> Result<Vec<u8>, Error> {
|
||||
let c = self.channels;
|
||||
if pcm.len() != N * c {
|
||||
return Err(Error::FrameLength { expected: N * c, got: pcm.len() });
|
||||
}
|
||||
let mut packet = vec![self.toc()];
|
||||
match self.mode {
|
||||
Mode::Celt => {
|
||||
let (log_e, silence) = self.celt_analysis(pcm);
|
||||
let len = self.frame_bytes;
|
||||
packet.extend_from_slice(&self.encode_celt(RangeEncoder::new(), len, 0..NB_EBANDS, &log_e, silence));
|
||||
}
|
||||
Mode::Silk => {
|
||||
let x = self.silk_input(pcm);
|
||||
// The SILK state exists whenever the mode uses SILK (set with the mode).
|
||||
if let Some(sp) = self.silk.as_mut() {
|
||||
let target = sp.target_bits + 0.25 * sp.reservoir;
|
||||
let mut enc = RangeEncoder::new();
|
||||
sp.enc.encode(&x, target.max(24.0), (MAX_FRAME_BYTES * 8) as i32, &mut enc);
|
||||
let bytes = (enc.tell() as usize).div_ceil(8).max(2);
|
||||
let lim = 4.0 * sp.target_bits;
|
||||
sp.reservoir = (sp.reservoir + sp.target_bits - (bytes * 8) as f64).clamp(-lim, lim);
|
||||
packet.extend_from_slice(&enc.done(bytes));
|
||||
}
|
||||
}
|
||||
Mode::Hybrid => {
|
||||
let x = self.silk_input(pcm);
|
||||
let len = self.frame_bytes;
|
||||
let total = (len * 8) as i32;
|
||||
let celt_min = (total / 5).max(80);
|
||||
let mut enc = RangeEncoder::new();
|
||||
if let Some(sp) = self.silk.as_mut() {
|
||||
sp.enc.encode(&x, sp.target_bits, total - celt_min, &mut enc);
|
||||
}
|
||||
// No redundancy (RFC 6716 §4.5.1); the flag is present when there is room.
|
||||
if enc.tell() + 17 + 20 <= total {
|
||||
enc.bit_logp(false, 12);
|
||||
}
|
||||
let (log_e, _) = self.celt_analysis(pcm);
|
||||
let end = if self.bandwidth == Bandwidth::SuperWideband { 19 } else { NB_EBANDS };
|
||||
packet.extend_from_slice(&self.encode_celt(enc, len, HYBRID_START_BAND..end, &log_e, false));
|
||||
}
|
||||
}
|
||||
Ok(packet)
|
||||
}
|
||||
|
||||
/// Decimates the input to the SILK internal rate (16-bit scale), one vector per channel.
|
||||
fn silk_input(&mut self, pcm: &[f32]) -> Vec<Vec<f32>> {
|
||||
let c = self.channels;
|
||||
let Some(sp) = self.silk.as_mut() else { return vec![vec![]; c] };
|
||||
let mut out = Vec::with_capacity(c);
|
||||
let mut tmp = vec![0f32; N];
|
||||
for ch in 0..c {
|
||||
for (j, t) in tmp.iter_mut().enumerate() {
|
||||
let s = pcm[j * c + ch];
|
||||
*t = if s.is_finite() { s.clamp(-1.0, 1.0) * 32768.0 } else { 0.0 };
|
||||
}
|
||||
let mut y = Vec::with_capacity(N / 3);
|
||||
sp.down[ch].process(&tmp, &mut y);
|
||||
out.push(y);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Pre-emphasis, the MDCT, band energies (log2, relative to `E_MEANS`) and normalised
|
||||
/// bands; returns the energies and whether the frame is digital silence.
|
||||
fn celt_analysis(&mut self, pcm: &[f32]) -> ([f32; 2 * NB_EBANDS], bool) {
|
||||
let c = self.channels;
|
||||
let mut log_e = [0f32; 2 * NB_EBANDS];
|
||||
let mut peak = 0f32;
|
||||
for ch in 0..c {
|
||||
self.block[..OVERLAP].copy_from_slice(&self.hist[ch * OVERLAP..(ch + 1) * OVERLAP]);
|
||||
let mut mem = self.preemph_mem[ch];
|
||||
for j in 0..N {
|
||||
let s = pcm[j * c + ch];
|
||||
let s = if s.is_finite() { s.clamp(-2.0, 2.0) * 32768.0 } else { 0.0 };
|
||||
let v = s - PREEMPH * mem;
|
||||
mem = s;
|
||||
self.block[OVERLAP + j] = v;
|
||||
}
|
||||
self.preemph_mem[ch] = mem;
|
||||
self.hist[ch * OVERLAP..(ch + 1) * OVERLAP].copy_from_slice(&self.block[N..]);
|
||||
peak = self.block.iter().fold(peak, |p, v| p.max(v.abs()));
|
||||
let freq = &mut self.freq[ch * N..(ch + 1) * N];
|
||||
self.mdct.forward(&self.block, freq);
|
||||
for i in 0..NB_EBANDS {
|
||||
let lo = (EBANDS[i] as usize) << LM;
|
||||
let hi = (EBANDS[i + 1] as usize) << LM;
|
||||
let e = (1e-27f32 + freq[lo..hi].iter().map(|v| v * v).sum::<f32>()).sqrt();
|
||||
log_e[ch * NB_EBANDS + i] = e.log2() - E_MEANS[i];
|
||||
let g = 1.0 / e;
|
||||
for v in freq[lo..hi].iter_mut() {
|
||||
*v *= g;
|
||||
}
|
||||
}
|
||||
}
|
||||
(log_e, peak < 1e-4)
|
||||
}
|
||||
|
||||
/// Codes one CELT frame (RFC 6716 §4.3, in bitstream order) of `len` bytes into `enc`
|
||||
/// (which already holds the SILK frame in hybrid mode), coding bands `start..end`, and
|
||||
/// finalises the frame.
|
||||
fn encode_celt(
|
||||
&mut self,
|
||||
mut enc: RangeEncoder,
|
||||
len: usize,
|
||||
bands: std::ops::Range<usize>,
|
||||
log_e: &[f32; 2 * NB_EBANDS],
|
||||
silence: bool,
|
||||
) -> Vec<u8> {
|
||||
let m = celt_mode();
|
||||
let c = self.channels;
|
||||
let frame = CeltFrame { start: bands.start, end: bands.end, channels: c, lm: LM };
|
||||
let CeltFrame { start, end, .. } = frame;
|
||||
if c == 1 {
|
||||
for i in 0..NB_EBANDS {
|
||||
self.old_band_e[i] = self.old_band_e[i].max(self.old_band_e[NB_EBANDS + i]);
|
||||
}
|
||||
}
|
||||
if silence && start == 0 {
|
||||
// The silence flag; the decoder treats every remaining bit as consumed.
|
||||
enc.bit_logp(true, 15);
|
||||
self.old_band_e = [-28.0; 2 * NB_EBANDS];
|
||||
return enc.done(2);
|
||||
}
|
||||
let total_bits_raw = len as i32 * 8;
|
||||
if enc.tell() == 1 {
|
||||
enc.bit_logp(false, 15);
|
||||
}
|
||||
// No pitch post-filter (only signalled in CELT-only frames).
|
||||
if start == 0 && enc.tell() + 16 <= total_bits_raw {
|
||||
enc.bit_logp(false, 1);
|
||||
}
|
||||
// Long blocks only.
|
||||
if enc.tell() + 3 <= total_bits_raw {
|
||||
enc.bit_logp(false, 3);
|
||||
}
|
||||
// Coarse energy, choosing intra or inter prediction by trial (whichever is cheaper).
|
||||
if enc.tell() + 3 <= total_bits_raw {
|
||||
let mut enc_intra = enc.clone();
|
||||
let mut old_intra = self.old_band_e;
|
||||
enc_intra.bit_logp(true, 3);
|
||||
quant_coarse_energy(frame, log_e, &mut old_intra, true, &mut enc_intra, total_bits_raw);
|
||||
enc.bit_logp(false, 3);
|
||||
let mut old_inter = self.old_band_e;
|
||||
quant_coarse_energy(frame, log_e, &mut old_inter, false, &mut enc, total_bits_raw);
|
||||
if enc_intra.tell_frac() < enc.tell_frac() {
|
||||
enc = enc_intra;
|
||||
self.old_band_e = old_intra;
|
||||
} else {
|
||||
self.old_band_e = old_inter;
|
||||
}
|
||||
} else {
|
||||
quant_coarse_energy(frame, log_e, &mut self.old_band_e, false, &mut enc, total_bits_raw);
|
||||
}
|
||||
// TF resolution: no changes.
|
||||
{
|
||||
let mut budget = total_bits_raw;
|
||||
let mut tell = enc.tell();
|
||||
let mut logp = 4;
|
||||
let tf_select_rsv = LM > 0 && tell + logp < budget;
|
||||
budget -= tf_select_rsv as i32;
|
||||
for _ in start..end {
|
||||
if tell + logp <= budget {
|
||||
enc.bit_logp(false, logp as u32);
|
||||
tell = enc.tell();
|
||||
}
|
||||
logp = 5;
|
||||
}
|
||||
if tf_select_rsv && TF_SELECT_TABLE[LM][0] != TF_SELECT_TABLE[LM][2] {
|
||||
enc.bit_logp(false, 1);
|
||||
}
|
||||
}
|
||||
if enc.tell() + 4 <= total_bits_raw {
|
||||
enc.icdf(SPREAD_NORMAL, &SPREAD_ICDF, 5);
|
||||
}
|
||||
// Dynamic allocation: no boosts.
|
||||
let cap = m.init_caps(LM, c);
|
||||
let offsets = [0i32; NB_EBANDS];
|
||||
let total_bits = total_bits_raw << BITRES;
|
||||
let mut tellf = enc.tell_frac();
|
||||
for &cp in cap.iter().take(end).skip(start) {
|
||||
if tellf + (6 << BITRES) < total_bits && 0 < cp {
|
||||
enc.bit_logp(false, 6);
|
||||
tellf = enc.tell_frac();
|
||||
}
|
||||
}
|
||||
let alloc_trim = 5;
|
||||
if tellf + (6 << BITRES) <= total_bits {
|
||||
enc.icdf(alloc_trim as usize, &TRIM_ICDF, 7);
|
||||
}
|
||||
let bits = (total_bits_raw << BITRES) - enc.tell_frac() - 1;
|
||||
let alloc = compute_allocation(m, frame, AllocationInput { offsets: &offsets, cap: &cap, trim: alloc_trim, bits }, &mut enc);
|
||||
quant_fine_energy(frame, log_e, &mut self.old_band_e, &alloc.fine_quant, &mut enc);
|
||||
quant_all_bands(m, frame, &self.freq, N, &alloc, BandCoding { spread: SPREAD_NORMAL, total_bits: total_bits_raw << BITRES }, &mut enc);
|
||||
let bits_left = total_bits_raw - enc.tell();
|
||||
quant_energy_finalise(frame, log_e, &mut self.old_band_e, &alloc, bits_left, &mut enc);
|
||||
debug_assert!(enc.tell() <= total_bits_raw, "frame overflow: {} > {}", enc.tell(), total_bits_raw);
|
||||
if c == 1 {
|
||||
let (a, b) = self.old_band_e.split_at_mut(NB_EBANDS);
|
||||
b.copy_from_slice(a);
|
||||
}
|
||||
// The decoder forgets the energies of bands it did not code.
|
||||
for ch in 0..2 {
|
||||
for i in (0..start).chain(end..NB_EBANDS) {
|
||||
self.old_band_e[ch * NB_EBANDS + i] = 0.0;
|
||||
}
|
||||
}
|
||||
enc.done(len)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn opus_head_layout() {
|
||||
let e = Encoder::new(2, 96_000).unwrap();
|
||||
let h = e.opus_head();
|
||||
assert_eq!(&h[..8], b"OpusHead");
|
||||
assert_eq!(h[8], 1);
|
||||
assert_eq!(h[9], 2);
|
||||
assert_eq!(u16::from_le_bytes([h[10], h[11]]), e.pre_skip());
|
||||
assert_eq!(u32::from_le_bytes([h[12], h[13], h[14], h[15]]), 48_000);
|
||||
assert_eq!(&h[16..], &[0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn packet_sizes_follow_bitrate() {
|
||||
for (ch, rate) in [(1u8, 64_000u32), (2, 128_000), (2, 256_000)] {
|
||||
let mut e = Encoder::new(ch, rate).unwrap();
|
||||
let pcm: Vec<f32> = (0..N * ch as usize).map(|i| (i as f32 * 0.05).sin() * 0.5).collect();
|
||||
let p = e.encode_float(&pcm).unwrap();
|
||||
assert_eq!(p.len(), rate as usize / 400);
|
||||
assert_eq!(p[0], 0xF8 | if ch == 2 { 4 } else { 0 });
|
||||
assert_eq!(e.bitrate(), rate);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn silence_is_a_tiny_packet() {
|
||||
let mut e = Encoder::new(1, 64_000).unwrap();
|
||||
let p = e.encode_float(&vec![0.0; N]).unwrap();
|
||||
assert_eq!(p.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_frame_length_is_an_error() {
|
||||
let mut e = Encoder::new(2, 128_000).unwrap();
|
||||
assert_eq!(e.encode_float(&vec![0.0; N]), Err(Error::FrameLength { expected: 2 * N, got: N }));
|
||||
assert_eq!(e.encode_float(&[]), Err(Error::FrameLength { expected: 2 * N, got: 0 }));
|
||||
assert!(e.encode_float(&vec![0.0; 2 * N]).is_ok());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,472 @@
|
||||
//! Range encoder (RFC 6716 §5.1), the exact inverse of the §4.1 range decoder.
|
||||
//!
|
||||
//! Range-coded symbols are written from the front of the buffer and raw bits from the back; the
|
||||
//! bit accounting ([`RangeEncoder::tell`], [`RangeEncoder::tell_frac`]) matches the decoder's, so
|
||||
//! every budget decision the decoder takes can be mirrored exactly.
|
||||
|
||||
const SYM_BITS: u32 = 8;
|
||||
const CODE_BITS: u32 = 32;
|
||||
const SYM_MAX: u32 = (1 << SYM_BITS) - 1;
|
||||
const CODE_SHIFT: u32 = CODE_BITS - SYM_BITS - 1;
|
||||
const CODE_TOP: u32 = 1 << (CODE_BITS - 1);
|
||||
const CODE_BOT: u32 = CODE_TOP >> SYM_BITS;
|
||||
/// Bit resolution of [`RangeEncoder::tell_frac`] (1/8 bit).
|
||||
pub const BITRES: u32 = 3;
|
||||
|
||||
/// Number of bits needed to represent `x` (0 for 0).
|
||||
#[inline]
|
||||
pub fn ilog(x: u32) -> i32 {
|
||||
(32 - x.leading_zeros()) as i32
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RangeEncoder {
|
||||
out: Vec<u8>,
|
||||
end: Vec<u8>,
|
||||
end_window: u32,
|
||||
nend_bits: i32,
|
||||
nbits_total: i32,
|
||||
rng: u32,
|
||||
low: u32,
|
||||
rem: i32,
|
||||
ext: u32,
|
||||
}
|
||||
|
||||
impl Default for RangeEncoder {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl RangeEncoder {
|
||||
pub fn new() -> Self {
|
||||
RangeEncoder {
|
||||
out: Vec::new(),
|
||||
end: Vec::new(),
|
||||
end_window: 0,
|
||||
nend_bits: 0,
|
||||
nbits_total: (CODE_BITS + 1) as i32,
|
||||
rng: CODE_TOP,
|
||||
low: 0,
|
||||
rem: -1,
|
||||
ext: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Current range (equals the decoder's range after the same symbols).
|
||||
#[cfg(test)]
|
||||
pub fn rng(&self) -> u32 {
|
||||
self.rng
|
||||
}
|
||||
|
||||
fn carry_out(&mut self, c: u32) {
|
||||
if c != SYM_MAX {
|
||||
let carry = c >> SYM_BITS;
|
||||
if self.rem >= 0 {
|
||||
self.out.push((self.rem as u32 + carry) as u8);
|
||||
}
|
||||
if self.ext > 0 {
|
||||
let sym = (SYM_MAX + carry) & SYM_MAX;
|
||||
for _ in 0..self.ext {
|
||||
self.out.push(sym as u8);
|
||||
}
|
||||
self.ext = 0;
|
||||
}
|
||||
self.rem = (c & SYM_MAX) as i32;
|
||||
} else {
|
||||
self.ext += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn normalize(&mut self) {
|
||||
while self.rng <= CODE_BOT {
|
||||
self.carry_out(self.low >> CODE_SHIFT);
|
||||
self.low = (self.low << SYM_BITS) & (CODE_TOP - 1);
|
||||
self.rng <<= SYM_BITS;
|
||||
self.nbits_total += SYM_BITS as i32;
|
||||
}
|
||||
}
|
||||
|
||||
/// Encodes the symbol with cumulative range `[fl, fh)` of total `ft`.
|
||||
pub fn encode(&mut self, fl: u32, fh: u32, ft: u32) {
|
||||
let r = self.rng / ft;
|
||||
if fl > 0 {
|
||||
self.low += self.rng - r * (ft - fl);
|
||||
self.rng = r * (fh - fl);
|
||||
} else {
|
||||
self.rng -= r * (ft - fh);
|
||||
}
|
||||
self.normalize();
|
||||
}
|
||||
|
||||
/// Like [`Self::encode`] with `ft = 1 << bits`.
|
||||
pub fn encode_bin(&mut self, fl: u32, fh: u32, bits: u32) {
|
||||
let r = self.rng >> bits;
|
||||
let ft = 1u32 << bits;
|
||||
if fl > 0 {
|
||||
self.low += self.rng - r * (ft - fl);
|
||||
self.rng = r * (fh - fl);
|
||||
} else {
|
||||
self.rng -= r * (ft - fh);
|
||||
}
|
||||
self.normalize();
|
||||
}
|
||||
|
||||
/// Encodes a binary symbol whose probability of being 1 is `1/2^logp`.
|
||||
pub fn bit_logp(&mut self, val: bool, logp: u32) {
|
||||
let r = self.rng;
|
||||
let s = r >> logp;
|
||||
let r2 = r - s;
|
||||
if val {
|
||||
self.low += r2;
|
||||
self.rng = s;
|
||||
} else {
|
||||
self.rng = r2;
|
||||
}
|
||||
self.normalize();
|
||||
}
|
||||
|
||||
/// Encodes symbol `s` with an "inverse CDF" table (`ft = 1 << ftb`).
|
||||
pub fn icdf(&mut self, s: usize, icdf: &[u8], ftb: u32) {
|
||||
let r = self.rng >> ftb;
|
||||
if s > 0 {
|
||||
self.low += self.rng - r * icdf[s - 1] as u32;
|
||||
self.rng = r * (icdf[s - 1] as u32 - icdf[s] as u32);
|
||||
} else {
|
||||
self.rng -= r * icdf[s] as u32;
|
||||
}
|
||||
self.normalize();
|
||||
}
|
||||
|
||||
/// Encodes a uniformly distributed integer `fl` in `[0, ft)` (`ft >= 2`).
|
||||
pub fn uint(&mut self, fl: u32, ft: u32) {
|
||||
debug_assert!(ft > 1 && fl < ft);
|
||||
let ft1 = ft - 1;
|
||||
let ftb = ilog(ft1);
|
||||
if ftb > 8 {
|
||||
let ftb = (ftb - 8) as u32;
|
||||
let top = (ft1 >> ftb) + 1;
|
||||
let v = fl >> ftb;
|
||||
self.encode(v, v + 1, top);
|
||||
self.bits(fl & ((1 << ftb) - 1), ftb);
|
||||
} else {
|
||||
self.encode(fl, fl + 1, ft);
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes `bits` raw bits (0..=25) to the end of the buffer.
|
||||
pub fn bits(&mut self, val: u32, bits: u32) {
|
||||
if bits == 0 {
|
||||
return;
|
||||
}
|
||||
let mut window = self.end_window;
|
||||
let mut used = self.nend_bits;
|
||||
if used as u32 + bits > CODE_BITS {
|
||||
while used >= SYM_BITS as i32 {
|
||||
self.end.push((window & SYM_MAX) as u8);
|
||||
window >>= SYM_BITS;
|
||||
used -= SYM_BITS as i32;
|
||||
}
|
||||
}
|
||||
window |= val << used;
|
||||
used += bits as i32;
|
||||
self.end_window = window;
|
||||
self.nend_bits = used;
|
||||
self.nbits_total += bits as i32;
|
||||
}
|
||||
|
||||
/// Number of bits written so far (rounded up), identical to the decoder's `tell`.
|
||||
#[inline]
|
||||
pub fn tell(&self) -> i32 {
|
||||
self.nbits_total - ilog(self.rng)
|
||||
}
|
||||
|
||||
/// Number of 1/8 bits written so far (rounded up), identical to the decoder's `tell_frac`.
|
||||
pub fn tell_frac(&self) -> i32 {
|
||||
let nbits = (self.nbits_total as u32) << BITRES;
|
||||
let mut l = ilog(self.rng) as u32;
|
||||
let mut r = self.rng >> (l - 16);
|
||||
for _ in 0..BITRES {
|
||||
r = (r * r) >> 15;
|
||||
let b = r >> 16;
|
||||
l = (l << 1) | b;
|
||||
r >>= b;
|
||||
}
|
||||
nbits.wrapping_sub(l) as i32
|
||||
}
|
||||
|
||||
/// Finalises the stream into a buffer of exactly `size` bytes (range data at the front, raw
|
||||
/// bits at the back, zero padding in between; a partial raw-bit byte is merged into the byte
|
||||
/// just before the full raw-bit bytes).
|
||||
pub fn done(mut self, size: usize) -> Vec<u8> {
|
||||
let mut l = CODE_BITS as i32 - ilog(self.rng);
|
||||
let mut msk = (CODE_TOP - 1) >> l;
|
||||
let mut end = (self.low.wrapping_add(msk)) & !msk;
|
||||
if (end | msk) >= self.low.wrapping_add(self.rng) {
|
||||
l += 1;
|
||||
msk >>= 1;
|
||||
end = (self.low.wrapping_add(msk)) & !msk;
|
||||
}
|
||||
while l > 0 {
|
||||
self.carry_out(end >> CODE_SHIFT);
|
||||
end = (end << SYM_BITS) & (CODE_TOP - 1);
|
||||
l -= SYM_BITS as i32;
|
||||
}
|
||||
if self.rem >= 0 || self.ext > 0 {
|
||||
self.carry_out(0);
|
||||
}
|
||||
let mut window = self.end_window;
|
||||
let mut used = self.nend_bits;
|
||||
while used >= SYM_BITS as i32 {
|
||||
self.end.push((window & SYM_MAX) as u8);
|
||||
window >>= SYM_BITS;
|
||||
used -= SYM_BITS as i32;
|
||||
}
|
||||
let mut buf = vec![0u8; size];
|
||||
let full = self.end.len().min(size);
|
||||
let room = size - full;
|
||||
let n = self.out.len().min(room);
|
||||
buf[..n].copy_from_slice(&self.out[..n]);
|
||||
for (k, &b) in self.end.iter().take(full).enumerate() {
|
||||
buf[size - 1 - k] = b;
|
||||
}
|
||||
if used > 0 && room > 0 {
|
||||
buf[room - 1] |= window as u8;
|
||||
}
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod dec {
|
||||
//! Minimal range decoder (RFC 6716 §4.1) used to verify the encoder.
|
||||
use super::*;
|
||||
const CODE_EXTRA: u32 = (CODE_BITS - 2) % SYM_BITS + 1;
|
||||
|
||||
pub struct RangeDecoder<'a> {
|
||||
buf: &'a [u8],
|
||||
offs: usize,
|
||||
end_offs: usize,
|
||||
end_window: u32,
|
||||
nend_bits: i32,
|
||||
nbits_total: i32,
|
||||
rng: u32,
|
||||
val: u32,
|
||||
rem: u32,
|
||||
}
|
||||
|
||||
impl<'a> RangeDecoder<'a> {
|
||||
pub fn new(buf: &'a [u8]) -> Self {
|
||||
let mut d = RangeDecoder {
|
||||
buf,
|
||||
offs: 0,
|
||||
end_offs: 0,
|
||||
end_window: 0,
|
||||
nend_bits: 0,
|
||||
nbits_total: (CODE_BITS + 1 - ((CODE_BITS - CODE_EXTRA) / SYM_BITS) * SYM_BITS) as i32,
|
||||
rng: 1 << CODE_EXTRA,
|
||||
val: 0,
|
||||
rem: 0,
|
||||
};
|
||||
d.rem = d.read_byte();
|
||||
d.val = d.rng - 1 - (d.rem >> (SYM_BITS - CODE_EXTRA));
|
||||
d.normalize();
|
||||
d
|
||||
}
|
||||
fn read_byte(&mut self) -> u32 {
|
||||
if self.offs < self.buf.len() {
|
||||
self.offs += 1;
|
||||
self.buf[self.offs - 1] as u32
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
fn read_byte_from_end(&mut self) -> u32 {
|
||||
if self.end_offs < self.buf.len() {
|
||||
self.end_offs += 1;
|
||||
self.buf[self.buf.len() - self.end_offs] as u32
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
fn normalize(&mut self) {
|
||||
while self.rng <= CODE_BOT {
|
||||
self.nbits_total += SYM_BITS as i32;
|
||||
self.rng <<= SYM_BITS;
|
||||
let sym = self.rem;
|
||||
self.rem = self.read_byte();
|
||||
let sym = ((sym << SYM_BITS) | self.rem) >> (SYM_BITS - CODE_EXTRA);
|
||||
self.val = ((self.val << SYM_BITS).wrapping_add(SYM_MAX & !sym)) & (CODE_TOP - 1);
|
||||
}
|
||||
}
|
||||
pub fn rng(&self) -> u32 {
|
||||
self.rng
|
||||
}
|
||||
pub fn decode(&mut self, ft: u32) -> u32 {
|
||||
let ext = self.rng / ft;
|
||||
let s = self.val / ext;
|
||||
ft - (s + 1).min(ft)
|
||||
}
|
||||
pub fn update(&mut self, fl: u32, fh: u32, ft: u32) {
|
||||
let ext = self.rng / ft;
|
||||
let s = ext * (ft - fh);
|
||||
self.val = self.val.wrapping_sub(s);
|
||||
self.rng = if fl > 0 { ext * (fh - fl) } else { self.rng - s };
|
||||
self.normalize();
|
||||
}
|
||||
pub fn bit_logp(&mut self, logp: u32) -> bool {
|
||||
let r = self.rng;
|
||||
let d = self.val;
|
||||
let s = r >> logp;
|
||||
let ret = d < s;
|
||||
if !ret {
|
||||
self.val = d - s;
|
||||
}
|
||||
self.rng = if ret { s } else { r - s };
|
||||
self.normalize();
|
||||
ret
|
||||
}
|
||||
pub fn icdf(&mut self, icdf: &[u8], ftb: u32) -> usize {
|
||||
let mut s = self.rng;
|
||||
let d = self.val;
|
||||
let r = s >> ftb;
|
||||
let mut ret = 0usize;
|
||||
let mut t;
|
||||
loop {
|
||||
t = s;
|
||||
s = r * icdf[ret] as u32;
|
||||
if d >= s {
|
||||
break;
|
||||
}
|
||||
ret += 1;
|
||||
}
|
||||
self.val = d - s;
|
||||
self.rng = t - s;
|
||||
self.normalize();
|
||||
ret
|
||||
}
|
||||
pub fn uint(&mut self, ft: u32) -> u32 {
|
||||
let ft1 = ft - 1;
|
||||
let ftb = ilog(ft1);
|
||||
if ftb > 8 {
|
||||
let ftb = (ftb - 8) as u32;
|
||||
let top = (ft1 >> ftb) + 1;
|
||||
let s = self.decode(top);
|
||||
self.update(s, s + 1, top);
|
||||
(s << ftb) | self.bits(ftb)
|
||||
} else {
|
||||
let s = self.decode(ft);
|
||||
self.update(s, s + 1, ft);
|
||||
s
|
||||
}
|
||||
}
|
||||
pub fn bits(&mut self, bits: u32) -> u32 {
|
||||
let mut window = self.end_window;
|
||||
let mut available = self.nend_bits;
|
||||
if (available as u32) < bits {
|
||||
loop {
|
||||
window |= self.read_byte_from_end() << available;
|
||||
available += SYM_BITS as i32;
|
||||
if available > (CODE_BITS - SYM_BITS) as i32 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let ret = window & ((1u32 << bits) - 1);
|
||||
self.end_window = window >> bits;
|
||||
self.nend_bits = available - bits as i32;
|
||||
self.nbits_total += bits as i32;
|
||||
ret
|
||||
}
|
||||
pub fn tell(&self) -> i32 {
|
||||
self.nbits_total - ilog(self.rng)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::dec::RangeDecoder;
|
||||
use super::*;
|
||||
|
||||
struct Lcg(u32);
|
||||
impl Lcg {
|
||||
fn next(&mut self) -> u32 {
|
||||
self.0 = self.0.wrapping_mul(1664525).wrapping_add(1013904223);
|
||||
self.0 >> 8
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starts_at_one_bit() {
|
||||
let e = RangeEncoder::new();
|
||||
assert_eq!(e.tell(), 1);
|
||||
assert_eq!(e.tell_frac(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn roundtrip_mixed_symbols_and_tell_matches() {
|
||||
let mut rng = Lcg(7);
|
||||
let icdf: [u8; 4] = [200, 120, 30, 0];
|
||||
for trial in 0..300 {
|
||||
let n = 1 + (rng.next() % 300) as usize;
|
||||
let mut ops = Vec::new();
|
||||
for _ in 0..n {
|
||||
match rng.next() % 5 {
|
||||
0 => {
|
||||
let ft = 2 + rng.next() % 1000;
|
||||
ops.push((0u8, rng.next() % ft, ft));
|
||||
}
|
||||
1 => {
|
||||
let logp = 1 + rng.next() % 15;
|
||||
ops.push((1, rng.next().is_multiple_of(5) as u32, logp));
|
||||
}
|
||||
2 => {
|
||||
let bits = 1 + rng.next() % 24;
|
||||
ops.push((2, rng.next() & ((1 << bits) - 1), bits));
|
||||
}
|
||||
3 => {
|
||||
let ft = 2 + rng.next() % 100_000_000;
|
||||
ops.push((3, rng.next() % ft, ft));
|
||||
}
|
||||
_ => ops.push((4, rng.next() % 4, 0)),
|
||||
}
|
||||
}
|
||||
let mut e = RangeEncoder::new();
|
||||
let mut tells = Vec::new();
|
||||
for &(k, v, p) in &ops {
|
||||
match k {
|
||||
0 => e.encode(v, v + 1, p),
|
||||
1 => e.bit_logp(v != 0, p),
|
||||
2 => e.bits(v, p),
|
||||
3 => e.uint(v, p),
|
||||
_ => e.icdf(v as usize, &icdf, 8),
|
||||
}
|
||||
tells.push(e.tell());
|
||||
}
|
||||
// Exactly the number of bytes `tell` says were used must suffice.
|
||||
let size = (e.tell() as usize).div_ceil(8);
|
||||
let final_rng = e.rng();
|
||||
let buf = e.done(size);
|
||||
assert_eq!(buf.len(), size);
|
||||
let mut d = RangeDecoder::new(&buf);
|
||||
for (i, &(k, v, p)) in ops.iter().enumerate() {
|
||||
let got = match k {
|
||||
0 => {
|
||||
let s = d.decode(p);
|
||||
d.update(s, s + 1, p);
|
||||
s
|
||||
}
|
||||
1 => d.bit_logp(p) as u32,
|
||||
2 => d.bits(p),
|
||||
3 => d.uint(p),
|
||||
_ => d.icdf(&icdf, 8) as u32,
|
||||
};
|
||||
assert_eq!(got, v, "trial {trial} op {i} kind {k}");
|
||||
assert_eq!(d.tell(), tells[i], "tell mismatch trial {trial} op {i}");
|
||||
}
|
||||
assert_eq!(d.rng(), final_rng);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
//! Pulse cache and bit allocation (RFC 6716 §4.3.3, §4.3.4.1).
|
||||
//!
|
||||
//! The allocation must be bit-identical to the decoder's, so this mirrors FilmCraft's
|
||||
//! spec-derived `filmcraft-opus` decoder (first-party, MIT OR Apache-2.0) step by step; the only
|
||||
//! difference is that the encoder *writes* the band-skip, intensity and dual-stereo symbols.
|
||||
|
||||
use crate::opus::encoder::CeltFrame;
|
||||
use crate::opus::encoder::range::{BITRES, RangeEncoder};
|
||||
use crate::opus::encoder::tables::{EBANDS, LOG2_FRAC_TABLE, NB_EBANDS};
|
||||
|
||||
#[cfg(test)]
|
||||
pub use crate::opus::rate_common::pvq_v;
|
||||
pub use crate::opus::rate_common::{Mode, get_pulses};
|
||||
pub const MAX_FINE_BITS: i32 = 8;
|
||||
const FINE_OFFSET: i32 = 21;
|
||||
pub const QTHETA_OFFSET: i32 = 4;
|
||||
const ALLOC_STEPS: i32 = 6;
|
||||
|
||||
/// Result of the allocation process.
|
||||
pub struct Allocation {
|
||||
pub coded_bands: usize,
|
||||
pub intensity: usize,
|
||||
pub dual_stereo: bool,
|
||||
pub balance: i32,
|
||||
pub pulses: [i32; NB_EBANDS],
|
||||
pub fine_quant: [i32; NB_EBANDS],
|
||||
pub fine_priority: [i32; NB_EBANDS],
|
||||
}
|
||||
|
||||
/// Per-band limits, boosts and available budget for the allocation search.
|
||||
pub struct AllocationInput<'a> {
|
||||
pub offsets: &'a [i32; NB_EBANDS],
|
||||
pub cap: &'a [i32; NB_EBANDS],
|
||||
pub trim: i32,
|
||||
pub bits: i32,
|
||||
}
|
||||
|
||||
/// `clt_compute_allocation`, encoder side. Stereo frames are always coded as dual (L/R) stereo
|
||||
/// with no intensity bands, and no band is skipped voluntarily.
|
||||
pub fn compute_allocation(m: &Mode, frame: CeltFrame, input: AllocationInput<'_>, enc: &mut RangeEncoder) -> Allocation {
|
||||
let CeltFrame { start, end, channels: c, lm } = frame;
|
||||
let AllocationInput { offsets, cap, trim: alloc_trim, bits: total } = input;
|
||||
let crate::opus::rate_common::Bracket { skip_start, bits1, bits2, thresh, total, skip_rsv, intensity_rsv, dual_stereo_rsv } =
|
||||
crate::opus::rate_common::bracket(start, end, offsets, cap, alloc_trim, total, c, lm);
|
||||
let interpolation =
|
||||
AllocationInterpolation { skip_start, bits1: &bits1, bits2: &bits2, thresh: &thresh, cap, total, skip_rsv, intensity_rsv, dual_stereo_rsv };
|
||||
interp_bits2pulses(m, frame, interpolation, enc)
|
||||
}
|
||||
|
||||
/// Bracketing allocation vectors and signalling reserves for the interpolation stage.
|
||||
struct AllocationInterpolation<'a> {
|
||||
skip_start: usize,
|
||||
bits1: &'a [i32; NB_EBANDS],
|
||||
bits2: &'a [i32; NB_EBANDS],
|
||||
thresh: &'a [i32; NB_EBANDS],
|
||||
cap: &'a [i32; NB_EBANDS],
|
||||
total: i32,
|
||||
skip_rsv: i32,
|
||||
intensity_rsv: i32,
|
||||
dual_stereo_rsv: i32,
|
||||
}
|
||||
|
||||
fn interp_bits2pulses(m: &Mode, frame: CeltFrame, interpolation: AllocationInterpolation<'_>, enc: &mut RangeEncoder) -> Allocation {
|
||||
let CeltFrame { start, end, channels: c, lm } = frame;
|
||||
let AllocationInterpolation { skip_start, bits1, bits2, thresh, cap, mut total, skip_rsv, mut intensity_rsv, mut dual_stereo_rsv } =
|
||||
interpolation;
|
||||
let ci = c as i32;
|
||||
let alloc_floor = ci << BITRES;
|
||||
let stereo = (c > 1) as i32;
|
||||
let log_m = (lm as i32) << BITRES;
|
||||
let mut lo = 0;
|
||||
let mut hi = 1 << ALLOC_STEPS;
|
||||
for _ in 0..ALLOC_STEPS {
|
||||
let mid = (lo + hi) >> 1;
|
||||
let mut psum = 0;
|
||||
let mut done = false;
|
||||
for j in (start..end).rev() {
|
||||
let tmp = bits1[j] + ((mid * bits2[j]) >> ALLOC_STEPS);
|
||||
if tmp >= thresh[j] || done {
|
||||
done = true;
|
||||
psum += tmp.min(cap[j]);
|
||||
} else if tmp >= alloc_floor {
|
||||
psum += alloc_floor;
|
||||
}
|
||||
}
|
||||
if psum > total {
|
||||
hi = mid;
|
||||
} else {
|
||||
lo = mid;
|
||||
}
|
||||
}
|
||||
let mut bits = [0i32; NB_EBANDS];
|
||||
let mut psum = 0;
|
||||
let mut done = false;
|
||||
for j in (start..end).rev() {
|
||||
let mut tmp = bits1[j] + ((lo * bits2[j]) >> ALLOC_STEPS);
|
||||
if tmp < thresh[j] && !done {
|
||||
tmp = if tmp >= alloc_floor { alloc_floor } else { 0 };
|
||||
} else {
|
||||
done = true;
|
||||
}
|
||||
tmp = tmp.min(cap[j]);
|
||||
bits[j] = tmp;
|
||||
psum += tmp;
|
||||
}
|
||||
let s = start;
|
||||
let mut coded_bands = end;
|
||||
loop {
|
||||
let j = coded_bands - 1;
|
||||
if j <= skip_start {
|
||||
total += skip_rsv;
|
||||
break;
|
||||
}
|
||||
let mut left = total - psum;
|
||||
let span = EBANDS[coded_bands] - EBANDS[s];
|
||||
let percoeff = left / span;
|
||||
left -= span * percoeff;
|
||||
let rem = (left - (EBANDS[j] - EBANDS[s])).max(0);
|
||||
let band_width = EBANDS[coded_bands] - EBANDS[j];
|
||||
let band_bits = bits[j] + percoeff * band_width + rem;
|
||||
if band_bits >= thresh[j].max(alloc_floor + (1 << BITRES)) {
|
||||
// Keep this band (and every band below it).
|
||||
enc.bit_logp(true, 1);
|
||||
break;
|
||||
}
|
||||
psum -= bits[j] + intensity_rsv;
|
||||
if intensity_rsv > 0 {
|
||||
intensity_rsv = LOG2_FRAC_TABLE[j - s];
|
||||
}
|
||||
psum += intensity_rsv;
|
||||
if band_bits >= alloc_floor {
|
||||
psum += alloc_floor;
|
||||
bits[j] = alloc_floor;
|
||||
} else {
|
||||
bits[j] = 0;
|
||||
}
|
||||
coded_bands -= 1;
|
||||
}
|
||||
let intensity = if intensity_rsv > 0 {
|
||||
// No intensity stereo: every coded band is coded in full.
|
||||
enc.uint((coded_bands - s) as u32, (coded_bands + 1 - s) as u32);
|
||||
coded_bands
|
||||
} else {
|
||||
0
|
||||
};
|
||||
if intensity <= s {
|
||||
total += dual_stereo_rsv;
|
||||
dual_stereo_rsv = 0;
|
||||
}
|
||||
let dual_stereo = if dual_stereo_rsv > 0 {
|
||||
enc.bit_logp(true, 1);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let mut left = total - psum;
|
||||
let span = EBANDS[coded_bands] - EBANDS[s];
|
||||
let percoeff = left / span;
|
||||
left -= span * percoeff;
|
||||
for j in s..coded_bands {
|
||||
bits[j] += percoeff * (EBANDS[j + 1] - EBANDS[j]);
|
||||
}
|
||||
for j in s..coded_bands {
|
||||
let tmp = left.min(EBANDS[j + 1] - EBANDS[j]);
|
||||
bits[j] += tmp;
|
||||
left -= tmp;
|
||||
}
|
||||
let mut ebits = [0i32; NB_EBANDS];
|
||||
let mut fine_priority = [0i32; NB_EBANDS];
|
||||
let mut balance = 0;
|
||||
for j in s..coded_bands {
|
||||
let n0 = EBANDS[j + 1] - EBANDS[j];
|
||||
let n = n0 << lm;
|
||||
let bit = bits[j] + balance;
|
||||
let mut excess;
|
||||
if n > 1 {
|
||||
excess = (bit - cap[j]).max(0);
|
||||
bits[j] = bit - excess;
|
||||
let den = ci * n + if c == 2 && n > 2 && !dual_stereo && j < intensity { 1 } else { 0 };
|
||||
let nclogn = den * (m.log_n[j] + log_m);
|
||||
let mut offset = (nclogn >> 1) - den * FINE_OFFSET;
|
||||
if n == 2 {
|
||||
offset += (den << BITRES) >> 2;
|
||||
}
|
||||
if bits[j] + offset < (den * 2) << BITRES {
|
||||
offset += nclogn >> 2;
|
||||
} else if bits[j] + offset < (den * 3) << BITRES {
|
||||
offset += nclogn >> 3;
|
||||
}
|
||||
let mut eb = (bits[j] + offset + (den << (BITRES - 1))).max(0);
|
||||
eb = (eb / den) >> BITRES;
|
||||
if ci * eb > (bits[j] >> BITRES) {
|
||||
eb = bits[j] >> stereo >> BITRES;
|
||||
}
|
||||
eb = eb.min(MAX_FINE_BITS);
|
||||
ebits[j] = eb;
|
||||
fine_priority[j] = (eb * (den << BITRES) >= bits[j] + offset) as i32;
|
||||
bits[j] -= (ci * eb) << BITRES;
|
||||
} else {
|
||||
excess = (bit - (ci << BITRES)).max(0);
|
||||
bits[j] = bit - excess;
|
||||
ebits[j] = 0;
|
||||
fine_priority[j] = 1;
|
||||
}
|
||||
if excess > 0 {
|
||||
let extra_fine = (excess >> (stereo + BITRES as i32)).min(MAX_FINE_BITS - ebits[j]);
|
||||
ebits[j] += extra_fine;
|
||||
let extra_bits = (extra_fine * ci) << BITRES;
|
||||
fine_priority[j] = (extra_bits >= excess - balance) as i32;
|
||||
excess -= extra_bits;
|
||||
}
|
||||
balance = excess;
|
||||
}
|
||||
for j in coded_bands..end {
|
||||
ebits[j] = bits[j] >> stereo >> BITRES;
|
||||
bits[j] = 0;
|
||||
fine_priority[j] = (ebits[j] < 1) as i32;
|
||||
}
|
||||
Allocation { coded_bands, intensity, dual_stereo, balance, pulses: bits, fine_quant: ebits, fine_priority }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn caps_match_decoder_reference() {
|
||||
let m = Mode::new();
|
||||
let first_rows: [u8; 42] = [
|
||||
224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 160, 160, 185, 185, 185, 178, 178, 168, 134, 61, 37, 224, 224, 224, 224, 224, 224, 224,
|
||||
224, 240, 240, 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40,
|
||||
];
|
||||
assert_eq!(&m.caps[..42], &first_rows[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pvq_counts() {
|
||||
assert_eq!(pvq_v(5, 1), 10);
|
||||
assert_eq!(pvq_v(1, 7), 2);
|
||||
assert_eq!(pvq_v(2, 9), 36);
|
||||
assert_eq!(pvq_v(3, 2), 18);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
//! 48 kHz → 8/12/16 kHz decimator feeding the SILK layer (own design).
|
||||
//!
|
||||
//! A linear-phase low-pass FIR (Kaiser-windowed sinc at 48 kHz) evaluated only at the retained
|
||||
//! output phases. Its group delay ([`delay`]) is a whole number of 48 kHz samples, chosen so
|
||||
//! that the SILK path (this filter + the decoder's internal-rate delay and resampler, RFC 6716
|
||||
//! Table 54) lands within a fraction of a sample of the CELT path's 120-sample MDCT overlap.
|
||||
|
||||
/// Group delay of the decimator to `fs_out` in 48 kHz samples (the filter has `2 * delay + 1`
|
||||
/// taps).
|
||||
pub fn delay(fs_out: u32) -> usize {
|
||||
if fs_out == 8_000 { 88 } else { 83 }
|
||||
}
|
||||
|
||||
/// Zeroth-order modified Bessel function of the first kind (power series).
|
||||
fn bessel_i0(x: f64) -> f64 {
|
||||
let mut sum = 1.0;
|
||||
let mut term = 1.0;
|
||||
let q = x * x / 4.0;
|
||||
for k in 1..64 {
|
||||
term *= q / (k * k) as f64;
|
||||
sum += term;
|
||||
if term < sum * 1e-17 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
/// Windowed-sinc low-pass with cutoff `fc` (Hz at 48 kHz), unit DC gain.
|
||||
fn design(fc: f64, beta: f64, delay: usize) -> Vec<f32> {
|
||||
let wc = 2.0 * std::f64::consts::PI * fc / 48_000.0;
|
||||
let i0b = bessel_i0(beta);
|
||||
let mut h: Vec<f64> = (0..2 * delay + 1)
|
||||
.map(|k| {
|
||||
let m = k as f64 - delay as f64;
|
||||
let sinc = if m == 0.0 { wc / std::f64::consts::PI } else { (wc * m).sin() / (std::f64::consts::PI * m) };
|
||||
let r = m / delay as f64;
|
||||
sinc * bessel_i0(beta * (1.0 - r * r).max(0.0).sqrt()) / i0b
|
||||
})
|
||||
.collect();
|
||||
let s: f64 = h.iter().sum();
|
||||
for v in h.iter_mut() {
|
||||
*v /= s;
|
||||
}
|
||||
h.iter().map(|&v| v as f32).collect()
|
||||
}
|
||||
|
||||
use crate::opus::dot::dot8;
|
||||
|
||||
/// Decimates one channel of 48 kHz audio by 3, 4 or 6.
|
||||
#[derive(Clone)]
|
||||
pub struct Downsampler {
|
||||
factor: usize,
|
||||
h: Vec<f32>,
|
||||
taps: usize,
|
||||
/// The last `taps - 1` input samples followed by the current frame.
|
||||
buf: Vec<f32>,
|
||||
}
|
||||
|
||||
impl Downsampler {
|
||||
/// `fs_out` is 8000, 12000 or 16000.
|
||||
pub fn new(fs_out: u32) -> Self {
|
||||
let factor = (48_000 / fs_out) as usize;
|
||||
// Cutoff at 95 % of the output Nyquist frequency (pass band to ~88 %, stop band from ~110 %).
|
||||
let fc = 0.95 * fs_out as f64 / 2.0;
|
||||
let d = delay(fs_out);
|
||||
let taps = 2 * d + 1;
|
||||
Downsampler { factor, h: design(fc, 6.5, d), taps, buf: vec![0.0; taps - 1] }
|
||||
}
|
||||
|
||||
/// Decimates `input` (a multiple of the factor long), appending to `out`. Output sample `j`
|
||||
/// is the filtered input at frame position `factor * j`, delayed by [`delay`].
|
||||
pub fn process(&mut self, input: &[f32], out: &mut Vec<f32>) {
|
||||
debug_assert!(input.len().is_multiple_of(self.factor));
|
||||
let taps = self.taps;
|
||||
self.buf.truncate(taps - 1);
|
||||
self.buf.extend_from_slice(input);
|
||||
for j in 0..input.len() / self.factor {
|
||||
let t = taps - 1 + self.factor * j;
|
||||
let win = &self.buf[t + 1 - taps..=t];
|
||||
// h is symmetric, so the window can be taken in forward order.
|
||||
out.push(dot8(win, &self.h));
|
||||
}
|
||||
let n = self.buf.len();
|
||||
self.buf.copy_within(n - (taps - 1).., 0);
|
||||
self.buf.truncate(taps - 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn tone(f: f64, n: usize) -> Vec<f32> {
|
||||
(0..n).map(|i| (2.0 * std::f64::consts::PI * f * i as f64 / 48_000.0).sin() as f32).collect()
|
||||
}
|
||||
|
||||
fn rms(x: &[f32]) -> f64 {
|
||||
(x.iter().map(|&v| (v as f64).powi(2)).sum::<f64>() / x.len() as f64).sqrt()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dot8_matches_sequential_sum() {
|
||||
for len in [0usize, 1, 7, 8, 9, 63, 167, 251] {
|
||||
let a: Vec<f32> = (0..len).map(|i| ((i * 37 % 101) as f32 - 50.0) / 50.0).collect();
|
||||
let b: Vec<f32> = (0..len).map(|i| ((i * 53 % 89) as f32 - 44.0) / 44.0).collect();
|
||||
let exact: f64 = a.iter().zip(&b).map(|(&x, &y)| x as f64 * y as f64).sum();
|
||||
assert!((dot8(&a, &b) as f64 - exact).abs() < 1e-4, "len {len}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn passes_band_and_rejects_aliases() {
|
||||
for &(fs, pass, stop) in &[(16_000u32, 6_000.0, 8_600.0), (12_000, 4_500.0, 6_500.0), (8_000, 3_000.0, 4_500.0)] {
|
||||
for &(f, want_pass) in &[(pass, true), (stop, false)] {
|
||||
let mut d = Downsampler::new(fs);
|
||||
let x = tone(f, 9600);
|
||||
let mut y = Vec::new();
|
||||
for c in x.chunks(960) {
|
||||
d.process(c, &mut y);
|
||||
}
|
||||
let r = rms(&y[y.len() / 2..]) * std::f64::consts::SQRT_2;
|
||||
if want_pass {
|
||||
assert!((r - 1.0).abs() < 0.02, "{fs}: {f} Hz gain {r}");
|
||||
} else {
|
||||
assert!(r < 0.01, "{fs}: {f} Hz leaks {r}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delay_is_exact() {
|
||||
let mut d = Downsampler::new(16_000);
|
||||
let mut x = vec![0f32; 960];
|
||||
x[300] = 1.0;
|
||||
let mut y = Vec::new();
|
||||
d.process(&x, &mut y);
|
||||
// The impulse at 300 shows up centred at 48 kHz time 300 + 83 = 383 (output 127.67).
|
||||
let peak = y.iter().enumerate().max_by(|a, b| a.1.total_cmp(b.1)).map(|(i, _)| i).unwrap_or(0);
|
||||
assert!((peak as f64 - (300.0 + delay(16_000) as f64) / 3.0).abs() < 1.0, "peak at {peak}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,418 @@
|
||||
//! Encoder-side signal analysis for SILK (own design): windowed autocorrelation LPC, LPC → NLSF
|
||||
//! conversion, open-loop pitch search and LTP codebook search.
|
||||
|
||||
use super::tables::{CB_NB_20, CB_WB_20, LTP_FILTER, LTP_PERIODICITY, LTP_TAPS_0, LTP_TAPS_1, LTP_TAPS_2};
|
||||
|
||||
/// Short-term prediction coefficients `a` (prediction `x[n] ≈ Σ a[j] x[n-1-j]`) from the
|
||||
/// windowed segment `x`, with a Gaussian lag window and white-noise correction.
|
||||
pub fn lpc(x: &[f32], order: usize, fs_khz: usize) -> Vec<f64> {
|
||||
let n = x.len();
|
||||
// Asymmetric window: sin² rise over the first half, flat, cos fall over the last 1/8.
|
||||
let rise = n / 2;
|
||||
let fall = n / 8;
|
||||
let w = |i: usize| -> f64 {
|
||||
if i < rise {
|
||||
let s = (std::f64::consts::FRAC_PI_2 * (i as f64 + 0.5) / rise as f64).sin();
|
||||
s * s
|
||||
} else if i >= n - fall {
|
||||
(std::f64::consts::FRAC_PI_2 * (i + fall - n) as f64 / fall as f64 + 0.0).cos()
|
||||
} else {
|
||||
1.0
|
||||
}
|
||||
};
|
||||
let xw: Vec<f64> = x.iter().enumerate().map(|(i, &v)| v as f64 * w(i)).collect();
|
||||
let mut r = vec![0f64; order + 1];
|
||||
for (k, rk) in r.iter_mut().enumerate() {
|
||||
*rk = xw[k..].iter().zip(&xw).map(|(a, b)| a * b).sum();
|
||||
}
|
||||
if r[0] <= 1e-6 {
|
||||
return vec![0.0; order];
|
||||
}
|
||||
// White-noise correction (-40 dB) and a 50 Hz Gaussian lag window.
|
||||
r[0] *= 1.0 + 1e-4;
|
||||
let f0 = 2.0 * std::f64::consts::PI * 50.0 / (fs_khz as f64 * 1000.0);
|
||||
for (k, rk) in r.iter_mut().enumerate().skip(1) {
|
||||
*rk *= (-0.5 * (f0 * k as f64).powi(2)).exp();
|
||||
}
|
||||
let mut a = levinson(&r, order);
|
||||
// Mild bandwidth expansion keeps the NLSFs apart.
|
||||
let mut g = 1.0;
|
||||
for v in a.iter_mut() {
|
||||
g *= 0.996;
|
||||
*v *= g;
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
/// Levinson-Durbin recursion on autocorrelation `r` (predictor convention as in [`lpc`]).
|
||||
pub fn levinson(r: &[f64], order: usize) -> Vec<f64> {
|
||||
let mut a = vec![0f64; order];
|
||||
let mut err = r[0];
|
||||
let mut tmp = vec![0f64; order];
|
||||
for i in 0..order {
|
||||
let mut acc = r[i + 1];
|
||||
for j in 0..i {
|
||||
acc -= a[j] * r[i - j];
|
||||
}
|
||||
let k = (acc / err).clamp(-0.9999, 0.9999);
|
||||
tmp[..i].copy_from_slice(&a[..i]);
|
||||
for j in 0..i {
|
||||
a[j] = tmp[j] - k * tmp[i - 1 - j];
|
||||
}
|
||||
a[i] = k;
|
||||
err *= 1.0 - k * k;
|
||||
if err <= 0.0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
a
|
||||
}
|
||||
|
||||
/// Evaluates the symmetric polynomial `p` (length `d + 1`, `d` even) as the cosine series
|
||||
/// `e^{jωd/2} P(e^{jω})`, with `cos_m(m)` giving `cos(m ω)`.
|
||||
#[inline]
|
||||
fn cos_series_with(p: &[f64], cos_m: impl Fn(usize) -> f64) -> f64 {
|
||||
let h = (p.len() - 1) / 2;
|
||||
let mut s = p[h];
|
||||
for k in 0..h {
|
||||
s += 2.0 * p[k] * cos_m(h - k);
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn cos_series(p: &[f64], w: f64) -> f64 {
|
||||
cos_series_with(p, |m| (m as f64 * w).cos())
|
||||
}
|
||||
|
||||
/// Number of grid intervals the root search scans over `[0, π]`.
|
||||
const GRID: usize = 1024;
|
||||
/// Largest multiple of ω in the series (half the LPC order, at most 16 / 2).
|
||||
const MAX_H: usize = 8;
|
||||
/// Bisection steps per root: the bracket shrinks to 3e-9 rad, far below one Q15 NLSF step (9.6e-5 rad).
|
||||
const BISECT_STEPS: usize = 20;
|
||||
|
||||
/// `cos(m · π g / GRID)` for every grid point `g` and `m` in `0..=MAX_H`, computed with the same
|
||||
/// expression as [`cos_series`], so the table values are bit-identical to calling `cos` there.
|
||||
fn grid_cos() -> &'static [[f64; MAX_H + 1]] {
|
||||
static TABLE: std::sync::OnceLock<Vec<[f64; MAX_H + 1]>> = std::sync::OnceLock::new();
|
||||
TABLE.get_or_init(|| {
|
||||
(0..=GRID)
|
||||
.map(|g| {
|
||||
let w = std::f64::consts::PI * g as f64 / GRID as f64;
|
||||
std::array::from_fn(|m| (m as f64 * w).cos())
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
|
||||
fn roots(p: &[f64], out: &mut Vec<f64>) {
|
||||
let grid = grid_cos();
|
||||
let at_grid = |g: usize| cos_series_with(p, |m| grid[g][m]);
|
||||
let mut w0 = 0.0;
|
||||
let mut f0 = at_grid(0);
|
||||
for g in 1..=GRID {
|
||||
let w1 = std::f64::consts::PI * g as f64 / GRID as f64;
|
||||
let f1 = at_grid(g);
|
||||
if f0 == 0.0 {
|
||||
out.push(w0);
|
||||
} else if f0 * f1 < 0.0 {
|
||||
let (mut lo, mut hi, mut flo) = (w0, w1, f0);
|
||||
for _ in 0..BISECT_STEPS {
|
||||
let mid = 0.5 * (lo + hi);
|
||||
let fm = cos_series(p, mid);
|
||||
if fm * flo <= 0.0 {
|
||||
hi = mid;
|
||||
} else {
|
||||
lo = mid;
|
||||
flo = fm;
|
||||
}
|
||||
}
|
||||
out.push(0.5 * (lo + hi));
|
||||
}
|
||||
w0 = w1;
|
||||
f0 = f1;
|
||||
}
|
||||
}
|
||||
|
||||
/// LPC → normalised line spectral frequencies in radians (ascending, `order` values), or `None`
|
||||
/// when the root search fails (unstable or degenerate filter).
|
||||
pub fn a2nlsf(a: &[f64]) -> Option<Vec<f64>> {
|
||||
let d = a.len();
|
||||
if d > 2 * MAX_H {
|
||||
return None;
|
||||
}
|
||||
let mut c = vec![0f64; d + 2];
|
||||
c[0] = 1.0;
|
||||
for k in 0..d {
|
||||
c[k + 1] = -a[k];
|
||||
}
|
||||
let mut p = vec![0f64; d + 2];
|
||||
let mut q = vec![0f64; d + 2];
|
||||
for k in 0..d + 2 {
|
||||
p[k] = c[k] + c[d + 1 - k];
|
||||
q[k] = c[k] - c[d + 1 - k];
|
||||
}
|
||||
// Remove the trivial roots at z = -1 (P) and z = +1 (Q).
|
||||
let mut p1 = vec![0f64; d + 1];
|
||||
let mut q1 = vec![0f64; d + 1];
|
||||
p1[0] = p[0];
|
||||
q1[0] = q[0];
|
||||
for k in 1..=d {
|
||||
p1[k] = p[k] - p1[k - 1];
|
||||
q1[k] = q[k] + q1[k - 1];
|
||||
}
|
||||
let mut r = Vec::with_capacity(d);
|
||||
roots(&p1, &mut r);
|
||||
roots(&q1, &mut r);
|
||||
if r.len() != d {
|
||||
return None;
|
||||
}
|
||||
r.sort_by(f64::total_cmp);
|
||||
Some(r)
|
||||
}
|
||||
|
||||
/// Normalised cross-correlation of `x[n]` and `x[n - lag]` over `n` in `range`.
|
||||
fn ncorr(x: &[f32], range: std::ops::Range<usize>, lag: usize) -> f64 {
|
||||
let (mut xy, mut xx, mut yy) = (0f64, 0f64, 0f64);
|
||||
for n in range {
|
||||
let a = x[n] as f64;
|
||||
let b = x[n - lag] as f64;
|
||||
xy += a * b;
|
||||
xx += a * a;
|
||||
yy += b * b;
|
||||
}
|
||||
if xy <= 0.0 { 0.0 } else { xy / (xx * yy + 1e-9).sqrt() }
|
||||
}
|
||||
|
||||
pub struct Pitch {
|
||||
pub lag_index: i32,
|
||||
pub contour: usize,
|
||||
pub lags: [i32; 4],
|
||||
pub score: f64,
|
||||
}
|
||||
|
||||
/// Open-loop pitch search on `x` (history followed by the frame starting at `start`, `len`
|
||||
/// samples, four subframes) at `fs_khz`.
|
||||
pub fn pitch_search(x: &[f32], start: usize, len: usize, fs_khz: usize) -> Pitch {
|
||||
let fs = fs_khz as i32;
|
||||
let min_lag = 2 * fs;
|
||||
let max_lag = 18 * fs - 1;
|
||||
let sub = len / 4;
|
||||
let nl = (max_lag - min_lag + 1) as usize;
|
||||
// Whole-frame score per lag.
|
||||
let full: Vec<f64> = (0..nl).map(|i| ncorr(x, start..start + len, min_lag as usize + i)).collect();
|
||||
let mut best = 0usize;
|
||||
for i in 0..nl {
|
||||
// Slight preference for shorter lags.
|
||||
if full[i] * (1.0 - 0.0004 * i as f64) > full[best] * (1.0 - 0.0004 * best as f64) {
|
||||
best = i;
|
||||
}
|
||||
}
|
||||
let mut lag = min_lag + best as i32;
|
||||
// Prefer a sub-multiple when it explains the signal almost as well (octave errors).
|
||||
for div in [4, 3, 2] {
|
||||
let cand = (lag as f64 / div as f64).round() as i32;
|
||||
let mut cbest = (0.0, 0);
|
||||
for c in cand - 1..=cand + 1 {
|
||||
if c >= min_lag && c <= max_lag {
|
||||
let s = full[(c - min_lag) as usize];
|
||||
if s > cbest.0 {
|
||||
cbest = (s, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if cbest.1 != 0 && cbest.0 > 0.9 * full[(lag - min_lag) as usize] {
|
||||
lag = cbest.1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
let score = full[(lag - min_lag) as usize];
|
||||
// Contour: per-subframe lag offsets around the chosen lag.
|
||||
let cb: &[[i32; 4]] = if fs_khz == 8 { &CB_NB_20 } else { &CB_WB_20 };
|
||||
// The contour search revisits the same (subframe, lag) pairs for neighbouring base lags and
|
||||
// codebook entries, so each correlation is computed once.
|
||||
let span = (18 * fs - min_lag + 1) as usize;
|
||||
let memo = std::cell::RefCell::new(vec![f64::NAN; 4 * span]);
|
||||
let sub_score = |k: usize, l: i32| -> f64 {
|
||||
let l = l.clamp(min_lag, 18 * fs);
|
||||
let slot = k * span + (l - min_lag) as usize;
|
||||
let cached = memo.borrow()[slot];
|
||||
if !cached.is_nan() {
|
||||
return cached;
|
||||
}
|
||||
let v = ncorr(x, start + k * sub..start + (k + 1) * sub, l as usize);
|
||||
memo.borrow_mut()[slot] = v;
|
||||
v
|
||||
};
|
||||
let mut best = (f64::MIN, lag, 0usize);
|
||||
for l0 in (lag - 2).max(min_lag)..=(lag + 2).min(max_lag) {
|
||||
for (ci, offs) in cb.iter().enumerate() {
|
||||
let s: f64 = (0..4).map(|k| sub_score(k, l0 + offs[k])).sum::<f64>() - 0.002 * ci as f64;
|
||||
if s > best.0 {
|
||||
best = (s, l0, ci);
|
||||
}
|
||||
}
|
||||
}
|
||||
let (_, l0, ci) = best;
|
||||
let mut lags = [0i32; 4];
|
||||
for k in 0..4 {
|
||||
lags[k] = (l0 + cb[ci][k]).clamp(min_lag, 18 * fs);
|
||||
}
|
||||
Pitch { lag_index: l0 - min_lag, contour: ci, lags, score }
|
||||
}
|
||||
|
||||
/// Result of the LTP codebook search.
|
||||
pub struct Ltp {
|
||||
pub per_index: usize,
|
||||
pub ltp_index: [usize; 4],
|
||||
/// Residual energy after LTP prediction / before, per subframe.
|
||||
pub err: [f64; 4],
|
||||
pub energy: [f64; 4],
|
||||
}
|
||||
|
||||
fn icdf_bits(icdf: &[u8], s: usize) -> f64 {
|
||||
let hi = if s == 0 { 256 } else { icdf[s - 1] as u32 };
|
||||
let p = (hi - icdf[s] as u32).max(1) as f64 / 256.0;
|
||||
-p.log2()
|
||||
}
|
||||
|
||||
/// Searches the three LTP codebooks (RFC 6716 Tables 39–42) for the taps that best predict the
|
||||
/// residual `r` (history + frame starting at `start`) from its past at the per-subframe `lags`.
|
||||
pub fn ltp_search(r: &[f64], start: usize, sub: usize, lags: &[i32; 4]) -> Ltp {
|
||||
let mut rr = [[0f64; 25]; 4];
|
||||
let mut cv = [[0f64; 5]; 4];
|
||||
let mut e = [0f64; 4];
|
||||
for k in 0..4 {
|
||||
let s0 = start + k * sub;
|
||||
let lag = lags[k] as usize;
|
||||
for n in s0..s0 + sub {
|
||||
let t = r[n];
|
||||
e[k] += t * t;
|
||||
let base = n + 2 - lag;
|
||||
for i in 0..5 {
|
||||
let vi = r[base - i];
|
||||
cv[k][i] += t * vi;
|
||||
for j in 0..5 {
|
||||
rr[k][i * 5 + j] += vi * r[base - j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let books: [&[[i32; 5]]; 3] = [<P_TAPS_0, <P_TAPS_1, <P_TAPS_2];
|
||||
let mut best = (f64::MAX, 0usize, [0usize; 4], [0f64; 4]);
|
||||
for (p, book) in books.iter().enumerate() {
|
||||
let mut total = icdf_bits(<P_PERIODICITY, p) * 1e-3 * e.iter().sum::<f64>() / 4.0;
|
||||
let mut idx = [0usize; 4];
|
||||
let mut errs = [0f64; 4];
|
||||
for k in 0..4 {
|
||||
let mut bk = (f64::MAX, 0usize, 0f64);
|
||||
for (i, taps) in book.iter().enumerate() {
|
||||
let b: [f64; 5] = std::array::from_fn(|j| taps[j] as f64 / 128.0);
|
||||
let mut err = e[k];
|
||||
for a in 0..5 {
|
||||
err -= 2.0 * b[a] * cv[k][a];
|
||||
for c in 0..5 {
|
||||
err += b[a] * b[c] * rr[k][a * 5 + c];
|
||||
}
|
||||
}
|
||||
let cost = err + icdf_bits(LTP_FILTER[p], i) * 1e-3 * e[k];
|
||||
if cost < bk.0 {
|
||||
bk = (cost, i, err.max(0.0));
|
||||
}
|
||||
}
|
||||
total += bk.0;
|
||||
idx[k] = bk.1;
|
||||
errs[k] = bk.2;
|
||||
}
|
||||
if total < best.0 {
|
||||
best = (total, p, idx, errs);
|
||||
}
|
||||
}
|
||||
Ltp { per_index: best.1, ltp_index: best.2, err: best.3, energy: e }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::lpc::nlsf2a;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn nlsf_roundtrip_through_decoder_conversion() {
|
||||
// A resonant 10th/16th-order predictor from a synthetic signal.
|
||||
for &(order, fs) in &[(10usize, 8usize), (16, 16)] {
|
||||
let mut st = [0f64; 4];
|
||||
let mut seed = 1u32;
|
||||
let x: Vec<f32> = (0..400)
|
||||
.map(|_| {
|
||||
seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
|
||||
let w = (seed >> 9) as f64 / (1u32 << 23) as f64 - 1.0;
|
||||
let y = w + 1.6 * st[0] - 0.9 * st[1] + 0.3 * st[2] - 0.2 * st[3];
|
||||
st = [y, st[0], st[1], st[2]];
|
||||
(y * 1000.0) as f32
|
||||
})
|
||||
.collect();
|
||||
let a = lpc(&x, order, fs);
|
||||
let nlsf = a2nlsf(&a).expect("roots");
|
||||
let q15: Vec<i32> = nlsf.iter().map(|w| (w / std::f64::consts::PI * 32768.0).round() as i32).collect();
|
||||
let a_q12 = nlsf2a(&q15);
|
||||
for j in 0..order {
|
||||
assert!((a_q12[j] as f64 / 4096.0 - a[j]).abs() < 0.02, "order {order} coef {j}: {} vs {}", a_q12[j] as f64 / 4096.0, a[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The root search as it was before the grid table: `cos` called for every evaluation.
|
||||
fn roots_reference(p: &[f64], out: &mut Vec<f64>) {
|
||||
let mut w0 = 0.0;
|
||||
let mut f0 = cos_series(p, w0);
|
||||
for g in 1..=GRID {
|
||||
let w1 = std::f64::consts::PI * g as f64 / GRID as f64;
|
||||
let f1 = cos_series(p, w1);
|
||||
if f0 == 0.0 {
|
||||
out.push(w0);
|
||||
} else if f0 * f1 < 0.0 {
|
||||
let (mut lo, mut hi, mut flo) = (w0, w1, f0);
|
||||
for _ in 0..BISECT_STEPS {
|
||||
let mid = 0.5 * (lo + hi);
|
||||
let fm = cos_series(p, mid);
|
||||
if fm * flo <= 0.0 {
|
||||
hi = mid;
|
||||
} else {
|
||||
lo = mid;
|
||||
flo = fm;
|
||||
}
|
||||
}
|
||||
out.push(0.5 * (lo + hi));
|
||||
}
|
||||
w0 = w1;
|
||||
f0 = f1;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grid_table_roots_are_bit_identical_to_direct_cos() {
|
||||
let mut seed = 7u64;
|
||||
let mut rnd = move || {
|
||||
seed ^= seed << 13;
|
||||
seed ^= seed >> 7;
|
||||
seed ^= seed << 17;
|
||||
(seed >> 11) as f64 / (1u64 << 53) as f64 * 2.0 - 1.0
|
||||
};
|
||||
let mut found = 0;
|
||||
for len in [11usize, 17] {
|
||||
for _ in 0..300 {
|
||||
let mut p = vec![0f64; len];
|
||||
for k in 0..len / 2 + 1 {
|
||||
p[k] = rnd();
|
||||
p[len - 1 - k] = p[k];
|
||||
}
|
||||
let (mut a, mut b) = (Vec::new(), Vec::new());
|
||||
roots(&p, &mut a);
|
||||
roots_reference(&p, &mut b);
|
||||
assert_eq!(a.iter().map(|v| v.to_bits()).collect::<Vec<_>>(), b.iter().map(|v| v.to_bits()).collect::<Vec<_>>());
|
||||
found += a.len();
|
||||
}
|
||||
}
|
||||
assert!(found > 1000, "{found}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,370 @@
|
||||
//! Fixed-point NLSF decoding, NLSF→LPC conversion and filter stabilisation
|
||||
//! (RFC 6716 §4.2.7.5, with the RFC 8251 §6/§7 overflow fixes).
|
||||
//!
|
||||
//! This is the decoder's normative arithmetic, mirrored from FilmCraft's first-party,
|
||||
//! spec-derived `filmcraft-opus` decoder (MIT OR Apache-2.0): the encoder must reconstruct the
|
||||
//! quantised NLSFs and LPC coefficients bit-exactly as the decoder does.
|
||||
|
||||
use super::tables::{
|
||||
COS_Q12, LSF_ORDER_NBMB, LSF_ORDER_WB, NLSF_CB_NBMB, NLSF_CB_WB, NLSF_MIN_SPACING_NBMB, NLSF_MIN_SPACING_WB, NLSF_PRED_NBMB, NLSF_PRED_WB,
|
||||
NLSF_PSEL_NBMB, NLSF_PSEL_WB,
|
||||
};
|
||||
|
||||
pub const MAX_LPC_ORDER: usize = 16;
|
||||
|
||||
#[inline]
|
||||
pub fn smulwb(a: i32, b: i32) -> i32 {
|
||||
((a as i64 * (b as i16) as i64) >> 16) as i32
|
||||
}
|
||||
#[inline]
|
||||
pub fn smlawb(a: i32, b: i32, c: i32) -> i32 {
|
||||
a.wrapping_add(smulwb(b, c))
|
||||
}
|
||||
#[inline]
|
||||
pub fn smulww(a: i32, b: i32) -> i32 {
|
||||
((a as i64 * b as i64) >> 16) as i32
|
||||
}
|
||||
#[inline]
|
||||
pub fn smmul(a: i32, b: i32) -> i32 {
|
||||
((a as i64 * b as i64) >> 32) as i32
|
||||
}
|
||||
#[inline]
|
||||
pub fn rshift_round(a: i32, s: u32) -> i32 {
|
||||
if s == 1 { (a >> 1) + (a & 1) } else { ((a >> (s - 1)) + 1) >> 1 }
|
||||
}
|
||||
#[inline]
|
||||
pub fn rshift_round64(a: i64, s: u32) -> i64 {
|
||||
if s == 1 { (a >> 1) + (a & 1) } else { ((a >> (s - 1)) + 1) >> 1 }
|
||||
}
|
||||
#[inline]
|
||||
pub fn sat16(a: i32) -> i32 {
|
||||
a.clamp(-32768, 32767)
|
||||
}
|
||||
|
||||
/// `silk_INVERSE32_varQ`: approximation of `(1 << q_res) / b32`.
|
||||
pub fn inverse32_varq(b32: i32, q_res: i32) -> i32 {
|
||||
let b_headrm = b32.unsigned_abs().leading_zeros() as i32 - 1;
|
||||
let b32_nrm = b32.wrapping_shl(b_headrm as u32);
|
||||
let b32_inv = (i32::MAX >> 2) / (b32_nrm >> 16);
|
||||
let mut result = b32_inv << 16;
|
||||
let err_q32 = ((1i32 << 29).wrapping_sub(smulwb(b32_nrm, b32_inv))).wrapping_shl(3);
|
||||
result = result.wrapping_add(((err_q32 as i64 * b32_inv as i64) >> 16) as i32);
|
||||
let lshift = 61 - b_headrm - q_res;
|
||||
if lshift <= 0 {
|
||||
let s = (-lshift) as u32;
|
||||
let lim_hi = i32::MAX >> s;
|
||||
let lim_lo = i32::MIN >> s;
|
||||
result.clamp(lim_lo, lim_hi) << s
|
||||
} else if lshift < 32 {
|
||||
result >> lshift
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
/// `silk_DIV32_varQ`: approximation of `(a32 << q_res) / b32`.
|
||||
pub fn div32_varq(a32: i32, b32: i32, q_res: i32) -> i32 {
|
||||
let a_headrm = a32.unsigned_abs().leading_zeros() as i32 - 1;
|
||||
let a32_nrm = a32.wrapping_shl(a_headrm as u32);
|
||||
let b_headrm = b32.unsigned_abs().leading_zeros() as i32 - 1;
|
||||
let b32_nrm = b32.wrapping_shl(b_headrm as u32);
|
||||
let b32_inv = (i32::MAX >> 2) / (b32_nrm >> 16);
|
||||
let mut result = smulwb(a32_nrm, b32_inv);
|
||||
let a32_nrm = a32_nrm.wrapping_sub(((smmul(b32_nrm, result) as i64) << 3) as i32);
|
||||
result = smlawb(result, a32_nrm, b32_inv);
|
||||
let lshift = 29 + a_headrm - b_headrm - q_res;
|
||||
if lshift < 0 {
|
||||
let s = (-lshift) as u32;
|
||||
let lim_hi = i32::MAX >> s;
|
||||
let lim_lo = i32::MIN >> s;
|
||||
result.clamp(lim_lo, lim_hi) << s
|
||||
} else if lshift < 32 {
|
||||
result >> lshift
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sqrt_approx(x: i32) -> i32 {
|
||||
if x <= 0 {
|
||||
return 0;
|
||||
}
|
||||
let lz = x.leading_zeros() as i32;
|
||||
let frac = (x.rotate_right((24 - lz).rem_euclid(32) as u32)) & 0x7f;
|
||||
let mut y = if lz & 1 != 0 { 32768 } else { 46214 };
|
||||
y >>= lz >> 1;
|
||||
smlawb(y, y, 213 * frac)
|
||||
}
|
||||
|
||||
/// Decodes the stage-1/stage-2 indices into stabilised NLSFs (Q15).
|
||||
pub fn nlsf_decode(indices: &[i32], wb: bool) -> [i32; MAX_LPC_ORDER] {
|
||||
let order = if wb { 16 } else { 10 };
|
||||
let i1 = indices[0] as usize;
|
||||
let cb1: &[i32] = if wb { &NLSF_CB_WB[i1] } else { &NLSF_CB_NBMB[i1] };
|
||||
let qstep = if wb { 9830 } else { 11796 };
|
||||
// Residual dequantisation with backwards prediction.
|
||||
let mut res_q10 = [0i32; MAX_LPC_ORDER];
|
||||
let mut out_q10 = 0i32;
|
||||
for i in (0..order).rev() {
|
||||
let pred = if i + 1 < order {
|
||||
let sel = if wb { NLSF_PSEL_WB[i1][i] } else { NLSF_PSEL_NBMB[i1][i] } as usize;
|
||||
let w = if wb { NLSF_PRED_WB[sel][i] } else { NLSF_PRED_NBMB[sel][i] };
|
||||
(out_q10 * w) >> 8
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let mut v = indices[i + 1] << 10;
|
||||
if v > 0 {
|
||||
v -= 102;
|
||||
} else if v < 0 {
|
||||
v += 102;
|
||||
}
|
||||
out_q10 = smlawb(pred, v, qstep);
|
||||
res_q10[i] = out_q10;
|
||||
}
|
||||
// Laroia weights.
|
||||
let mut nlsf = [0i32; MAX_LPC_ORDER];
|
||||
for k in 0..order {
|
||||
let prev = if k == 0 { 0 } else { cb1[k - 1] };
|
||||
let next = if k + 1 == order { 256 } else { cb1[k + 1] };
|
||||
let w = (1024 / (cb1[k] - prev).max(1) + 1024 / (next - cb1[k]).max(1)).min(32767);
|
||||
let w_q9 = sqrt_approx(w << 16);
|
||||
let v = (cb1[k] << 7) + (res_q10[k] << 14) / w_q9.max(1);
|
||||
nlsf[k] = v.clamp(0, 32767);
|
||||
}
|
||||
let dmin: &[i32] = if wb { &NLSF_MIN_SPACING_WB } else { &NLSF_MIN_SPACING_NBMB };
|
||||
nlsf_stabilize(&mut nlsf[..order], dmin);
|
||||
nlsf
|
||||
}
|
||||
|
||||
pub fn nlsf_stabilize(nlsf: &mut [i32], dmin: &[i32]) {
|
||||
let l = nlsf.len();
|
||||
for _ in 0..20 {
|
||||
let mut min_diff = nlsf[0] - dmin[0];
|
||||
let mut idx = 0;
|
||||
for i in 1..l {
|
||||
let d = nlsf[i] - (nlsf[i - 1] + dmin[i]);
|
||||
if d < min_diff {
|
||||
min_diff = d;
|
||||
idx = i;
|
||||
}
|
||||
}
|
||||
let d = 32768 - (nlsf[l - 1] + dmin[l]);
|
||||
if d < min_diff {
|
||||
min_diff = d;
|
||||
idx = l;
|
||||
}
|
||||
if min_diff >= 0 {
|
||||
return;
|
||||
}
|
||||
if idx == 0 {
|
||||
nlsf[0] = dmin[0];
|
||||
} else if idx == l {
|
||||
nlsf[l - 1] = 32768 - dmin[l];
|
||||
} else {
|
||||
let mut min_center = dmin[..idx].iter().sum::<i32>();
|
||||
min_center += dmin[idx] >> 1;
|
||||
let mut max_center = 32768 - dmin[idx + 1..=l].iter().sum::<i32>();
|
||||
max_center -= dmin[idx] >> 1;
|
||||
let c = rshift_round(nlsf[idx - 1] + nlsf[idx], 1);
|
||||
let c = if min_center > max_center { c.clamp(max_center, min_center) } else { c.clamp(min_center, max_center) };
|
||||
nlsf[idx - 1] = c - (dmin[idx] >> 1);
|
||||
nlsf[idx] = nlsf[idx - 1] + dmin[idx];
|
||||
}
|
||||
}
|
||||
// Fallback.
|
||||
nlsf.sort_unstable();
|
||||
nlsf[0] = nlsf[0].max(dmin[0]);
|
||||
for i in 1..l {
|
||||
nlsf[i] = nlsf[i].max(sat16(nlsf[i - 1] + dmin[i]));
|
||||
}
|
||||
nlsf[l - 1] = nlsf[l - 1].min(32768 - dmin[l]);
|
||||
for i in (0..l - 1).rev() {
|
||||
nlsf[i] = nlsf[i].min(nlsf[i + 1] - dmin[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
fn bwexpander_32(ar: &mut [i32], mut chirp_q16: i32) {
|
||||
let d = ar.len();
|
||||
let chirp_minus_one = chirp_q16 - 65536;
|
||||
for v in ar.iter_mut().take(d - 1) {
|
||||
*v = smulww(chirp_q16, *v);
|
||||
chirp_q16 += rshift_round(chirp_q16.wrapping_mul(chirp_minus_one), 16);
|
||||
}
|
||||
ar[d - 1] = smulww(chirp_q16, ar[d - 1]);
|
||||
}
|
||||
|
||||
fn lpc_inverse_pred_gain(a_q12: &[i32]) -> i32 {
|
||||
const QA: u32 = 24;
|
||||
const A_LIMIT: i32 = 16_773_022;
|
||||
let order = a_q12.len();
|
||||
let mut a = [0i32; MAX_LPC_ORDER];
|
||||
let mut dc = 0;
|
||||
for k in 0..order {
|
||||
dc += a_q12[k];
|
||||
a[k] = a_q12[k] << (QA - 12);
|
||||
}
|
||||
if dc >= 4096 {
|
||||
return 0;
|
||||
}
|
||||
let mut inv_gain: i32 = 1 << 30;
|
||||
let mul32_frac_q31 = |x: i32, y: i32| rshift_round64(x as i64 * y as i64, 31) as i32;
|
||||
for k in (1..order).rev() {
|
||||
if a[k] > A_LIMIT || a[k] < -A_LIMIT {
|
||||
return 0;
|
||||
}
|
||||
let rc_q31 = -(a[k] << (31 - QA));
|
||||
let rc_mult1 = (1i32 << 30) - smmul(rc_q31, rc_q31);
|
||||
inv_gain = smmul(inv_gain, rc_mult1) << 2;
|
||||
if inv_gain < 107_374 {
|
||||
return 0;
|
||||
}
|
||||
let mult2q = 32 - rc_mult1.unsigned_abs().leading_zeros() as i32;
|
||||
let rc_mult2 = inverse32_varq(rc_mult1, mult2q + 30);
|
||||
for n in 0..(k + 1) >> 1 {
|
||||
let t1 = a[n];
|
||||
let t2 = a[k - n - 1];
|
||||
let v1 = rshift_round64(t1.saturating_sub(mul32_frac_q31(t2, rc_q31)) as i64 * rc_mult2 as i64, mult2q as u32);
|
||||
if v1 > i32::MAX as i64 || v1 < i32::MIN as i64 {
|
||||
return 0;
|
||||
}
|
||||
let v2 = rshift_round64(t2.saturating_sub(mul32_frac_q31(t1, rc_q31)) as i64 * rc_mult2 as i64, mult2q as u32);
|
||||
if v2 > i32::MAX as i64 || v2 < i32::MIN as i64 {
|
||||
return 0;
|
||||
}
|
||||
a[n] = v1 as i32;
|
||||
a[k - n - 1] = v2 as i32;
|
||||
}
|
||||
}
|
||||
if a[0] > A_LIMIT || a[0] < -A_LIMIT {
|
||||
return 0;
|
||||
}
|
||||
let rc_q31 = -(a[0] << (31 - QA));
|
||||
let rc_mult1 = (1i32 << 30) - smmul(rc_q31, rc_q31);
|
||||
inv_gain = smmul(inv_gain, rc_mult1) << 2;
|
||||
if inv_gain < 107_374 {
|
||||
return 0;
|
||||
}
|
||||
inv_gain
|
||||
}
|
||||
|
||||
/// Converts NLSFs (Q15) to stable Q12 LPC coefficients (`silk_NLSF2A`).
|
||||
pub fn nlsf2a(nlsf: &[i32]) -> [i32; MAX_LPC_ORDER] {
|
||||
let d = nlsf.len();
|
||||
let ordering: &[usize] = if d == 16 { &LSF_ORDER_WB } else { &LSF_ORDER_NBMB };
|
||||
let mut c_q17 = [0i32; MAX_LPC_ORDER];
|
||||
for k in 0..d {
|
||||
let i = (nlsf[k] >> 8) as usize;
|
||||
let f = nlsf[k] & 255;
|
||||
let c0 = COS_Q12[i];
|
||||
let c1 = COS_Q12[(i + 1).min(128)];
|
||||
c_q17[ordering[k]] = (c0 * 256 + (c1 - c0) * f + 4) >> 3;
|
||||
}
|
||||
let d2 = d / 2;
|
||||
let find_poly = |off: usize| -> [i64; 10] {
|
||||
let mut out = [0i64; 10];
|
||||
out[0] = 1 << 16;
|
||||
out[1] = -(c_q17[off] as i64);
|
||||
for k in 1..d2 {
|
||||
let ftmp = c_q17[2 * k + off] as i64;
|
||||
out[k + 1] = 2 * out[k - 1] - ((ftmp * out[k] + 32768) >> 16);
|
||||
for n in (2..=k).rev() {
|
||||
out[n] += out[n - 2] - ((ftmp * out[n - 1] + 32768) >> 16);
|
||||
}
|
||||
out[1] -= ftmp;
|
||||
}
|
||||
out
|
||||
};
|
||||
let p = find_poly(0);
|
||||
let q = find_poly(1);
|
||||
let mut a32 = [0i32; MAX_LPC_ORDER];
|
||||
for k in 0..d2 {
|
||||
let ptmp = p[k + 1] + p[k];
|
||||
let qtmp = q[k + 1] - q[k];
|
||||
a32[k] = (-qtmp - ptmp) as i32;
|
||||
a32[d - k - 1] = (qtmp - ptmp) as i32;
|
||||
}
|
||||
let a32 = &mut a32[..d];
|
||||
// Limit the dynamic range (silk_LPC_fit with QIN = 17, QOUT = 12).
|
||||
let mut a_q12 = [0i32; MAX_LPC_ORDER];
|
||||
let mut i = 0;
|
||||
while i < 10 {
|
||||
let mut maxabs = 0i32;
|
||||
let mut idx = 0;
|
||||
for (k, &v) in a32.iter().enumerate() {
|
||||
let av = v.saturating_abs();
|
||||
if av > maxabs {
|
||||
maxabs = av;
|
||||
idx = k;
|
||||
}
|
||||
}
|
||||
let maxabs = rshift_round(maxabs, 5);
|
||||
if maxabs > 32767 {
|
||||
let maxabs = maxabs.min(163_838);
|
||||
let chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (idx as i32 + 1)) >> 2);
|
||||
bwexpander_32(a32, chirp);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
if i == 10 {
|
||||
for k in 0..d {
|
||||
a_q12[k] = sat16(rshift_round(a32[k], 5));
|
||||
a32[k] = a_q12[k] << 5;
|
||||
}
|
||||
} else {
|
||||
for k in 0..d {
|
||||
a_q12[k] = rshift_round(a32[k], 5);
|
||||
}
|
||||
}
|
||||
for i in 0..16 {
|
||||
if lpc_inverse_pred_gain(&a_q12[..d]) == 0 {
|
||||
bwexpander_32(a32, 65536 - (2 << i));
|
||||
for k in 0..d {
|
||||
a_q12[k] = rshift_round(a32[k], 5);
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
a_q12
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn flat_nlsf_gives_small_lpc() {
|
||||
// Uniformly spaced NLSFs correspond to an (almost) flat spectrum.
|
||||
let nlsf: Vec<i32> = (1..=16).map(|k| k * 32768 / 17).collect();
|
||||
let a = nlsf2a(&nlsf);
|
||||
assert!(a.iter().all(|v| v.abs() < 600), "{a:?}");
|
||||
let nlsf: Vec<i32> = (1..=10).map(|k| k * 32768 / 11).collect();
|
||||
let a = nlsf2a(&nlsf);
|
||||
assert!(a[..10].iter().all(|v| v.abs() < 600), "{a:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stabilize_enforces_spacing() {
|
||||
let mut v = [100, 90, 80, 30000, 30001, 30002, 31000, 32000, 32700, 32760];
|
||||
nlsf_stabilize(&mut v, &NLSF_MIN_SPACING_NBMB);
|
||||
assert!(v[0] >= NLSF_MIN_SPACING_NBMB[0]);
|
||||
for i in 1..10 {
|
||||
assert!(v[i] - v[i - 1] >= NLSF_MIN_SPACING_NBMB[i], "{v:?}");
|
||||
}
|
||||
assert!(32768 - v[9] >= NLSF_MIN_SPACING_NBMB[10]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inverse_approximations() {
|
||||
for &b in &[65536, 100_000, 1 << 20, 81920, 1_686_110_208] {
|
||||
let inv = inverse32_varq(b, 47) as f64;
|
||||
let exact = 2f64.powi(47) / b as f64;
|
||||
assert!((inv - exact).abs() / exact < 1e-3, "{b}");
|
||||
}
|
||||
let q = div32_varq(65536, 131072, 16) as f64;
|
||||
assert!((q - 32768.0).abs() < 2.0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,862 @@
|
||||
//! SILK layer encoder (RFC 6716 §4.2, encoder direction), 20 ms frames at 8, 12 or 16 kHz.
|
||||
//!
|
||||
//! Each frame is analysed (VAD, open-loop pitch, windowed-autocorrelation LPC, LTP codebook
|
||||
//! search), its NLSFs are quantised, and the excitation is chosen sample by sample by running
|
||||
//! the decoder's exact fixed-point reconstruction (LTP and LPC synthesis with the dequantised
|
||||
//! Q12/Q14 coefficients, quantisation offsets and the pseudo-random sign sequence), so the
|
||||
//! encoder always holds the decoder's state and there is no drift. A noise-feedback loop
|
||||
//! shapes the coding noise with a bandwidth-expanded LPC envelope, and a per-frame search over
|
||||
//! a gain offset hits the bit budget. Stereo input is coded as mid/side with the decoder's
|
||||
//! stereo prediction (§4.2.8). No LBRR, no NLSF interpolation.
|
||||
|
||||
pub(crate) mod analysis;
|
||||
pub(crate) mod lpc;
|
||||
mod nlsf;
|
||||
mod pulses;
|
||||
#[allow(dead_code)]
|
||||
pub(crate) mod tables;
|
||||
|
||||
use lpc::{MAX_LPC_ORDER, div32_varq, inverse32_varq, nlsf2a, rshift_round, sat16, smlawb, smulwb, smulww};
|
||||
use tables::{
|
||||
DELTA_GAIN, GAIN_MSB, LTP_FILTER, LTP_PERIODICITY, LTP_SCALE, LTP_TAPS_0, LTP_TAPS_1, LTP_TAPS_2, MID_ONLY, NLSF_EXT, NLSF_INTERP, NLSF_SEL_NBMB,
|
||||
NLSF_SEL_WB, NLSF_STAGE1, NLSF_STAGE2, PITCH_CONTOUR, PITCH_HIGH, PITCH_LOW, STEREO_PRED_JOINT, STEREO_WEIGHTS_Q13, TYPE_ACTIVE, TYPE_INACTIVE,
|
||||
UNIFORM3, UNIFORM4, UNIFORM5, UNIFORM8,
|
||||
};
|
||||
|
||||
use crate::opus::encoder::range::RangeEncoder;
|
||||
|
||||
const MAX_FRAME: usize = 320;
|
||||
const LTP_ORDER: usize = 5;
|
||||
/// Frame RMS (16-bit scale) below which a frame is coded as inactive (VAD off).
|
||||
const VAD_RMS: f32 = 30.0;
|
||||
/// Normalised pitch correlation above which a frame is a voicing candidate.
|
||||
const VOICING_THRESHOLD: f64 = 0.45;
|
||||
/// Minimum LTP prediction gain (energy ratio) for a frame to be coded as voiced.
|
||||
const MIN_LTP_GAIN: f64 = 1.3;
|
||||
/// Rate penalty (squared pulse units per unit magnitude) of the excitation quantiser.
|
||||
const RD_LAMBDA: f64 = 0.15;
|
||||
/// Extra gain offset (log2) for inactive frames.
|
||||
const INACTIVE_OFFSET: f64 = 1.0;
|
||||
/// Noise feedback: the coding noise is shaped by `1 / A(z/γ)` (A the short-term predictor).
|
||||
/// γ = 0 leaves it white (closed-loop DPCM, best waveform SNR at high rates), γ = 1 shapes it
|
||||
/// like the spectral envelope (open-loop residual coding, needed at low rates). γ grows with the
|
||||
/// gain offset, i.e. as the quantisation gets coarser.
|
||||
fn nf_gamma(offset: f64) -> f64 {
|
||||
(0.75 + 0.05 * offset).clamp(0.75, 0.9)
|
||||
}
|
||||
|
||||
/// `silk_log2lin` (decoder arithmetic).
|
||||
fn log2lin(in_log_q7: i32) -> i32 {
|
||||
if in_log_q7 < 0 {
|
||||
return 0;
|
||||
}
|
||||
if in_log_q7 >= 3967 {
|
||||
return i32::MAX;
|
||||
}
|
||||
let out = 1i32 << (in_log_q7 >> 7);
|
||||
let frac = in_log_q7 & 0x7F;
|
||||
if in_log_q7 < 2048 {
|
||||
out + ((out * smlawb(frac, frac * (128 - frac), -174)) >> 7)
|
||||
} else {
|
||||
out + (out >> 7) * smlawb(frac, frac * (128 - frac), -174)
|
||||
}
|
||||
}
|
||||
|
||||
fn gain_q16(index: i32) -> i32 {
|
||||
log2lin((smulwb(0x1D1C71, index) + 2090).min(3967))
|
||||
}
|
||||
|
||||
/// The decoder's LPC analysis filter on 16-bit samples.
|
||||
fn lpc_analysis_filter(out: &mut [i16], input: &[i16], b: &[i32], d: usize) {
|
||||
let len = out.len();
|
||||
for ix in d..len {
|
||||
let mut acc: i32 = 0;
|
||||
for j in 0..d {
|
||||
acc = acc.wrapping_add((input[ix - 1 - j] as i32).wrapping_mul(b[j]));
|
||||
}
|
||||
let v = ((input[ix] as i32) << 12).wrapping_sub(acc);
|
||||
out[ix] = sat16(rshift_round(v, 12)) as i16;
|
||||
}
|
||||
for v in out.iter_mut().take(d.min(len)) {
|
||||
*v = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// The part of the decoder's channel state that the reconstruction depends on.
|
||||
#[derive(Clone)]
|
||||
struct DecState {
|
||||
prev_gain_q16: i32,
|
||||
s_lpc_q14: [i32; MAX_LPC_ORDER],
|
||||
out_buf: [i16; MAX_FRAME],
|
||||
last_gain_index: i32,
|
||||
}
|
||||
|
||||
impl DecState {
|
||||
fn new() -> Self {
|
||||
DecState { prev_gain_q16: 65536, s_lpc_q14: [0; MAX_LPC_ORDER], out_buf: [0; MAX_FRAME], last_gain_index: 10 }
|
||||
}
|
||||
|
||||
/// The decoder's reset of the side channel when it is coded again after mid-only frames.
|
||||
fn reset_side(&mut self) {
|
||||
self.out_buf = [0; MAX_FRAME];
|
||||
self.s_lpc_q14 = [0; MAX_LPC_ORDER];
|
||||
self.last_gain_index = 10;
|
||||
}
|
||||
}
|
||||
|
||||
/// Coded parameters of one frame (everything except gains and pulses).
|
||||
#[derive(Clone, Default)]
|
||||
struct Params {
|
||||
vad: bool,
|
||||
signal_type: usize,
|
||||
quant_offset_type: usize,
|
||||
nlsf_ix: [i32; 17],
|
||||
a_q12: [i32; MAX_LPC_ORDER],
|
||||
lag_index: i32,
|
||||
contour: usize,
|
||||
per_index: usize,
|
||||
ltp_index: [usize; 4],
|
||||
pitch_l: [i32; 4],
|
||||
/// RMS (16-bit scale) of the open-loop prediction residual per subframe.
|
||||
res_rms: [f32; 4],
|
||||
}
|
||||
|
||||
/// Gains, seed and pulses chosen by the quantiser.
|
||||
struct Coded {
|
||||
gain_ix: [i32; 4],
|
||||
gains_q16: [i32; 4],
|
||||
seed: usize,
|
||||
pulses: Vec<i32>,
|
||||
}
|
||||
|
||||
/// Noise-feedback history (decoded − input), most recent first, as a double-length ring: each
|
||||
/// sample is stored at `pos` and `pos + MAX_LPC_ORDER`, so `buf[pos..pos + MAX_LPC_ORDER]` is
|
||||
/// always the contiguous window and a push costs two stores instead of a shift.
|
||||
struct NfRing {
|
||||
buf: [f64; 2 * MAX_LPC_ORDER],
|
||||
/// Index of the most recent sample, always below `MAX_LPC_ORDER`.
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl NfRing {
|
||||
fn new(hist: &[f64; MAX_LPC_ORDER]) -> Self {
|
||||
let mut buf = [0.0; 2 * MAX_LPC_ORDER];
|
||||
buf[..MAX_LPC_ORDER].copy_from_slice(hist);
|
||||
buf[MAX_LPC_ORDER..].copy_from_slice(hist);
|
||||
NfRing { buf, pos: 0 }
|
||||
}
|
||||
|
||||
/// The history, most recent first.
|
||||
fn window(&self) -> &[f64] {
|
||||
self.buf.get(self.pos..self.pos + MAX_LPC_ORDER).unwrap_or(&[])
|
||||
}
|
||||
|
||||
/// Makes `v` the most recent sample and drops the oldest.
|
||||
fn push(&mut self, v: f64) {
|
||||
self.pos = self.pos.checked_sub(1).unwrap_or(MAX_LPC_ORDER - 1);
|
||||
for i in [self.pos, self.pos + MAX_LPC_ORDER] {
|
||||
if let Some(slot) = self.buf.get_mut(i) {
|
||||
*slot = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The history as a plain array, most recent first.
|
||||
fn to_array(&self) -> [f64; MAX_LPC_ORDER] {
|
||||
let mut out = [0.0; MAX_LPC_ORDER];
|
||||
for (o, v) in out.iter_mut().zip(self.window()) {
|
||||
*o = *v;
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Channel {
|
||||
fs_khz: usize,
|
||||
len: usize,
|
||||
sub: usize,
|
||||
order: usize,
|
||||
dec: DecState,
|
||||
/// The last two frames of input (internal rate, 16-bit scale).
|
||||
hist: Vec<f32>,
|
||||
/// Recent output errors (decoded − input), most recent first, for noise feedback.
|
||||
nf: [f64; MAX_LPC_ORDER],
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
fn new(fs_khz: usize) -> Self {
|
||||
let len = 20 * fs_khz;
|
||||
Channel {
|
||||
fs_khz,
|
||||
len,
|
||||
sub: 5 * fs_khz,
|
||||
order: if fs_khz == 16 { 16 } else { 10 },
|
||||
dec: DecState::new(),
|
||||
hist: vec![0.0; 2 * len],
|
||||
nf: [0.0; MAX_LPC_ORDER],
|
||||
}
|
||||
}
|
||||
|
||||
fn push_history(&mut self, x: &[f32]) {
|
||||
let l = self.len;
|
||||
self.hist.copy_within(l.., 0);
|
||||
self.hist[l..].copy_from_slice(x);
|
||||
}
|
||||
|
||||
/// Frame analysis: VAD, signal type, pitch, LPC/NLSF quantisation and LTP taps.
|
||||
fn analyze(&self, x: &[f32]) -> Params {
|
||||
let l = self.len;
|
||||
let sub = self.sub;
|
||||
let order = self.order;
|
||||
let wb = order == 16;
|
||||
let mut buf = self.hist.clone();
|
||||
buf.extend_from_slice(x);
|
||||
let start = 2 * l;
|
||||
let rms = (x.iter().map(|v| v * v).sum::<f32>() / l as f32).sqrt();
|
||||
let vad = rms > VAD_RMS;
|
||||
let pitch = if vad { Some(analysis::pitch_search(&buf, start, l, self.fs_khz)) } else { None };
|
||||
let mut voiced = pitch.as_ref().is_some_and(|p| p.score > VOICING_THRESHOLD);
|
||||
|
||||
let a = analysis::lpc(&buf[start - l / 4..], order, self.fs_khz);
|
||||
let target: Vec<f64> = match analysis::a2nlsf(&a) {
|
||||
Some(w) => w.iter().map(|v| v / std::f64::consts::PI * 32768.0).collect(),
|
||||
None => (1..=order).map(|k| k as f64 * 32768.0 / (order + 1) as f64).collect(),
|
||||
};
|
||||
let quant = |signal_type: usize| {
|
||||
let (ix, nlsf) = nlsf::quantize(&target, signal_type, wb);
|
||||
(ix, nlsf2a(&nlsf[..order]))
|
||||
};
|
||||
let mut p = Params { vad, ..Default::default() };
|
||||
let (mut ix, mut a_q12) = quant(if voiced { 2 } else { 1 });
|
||||
// Open-loop residual with the quantised predictor.
|
||||
let mut r = vec![0f64; buf.len()];
|
||||
for n in order..buf.len() {
|
||||
let mut acc = buf[n] as f64;
|
||||
for j in 0..order {
|
||||
acc -= a_q12[j] as f64 / 4096.0 * buf[n - 1 - j] as f64;
|
||||
}
|
||||
r[n] = acc;
|
||||
}
|
||||
if voiced && let Some(pitch) = &pitch {
|
||||
let ltp = analysis::ltp_search(&r, start, sub, &pitch.lags);
|
||||
let gain = ltp.energy.iter().sum::<f64>() / ltp.err.iter().sum::<f64>().max(1e-9);
|
||||
if gain >= MIN_LTP_GAIN {
|
||||
p.lag_index = pitch.lag_index;
|
||||
p.contour = pitch.contour;
|
||||
p.pitch_l = pitch.lags;
|
||||
p.per_index = ltp.per_index;
|
||||
p.ltp_index = ltp.ltp_index;
|
||||
} else {
|
||||
voiced = false;
|
||||
(ix, a_q12) = quant(1);
|
||||
}
|
||||
}
|
||||
// Gains follow the short-term residual level (the long-term predictor then only
|
||||
// reduces the number of pulses), which keeps the noise-to-signal ratio steady.
|
||||
for k in 0..4 {
|
||||
let e: f64 = r[start + k * sub..start + (k + 1) * sub].iter().map(|v| v * v).sum();
|
||||
p.res_rms[k] = (e / sub as f64).sqrt() as f32;
|
||||
}
|
||||
p.signal_type = if !vad {
|
||||
0
|
||||
} else if voiced {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
p.quant_offset_type = 0;
|
||||
p.nlsf_ix = ix;
|
||||
p.a_q12 = a_q12;
|
||||
p
|
||||
}
|
||||
|
||||
/// Chooses gains and pulses for target `x` by running the decoder's reconstruction, and
|
||||
/// advances the mirrored decoder state. `offset` scales the gains (log2), `floor` gives
|
||||
/// minimum gains (Q16) per subframe. Returns the coded values and the decoded frame.
|
||||
fn quantize(&mut self, p: &Params, x: &[f32], offset: f64, floor: Option<&[i32; 4]>, seed: usize) -> (Coded, Vec<i16>) {
|
||||
let l = self.len;
|
||||
let sub = self.sub;
|
||||
let order = self.order;
|
||||
let st = &mut self.dec;
|
||||
let mut ring = NfRing::new(&self.nf);
|
||||
let off = if p.signal_type == 0 { offset + INACTIVE_OFFSET } else { offset };
|
||||
let gamma = nf_gamma(off);
|
||||
let mut nf_c = [0f64; MAX_LPC_ORDER];
|
||||
let mut gpow = 1.0;
|
||||
for j in 0..order {
|
||||
gpow *= gamma;
|
||||
nf_c[j] = p.a_q12[j] as f64 / 4096.0 * gpow;
|
||||
}
|
||||
// Gains (independent coding of the first subframe, deltas for the rest).
|
||||
let mut gain_ix = [0i32; 4];
|
||||
let mut gains_q16 = [0i32; 4];
|
||||
let mut prev = st.last_gain_index;
|
||||
for k in 0..4 {
|
||||
let mut g = p.res_rms[k] as f64 * off.exp2();
|
||||
if let Some(f) = floor {
|
||||
g = g.max(f[k] as f64 / 65536.0);
|
||||
}
|
||||
let want = (((g * 65536.0).max(1.0).log2() * 128.0 - 2090.0) / (1_907_825.0 / 65536.0)).round().clamp(0.0, 63.0) as i32;
|
||||
if k == 0 {
|
||||
gain_ix[0] = want;
|
||||
prev = want.max(prev - 16).clamp(0, 63);
|
||||
} else {
|
||||
let mut best = (i32::MAX, 0i32, prev);
|
||||
for ind in 0..41 {
|
||||
let tmp = ind - 4;
|
||||
let thr = 2 * 36 - 64 + prev;
|
||||
let d = if tmp > thr { prev + (tmp << 1) - thr } else { prev + tmp }.clamp(0, 63);
|
||||
let e = (d - want).abs();
|
||||
if e < best.0 {
|
||||
best = (e, ind, d);
|
||||
}
|
||||
}
|
||||
gain_ix[k] = best.1;
|
||||
prev = best.2;
|
||||
}
|
||||
gains_q16[k] = gain_q16(prev);
|
||||
}
|
||||
st.last_gain_index = prev;
|
||||
|
||||
// Analysis-by-synthesis excitation quantisation through the decoder's exact synthesis.
|
||||
let voiced = p.signal_type == 2;
|
||||
let offset_q10 = [[100, 240], [32, 100]][p.signal_type >> 1][p.quant_offset_type];
|
||||
let off16 = offset_q10 << 4;
|
||||
let mut seed_v = seed as i32;
|
||||
let mut pulses = vec![0i32; l];
|
||||
let mut xq = vec![0i16; l];
|
||||
let mut s_lpc = [0i32; MAX_FRAME / 4 + MAX_LPC_ORDER];
|
||||
s_lpc[..MAX_LPC_ORDER].copy_from_slice(&st.s_lpc_q14);
|
||||
let mut s_ltp = [0i16; MAX_FRAME];
|
||||
let mut s_ltp_q15 = [0i32; 2 * MAX_FRAME];
|
||||
let ltp_mem = l;
|
||||
let mut ltp_buf_idx = ltp_mem;
|
||||
let a_q12 = &p.a_q12;
|
||||
let ltp_scale_q14 = 15565;
|
||||
for k in 0..4 {
|
||||
let mut b_q14 = [0i32; LTP_ORDER];
|
||||
if voiced {
|
||||
let ix = p.ltp_index[k];
|
||||
let taps = match p.per_index {
|
||||
0 => LTP_TAPS_0[ix],
|
||||
1 => LTP_TAPS_1[ix],
|
||||
_ => LTP_TAPS_2[ix],
|
||||
};
|
||||
for i in 0..LTP_ORDER {
|
||||
b_q14[i] = taps[i] << 7;
|
||||
}
|
||||
}
|
||||
let gain_q10 = gains_q16[k] >> 6;
|
||||
let mut inv_gain_q31 = inverse32_varq(gains_q16[k], 47);
|
||||
let gain_adj_q16 = if gains_q16[k] != st.prev_gain_q16 {
|
||||
let g = div32_varq(st.prev_gain_q16, gains_q16[k], 16);
|
||||
for v in s_lpc[..MAX_LPC_ORDER].iter_mut() {
|
||||
*v = smulww(g, *v);
|
||||
}
|
||||
g
|
||||
} else {
|
||||
1 << 16
|
||||
};
|
||||
st.prev_gain_q16 = gains_q16[k];
|
||||
let lag = p.pitch_l[k];
|
||||
if voiced {
|
||||
let lag_u = lag as usize;
|
||||
if k == 0 {
|
||||
let start_idx = (ltp_mem as isize - lag as isize - order as isize - (LTP_ORDER / 2) as isize).max(0) as usize;
|
||||
lpc_analysis_filter(&mut s_ltp[start_idx..ltp_mem], &st.out_buf[start_idx..ltp_mem], a_q12, order);
|
||||
inv_gain_q31 = smulwb(inv_gain_q31, ltp_scale_q14) << 2;
|
||||
for i in 0..(lag_u + LTP_ORDER / 2).min(ltp_mem) {
|
||||
s_ltp_q15[ltp_buf_idx - i - 1] = smulwb(inv_gain_q31, s_ltp[ltp_mem - i - 1] as i32);
|
||||
}
|
||||
} else if gain_adj_q16 != 1 << 16 {
|
||||
for i in 0..(lag_u + LTP_ORDER / 2).min(ltp_buf_idx) {
|
||||
let q = ltp_buf_idx - i - 1;
|
||||
s_ltp_q15[q] = smulww(gain_adj_q16, s_ltp_q15[q]);
|
||||
}
|
||||
}
|
||||
}
|
||||
let inv_g = 16_777_216.0 / gain_q10.max(1) as f64;
|
||||
for i in 0..sub {
|
||||
let n = k * sub + i;
|
||||
seed_v = seed_v.wrapping_mul(196_314_165).wrapping_add(907_633_515);
|
||||
let ltp_q14 = if voiced {
|
||||
let base = ltp_buf_idx as isize - lag as isize + (LTP_ORDER / 2) as isize;
|
||||
let g = |o: isize| -> i32 { if base - o >= 0 { s_ltp_q15[(base - o) as usize] } else { 0 } };
|
||||
let mut pred = 2i32;
|
||||
for o in 0..LTP_ORDER {
|
||||
pred = smlawb(pred, g(o as isize), b_q14[o]);
|
||||
}
|
||||
pred << 1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let mut lpc_pred = (order >> 1) as i32;
|
||||
for j in 0..order {
|
||||
lpc_pred = smlawb(lpc_pred, s_lpc[MAX_LPC_ORDER + i - 1 - j], a_q12[j]);
|
||||
}
|
||||
let lpc_q14 = lpc_pred.saturating_mul(16);
|
||||
// Target excitation (Q14) for this sample.
|
||||
let fb: f64 = nf_c.iter().zip(ring.window()).take(order).map(|(c, e)| c * e).sum();
|
||||
let tgt = (x[n] as f64 + fb).clamp(-32768.0, 32767.0) * inv_g - lpc_q14 as f64 - ltp_q14 as f64;
|
||||
let sgn = if seed_v < 0 { -1.0 } else { 1.0 };
|
||||
let u = sgn * tgt;
|
||||
let pf = (u - off16 as f64) / 16384.0;
|
||||
let p0 = if pf > 0.0 { (pf + 0.078_125).round() } else { (pf - 0.078_125).round() } as i32;
|
||||
let level = |q: i32| -> i32 {
|
||||
let mut e = q << 14;
|
||||
if e > 0 {
|
||||
e -= 80 << 4;
|
||||
} else if e < 0 {
|
||||
e += 80 << 4;
|
||||
}
|
||||
e + off16
|
||||
};
|
||||
let mut best = (f64::MAX, 0i32);
|
||||
for q in [p0 - 1, p0, p0 + 1, 0] {
|
||||
let q = q.clamp(-pulses::MAX_PULSE, pulses::MAX_PULSE);
|
||||
let d = (level(q) as f64 - u) / 16384.0;
|
||||
let cost = d * d + RD_LAMBDA * q.abs() as f64;
|
||||
if cost < best.0 {
|
||||
best = (cost, q);
|
||||
}
|
||||
}
|
||||
let q = best.1;
|
||||
pulses[n] = q;
|
||||
let mut e = level(q);
|
||||
if seed_v < 0 {
|
||||
e = -e;
|
||||
}
|
||||
seed_v = seed_v.wrapping_add(q);
|
||||
let res = e.wrapping_add(ltp_q14);
|
||||
if voiced {
|
||||
s_ltp_q15[ltp_buf_idx] = res << 1;
|
||||
ltp_buf_idx += 1;
|
||||
}
|
||||
let v = res.saturating_add(lpc_q14);
|
||||
s_lpc[MAX_LPC_ORDER + i] = v;
|
||||
xq[n] = sat16(rshift_round(smulww(v, gain_q10), 8)) as i16;
|
||||
ring.push(xq[n] as f64 - x[n] as f64);
|
||||
}
|
||||
s_lpc.copy_within(sub..sub + MAX_LPC_ORDER, 0);
|
||||
}
|
||||
self.nf = ring.to_array();
|
||||
st.s_lpc_q14.copy_from_slice(&s_lpc[..MAX_LPC_ORDER]);
|
||||
st.out_buf[..l].copy_from_slice(&xq);
|
||||
(Coded { gain_ix, gains_q16, seed, pulses }, xq)
|
||||
}
|
||||
|
||||
/// Writes the frame's indices and pulses (`decode_indices` + `decode_pulses` order, coded
|
||||
/// independently).
|
||||
fn write(&self, enc: &mut RangeEncoder, p: &Params, c: &Coded) {
|
||||
let ix = p.signal_type * 2 + p.quant_offset_type;
|
||||
if p.vad {
|
||||
enc.icdf(ix - 2, &TYPE_ACTIVE, 8);
|
||||
} else {
|
||||
enc.icdf(ix, &TYPE_INACTIVE, 8);
|
||||
}
|
||||
enc.icdf((c.gain_ix[0] >> 3) as usize, GAIN_MSB[p.signal_type], 8);
|
||||
enc.icdf((c.gain_ix[0] & 7) as usize, &UNIFORM8, 8);
|
||||
for k in 1..4 {
|
||||
enc.icdf(c.gain_ix[k] as usize, &DELTA_GAIN, 8);
|
||||
}
|
||||
let wb = self.order == 16;
|
||||
let i1 = p.nlsf_ix[0] as usize;
|
||||
enc.icdf(i1, NLSF_STAGE1[(p.signal_type >> 1) + if wb { 2 } else { 0 }], 8);
|
||||
for i in 0..self.order {
|
||||
let cb = if wb { 8 + NLSF_SEL_WB[i1][i] as usize } else { NLSF_SEL_NBMB[i1][i] as usize };
|
||||
let v = p.nlsf_ix[i + 1] + 4;
|
||||
if v <= 0 {
|
||||
enc.icdf(0, NLSF_STAGE2[cb], 8);
|
||||
enc.icdf((-v) as usize, &NLSF_EXT, 8);
|
||||
} else if v >= 8 {
|
||||
enc.icdf(8, NLSF_STAGE2[cb], 8);
|
||||
enc.icdf((v - 8) as usize, &NLSF_EXT, 8);
|
||||
} else {
|
||||
enc.icdf(v as usize, NLSF_STAGE2[cb], 8);
|
||||
}
|
||||
}
|
||||
// No NLSF interpolation.
|
||||
enc.icdf(4, &NLSF_INTERP, 8);
|
||||
if p.signal_type == 2 {
|
||||
let half = (self.fs_khz >> 1) as i32;
|
||||
enc.icdf((p.lag_index / half) as usize, &PITCH_HIGH, 8);
|
||||
let low = match self.fs_khz {
|
||||
8 => PITCH_LOW[0],
|
||||
12 => PITCH_LOW[1],
|
||||
_ => PITCH_LOW[2],
|
||||
};
|
||||
enc.icdf((p.lag_index % half) as usize, low, 8);
|
||||
enc.icdf(p.contour, if self.fs_khz == 8 { PITCH_CONTOUR[1] } else { PITCH_CONTOUR[3] }, 8);
|
||||
enc.icdf(p.per_index, <P_PERIODICITY, 8);
|
||||
for k in 0..4 {
|
||||
enc.icdf(p.ltp_index[k], LTP_FILTER[p.per_index], 8);
|
||||
}
|
||||
// LTP scaling index 0 (15565 in Q14), matching `quantize`.
|
||||
enc.icdf(0, <P_SCALE, 8);
|
||||
}
|
||||
enc.icdf(c.seed, &UNIFORM4, 8);
|
||||
pulses::encode(enc, &c.pulses, p.signal_type, p.quant_offset_type);
|
||||
}
|
||||
}
|
||||
|
||||
/// Quantises a stereo prediction weight (Q13) to the decoder's grid (§4.2.7.1): returns the
|
||||
/// (UNIFORM3, UNIFORM5, joint) indices and the reconstructed value.
|
||||
fn quant_pred(w: f64) -> ([usize; 3], i32) {
|
||||
let mut best = (f64::MAX, [0usize; 3], 0i32);
|
||||
for i in 0..15 {
|
||||
let low = STEREO_WEIGHTS_Q13[i];
|
||||
let step = smulwb(STEREO_WEIGHTS_Q13[i + 1] - low, 6554);
|
||||
for j in 0..5 {
|
||||
let v = low + step * (2 * j as i32 + 1);
|
||||
let e = (v as f64 - w).abs();
|
||||
if e < best.0 {
|
||||
best = (e, [i % 3, j, i / 3], v);
|
||||
}
|
||||
}
|
||||
}
|
||||
(best.1, best.2)
|
||||
}
|
||||
|
||||
/// Per-frame preparation shared by every rate-loop trial.
|
||||
struct Prep {
|
||||
x: [Vec<f32>; 2],
|
||||
params: [Params; 2],
|
||||
mid_only: bool,
|
||||
pred_ix: [[usize; 3]; 2],
|
||||
pred_q13: [i32; 2],
|
||||
}
|
||||
|
||||
/// One rate-loop trial: the frame coded at gain offset `offset`.
|
||||
struct Trial {
|
||||
offset: f64,
|
||||
bits: i32,
|
||||
state: SilkEncoder,
|
||||
coder: RangeEncoder,
|
||||
}
|
||||
|
||||
/// SILK encoder for one mono or stereo (mid/side) stream.
|
||||
#[derive(Clone)]
|
||||
pub struct SilkEncoder {
|
||||
fs_khz: usize,
|
||||
channels: usize,
|
||||
ch: [Channel; 2],
|
||||
pred_prev_q13: [i32; 2],
|
||||
prev_mid_only: bool,
|
||||
/// Last mid sample of the previous frame (for the stereo predictor's low-pass).
|
||||
prev_mid: f32,
|
||||
frame: usize,
|
||||
/// Gain offset chosen for the previous frame (search starting point).
|
||||
pub last_offset: f64,
|
||||
}
|
||||
|
||||
impl SilkEncoder {
|
||||
pub fn new(fs_khz: usize, channels: usize) -> Self {
|
||||
SilkEncoder {
|
||||
fs_khz,
|
||||
channels,
|
||||
ch: [Channel::new(fs_khz), Channel::new(fs_khz)],
|
||||
pred_prev_q13: [0; 2],
|
||||
prev_mid_only: false,
|
||||
prev_mid: 0.0,
|
||||
frame: 0,
|
||||
last_offset: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Samples per channel of one 20 ms frame at the internal rate.
|
||||
pub fn frame_len(&self) -> usize {
|
||||
20 * self.fs_khz
|
||||
}
|
||||
|
||||
fn prepare(&self, input: &[Vec<f32>]) -> Prep {
|
||||
let l = self.frame_len();
|
||||
if self.channels == 1 {
|
||||
let x = input[0].clone();
|
||||
let p = self.ch[0].analyze(&x);
|
||||
return Prep { x: [x, vec![0.0; l]], params: [p, Params::default()], mid_only: true, pred_ix: [[0; 3]; 2], pred_q13: [0; 2] };
|
||||
}
|
||||
let mid: Vec<f32> = (0..l).map(|i| 0.5 * (input[0][i] + input[1][i])).collect();
|
||||
let side: Vec<f32> = (0..l).map(|i| 0.5 * (input[0][i] - input[1][i])).collect();
|
||||
let lp = |j: usize| -> f64 {
|
||||
let a = if j == 0 { self.prev_mid } else { mid[j - 1] };
|
||||
let c = if j + 1 < l { mid[j + 1] } else { mid[l - 1] };
|
||||
(a as f64 + 2.0 * mid[j] as f64 + c as f64) / 4.0
|
||||
};
|
||||
// Least-squares prediction of the side from the low-passed mid and the mid.
|
||||
let (mut r00, mut r01, mut r11, mut c0, mut c1, mut ss, mut mm) = (0f64, 0f64, 0f64, 0f64, 0f64, 0f64, 0f64);
|
||||
for j in 0..l {
|
||||
let (a, b, s) = (lp(j), mid[j] as f64, side[j] as f64);
|
||||
r00 += a * a;
|
||||
r01 += a * b;
|
||||
r11 += b * b;
|
||||
c0 += a * s;
|
||||
c1 += b * s;
|
||||
ss += s * s;
|
||||
mm += b * b;
|
||||
}
|
||||
// Ridge regularisation: the low-passed mid and the mid are nearly collinear.
|
||||
let lam = 0.02 * 0.5 * (r00 + r11) + 1e-9;
|
||||
let (a00, a11) = (r00 + lam, r11 + lam);
|
||||
let det = a00 * a11 - r01 * r01;
|
||||
let (p0, p1) = ((c0 * a11 - c1 * r01) / det, (c1 * a00 - c0 * r01) / det);
|
||||
let lim = 13732.0;
|
||||
let quant = |p0: f64, p1: f64| {
|
||||
let (ix0, q0) = quant_pred(((p0 + p1) * 8192.0).clamp(-lim, lim));
|
||||
let (ix1, q1) = quant_pred((p1 * 8192.0).clamp(-lim, lim));
|
||||
([ix0, ix1], [q0 - q1, q1])
|
||||
};
|
||||
// Side residual with the decoder's interpolated predictor.
|
||||
let fs = self.fs_khz as i32;
|
||||
let denom_q16 = (1 << 16) / (8 * fs);
|
||||
let interp = 8 * self.fs_khz;
|
||||
let residual = |pred_q13: [i32; 2]| -> Vec<f32> {
|
||||
let d0 = rshift_round((pred_q13[0] - self.pred_prev_q13[0]) as i16 as i32 * denom_q16 as i16 as i32, 16);
|
||||
let d1 = rshift_round((pred_q13[1] - self.pred_prev_q13[1]) as i16 as i32 * denom_q16 as i16 as i32, 16);
|
||||
let (mut pp0, mut pp1) = (self.pred_prev_q13[0] + d0, self.pred_prev_q13[1] + d1);
|
||||
(0..l)
|
||||
.map(|j| {
|
||||
if j + 1 < interp {
|
||||
pp0 += d0;
|
||||
pp1 += d1;
|
||||
} else {
|
||||
pp0 = pred_q13[0];
|
||||
pp1 = pred_q13[1];
|
||||
}
|
||||
(side[j] as f64 - (pp0 as f64 * lp(j) + pp1 as f64 * mid[j] as f64) / 8192.0) as f32
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
let energy = |v: &[f32]| -> f64 { v.iter().map(|&x| (x as f64).powi(2)).sum() };
|
||||
let (mut pred_ix, mut pred_q13) = quant(p0, p1);
|
||||
let mut res = residual(pred_q13);
|
||||
let mut res_e = energy(&res);
|
||||
// Never predict worse than not predicting at all.
|
||||
if res_e > ss {
|
||||
(pred_ix, pred_q13) = quant(0.0, 0.0);
|
||||
res = residual(pred_q13);
|
||||
res_e = energy(&res);
|
||||
}
|
||||
let res_rms = (res_e / l as f64).sqrt();
|
||||
let mid_rms = (mm / l as f64).sqrt();
|
||||
let mid_only = res_rms < 0.01 * mid_rms + 1.0;
|
||||
let pm = self.ch[0].analyze(&mid);
|
||||
let ps = if mid_only { Params::default() } else { self.ch[1].analyze(&res) };
|
||||
Prep { x: [mid, res], params: [pm, ps], mid_only, pred_ix, pred_q13 }
|
||||
}
|
||||
|
||||
/// Codes one frame into `enc` with gain offset `offset`.
|
||||
fn code(&mut self, prep: &Prep, offset: f64, enc: &mut RangeEncoder) {
|
||||
let stereo = self.channels == 2;
|
||||
let side_vad = stereo && !prep.mid_only && prep.params[1].vad;
|
||||
enc.bit_logp(prep.params[0].vad, 1);
|
||||
enc.bit_logp(false, 1);
|
||||
if stereo {
|
||||
enc.bit_logp(side_vad, 1);
|
||||
enc.bit_logp(false, 1);
|
||||
let [a, b] = prep.pred_ix;
|
||||
enc.icdf(5 * a[2] + b[2], &STEREO_PRED_JOINT, 8);
|
||||
enc.icdf(a[0], &UNIFORM3, 8);
|
||||
enc.icdf(a[1], &UNIFORM5, 8);
|
||||
enc.icdf(b[0], &UNIFORM3, 8);
|
||||
enc.icdf(b[1], &UNIFORM5, 8);
|
||||
if !side_vad {
|
||||
enc.icdf(prep.mid_only as usize, &MID_ONLY, 8);
|
||||
}
|
||||
}
|
||||
let seed = self.frame & 3;
|
||||
let (cm, _) = self.ch[0].quantize(&prep.params[0], &prep.x[0], offset, None, seed);
|
||||
self.ch[0].write(enc, &prep.params[0], &cm);
|
||||
if stereo {
|
||||
if !prep.mid_only {
|
||||
if self.prev_mid_only {
|
||||
self.ch[1].dec.reset_side();
|
||||
}
|
||||
let (cs, _) = self.ch[1].quantize(&prep.params[1], &prep.x[1], offset, Some(&cm.gains_q16), seed);
|
||||
self.ch[1].write(enc, &prep.params[1], &cs);
|
||||
}
|
||||
self.pred_prev_q13 = prep.pred_q13;
|
||||
self.prev_mid_only = prep.mid_only;
|
||||
}
|
||||
}
|
||||
|
||||
/// Encodes one 20 ms frame (`input`: one internal-rate, 16-bit-scale vector per channel)
|
||||
/// into `enc`, aiming at `target_bits` total and never exceeding `max_bits`.
|
||||
pub fn encode(&mut self, input: &[Vec<f32>], target_bits: f64, max_bits: i32, enc: &mut RangeEncoder) {
|
||||
let prep = self.prepare(input);
|
||||
let start_bits = enc.tell();
|
||||
// Every trial codes the frame into a clone of the encoder and the range coder. The result
|
||||
// is kept per offset: the hard-limit check below hits the memo for the chosen offset, and
|
||||
// the final step adopts that trial instead of coding the frame once more.
|
||||
let mut trials: Vec<Trial> = Vec::new();
|
||||
let mut trial = |s: &Self, o: f64| -> i32 {
|
||||
if let Some(t) = trials.iter().find(|t| t.offset == o) {
|
||||
return t.bits;
|
||||
}
|
||||
let mut state = s.clone();
|
||||
let mut coder = enc.clone();
|
||||
state.code(&prep, o, &mut coder);
|
||||
let bits = coder.tell() - start_bits;
|
||||
trials.push(Trial { offset: o, bits, state, coder });
|
||||
bits
|
||||
};
|
||||
// Bisection on the gain offset: the smallest offset (finest quantisation) within budget.
|
||||
let target = target_bits.min(max_bits as f64);
|
||||
// Bits are not strictly monotonic in the offset (very coarse quantisation feeds large
|
||||
// errors back), so walk an integer grid from the previous frame's offset to find a
|
||||
// bracket [infeasible, feasible], then bisect inside it.
|
||||
const MIN_O: f64 = -3.0;
|
||||
const MAX_O: f64 = 6.0;
|
||||
let mut best_any = (i32::MAX, MAX_O);
|
||||
let mut probe = |o: f64| -> bool {
|
||||
let b = trial(self, o);
|
||||
if b < best_any.0 {
|
||||
best_any = (b, o);
|
||||
}
|
||||
b as f64 <= target
|
||||
};
|
||||
let g0 = self.last_offset.round().clamp(MIN_O, MAX_O);
|
||||
let mut bracket = None;
|
||||
if probe(g0) {
|
||||
let mut hi = g0;
|
||||
loop {
|
||||
if hi - 1.0 < MIN_O {
|
||||
bracket = Some((None, hi));
|
||||
break;
|
||||
}
|
||||
if probe(hi - 1.0) {
|
||||
hi -= 1.0;
|
||||
} else {
|
||||
bracket = Some((Some(hi - 1.0), hi));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let mut lo = g0;
|
||||
while lo + 1.0 <= MAX_O {
|
||||
if probe(lo + 1.0) {
|
||||
bracket = Some((Some(lo), lo + 1.0));
|
||||
break;
|
||||
}
|
||||
lo += 1.0;
|
||||
}
|
||||
}
|
||||
let mut o = match bracket {
|
||||
Some((Some(mut lo), mut hi)) => {
|
||||
for _ in 0..4 {
|
||||
let m = 0.5 * (lo + hi);
|
||||
if probe(m) {
|
||||
hi = m;
|
||||
} else {
|
||||
lo = m;
|
||||
}
|
||||
}
|
||||
hi
|
||||
}
|
||||
Some((None, hi)) => hi,
|
||||
None => best_any.1,
|
||||
};
|
||||
// Hard limit (hybrid packets must leave room for CELT).
|
||||
while trial(self, o) > max_bits && o < 12.0 {
|
||||
o += 1.0;
|
||||
}
|
||||
match trials.iter().position(|t| t.offset == o) {
|
||||
Some(i) => {
|
||||
let t = trials.swap_remove(i);
|
||||
*self = t.state;
|
||||
*enc = t.coder;
|
||||
}
|
||||
None => self.code(&prep, o, enc),
|
||||
}
|
||||
self.end_frame(&prep, o);
|
||||
}
|
||||
|
||||
/// Advances the input history and the frame counter after frame `prep` was coded at `o`.
|
||||
fn end_frame(&mut self, prep: &Prep, o: f64) {
|
||||
self.last_offset = o;
|
||||
for ch in 0..self.channels {
|
||||
self.ch[ch].push_history(&prep.x[ch]);
|
||||
}
|
||||
if self.channels == 2 {
|
||||
self.prev_mid = prep.x[0][self.frame_len() - 1];
|
||||
}
|
||||
self.frame += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Deterministic speech-like test input: two harmonics with a moving pitch, a syllable
|
||||
/// envelope, noise and short silences, at the internal rate and 16-bit scale.
|
||||
fn signal(len: usize, fs_khz: usize, seed: u32) -> Vec<f32> {
|
||||
let mut s = seed;
|
||||
let fs = (fs_khz * 1000) as f64;
|
||||
(0..len)
|
||||
.map(|i| {
|
||||
s = s.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
|
||||
let noise = (s >> 8) as f64 / (1u32 << 24) as f64 - 0.5;
|
||||
let t = i as f64 / fs;
|
||||
let f0 = 140.0 + 40.0 * (t * 1.3).sin();
|
||||
let env = ((t * 4.0).sin().max(0.0)).powf(0.7);
|
||||
let voiced = (std::f64::consts::TAU * f0 * t).sin() + 0.5 * (std::f64::consts::TAU * 2.0 * f0 * t).sin();
|
||||
(8000.0 * env * voiced + 600.0 * noise) as f32
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nf_ring_matches_a_shifted_history() {
|
||||
let mut hist = [0f64; MAX_LPC_ORDER];
|
||||
for (i, h) in hist.iter_mut().enumerate() {
|
||||
*h = i as f64 * 0.5 - 3.0;
|
||||
}
|
||||
let mut ring = NfRing::new(&hist);
|
||||
assert_eq!(ring.window(), &hist[..]);
|
||||
for n in 0..100 {
|
||||
let v = (n as f64 * 0.37).sin() * 100.0;
|
||||
hist.copy_within(0..MAX_LPC_ORDER - 1, 1);
|
||||
hist[0] = v;
|
||||
ring.push(v);
|
||||
assert_eq!(ring.window(), &hist[..], "after push {n}");
|
||||
}
|
||||
assert_eq!(ring.to_array(), hist);
|
||||
}
|
||||
|
||||
/// The rate loop adopts the memoised trial for the chosen offset. A second encoder that codes
|
||||
/// every frame afresh at that offset (what the loop did before the memo) must produce the
|
||||
/// same bytes, frame after frame, so the adopted encoder and range-coder states are exact.
|
||||
#[test]
|
||||
fn adopting_the_memoised_trial_equals_coding_the_frame_again() {
|
||||
for (fs_khz, channels, target_bits, max_bits) in
|
||||
[(8, 1, 200.0, 1000), (12, 1, 320.0, 1000), (16, 1, 480.0, 1000), (16, 2, 640.0, 1000), (16, 1, 900.0, 400), (8, 2, 120.0, 160)]
|
||||
{
|
||||
let mut memo = SilkEncoder::new(fs_khz, channels);
|
||||
let mut fresh = memo.clone();
|
||||
let l = memo.frame_len();
|
||||
let frames = 40;
|
||||
let left = signal(l * frames, fs_khz, 7);
|
||||
let right: Vec<f32> = signal(l * frames + 37, fs_khz, 11)[37..].iter().map(|v| v * 0.6).collect();
|
||||
for f in 0..frames {
|
||||
let mut input = vec![left[f * l..(f + 1) * l].to_vec()];
|
||||
if channels == 2 {
|
||||
input.push(right[f * l..(f + 1) * l].to_vec());
|
||||
}
|
||||
let mut enc = RangeEncoder::new();
|
||||
memo.encode(&input, target_bits, max_bits, &mut enc);
|
||||
let o = memo.last_offset;
|
||||
let prep = fresh.prepare(&input);
|
||||
let mut enc_fresh = RangeEncoder::new();
|
||||
fresh.code(&prep, o, &mut enc_fresh);
|
||||
fresh.end_frame(&prep, o);
|
||||
assert_eq!(enc.tell(), enc_fresh.tell(), "{fs_khz} kHz {channels} ch frame {f}");
|
||||
assert_eq!(enc.done(1275), enc_fresh.done(1275), "{fs_khz} kHz {channels} ch frame {f}");
|
||||
for c in 0..channels {
|
||||
assert_eq!(memo.ch[c].nf, fresh.ch[c].nf, "noise feedback, channel {c}, frame {f}");
|
||||
assert_eq!(memo.ch[c].dec.s_lpc_q14, fresh.ch[c].dec.s_lpc_q14, "LPC state, channel {c}, frame {f}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
//! NLSF quantisation (encoder side of RFC 6716 §4.2.7.5): stage-1 vector codebook preselection
|
||||
//! and a backwards-predictive stage-2 scalar quantiser that reproduces the decoder's residual
|
||||
//! dequantisation exactly. The search strategy is our own.
|
||||
|
||||
use super::lpc::{MAX_LPC_ORDER, nlsf_decode, smlawb, sqrt_approx};
|
||||
use super::tables::{
|
||||
NLSF_CB_NBMB, NLSF_CB_WB, NLSF_EXT, NLSF_PRED_NBMB, NLSF_PRED_WB, NLSF_PSEL_NBMB, NLSF_PSEL_WB, NLSF_SEL_NBMB, NLSF_SEL_WB, NLSF_STAGE1,
|
||||
NLSF_STAGE2,
|
||||
};
|
||||
|
||||
/// Number of stage-1 candidates refined with stage 2.
|
||||
const SURVIVORS: usize = 8;
|
||||
/// Rate weight (weighted squared Q15 error per bit).
|
||||
const LAMBDA: f64 = 60.0;
|
||||
|
||||
fn icdf_bits(icdf: &[u8], s: usize) -> f64 {
|
||||
let hi = if s == 0 { 256 } else { icdf[s - 1] as u32 };
|
||||
let p = (hi - icdf[s] as u32).max(1) as f64 / 256.0;
|
||||
-p.log2()
|
||||
}
|
||||
|
||||
/// Bits of a stage-2 index `q` (-10..=10) with stage-2 table `tab`.
|
||||
fn stage2_bits(tab: &[u8], q: i32) -> f64 {
|
||||
let v = q + 4;
|
||||
if v <= 0 {
|
||||
icdf_bits(tab, 0) + icdf_bits(&NLSF_EXT, (-v) as usize)
|
||||
} else if v >= 8 {
|
||||
icdf_bits(tab, 8) + icdf_bits(&NLSF_EXT, (v - 8) as usize)
|
||||
} else {
|
||||
icdf_bits(tab, v as usize)
|
||||
}
|
||||
}
|
||||
|
||||
/// Quantises target NLSFs (`target`, Q15 as f64, ascending) and returns the 1 + order indices
|
||||
/// in the decoder's layout (`[stage1, stage2...]`) together with the reconstructed NLSFs.
|
||||
pub fn quantize(target: &[f64], signal_type: usize, wb: bool) -> ([i32; 17], [i32; MAX_LPC_ORDER]) {
|
||||
let order = target.len();
|
||||
// Laroia-style weights.
|
||||
let w: Vec<f64> = (0..order)
|
||||
.map(|k| {
|
||||
let prev = if k == 0 { 0.0 } else { target[k - 1] };
|
||||
let next = if k + 1 == order { 32768.0 } else { target[k + 1] };
|
||||
1.0 / (target[k] - prev).max(50.0) + 1.0 / (next - target[k]).max(50.0)
|
||||
})
|
||||
.collect();
|
||||
let s1_tab = NLSF_STAGE1[(signal_type >> 1) + if wb { 2 } else { 0 }];
|
||||
let n_cb = 32;
|
||||
let mut cands: Vec<(f64, usize)> = (0..n_cb)
|
||||
.map(|i1| {
|
||||
let cb: &[i32] = if wb { &NLSF_CB_WB[i1] } else { &NLSF_CB_NBMB[i1] };
|
||||
let d: f64 = (0..order).map(|k| w[k] * ((cb[k] << 7) as f64 - target[k]).powi(2)).sum();
|
||||
(d + LAMBDA * icdf_bits(s1_tab, i1), i1)
|
||||
})
|
||||
.collect();
|
||||
cands.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
let qstep = if wb { 9830 } else { 11796 };
|
||||
let mut best = (f64::MAX, [0i32; 17], [0i32; MAX_LPC_ORDER]);
|
||||
for &(_, i1) in cands.iter().take(SURVIVORS) {
|
||||
let cb1: &[i32] = if wb { &NLSF_CB_WB[i1] } else { &NLSF_CB_NBMB[i1] };
|
||||
let mut ix = [0i32; 17];
|
||||
ix[0] = i1 as i32;
|
||||
let mut bits = icdf_bits(s1_tab, i1);
|
||||
// Exact decoder weights per coefficient.
|
||||
let w_q9: Vec<i32> = (0..order)
|
||||
.map(|k| {
|
||||
let prev = if k == 0 { 0 } else { cb1[k - 1] };
|
||||
let next = if k + 1 == order { 256 } else { cb1[k + 1] };
|
||||
let ww = (1024 / (cb1[k] - prev).max(1) + 1024 / (next - cb1[k]).max(1)).min(32767);
|
||||
sqrt_approx(ww << 16).max(1)
|
||||
})
|
||||
.collect();
|
||||
let mut out_q10 = 0i32;
|
||||
for i in (0..order).rev() {
|
||||
let pred = if i + 1 < order {
|
||||
let sel = if wb { NLSF_PSEL_WB[i1][i] } else { NLSF_PSEL_NBMB[i1][i] } as usize;
|
||||
let wp = if wb { NLSF_PRED_WB[sel][i] } else { NLSF_PRED_NBMB[sel][i] };
|
||||
(out_q10 * wp) >> 8
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let cb = if wb { 8 + NLSF_SEL_WB[i1][i] as usize } else { NLSF_SEL_NBMB[i1][i] as usize };
|
||||
let tab = NLSF_STAGE2[cb];
|
||||
let want_q10 = (target[i] - (cb1[i] << 7) as f64) * w_q9[i] as f64 / 16384.0;
|
||||
let step = qstep as f64 * 1024.0 / 65536.0;
|
||||
let q0 = ((want_q10 - pred as f64) / step).round() as i32;
|
||||
let mut bq = (f64::MAX, 0i32, 0i32);
|
||||
for q in (q0 - 1).max(-10)..=(q0 + 1).min(10) {
|
||||
let mut v = q << 10;
|
||||
if v > 0 {
|
||||
v -= 102;
|
||||
} else if v < 0 {
|
||||
v += 102;
|
||||
}
|
||||
let o = smlawb(pred, v, qstep);
|
||||
let nl = (cb1[i] << 7) as f64 + ((o << 14) / w_q9[i]) as f64;
|
||||
let cost = w[i] * (nl - target[i]).powi(2) + LAMBDA * stage2_bits(tab, q);
|
||||
if cost < bq.0 {
|
||||
bq = (cost, q, o);
|
||||
}
|
||||
}
|
||||
ix[i + 1] = bq.1;
|
||||
out_q10 = bq.2;
|
||||
bits += stage2_bits(tab, bq.1);
|
||||
}
|
||||
let nlsf = nlsf_decode(&ix, wb);
|
||||
let d: f64 = (0..order).map(|k| w[k] * (nlsf[k] as f64 - target[k]).powi(2)).sum();
|
||||
let cost = d + LAMBDA * bits;
|
||||
if cost < best.0 {
|
||||
best = (cost, ix, nlsf);
|
||||
}
|
||||
}
|
||||
(best.1, best.2)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn quantised_nlsfs_track_the_target() {
|
||||
for wb in [false, true] {
|
||||
let order = if wb { 16 } else { 10 };
|
||||
// A smooth, plausible NLSF vector.
|
||||
let target: Vec<f64> = (0..order).map(|k| 32768.0 * (k as f64 + 0.8 + 0.3 * (k as f64).sin()) / (order as f64 + 1.0)).collect();
|
||||
let (ix, nlsf) = quantize(&target, 1, wb);
|
||||
assert!(ix[1..=order].iter().all(|v| (-10..=10).contains(v)));
|
||||
let max_err = (0..order).map(|k| (nlsf[k] as f64 - target[k]).abs()).fold(0.0, f64::max);
|
||||
assert!(max_err < 1200.0, "wb={wb} max err {max_err}: {:?} vs {target:?}", &nlsf[..order]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
//! Excitation coding (encoder side of RFC 6716 §4.2.7.8): rate level, per-block pulse counts
|
||||
//! with LSB escapes, the recursive shell code, LSBs and signs, written in exactly the order the
|
||||
//! decoder reads them.
|
||||
|
||||
use super::tables::{EXC_LSB, EXC_SIGN, PULSE_COUNT, RATE_LEVEL, SHELL_2, SHELL_4, SHELL_8, SHELL_16};
|
||||
use crate::opus::encoder::range::RangeEncoder;
|
||||
|
||||
/// Largest pulse magnitude the quantiser produces (keeps every 16-sample block codable).
|
||||
pub const MAX_PULSE: i32 = 500;
|
||||
|
||||
pub fn icdf_bits(icdf: &[u8], s: usize) -> f32 {
|
||||
let hi = if s == 0 { 256 } else { icdf[s - 1] as u32 };
|
||||
let p = (hi - icdf[s] as u32).max(1) as f32 / 256.0;
|
||||
-p.log2()
|
||||
}
|
||||
|
||||
fn count_table(nls: usize) -> &'static [u8] {
|
||||
if nls == 10 { PULSE_COUNT[10] } else { PULSE_COUNT[9] }
|
||||
}
|
||||
|
||||
fn shell(enc: &mut RangeEncoder, v: &[i32], count: i32) {
|
||||
let n = v.len();
|
||||
if n == 1 || count == 0 {
|
||||
return;
|
||||
}
|
||||
let tab = match n {
|
||||
16 => SHELL_16[(count - 1) as usize],
|
||||
8 => SHELL_8[(count - 1) as usize],
|
||||
4 => SHELL_4[(count - 1) as usize],
|
||||
_ => SHELL_2[(count - 1) as usize],
|
||||
};
|
||||
let (a, b) = v.split_at(n / 2);
|
||||
let left: i32 = a.iter().sum();
|
||||
enc.icdf(left as usize, tab, 8);
|
||||
shell(enc, a, left);
|
||||
shell(enc, b, count - left);
|
||||
}
|
||||
|
||||
/// Codes the frame's pulses (`len` a multiple of 16).
|
||||
pub fn encode(enc: &mut RangeEncoder, pulses: &[i32], signal_type: usize, quant_offset_type: usize) {
|
||||
let nblk = pulses.len() / 16;
|
||||
let mut nls = vec![0usize; nblk];
|
||||
let mut sums = vec![0i32; nblk];
|
||||
let mut hi = vec![0i32; pulses.len()];
|
||||
for b in 0..nblk {
|
||||
let blk = &pulses[b * 16..(b + 1) * 16];
|
||||
let mut s = 0usize;
|
||||
loop {
|
||||
let sum: i32 = blk.iter().map(|p| p.abs() >> s).sum();
|
||||
if sum <= 16 || s == 10 {
|
||||
sums[b] = sum.min(16);
|
||||
break;
|
||||
}
|
||||
s += 1;
|
||||
}
|
||||
nls[b] = s;
|
||||
for (h, p) in hi[b * 16..(b + 1) * 16].iter_mut().zip(blk) {
|
||||
*h = p.abs() >> s;
|
||||
}
|
||||
}
|
||||
// Rate level with the cheapest pulse-count symbols.
|
||||
let rl_tab = RATE_LEVEL[signal_type >> 1];
|
||||
let mut best = (f32::MAX, 0usize);
|
||||
for rl in 0..9 {
|
||||
let mut bits = icdf_bits(rl_tab, rl);
|
||||
for b in 0..nblk {
|
||||
bits += if nls[b] == 0 { icdf_bits(PULSE_COUNT[rl], sums[b] as usize) } else { icdf_bits(PULSE_COUNT[rl], 17) };
|
||||
}
|
||||
if bits < best.0 {
|
||||
best = (bits, rl);
|
||||
}
|
||||
}
|
||||
let rl = best.1;
|
||||
enc.icdf(rl, rl_tab, 8);
|
||||
for b in 0..nblk {
|
||||
if nls[b] == 0 {
|
||||
enc.icdf(sums[b] as usize, PULSE_COUNT[rl], 8);
|
||||
} else {
|
||||
enc.icdf(17, PULSE_COUNT[rl], 8);
|
||||
for j in 1..nls[b] {
|
||||
enc.icdf(17, count_table(j), 8);
|
||||
}
|
||||
enc.icdf(sums[b] as usize, count_table(nls[b]), 8);
|
||||
}
|
||||
}
|
||||
for b in 0..nblk {
|
||||
if sums[b] > 0 {
|
||||
shell(enc, &hi[b * 16..(b + 1) * 16], sums[b]);
|
||||
}
|
||||
}
|
||||
for b in 0..nblk {
|
||||
if nls[b] > 0 {
|
||||
for p in &pulses[b * 16..(b + 1) * 16] {
|
||||
let a = p.abs();
|
||||
for j in (0..nls[b]).rev() {
|
||||
enc.icdf(((a >> j) & 1) as usize, &EXC_LSB, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let base = 7 * (quant_offset_type + (signal_type << 1));
|
||||
for b in 0..nblk {
|
||||
let p = sums[b] | ((nls[b] as i32) << 5);
|
||||
if p > 0 {
|
||||
let tab = EXC_SIGN[base + ((p & 0x1F) as usize).min(6)];
|
||||
for &q in &pulses[b * 16..(b + 1) * 16] {
|
||||
if q != 0 {
|
||||
enc.icdf((q > 0) as usize, tab, 8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,543 @@
|
||||
//! SILK probability tables and codebooks, transcribed from RFC 6716 §4.2 (Tables 4–53).
|
||||
//!
|
||||
//! PDFs are written as in the RFC (`{...}/256`) and converted at compile time to the inverse-CDF
|
||||
//! form consumed by the range coder.
|
||||
//!
|
||||
//! Copied from FilmCraft's first-party, spec-derived `filmcraft-opus` decoder (MIT OR
|
||||
//! Apache-2.0) so encoder and decoder share exactly the same values.
|
||||
|
||||
/// Converts a PDF (summing to 256) into an inverse CDF (`256 - cdf[i+1]`).
|
||||
pub const fn icdf<const N: usize>(pdf: &[u8; N]) -> [u8; N] {
|
||||
let mut out = [0u8; N];
|
||||
let mut acc: u32 = 0;
|
||||
let mut i = 0;
|
||||
while i < N {
|
||||
acc += pdf[i] as u32;
|
||||
out[i] = (256 - acc) as u8;
|
||||
i += 1;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub const STEREO_PRED_JOINT: [u8; 25] = icdf(&[7, 2, 1, 1, 1, 10, 24, 8, 1, 1, 3, 23, 92, 23, 3, 1, 1, 8, 24, 10, 1, 1, 1, 2, 7]);
|
||||
pub const UNIFORM3: [u8; 3] = icdf(&[85, 86, 85]);
|
||||
pub const UNIFORM5: [u8; 5] = icdf(&[51, 51, 52, 51, 51]);
|
||||
pub const STEREO_WEIGHTS_Q13: [i32; 16] = [-13732, -10050, -8266, -7526, -6500, -5000, -2950, -820, 820, 2950, 5000, 6500, 7526, 8266, 10050, 13732];
|
||||
pub const MID_ONLY: [u8; 2] = icdf(&[192, 64]);
|
||||
pub const TYPE_INACTIVE: [u8; 2] = icdf(&[26, 230]);
|
||||
pub const TYPE_ACTIVE: [u8; 4] = icdf(&[24, 74, 148, 10]);
|
||||
pub const GAIN_MSB: [&[u8]; 3] =
|
||||
[&icdf::<8>(&[32, 112, 68, 29, 12, 1, 1, 1]), &icdf::<8>(&[2, 17, 45, 60, 62, 47, 19, 4]), &icdf::<8>(&[1, 3, 26, 71, 94, 50, 9, 2])];
|
||||
pub const UNIFORM8: [u8; 8] = icdf(&[32, 32, 32, 32, 32, 32, 32, 32]);
|
||||
pub const DELTA_GAIN: [u8; 41] =
|
||||
icdf(&[6, 5, 11, 31, 132, 21, 8, 4, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
|
||||
pub const NLSF_STAGE1: [&[u8]; 4] = [
|
||||
&icdf::<32>(&[44, 34, 30, 19, 21, 12, 11, 3, 3, 2, 16, 2, 2, 1, 5, 2, 1, 3, 3, 1, 1, 2, 2, 2, 3, 1, 9, 9, 2, 7, 2, 1]),
|
||||
&icdf::<32>(&[1, 10, 1, 8, 3, 8, 8, 14, 13, 14, 1, 14, 12, 13, 11, 11, 12, 11, 10, 10, 11, 8, 9, 8, 7, 8, 1, 1, 6, 1, 6, 5]),
|
||||
&icdf::<32>(&[31, 21, 3, 17, 1, 8, 17, 4, 1, 18, 16, 4, 2, 3, 1, 10, 1, 3, 16, 11, 16, 2, 2, 3, 2, 11, 1, 4, 9, 8, 7, 3]),
|
||||
&icdf::<32>(&[1, 4, 16, 5, 18, 11, 5, 14, 15, 1, 3, 12, 13, 14, 14, 6, 14, 12, 2, 6, 1, 12, 12, 11, 10, 3, 10, 5, 1, 1, 1, 3]),
|
||||
];
|
||||
pub const NLSF_STAGE2: [&[u8]; 16] = [
|
||||
&icdf::<9>(&[1, 1, 1, 15, 224, 11, 1, 1, 1]),
|
||||
&icdf::<9>(&[1, 1, 2, 34, 183, 32, 1, 1, 1]),
|
||||
&icdf::<9>(&[1, 1, 4, 42, 149, 55, 2, 1, 1]),
|
||||
&icdf::<9>(&[1, 1, 8, 52, 123, 61, 8, 1, 1]),
|
||||
&icdf::<9>(&[1, 3, 16, 53, 101, 74, 6, 1, 1]),
|
||||
&icdf::<9>(&[1, 3, 17, 55, 90, 73, 15, 1, 1]),
|
||||
&icdf::<9>(&[1, 7, 24, 53, 74, 67, 26, 3, 1]),
|
||||
&icdf::<9>(&[1, 1, 18, 63, 78, 58, 30, 6, 1]),
|
||||
&icdf::<9>(&[1, 1, 1, 9, 232, 9, 1, 1, 1]),
|
||||
&icdf::<9>(&[1, 1, 2, 28, 186, 35, 1, 1, 1]),
|
||||
&icdf::<9>(&[1, 1, 3, 42, 152, 53, 2, 1, 1]),
|
||||
&icdf::<9>(&[1, 1, 10, 49, 126, 65, 2, 1, 1]),
|
||||
&icdf::<9>(&[1, 4, 19, 48, 100, 77, 5, 1, 1]),
|
||||
&icdf::<9>(&[1, 1, 14, 54, 100, 72, 12, 1, 1]),
|
||||
&icdf::<9>(&[1, 1, 15, 61, 87, 61, 25, 4, 1]),
|
||||
&icdf::<9>(&[1, 7, 21, 50, 77, 81, 17, 1, 1]),
|
||||
];
|
||||
pub const NLSF_SEL_NBMB: [[u8; 10]; 32] = [
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[1, 3, 1, 2, 2, 1, 2, 1, 1, 1],
|
||||
[2, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
||||
[1, 2, 2, 2, 2, 1, 2, 1, 1, 1],
|
||||
[2, 3, 3, 3, 3, 2, 2, 2, 2, 2],
|
||||
[0, 5, 3, 3, 2, 2, 2, 2, 1, 1],
|
||||
[0, 2, 2, 2, 2, 2, 2, 2, 2, 1],
|
||||
[2, 3, 6, 4, 4, 4, 5, 4, 5, 5],
|
||||
[2, 4, 5, 5, 4, 5, 4, 6, 4, 4],
|
||||
[2, 4, 4, 7, 4, 5, 4, 5, 5, 4],
|
||||
[4, 3, 3, 3, 2, 3, 2, 2, 2, 2],
|
||||
[1, 5, 5, 6, 4, 5, 4, 5, 5, 5],
|
||||
[2, 7, 4, 6, 5, 5, 5, 5, 5, 5],
|
||||
[2, 7, 5, 5, 5, 5, 5, 6, 5, 4],
|
||||
[3, 3, 5, 4, 4, 5, 4, 5, 4, 4],
|
||||
[2, 3, 3, 5, 5, 4, 4, 4, 4, 4],
|
||||
[2, 4, 4, 6, 4, 5, 4, 5, 5, 5],
|
||||
[2, 5, 4, 6, 5, 5, 5, 4, 5, 4],
|
||||
[2, 7, 4, 5, 4, 5, 4, 5, 5, 5],
|
||||
[2, 5, 4, 6, 7, 6, 5, 6, 5, 4],
|
||||
[3, 6, 7, 4, 6, 5, 5, 6, 4, 5],
|
||||
[2, 7, 6, 4, 4, 4, 5, 4, 5, 5],
|
||||
[4, 5, 5, 4, 6, 6, 5, 6, 5, 4],
|
||||
[2, 5, 5, 6, 5, 6, 4, 6, 4, 4],
|
||||
[4, 5, 5, 5, 3, 7, 4, 5, 5, 4],
|
||||
[2, 3, 4, 5, 5, 6, 4, 5, 5, 4],
|
||||
[2, 3, 2, 3, 3, 4, 2, 3, 3, 3],
|
||||
[1, 1, 2, 2, 2, 2, 2, 3, 2, 2],
|
||||
[4, 5, 5, 6, 6, 6, 5, 6, 4, 5],
|
||||
[3, 5, 5, 4, 4, 4, 4, 3, 3, 2],
|
||||
[2, 5, 3, 7, 5, 5, 4, 4, 5, 4],
|
||||
[4, 4, 5, 4, 5, 6, 5, 6, 5, 4],
|
||||
];
|
||||
pub const NLSF_SEL_WB: [[u8; 16]; 32] = [
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[2, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 0, 3],
|
||||
[2, 5, 5, 3, 7, 4, 4, 5, 2, 5, 4, 5, 5, 4, 3, 3],
|
||||
[0, 2, 1, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1],
|
||||
[0, 6, 5, 4, 6, 4, 7, 5, 4, 4, 4, 5, 5, 4, 4, 3],
|
||||
[0, 3, 5, 5, 4, 3, 3, 5, 3, 3, 3, 3, 3, 3, 2, 4],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 2, 6, 3, 7, 2, 5, 3, 4, 5, 5, 4, 3, 3, 2, 3],
|
||||
[0, 6, 2, 6, 6, 4, 5, 4, 6, 5, 4, 4, 5, 3, 3, 3],
|
||||
[2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
||||
[2, 2, 3, 4, 5, 3, 3, 3, 3, 3, 3, 3, 2, 2, 1, 3],
|
||||
[2, 2, 3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 3],
|
||||
[3, 4, 4, 4, 6, 4, 4, 5, 3, 5, 4, 4, 5, 4, 3, 4],
|
||||
[0, 6, 4, 5, 4, 7, 5, 2, 6, 5, 7, 4, 4, 3, 5, 3],
|
||||
[0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0],
|
||||
[1, 6, 5, 7, 5, 4, 5, 3, 4, 5, 4, 4, 4, 3, 3, 4],
|
||||
[1, 3, 3, 4, 4, 3, 3, 5, 2, 3, 3, 5, 5, 5, 3, 4],
|
||||
[2, 3, 3, 2, 2, 2, 3, 2, 1, 2, 1, 2, 1, 1, 1, 4],
|
||||
[0, 2, 3, 5, 3, 3, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0],
|
||||
[3, 4, 3, 5, 3, 3, 2, 2, 1, 1, 1, 1, 1, 2, 2, 4],
|
||||
[2, 6, 3, 7, 7, 4, 5, 4, 5, 3, 5, 3, 3, 2, 3, 3],
|
||||
[2, 3, 5, 6, 6, 3, 5, 3, 4, 4, 3, 3, 3, 3, 2, 4],
|
||||
[1, 3, 3, 4, 4, 4, 4, 3, 5, 5, 5, 3, 1, 1, 1, 1],
|
||||
[2, 5, 3, 6, 6, 4, 7, 4, 4, 5, 3, 4, 4, 3, 3, 3],
|
||||
[0, 6, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 6, 6, 3, 5, 2, 5, 5, 3, 4, 4, 7, 7, 4, 4, 4],
|
||||
[3, 3, 7, 3, 5, 4, 3, 3, 3, 2, 2, 3, 3, 3, 2, 3],
|
||||
[0, 0, 1, 0, 0, 0, 2, 1, 2, 1, 1, 2, 2, 2, 1, 1],
|
||||
[0, 3, 2, 5, 3, 3, 2, 3, 2, 1, 0, 0, 1, 0, 0, 1],
|
||||
[3, 5, 5, 4, 7, 5, 3, 3, 2, 3, 2, 2, 1, 0, 1, 0],
|
||||
[2, 3, 5, 3, 4, 3, 3, 3, 2, 1, 2, 6, 4, 0, 0, 0],
|
||||
];
|
||||
pub const NLSF_EXT: [u8; 7] = icdf(&[156, 60, 24, 9, 4, 2, 1]);
|
||||
pub const NLSF_PRED_NBMB: [[i32; 9]; 2] = [[179, 138, 140, 148, 151, 149, 153, 151, 163], [116, 67, 82, 59, 92, 72, 100, 89, 92]];
|
||||
pub const NLSF_PRED_WB: [[i32; 15]; 2] = [
|
||||
[175, 148, 160, 176, 178, 173, 174, 164, 177, 174, 196, 182, 198, 192, 182],
|
||||
[68, 62, 66, 60, 72, 117, 85, 90, 118, 136, 151, 142, 160, 142, 155],
|
||||
];
|
||||
pub const NLSF_PSEL_NBMB: [[u8; 9]; 32] = [
|
||||
[0, 1, 0, 0, 0, 0, 0, 0, 0],
|
||||
[1, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[1, 1, 1, 0, 0, 0, 0, 1, 0],
|
||||
[0, 1, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 1, 0, 0, 0, 0, 0, 0, 0],
|
||||
[1, 0, 1, 1, 0, 0, 0, 1, 0],
|
||||
[0, 1, 1, 0, 0, 1, 1, 0, 0],
|
||||
[0, 0, 1, 1, 0, 1, 0, 1, 1],
|
||||
[0, 0, 1, 1, 0, 0, 1, 1, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 1, 0, 1, 1, 1, 1, 1, 0],
|
||||
[0, 1, 0, 1, 1, 1, 1, 1, 0],
|
||||
[0, 1, 1, 1, 1, 1, 1, 1, 0],
|
||||
[1, 0, 1, 1, 0, 1, 1, 1, 1],
|
||||
[0, 1, 1, 1, 1, 1, 0, 1, 0],
|
||||
[0, 0, 1, 1, 0, 1, 0, 1, 0],
|
||||
[0, 0, 1, 1, 1, 0, 1, 1, 1],
|
||||
[0, 1, 1, 0, 0, 1, 1, 1, 0],
|
||||
[0, 0, 0, 1, 1, 1, 0, 1, 0],
|
||||
[0, 1, 1, 0, 0, 1, 0, 1, 0],
|
||||
[0, 1, 1, 0, 0, 0, 1, 1, 0],
|
||||
[0, 0, 0, 0, 0, 1, 1, 1, 1],
|
||||
[0, 0, 1, 1, 0, 0, 0, 1, 1],
|
||||
[0, 0, 0, 1, 0, 1, 1, 1, 1],
|
||||
[0, 1, 1, 1, 1, 1, 1, 1, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 1, 0, 1, 1, 0, 1, 0],
|
||||
[1, 0, 0, 1, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 1, 1, 0, 1, 0, 1],
|
||||
[1, 0, 1, 1, 0, 1, 1, 1, 1],
|
||||
];
|
||||
pub const NLSF_PSEL_WB: [[u8; 15]; 32] = [
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
||||
[0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0],
|
||||
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
|
||||
[0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1],
|
||||
[0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
||||
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0],
|
||||
[0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0],
|
||||
[0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0],
|
||||
[0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1],
|
||||
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
|
||||
[0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0],
|
||||
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0],
|
||||
[0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0],
|
||||
[0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0],
|
||||
[0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
||||
[0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1],
|
||||
[0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
||||
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
|
||||
[0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0],
|
||||
];
|
||||
pub const NLSF_CB_NBMB: [[i32; 10]; 32] = [
|
||||
[12, 35, 60, 83, 108, 132, 157, 180, 206, 228],
|
||||
[15, 32, 55, 77, 101, 125, 151, 175, 201, 225],
|
||||
[19, 42, 66, 89, 114, 137, 162, 184, 209, 230],
|
||||
[12, 25, 50, 72, 97, 120, 147, 172, 200, 223],
|
||||
[26, 44, 69, 90, 114, 135, 159, 180, 205, 225],
|
||||
[13, 22, 53, 80, 106, 130, 156, 180, 205, 228],
|
||||
[15, 25, 44, 64, 90, 115, 142, 168, 196, 222],
|
||||
[19, 24, 62, 82, 100, 120, 145, 168, 190, 214],
|
||||
[22, 31, 50, 79, 103, 120, 151, 170, 203, 227],
|
||||
[21, 29, 45, 65, 106, 124, 150, 171, 196, 224],
|
||||
[30, 49, 75, 97, 121, 142, 165, 186, 209, 229],
|
||||
[19, 25, 52, 70, 93, 116, 143, 166, 192, 219],
|
||||
[26, 34, 62, 75, 97, 118, 145, 167, 194, 217],
|
||||
[25, 33, 56, 70, 91, 113, 143, 165, 196, 223],
|
||||
[21, 34, 51, 72, 97, 117, 145, 171, 196, 222],
|
||||
[20, 29, 50, 67, 90, 117, 144, 168, 197, 221],
|
||||
[22, 31, 48, 66, 95, 117, 146, 168, 196, 222],
|
||||
[24, 33, 51, 77, 116, 134, 158, 180, 200, 224],
|
||||
[21, 28, 70, 87, 106, 124, 149, 170, 194, 217],
|
||||
[26, 33, 53, 64, 83, 117, 152, 173, 204, 225],
|
||||
[27, 34, 65, 95, 108, 129, 155, 174, 210, 225],
|
||||
[20, 26, 72, 99, 113, 131, 154, 176, 200, 219],
|
||||
[34, 43, 61, 78, 93, 114, 155, 177, 205, 229],
|
||||
[23, 29, 54, 97, 124, 138, 163, 179, 209, 229],
|
||||
[30, 38, 56, 89, 118, 129, 158, 178, 200, 231],
|
||||
[21, 29, 49, 63, 85, 111, 142, 163, 193, 222],
|
||||
[27, 48, 77, 103, 133, 158, 179, 196, 215, 232],
|
||||
[29, 47, 74, 99, 124, 151, 176, 198, 220, 237],
|
||||
[33, 42, 61, 76, 93, 121, 155, 174, 207, 225],
|
||||
[29, 53, 87, 112, 136, 154, 170, 188, 208, 227],
|
||||
[24, 30, 52, 84, 131, 150, 166, 186, 203, 229],
|
||||
[37, 48, 64, 84, 104, 118, 156, 177, 201, 230],
|
||||
];
|
||||
pub const NLSF_CB_WB: [[i32; 16]; 32] = [
|
||||
[7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239],
|
||||
[13, 25, 41, 55, 69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236],
|
||||
[15, 21, 34, 51, 61, 78, 92, 106, 126, 136, 152, 167, 185, 205, 225, 240],
|
||||
[10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173, 189, 205, 221, 237],
|
||||
[17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240],
|
||||
[10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236],
|
||||
[8, 21, 37, 51, 65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218],
|
||||
[12, 15, 34, 55, 63, 78, 87, 108, 118, 131, 148, 167, 185, 203, 219, 236],
|
||||
[16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171, 186, 204, 220, 237],
|
||||
[11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241],
|
||||
[6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225],
|
||||
[11, 19, 30, 44, 57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234],
|
||||
[12, 19, 29, 46, 57, 71, 88, 100, 120, 132, 148, 165, 182, 199, 216, 233],
|
||||
[17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167, 185, 204, 222, 237],
|
||||
[14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240],
|
||||
[9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237],
|
||||
[16, 19, 36, 48, 57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236],
|
||||
[12, 17, 29, 54, 71, 81, 94, 104, 126, 136, 149, 164, 182, 201, 221, 237],
|
||||
[15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180, 194, 208, 223, 238],
|
||||
[8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239],
|
||||
[17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235],
|
||||
[14, 19, 36, 45, 61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238],
|
||||
[12, 18, 31, 45, 60, 76, 91, 107, 123, 138, 154, 171, 187, 204, 221, 236],
|
||||
[13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167, 185, 203, 220, 237],
|
||||
[17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240],
|
||||
[8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239],
|
||||
[13, 16, 41, 66, 73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241],
|
||||
[17, 25, 37, 52, 63, 75, 92, 102, 119, 132, 144, 160, 175, 191, 212, 231],
|
||||
[19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187, 200, 213, 227, 242],
|
||||
[18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239],
|
||||
[16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240],
|
||||
[15, 21, 35, 50, 61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237],
|
||||
];
|
||||
pub const NLSF_MIN_SPACING_NBMB: [i32; 11] = [250, 3, 6, 3, 3, 3, 4, 3, 3, 3, 461];
|
||||
pub const NLSF_MIN_SPACING_WB: [i32; 17] = [100, 3, 40, 3, 3, 3, 5, 14, 14, 10, 11, 3, 8, 9, 7, 3, 347];
|
||||
pub const NLSF_INTERP: [u8; 5] = icdf(&[13, 22, 29, 11, 181]);
|
||||
pub const LSF_ORDER_NBMB: [usize; 10] = [0, 9, 6, 3, 4, 5, 8, 1, 2, 7];
|
||||
pub const LSF_ORDER_WB: [usize; 16] = [0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1];
|
||||
pub const COS_Q12: [i32; 129] = [
|
||||
4096, 4095, 4091, 4085, 4076, 4065, 4052, 4036, 4017, 3997, 3973, 3948, 3920, 3889, 3857, 3822, 3784, 3745, 3703, 3659, 3613, 3564, 3513, 3461,
|
||||
3406, 3349, 3290, 3229, 3166, 3102, 3035, 2967, 2896, 2824, 2751, 2676, 2599, 2520, 2440, 2359, 2276, 2191, 2106, 2019, 1931, 1842, 1751, 1660,
|
||||
1568, 1474, 1380, 1285, 1189, 1093, 995, 897, 799, 700, 601, 501, 401, 301, 201, 101, 0, -101, -201, -301, -401, -501, -601, -700, -799, -897,
|
||||
-995, -1093, -1189, -1285, -1380, -1474, -1568, -1660, -1751, -1842, -1931, -2019, -2106, -2191, -2276, -2359, -2440, -2520, -2599, -2676, -2751,
|
||||
-2824, -2896, -2967, -3035, -3102, -3166, -3229, -3290, -3349, -3406, -3461, -3513, -3564, -3613, -3659, -3703, -3745, -3784, -3822, -3857,
|
||||
-3889, -3920, -3948, -3973, -3997, -4017, -4036, -4052, -4065, -4076, -4085, -4091, -4095, -4096,
|
||||
];
|
||||
pub const PITCH_HIGH: [u8; 32] = icdf(&[3, 3, 6, 11, 21, 30, 32, 19, 11, 10, 12, 13, 13, 12, 11, 9, 8, 7, 6, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
|
||||
pub const PITCH_LOW: [&[u8]; 3] =
|
||||
[&icdf::<4>(&[64, 64, 64, 64]), &icdf::<6>(&[43, 42, 43, 43, 42, 43]), &icdf::<8>(&[32, 32, 32, 32, 32, 32, 32, 32])];
|
||||
pub const PITCH_DELTA: [u8; 21] = icdf(&[46, 2, 2, 3, 4, 6, 10, 15, 26, 38, 30, 22, 15, 10, 7, 6, 4, 4, 2, 2, 2]);
|
||||
pub const PITCH_CONTOUR: [&[u8]; 4] = [
|
||||
&icdf::<3>(&[143, 50, 63]),
|
||||
&icdf::<11>(&[68, 12, 21, 17, 19, 22, 30, 24, 17, 16, 10]),
|
||||
&icdf::<12>(&[91, 46, 39, 19, 14, 12, 8, 7, 6, 5, 5, 4]),
|
||||
&icdf::<34>(&[33, 22, 18, 16, 15, 14, 14, 13, 13, 10, 9, 9, 8, 6, 6, 6, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1]),
|
||||
];
|
||||
pub const CB_NB_10: [[i32; 2]; 3] = [[0, 0], [1, 0], [0, 1]];
|
||||
pub const CB_NB_20: [[i32; 4]; 11] = [
|
||||
[0, 0, 0, 0],
|
||||
[2, 1, 0, -1],
|
||||
[-1, 0, 1, 2],
|
||||
[-1, 0, 0, 1],
|
||||
[-1, 0, 0, 0],
|
||||
[0, 0, 0, 1],
|
||||
[0, 0, 1, 1],
|
||||
[1, 1, 0, 0],
|
||||
[1, 0, 0, 0],
|
||||
[0, 0, 0, -1],
|
||||
[1, 0, 0, -1],
|
||||
];
|
||||
pub const CB_WB_10: [[i32; 2]; 12] = [[0, 0], [0, 1], [1, 0], [-1, 1], [1, -1], [-1, 2], [2, -1], [-2, 2], [2, -2], [-2, 3], [3, -2], [-3, 3]];
|
||||
pub const CB_WB_20: [[i32; 4]; 34] = [
|
||||
[0, 0, 0, 0],
|
||||
[0, 0, 1, 1],
|
||||
[1, 1, 0, 0],
|
||||
[-1, 0, 0, 0],
|
||||
[0, 0, 0, 1],
|
||||
[1, 0, 0, 0],
|
||||
[-1, 0, 0, 1],
|
||||
[0, 0, 0, -1],
|
||||
[-1, 0, 1, 2],
|
||||
[1, 0, 0, -1],
|
||||
[-2, -1, 1, 2],
|
||||
[2, 1, 0, -1],
|
||||
[-2, 0, 0, 2],
|
||||
[-2, 0, 1, 3],
|
||||
[2, 1, -1, -2],
|
||||
[-3, -1, 1, 3],
|
||||
[2, 0, 0, -2],
|
||||
[3, 1, 0, -2],
|
||||
[-3, -1, 2, 4],
|
||||
[-4, -1, 1, 4],
|
||||
[3, 1, -1, -3],
|
||||
[-4, -1, 2, 5],
|
||||
[4, 2, -1, -3],
|
||||
[4, 1, -1, -4],
|
||||
[-5, -1, 2, 6],
|
||||
[5, 2, -1, -4],
|
||||
[-6, -2, 2, 6],
|
||||
[-5, -2, 2, 5],
|
||||
[6, 2, -1, -5],
|
||||
[-7, -2, 3, 8],
|
||||
[6, 2, -2, -6],
|
||||
[5, 2, -2, -5],
|
||||
[8, 3, -2, -7],
|
||||
[-9, -3, 3, 9],
|
||||
];
|
||||
pub const LTP_PERIODICITY: [u8; 3] = icdf(&[77, 80, 99]);
|
||||
pub const LTP_FILTER: [&[u8]; 3] = [
|
||||
&icdf::<8>(&[185, 15, 13, 13, 9, 9, 6, 6]),
|
||||
&icdf::<16>(&[57, 34, 21, 20, 15, 13, 12, 13, 10, 10, 9, 10, 9, 8, 7, 8]),
|
||||
&icdf::<32>(&[15, 16, 14, 12, 12, 12, 11, 11, 11, 10, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7, 6, 6, 5, 4, 5, 4, 4, 4, 3, 4, 3, 2]),
|
||||
];
|
||||
pub const LTP_TAPS_0: [[i32; 5]; 8] = [
|
||||
[4, 6, 24, 7, 5],
|
||||
[0, 0, 2, 0, 0],
|
||||
[12, 28, 41, 13, -4],
|
||||
[-9, 15, 42, 25, 14],
|
||||
[1, -2, 62, 41, -9],
|
||||
[-10, 37, 65, -4, 3],
|
||||
[-6, 4, 66, 7, -8],
|
||||
[16, 14, 38, -3, 33],
|
||||
];
|
||||
pub const LTP_TAPS_1: [[i32; 5]; 16] = [
|
||||
[13, 22, 39, 23, 12],
|
||||
[-1, 36, 64, 27, -6],
|
||||
[-7, 10, 55, 43, 17],
|
||||
[1, 1, 8, 1, 1],
|
||||
[6, -11, 74, 53, -9],
|
||||
[-12, 55, 76, -12, 8],
|
||||
[-3, 3, 93, 27, -4],
|
||||
[26, 39, 59, 3, -8],
|
||||
[2, 0, 77, 11, 9],
|
||||
[-8, 22, 44, -6, 7],
|
||||
[40, 9, 26, 3, 9],
|
||||
[-7, 20, 101, -7, 4],
|
||||
[3, -8, 42, 26, 0],
|
||||
[-15, 33, 68, 2, 23],
|
||||
[-2, 55, 46, -2, 15],
|
||||
[3, -1, 21, 16, 41],
|
||||
];
|
||||
pub const LTP_TAPS_2: [[i32; 5]; 32] = [
|
||||
[-6, 27, 61, 39, 5],
|
||||
[-11, 42, 88, 4, 1],
|
||||
[-2, 60, 65, 6, -4],
|
||||
[-1, -5, 73, 56, 1],
|
||||
[-9, 19, 94, 29, -9],
|
||||
[0, 12, 99, 6, 4],
|
||||
[8, -19, 102, 46, -13],
|
||||
[3, 2, 13, 3, 2],
|
||||
[9, -21, 84, 72, -18],
|
||||
[-11, 46, 104, -22, 8],
|
||||
[18, 38, 48, 23, 0],
|
||||
[-16, 70, 83, -21, 11],
|
||||
[5, -11, 117, 22, -8],
|
||||
[-6, 23, 117, -12, 3],
|
||||
[3, -8, 95, 28, 4],
|
||||
[-10, 15, 77, 60, -15],
|
||||
[-1, 4, 124, 2, -4],
|
||||
[3, 38, 84, 24, -25],
|
||||
[2, 13, 42, 13, 31],
|
||||
[21, -4, 56, 46, -1],
|
||||
[-1, 35, 79, -13, 19],
|
||||
[-7, 65, 88, -9, -14],
|
||||
[20, 4, 81, 49, -29],
|
||||
[20, 0, 75, 3, -17],
|
||||
[5, -9, 44, 92, -8],
|
||||
[1, -3, 22, 69, 31],
|
||||
[-6, 95, 41, -12, 5],
|
||||
[39, 67, 16, -4, 1],
|
||||
[0, -6, 120, 55, -36],
|
||||
[-13, 44, 122, 4, -24],
|
||||
[81, 5, 11, 3, 7],
|
||||
[2, 0, 9, 10, 88],
|
||||
];
|
||||
pub const LTP_SCALE: [u8; 3] = icdf(&[128, 64, 64]);
|
||||
pub const UNIFORM4: [u8; 4] = icdf(&[64, 64, 64, 64]);
|
||||
pub const RATE_LEVEL: [&[u8]; 2] = [&icdf::<9>(&[15, 51, 12, 46, 45, 13, 33, 27, 14]), &icdf::<9>(&[33, 30, 36, 17, 34, 49, 18, 21, 18])];
|
||||
pub const PULSE_COUNT: [&[u8]; 11] = [
|
||||
&icdf::<18>(&[131, 74, 25, 8, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
|
||||
&icdf::<18>(&[58, 93, 60, 23, 7, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
|
||||
&icdf::<18>(&[43, 51, 46, 33, 24, 16, 11, 8, 6, 3, 3, 3, 2, 1, 1, 2, 1, 2]),
|
||||
&icdf::<18>(&[17, 52, 71, 57, 31, 12, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
|
||||
&icdf::<18>(&[6, 21, 41, 53, 49, 35, 21, 11, 6, 3, 2, 2, 1, 1, 1, 1, 1, 1]),
|
||||
&icdf::<18>(&[7, 14, 22, 28, 29, 28, 25, 20, 17, 13, 11, 9, 7, 5, 4, 4, 3, 10]),
|
||||
&icdf::<18>(&[2, 5, 14, 29, 42, 46, 41, 31, 19, 11, 6, 3, 2, 1, 1, 1, 1, 1]),
|
||||
&icdf::<18>(&[1, 2, 4, 10, 19, 29, 35, 37, 34, 28, 20, 14, 8, 5, 4, 2, 2, 2]),
|
||||
&icdf::<18>(&[1, 2, 2, 5, 9, 14, 20, 24, 27, 28, 26, 23, 20, 15, 11, 8, 6, 15]),
|
||||
&icdf::<18>(&[1, 1, 1, 6, 27, 58, 56, 39, 25, 14, 10, 6, 3, 3, 2, 1, 1, 2]),
|
||||
&icdf::<18>(&[2, 1, 6, 27, 58, 56, 39, 25, 14, 10, 6, 3, 3, 2, 1, 1, 2, 0]),
|
||||
];
|
||||
pub const SHELL_16: [&[u8]; 16] = [
|
||||
&icdf::<2>(&[126, 130]),
|
||||
&icdf::<3>(&[56, 142, 58]),
|
||||
&icdf::<4>(&[25, 101, 104, 26]),
|
||||
&icdf::<5>(&[12, 60, 108, 64, 12]),
|
||||
&icdf::<6>(&[7, 35, 84, 87, 37, 6]),
|
||||
&icdf::<7>(&[4, 20, 59, 86, 63, 21, 3]),
|
||||
&icdf::<8>(&[3, 12, 38, 72, 75, 42, 12, 2]),
|
||||
&icdf::<9>(&[2, 8, 25, 54, 73, 59, 27, 7, 1]),
|
||||
&icdf::<10>(&[2, 5, 17, 39, 63, 65, 42, 18, 4, 1]),
|
||||
&icdf::<11>(&[1, 4, 12, 28, 49, 63, 54, 30, 11, 3, 1]),
|
||||
&icdf::<12>(&[1, 4, 8, 20, 37, 55, 57, 41, 22, 8, 2, 1]),
|
||||
&icdf::<13>(&[1, 3, 7, 15, 28, 44, 53, 48, 33, 16, 6, 1, 1]),
|
||||
&icdf::<14>(&[1, 2, 6, 12, 21, 35, 47, 48, 40, 25, 12, 5, 1, 1]),
|
||||
&icdf::<15>(&[1, 1, 4, 10, 17, 27, 37, 47, 43, 33, 21, 9, 4, 1, 1]),
|
||||
&icdf::<16>(&[1, 1, 1, 8, 14, 22, 33, 40, 43, 38, 28, 16, 8, 1, 1, 1]),
|
||||
&icdf::<17>(&[1, 1, 1, 1, 13, 18, 27, 36, 41, 41, 34, 24, 14, 1, 1, 1, 1]),
|
||||
];
|
||||
pub const SHELL_8: [&[u8]; 16] = [
|
||||
&icdf::<2>(&[127, 129]),
|
||||
&icdf::<3>(&[53, 149, 54]),
|
||||
&icdf::<4>(&[22, 105, 106, 23]),
|
||||
&icdf::<5>(&[11, 61, 111, 63, 10]),
|
||||
&icdf::<6>(&[6, 35, 86, 88, 36, 5]),
|
||||
&icdf::<7>(&[4, 20, 59, 87, 62, 21, 3]),
|
||||
&icdf::<8>(&[3, 13, 40, 71, 73, 41, 13, 2]),
|
||||
&icdf::<9>(&[3, 9, 27, 53, 70, 56, 28, 9, 1]),
|
||||
&icdf::<10>(&[3, 8, 19, 37, 57, 61, 44, 20, 6, 1]),
|
||||
&icdf::<11>(&[3, 7, 15, 28, 44, 54, 49, 33, 17, 5, 1]),
|
||||
&icdf::<12>(&[1, 7, 13, 22, 34, 46, 48, 38, 28, 14, 4, 1]),
|
||||
&icdf::<13>(&[1, 1, 11, 22, 27, 35, 42, 47, 33, 25, 10, 1, 1]),
|
||||
&icdf::<14>(&[1, 1, 6, 14, 26, 37, 43, 43, 37, 26, 14, 6, 1, 1]),
|
||||
&icdf::<15>(&[1, 1, 4, 10, 20, 31, 40, 42, 40, 31, 20, 10, 4, 1, 1]),
|
||||
&icdf::<16>(&[1, 1, 3, 8, 16, 26, 35, 38, 38, 35, 26, 16, 8, 3, 1, 1]),
|
||||
&icdf::<17>(&[1, 1, 2, 6, 12, 21, 30, 36, 38, 36, 30, 21, 12, 6, 2, 1, 1]),
|
||||
];
|
||||
pub const SHELL_4: [&[u8]; 16] = [
|
||||
&icdf::<2>(&[127, 129]),
|
||||
&icdf::<3>(&[49, 157, 50]),
|
||||
&icdf::<4>(&[20, 107, 109, 20]),
|
||||
&icdf::<5>(&[11, 60, 113, 62, 10]),
|
||||
&icdf::<6>(&[7, 36, 84, 87, 36, 6]),
|
||||
&icdf::<7>(&[6, 24, 57, 82, 60, 23, 4]),
|
||||
&icdf::<8>(&[5, 18, 39, 64, 68, 42, 16, 4]),
|
||||
&icdf::<9>(&[6, 14, 29, 47, 61, 52, 30, 14, 3]),
|
||||
&icdf::<10>(&[1, 15, 23, 35, 51, 50, 40, 30, 10, 1]),
|
||||
&icdf::<11>(&[1, 1, 21, 32, 42, 52, 46, 41, 18, 1, 1]),
|
||||
&icdf::<12>(&[1, 6, 16, 27, 36, 42, 42, 36, 27, 16, 6, 1]),
|
||||
&icdf::<13>(&[1, 5, 12, 21, 31, 38, 40, 38, 31, 21, 12, 5, 1]),
|
||||
&icdf::<14>(&[1, 3, 9, 17, 26, 34, 38, 38, 34, 26, 17, 9, 3, 1]),
|
||||
&icdf::<15>(&[1, 3, 7, 14, 22, 29, 34, 36, 34, 29, 22, 14, 7, 3, 1]),
|
||||
&icdf::<16>(&[1, 2, 5, 11, 18, 25, 31, 35, 35, 31, 25, 18, 11, 5, 2, 1]),
|
||||
&icdf::<17>(&[1, 1, 4, 9, 15, 21, 28, 32, 34, 32, 28, 21, 15, 9, 4, 1, 1]),
|
||||
];
|
||||
pub const SHELL_2: [&[u8]; 16] = [
|
||||
&icdf::<2>(&[128, 128]),
|
||||
&icdf::<3>(&[42, 172, 42]),
|
||||
&icdf::<4>(&[21, 107, 107, 21]),
|
||||
&icdf::<5>(&[12, 60, 112, 61, 11]),
|
||||
&icdf::<6>(&[8, 34, 86, 86, 35, 7]),
|
||||
&icdf::<7>(&[8, 23, 55, 90, 55, 20, 5]),
|
||||
&icdf::<8>(&[5, 15, 38, 72, 72, 36, 15, 3]),
|
||||
&icdf::<9>(&[6, 12, 27, 52, 77, 47, 20, 10, 5]),
|
||||
&icdf::<10>(&[6, 19, 28, 35, 40, 40, 35, 28, 19, 6]),
|
||||
&icdf::<11>(&[4, 14, 22, 31, 37, 40, 37, 31, 22, 14, 4]),
|
||||
&icdf::<12>(&[3, 10, 18, 26, 33, 38, 38, 33, 26, 18, 10, 3]),
|
||||
&icdf::<13>(&[2, 8, 13, 21, 29, 36, 38, 36, 29, 21, 13, 8, 2]),
|
||||
&icdf::<14>(&[1, 5, 10, 17, 25, 32, 38, 38, 32, 25, 17, 10, 5, 1]),
|
||||
&icdf::<15>(&[1, 4, 7, 13, 21, 29, 35, 36, 35, 29, 21, 13, 7, 4, 1]),
|
||||
&icdf::<16>(&[1, 2, 5, 10, 17, 25, 32, 36, 36, 32, 25, 17, 10, 5, 2, 1]),
|
||||
&icdf::<17>(&[1, 2, 4, 7, 13, 21, 28, 34, 36, 34, 28, 21, 13, 7, 4, 2, 1]),
|
||||
];
|
||||
pub const EXC_LSB: [u8; 2] = icdf(&[136, 120]);
|
||||
pub const EXC_SIGN: [&[u8]; 42] = [
|
||||
&icdf::<2>(&[2, 254]),
|
||||
&icdf::<2>(&[207, 49]),
|
||||
&icdf::<2>(&[189, 67]),
|
||||
&icdf::<2>(&[179, 77]),
|
||||
&icdf::<2>(&[174, 82]),
|
||||
&icdf::<2>(&[163, 93]),
|
||||
&icdf::<2>(&[157, 99]),
|
||||
&icdf::<2>(&[58, 198]),
|
||||
&icdf::<2>(&[245, 11]),
|
||||
&icdf::<2>(&[238, 18]),
|
||||
&icdf::<2>(&[232, 24]),
|
||||
&icdf::<2>(&[225, 31]),
|
||||
&icdf::<2>(&[220, 36]),
|
||||
&icdf::<2>(&[211, 45]),
|
||||
&icdf::<2>(&[1, 255]),
|
||||
&icdf::<2>(&[210, 46]),
|
||||
&icdf::<2>(&[190, 66]),
|
||||
&icdf::<2>(&[178, 78]),
|
||||
&icdf::<2>(&[169, 87]),
|
||||
&icdf::<2>(&[162, 94]),
|
||||
&icdf::<2>(&[152, 104]),
|
||||
&icdf::<2>(&[48, 208]),
|
||||
&icdf::<2>(&[242, 14]),
|
||||
&icdf::<2>(&[235, 21]),
|
||||
&icdf::<2>(&[224, 32]),
|
||||
&icdf::<2>(&[214, 42]),
|
||||
&icdf::<2>(&[205, 51]),
|
||||
&icdf::<2>(&[190, 66]),
|
||||
&icdf::<2>(&[1, 255]),
|
||||
&icdf::<2>(&[162, 94]),
|
||||
&icdf::<2>(&[152, 104]),
|
||||
&icdf::<2>(&[147, 109]),
|
||||
&icdf::<2>(&[144, 112]),
|
||||
&icdf::<2>(&[141, 115]),
|
||||
&icdf::<2>(&[138, 118]),
|
||||
&icdf::<2>(&[8, 248]),
|
||||
&icdf::<2>(&[203, 53]),
|
||||
&icdf::<2>(&[187, 69]),
|
||||
&icdf::<2>(&[176, 80]),
|
||||
&icdf::<2>(&[168, 88]),
|
||||
&icdf::<2>(&[161, 95]),
|
||||
&icdf::<2>(&[154, 102]),
|
||||
];
|
||||
pub const LBRR_FLAGS: [&[u8]; 2] = [&icdf::<3>(&[53, 53, 150]), &icdf::<7>(&[41, 20, 29, 41, 15, 28, 82])];
|
||||
@@ -0,0 +1,123 @@
|
||||
//! Constant tables of the 48 kHz CELT mode (RFC 6716 §4.3, Tables 55–58 and §4.3.2).
|
||||
//!
|
||||
//! Transcribed from RFC 6716; copied from FilmCraft's spec-derived `filmcraft-opus` decoder
|
||||
//! (first-party, MIT OR Apache-2.0) so both sides share the exact same values.
|
||||
|
||||
/// Number of energy bands.
|
||||
pub const NB_EBANDS: usize = 21;
|
||||
/// Band edges in units of 2.5 ms MDCT bins (Table 55).
|
||||
pub const EBANDS: [i32; NB_EBANDS + 1] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100];
|
||||
/// Overlap of the low-overlap window (samples at 48 kHz).
|
||||
pub const OVERLAP: usize = 120;
|
||||
/// Largest LM (20 ms frames).
|
||||
pub const MAX_LM: usize = 3;
|
||||
/// Pre-emphasis coefficient.
|
||||
pub const PREEMPH: f32 = 0.850_006_1;
|
||||
|
||||
/// Static allocation table (Table 57), indexed `[quality][band]`, in 1/32 bit per MDCT bin.
|
||||
pub const ALLOC: [[u8; NB_EBANDS]; 11] = transpose([
|
||||
[0, 90, 110, 118, 126, 134, 144, 152, 162, 172, 200],
|
||||
[0, 80, 100, 110, 119, 127, 137, 145, 155, 165, 200],
|
||||
[0, 75, 90, 103, 112, 120, 130, 138, 148, 158, 200],
|
||||
[0, 69, 84, 93, 104, 114, 124, 132, 142, 152, 200],
|
||||
[0, 63, 78, 86, 95, 103, 113, 123, 133, 143, 200],
|
||||
[0, 56, 71, 80, 89, 97, 107, 117, 127, 137, 200],
|
||||
[0, 49, 65, 75, 83, 91, 101, 111, 121, 131, 200],
|
||||
[0, 40, 58, 70, 78, 85, 95, 105, 115, 125, 200],
|
||||
[0, 34, 51, 65, 72, 78, 88, 98, 108, 118, 198],
|
||||
[0, 29, 45, 59, 66, 72, 82, 92, 102, 112, 193],
|
||||
[0, 20, 39, 53, 60, 66, 76, 86, 96, 106, 188],
|
||||
[0, 18, 32, 47, 54, 60, 70, 80, 90, 100, 183],
|
||||
[0, 10, 26, 40, 47, 54, 64, 74, 84, 94, 178],
|
||||
[0, 0, 20, 31, 39, 47, 57, 67, 77, 87, 173],
|
||||
[0, 0, 12, 23, 32, 41, 51, 61, 71, 81, 168],
|
||||
[0, 0, 0, 15, 25, 35, 45, 55, 65, 75, 163],
|
||||
[0, 0, 0, 4, 17, 29, 39, 49, 59, 69, 158],
|
||||
[0, 0, 0, 0, 12, 23, 33, 43, 53, 63, 153],
|
||||
[0, 0, 0, 0, 1, 16, 26, 36, 46, 56, 148],
|
||||
[0, 0, 0, 0, 0, 10, 15, 20, 30, 45, 129],
|
||||
[0, 0, 0, 0, 0, 1, 1, 1, 1, 20, 104],
|
||||
]);
|
||||
|
||||
const fn transpose(t: [[u8; 11]; NB_EBANDS]) -> [[u8; NB_EBANDS]; 11] {
|
||||
let mut out = [[0u8; NB_EBANDS]; 11];
|
||||
let mut b = 0;
|
||||
while b < NB_EBANDS {
|
||||
let mut q = 0;
|
||||
while q < 11 {
|
||||
out[q][b] = t[b][q];
|
||||
q += 1;
|
||||
}
|
||||
b += 1;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Mean band energy (log2 domain) removed before coarse quantisation.
|
||||
pub const E_MEANS: [f32; 25] = [
|
||||
6.4375, 6.25, 5.75, 5.3125, 5.0625, 4.8125, 4.5, 4.375, 4.875, 4.6875, 4.5625, 4.4375, 4.875, 4.625, 4.3125, 4.5, 4.375, 4.625, 4.75, 4.4375,
|
||||
3.75, 3.75, 3.75, 3.75, 3.75,
|
||||
];
|
||||
|
||||
/// Inter-frame prediction coefficient (alpha) per LM, Q15.
|
||||
pub const PRED_COEF: [f32; 4] = [29440.0 / 32768.0, 26112.0 / 32768.0, 21248.0 / 32768.0, 16384.0 / 32768.0];
|
||||
/// Inter-band prediction coefficient (beta) per LM for inter frames, Q15.
|
||||
pub const BETA_COEF: [f32; 4] = [30147.0 / 32768.0, 22282.0 / 32768.0, 12124.0 / 32768.0, 6554.0 / 32768.0];
|
||||
/// Beta for intra frames.
|
||||
pub const BETA_INTRA: f32 = 4915.0 / 32768.0;
|
||||
|
||||
/// Laplace model parameters for coarse energy: `[LM][intra][2*band + {0: prob, 1: decay}]`.
|
||||
pub const E_PROB_MODEL: [[[u8; 42]; 2]; 4] = [
|
||||
[
|
||||
[
|
||||
72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40, 114, 40, 132,
|
||||
26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11,
|
||||
],
|
||||
[
|
||||
24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, 91, 67, 100, 59,
|
||||
108, 50, 120, 40, 122, 37, 97, 43, 78, 50,
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, 146, 19, 162, 12,
|
||||
165, 10, 178, 7, 189, 6, 190, 8, 177, 9,
|
||||
],
|
||||
[
|
||||
23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60, 104, 60, 117, 52,
|
||||
123, 44, 138, 35, 133, 31, 97, 38, 77, 45,
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16, 158, 18, 170,
|
||||
13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10,
|
||||
],
|
||||
[
|
||||
21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, 112, 56, 129, 51,
|
||||
132, 40, 150, 33, 140, 29, 98, 35, 77, 42,
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, 154, 26,
|
||||
166, 21, 173, 16, 184, 13, 184, 10, 150, 13, 139, 15,
|
||||
],
|
||||
[
|
||||
22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52, 117, 55, 135,
|
||||
49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
/// Inverse CDF for coarse energy when only a few bits remain.
|
||||
pub const SMALL_ENERGY_ICDF: [u8; 3] = [2, 1, 0];
|
||||
/// Conservative log2 in 1/8 bits of the number of coded bands (intensity reservation).
|
||||
pub const LOG2_FRAC_TABLE: [i32; 24] = [0, 8, 13, 16, 19, 21, 23, 24, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 34, 35, 36, 36, 37, 37];
|
||||
/// TF resolution changes: `[LM][4*transient + 2*tf_select + tf_flag]`.
|
||||
pub const TF_SELECT_TABLE: [[i8; 8]; 4] =
|
||||
[[0, -1, 0, -1, 0, -1, 0, -1], [0, -1, 0, -2, 1, 0, 1, -1], [0, -2, 0, -3, 2, 0, 1, -1], [0, -2, 0, -3, 3, 0, 1, -1]];
|
||||
/// Spreading decision `{7, 2, 21, 2}/32`.
|
||||
pub const SPREAD_ICDF: [u8; 4] = [25, 23, 2, 0];
|
||||
/// Allocation trim (Table 58).
|
||||
pub const TRIM_ICDF: [u8; 11] = [126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0];
|
||||
@@ -0,0 +1,130 @@
|
||||
//! Shared CELT forward complex FFT.
|
||||
|
||||
#[derive(Clone, Copy, Default, Debug)]
|
||||
pub struct Cpx {
|
||||
pub re: f32,
|
||||
pub im: f32,
|
||||
}
|
||||
|
||||
impl Cpx {
|
||||
#[inline]
|
||||
pub(crate) fn mul(self, o: Cpx) -> Cpx {
|
||||
Cpx { re: self.re * o.re - self.im * o.im, im: self.re * o.im + self.im * o.re }
|
||||
}
|
||||
#[inline]
|
||||
pub(crate) fn add(self, o: Cpx) -> Cpx {
|
||||
Cpx { re: self.re + o.re, im: self.im + o.im }
|
||||
}
|
||||
#[inline]
|
||||
pub(crate) fn sub(self, o: Cpx) -> Cpx {
|
||||
Cpx { re: self.re - o.re, im: self.im - o.im }
|
||||
}
|
||||
}
|
||||
|
||||
/// Mixed-radix (2, 3, 4, 5) forward complex FFT, unscaled (decimation in time, recursive).
|
||||
pub struct Fft {
|
||||
pub(crate) n: usize,
|
||||
pub(crate) factors: Vec<usize>,
|
||||
pub(crate) twiddle: Vec<Cpx>,
|
||||
}
|
||||
|
||||
impl Fft {
|
||||
pub fn new(n: usize) -> Fft {
|
||||
let mut factors = Vec::new();
|
||||
let mut m = n;
|
||||
for p in [4usize, 2, 3, 5] {
|
||||
while m.is_multiple_of(p) {
|
||||
factors.push(p);
|
||||
m /= p;
|
||||
}
|
||||
}
|
||||
assert_eq!(m, 1, "unsupported FFT size {n}");
|
||||
let twiddle = (0..n)
|
||||
.map(|k| {
|
||||
let ph = -2.0 * std::f64::consts::PI * k as f64 / n as f64;
|
||||
Cpx { re: ph.cos() as f32, im: ph.sin() as f32 }
|
||||
})
|
||||
.collect();
|
||||
Fft { n, factors, twiddle }
|
||||
}
|
||||
|
||||
pub fn process(&self, data: &mut [Cpx], scratch: &mut Vec<Cpx>) {
|
||||
scratch.clear();
|
||||
scratch.extend_from_slice(&data[..self.n]);
|
||||
self.rec(&mut data[..self.n], scratch, 1, self.n, 0, 1);
|
||||
}
|
||||
|
||||
fn rec(&self, out: &mut [Cpx], inp: &[Cpx], in_stride: usize, n: usize, fi: usize, tw_stride: usize) {
|
||||
let p = self.factors[fi];
|
||||
let m = n / p;
|
||||
if m == 1 {
|
||||
for (i, o) in out.iter_mut().enumerate().take(p) {
|
||||
*o = inp[i * in_stride];
|
||||
}
|
||||
} else {
|
||||
for q in 0..p {
|
||||
self.rec(&mut out[q * m..(q + 1) * m], &inp[q * in_stride..], in_stride * p, m, fi + 1, tw_stride * p);
|
||||
}
|
||||
}
|
||||
// The twiddle index q * k * tw_stride is always below n, so no modulo is needed.
|
||||
let tw = &self.twiddle;
|
||||
match p {
|
||||
2 => {
|
||||
for k in 0..m {
|
||||
let a0 = out[k];
|
||||
let a1 = out[m + k].mul(tw[k * tw_stride]);
|
||||
out[k] = a0.add(a1);
|
||||
out[m + k] = a0.sub(a1);
|
||||
}
|
||||
}
|
||||
4 => {
|
||||
for k in 0..m {
|
||||
let t0 = out[k];
|
||||
let t1 = out[m + k].mul(tw[k * tw_stride]);
|
||||
let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]);
|
||||
let t3 = out[3 * m + k].mul(tw[3 * k * tw_stride]);
|
||||
let (s0, d0) = (t0.add(t2), t0.sub(t2));
|
||||
let (s1, d1) = (t1.add(t3), t1.sub(t3));
|
||||
out[k] = s0.add(s1);
|
||||
out[2 * m + k] = s0.sub(s1);
|
||||
// The odd outputs combine d0 with -j * d1 = (d1.im, -d1.re) and its negative.
|
||||
out[m + k] = Cpx { re: d0.re + d1.im, im: d0.im - d1.re };
|
||||
out[3 * m + k] = Cpx { re: d0.re - d1.im, im: d0.im + d1.re };
|
||||
}
|
||||
}
|
||||
3 => {
|
||||
// Imaginary part of exp(-2 pi i / 3) = -sqrt(3) / 2.
|
||||
let h = tw[self.n / 3].im;
|
||||
for k in 0..m {
|
||||
let t0 = out[k];
|
||||
let t1 = out[m + k].mul(tw[k * tw_stride]);
|
||||
let t2 = out[2 * m + k].mul(tw[2 * k * tw_stride]);
|
||||
let (s, d) = (t1.add(t2), t1.sub(t2));
|
||||
let a = Cpx { re: t0.re - 0.5 * s.re, im: t0.im - 0.5 * s.im };
|
||||
out[k] = t0.add(s);
|
||||
// j * h * d = (-h * d.im, h * d.re)
|
||||
out[m + k] = Cpx { re: a.re - h * d.im, im: a.im + h * d.re };
|
||||
out[2 * m + k] = Cpx { re: a.re + h * d.im, im: a.im - h * d.re };
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Generic DFT butterfly (radix 5, once per transform).
|
||||
let roots: [Cpx; 5] = std::array::from_fn(|q| if q < p { tw[q * (self.n / p)] } else { Cpx::default() });
|
||||
let mut t = [Cpx::default(); 5];
|
||||
for k in 0..m {
|
||||
t[0] = out[k];
|
||||
for q in 1..p {
|
||||
t[q] = out[q * m + k].mul(tw[q * k * tw_stride]);
|
||||
}
|
||||
for q2 in 0..p {
|
||||
let mut acc = t[0];
|
||||
for q in 1..p {
|
||||
acc = acc.add(t[q].mul(roots[(q * q2) % p]));
|
||||
}
|
||||
out[q2 * m + k] = acc;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,35 @@
|
||||
//! Clean-room pure-Rust Opus (RFC 6716, updated by RFC 8251; RFC 7845 `OpusHead`).
|
||||
//!
|
||||
//! Decoding (feature `opus-decode`): [`Decoder`] (multistream, surround), [`StreamDecoder`] (one
|
||||
//! elementary stream), [`OpusHead`] and the packet parser [`packet`]. From FilmCraft's
|
||||
//! `filmcraft-opus` (storytold/filmcraft@8fda2f2).
|
||||
//! - Decoding (feature `opus-decode`): [`Decoder`] (multistream, surround), [`StreamDecoder`] (one
|
||||
//! elementary stream), [`OpusHead`] and the packet parser [`packet`]. From FilmCraft's
|
||||
//! `filmcraft-opus` (storytold/filmcraft@8fda2f2).
|
||||
//! - Encoding (feature `opus-encode`): [`Encoder`] (in [`encoder`]) (SILK, hybrid and CELT modes, 48 kHz,
|
||||
//! 20 ms, mono or stereo). From EffectCraft's `effectcraft-opusenc` (storytold/effectcraft@c618429).
|
||||
//!
|
||||
//! Public items keep their names, so FilmCraft can re-export this module.
|
||||
//! Public items keep their names, so FilmCraft and EffectCraft can re-export this module. The two
|
||||
//! halves share the FFT, resampling dot product and CELT allocation preparation; range coders and
|
||||
//! mode-specific MDCT processing remain separate. [`Bandwidth`] is one type for both (`encoder::Bandwidth` is the same type);
|
||||
//! the two `Mode` enums keep their apps' variant names and convert into each other.
|
||||
|
||||
// Index loops and long argument lists follow the specification's pseudo-code (FilmCraft allows
|
||||
// these lints workspace-wide).
|
||||
// Index loops and long argument lists follow the specification's pseudo-code (FilmCraft and
|
||||
// EffectCraft allow these lints workspace-wide).
|
||||
#![allow(clippy::needless_range_loop, clippy::too_many_arguments)]
|
||||
|
||||
mod dot;
|
||||
mod fft;
|
||||
mod rate_common;
|
||||
|
||||
#[cfg(feature = "opus-decode")]
|
||||
mod decode;
|
||||
#[cfg(feature = "opus-encode")]
|
||||
pub mod encoder;
|
||||
|
||||
#[cfg(feature = "opus-decode")]
|
||||
pub use decode::{Decoder, Error, Mode, OpusHead, Packet, Result, SAMPLE_RATES, StreamDecoder, Toc, packet};
|
||||
#[cfg(feature = "opus-encode")]
|
||||
pub use encoder::Encoder;
|
||||
|
||||
/// Coded audio bandwidth (RFC 6716 §2, Table 1).
|
||||
/// Coded audio bandwidth (RFC 6716 §2, Table 1), for the decoder and the encoder alike.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum Bandwidth {
|
||||
/// 4 kHz (8 kHz sampling).
|
||||
@@ -29,9 +44,32 @@ pub enum Bandwidth {
|
||||
Fullband,
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "opus-decode", feature = "opus-encode"))]
|
||||
impl From<Mode> for encoder::Mode {
|
||||
fn from(m: Mode) -> Self {
|
||||
match m {
|
||||
Mode::SilkOnly => encoder::Mode::Silk,
|
||||
Mode::Hybrid => encoder::Mode::Hybrid,
|
||||
Mode::CeltOnly => encoder::Mode::Celt,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "opus-decode", feature = "opus-encode"))]
|
||||
impl From<encoder::Mode> for Mode {
|
||||
fn from(m: encoder::Mode) -> Self {
|
||||
match m {
|
||||
encoder::Mode::Silk => Mode::SilkOnly,
|
||||
encoder::Mode::Hybrid => Mode::Hybrid,
|
||||
encoder::Mode::Celt => Mode::CeltOnly,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Channel order for Vorbis-ordered surround (mapping family 1, RFC 7845 §5.1.1.2): output channel
|
||||
/// → decoded channel, giving the WAV/SMPTE order (L R C LFE back… side…) the rest of craft-audio
|
||||
/// uses. From FilmCraft's `filmcraft-codecs`.
|
||||
#[cfg(feature = "opus-decode")]
|
||||
pub fn wav_order(channels: usize) -> Option<&'static [usize]> {
|
||||
Some(match channels {
|
||||
3 => &[0, 2, 1],
|
||||
|
||||
@@ -0,0 +1,362 @@
|
||||
//! CELT pulse cache and allocation caps shared by encoder and decoder.
|
||||
|
||||
#[cfg(not(feature = "opus-encode"))]
|
||||
use super::decode::celt::tables::{ALLOC, EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS};
|
||||
#[cfg(feature = "opus-encode")]
|
||||
use super::encoder::tables::{ALLOC, EBANDS, LOG2_FRAC_TABLE, MAX_LM, NB_EBANDS};
|
||||
|
||||
const BITRES: u32 = 3;
|
||||
const MAX_PSEUDO: i32 = 40;
|
||||
const LOG_MAX_PSEUDO: i32 = 6;
|
||||
const MAX_FINE_BITS: i32 = 8;
|
||||
const FINE_OFFSET: i32 = 21;
|
||||
const QTHETA_OFFSET: i32 = 4;
|
||||
const QTHETA_OFFSET_TWOPHASE: i32 = 16;
|
||||
fn ilog(x: u32) -> i32 {
|
||||
(32 - x.leading_zeros()) as i32
|
||||
}
|
||||
|
||||
/// Number of pulses represented by pseudo-pulse index `i`.
|
||||
pub fn get_pulses(i: i32) -> i32 {
|
||||
if i < 8 { i } else { (8 + (i & 7)) << ((i >> 3) - 1) }
|
||||
}
|
||||
|
||||
/// Conservative (rounded up) log2 of `val` with `frac` fractional bits.
|
||||
pub fn log2_frac(mut val: u32, mut frac: u32) -> i32 {
|
||||
let mut l = ilog(val);
|
||||
if val & val.wrapping_sub(1) != 0 {
|
||||
if l > 16 {
|
||||
val = ((val - 1) >> (l - 16)) + 1;
|
||||
} else {
|
||||
val <<= 16 - l;
|
||||
}
|
||||
let mut l = (l - 1) << frac;
|
||||
loop {
|
||||
let b = (val >> 16) as i32;
|
||||
l += b << frac;
|
||||
val = (val + b as u32) >> b;
|
||||
val = (val * val + 0x7FFF) >> 15;
|
||||
if frac == 0 {
|
||||
break;
|
||||
}
|
||||
frac -= 1;
|
||||
}
|
||||
l + (val > 0x8000) as i32
|
||||
} else {
|
||||
l -= 1;
|
||||
l << frac
|
||||
}
|
||||
}
|
||||
|
||||
/// PVQ codebook size V(N, K) (saturating far above 2^32).
|
||||
pub fn pvq_v(n: usize, k: usize) -> u128 {
|
||||
if k == 0 {
|
||||
return 1;
|
||||
}
|
||||
if n == 0 {
|
||||
return 0;
|
||||
}
|
||||
let mut row = vec![0u128; k + 1];
|
||||
row[0] = 1;
|
||||
for _ in 0..n {
|
||||
let mut prev_diag = row[0];
|
||||
for kk in 1..=k {
|
||||
let up = row[kk];
|
||||
let v = up.saturating_add(row[kk - 1]).saturating_add(prev_diag);
|
||||
prev_diag = up;
|
||||
row[kk] = v.min(u128::MAX >> 8);
|
||||
}
|
||||
row[0] = 1;
|
||||
}
|
||||
row[k]
|
||||
}
|
||||
|
||||
fn fits_in32(n: usize, k: i32) -> bool {
|
||||
pvq_v(n, k as usize) <= u32::MAX as u128
|
||||
}
|
||||
|
||||
/// Precomputed per-mode data.
|
||||
pub struct Mode {
|
||||
/// log2 of band width in 1/8 bits.
|
||||
pub log_n: [i32; NB_EBANDS],
|
||||
pub(crate) cache_index: Vec<i16>,
|
||||
pub(crate) cache_bits: Vec<u8>,
|
||||
pub(crate) caps: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Mode {
|
||||
pub fn new() -> Mode {
|
||||
let mut log_n = [0i32; NB_EBANDS];
|
||||
for (i, l) in log_n.iter_mut().enumerate() {
|
||||
*l = log2_frac((EBANDS[i + 1] - EBANDS[i]) as u32, BITRES);
|
||||
}
|
||||
let (cache_index, cache_bits) = compute_pulse_cache(MAX_LM as i32);
|
||||
let mut m = Mode { log_n, cache_index, cache_bits, caps: Vec::new() };
|
||||
m.caps = compute_caps(&m);
|
||||
m
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn cache(&self, lm: i32, band: usize) -> &[u8] {
|
||||
let idx = self.cache_index[((lm + 1) as usize) * NB_EBANDS + band];
|
||||
&self.cache_bits[idx.max(0) as usize..]
|
||||
}
|
||||
|
||||
pub fn bits2pulses(&self, band: usize, lm: i32, bits: i32) -> i32 {
|
||||
let cache = self.cache(lm, band);
|
||||
let mut lo = 0i32;
|
||||
let mut hi = cache[0] as i32;
|
||||
let bits = bits - 1;
|
||||
for _ in 0..LOG_MAX_PSEUDO {
|
||||
let mid = (lo + hi + 1) >> 1;
|
||||
if cache[mid as usize] as i32 >= bits {
|
||||
hi = mid;
|
||||
} else {
|
||||
lo = mid;
|
||||
}
|
||||
}
|
||||
let lo_bits = if lo == 0 { -1 } else { cache[lo as usize] as i32 };
|
||||
if bits - lo_bits <= cache[hi as usize] as i32 - bits { lo } else { hi }
|
||||
}
|
||||
|
||||
pub fn pulses2bits(&self, band: usize, lm: i32, pulses: i32) -> i32 {
|
||||
if pulses == 0 { 0 } else { self.cache(lm, band)[pulses as usize] as i32 + 1 }
|
||||
}
|
||||
|
||||
/// Per-band allocation caps for this frame (`init_caps`).
|
||||
pub fn init_caps(&self, lm: usize, c: usize) -> [i32; NB_EBANDS] {
|
||||
let mut cap = [0i32; NB_EBANDS];
|
||||
for (i, cp) in cap.iter_mut().enumerate() {
|
||||
let n = (EBANDS[i + 1] - EBANDS[i]) << lm;
|
||||
*cp = ((self.caps[NB_EBANDS * (2 * lm + c - 1) + i] as i32 + 64) * c as i32 * n) >> 2;
|
||||
}
|
||||
cap
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_pulse_cache(lm_max: i32) -> (Vec<i16>, Vec<u8>) {
|
||||
let nb = NB_EBANDS;
|
||||
let rows = (lm_max + 2) as usize;
|
||||
let mut index = vec![-1i16; nb * rows];
|
||||
let mut entries: Vec<(usize, i32, usize)> = Vec::new();
|
||||
let mut curr = 0usize;
|
||||
for i in 0..rows {
|
||||
for j in 0..nb {
|
||||
let n = (((EBANDS[j + 1] - EBANDS[j]) << i) >> 1) as usize;
|
||||
index[i * nb + j] = -1;
|
||||
'search: for k in 0..=i {
|
||||
for nn in 0..nb {
|
||||
if k == i && nn >= j {
|
||||
break;
|
||||
}
|
||||
if n == (((EBANDS[nn + 1] - EBANDS[nn]) << k) >> 1) as usize {
|
||||
index[i * nb + j] = index[k * nb + nn];
|
||||
break 'search;
|
||||
}
|
||||
}
|
||||
}
|
||||
if index[i * nb + j] == -1 && n != 0 {
|
||||
let mut k = 0;
|
||||
while k < MAX_PSEUDO && fits_in32(n, get_pulses(k + 1)) {
|
||||
k += 1;
|
||||
}
|
||||
index[i * nb + j] = curr as i16;
|
||||
entries.push((n, k, curr));
|
||||
curr += (k + 1) as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut bits = vec![0u8; curr];
|
||||
for &(n, k, off) in &entries {
|
||||
let maxk = get_pulses(k) as usize;
|
||||
let tmp = required_bits(n, maxk, BITRES);
|
||||
for j in 1..=k {
|
||||
bits[off + j as usize] = (tmp[get_pulses(j) as usize] - 1) as u8;
|
||||
}
|
||||
bits[off] = k as u8;
|
||||
}
|
||||
(index, bits)
|
||||
}
|
||||
|
||||
fn required_bits(n: usize, maxk: usize, frac: u32) -> Vec<i32> {
|
||||
let mut out = vec![0i32; maxk + 1];
|
||||
if n == 1 {
|
||||
for o in out.iter_mut().skip(1) {
|
||||
*o = 1 << frac;
|
||||
}
|
||||
} else {
|
||||
for (k, o) in out.iter_mut().enumerate().skip(1) {
|
||||
*o = log2_frac(pvq_v(n, k) as u32, frac);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn compute_caps(m: &Mode) -> Vec<u8> {
|
||||
let nb = NB_EBANDS;
|
||||
let mut caps = vec![0u8; (MAX_LM + 1) * 2 * nb];
|
||||
for i in 0..=MAX_LM as i32 {
|
||||
for c in 1..=2i32 {
|
||||
for j in 0..nb {
|
||||
let mut n0 = EBANDS[j + 1] - EBANDS[j];
|
||||
|
||||
let max_bits = if n0 << i == 1 {
|
||||
(c * (1 + MAX_FINE_BITS)) << BITRES
|
||||
} else {
|
||||
let mut lm0 = 0;
|
||||
if n0 > 2 {
|
||||
n0 >>= 1;
|
||||
lm0 -= 1;
|
||||
} else if n0 <= 1 {
|
||||
lm0 = i.min(1);
|
||||
n0 <<= lm0;
|
||||
}
|
||||
let pcache = m.cache(lm0, j);
|
||||
let mut mb = pcache[pcache[0] as usize] as i32 + 1;
|
||||
let mut n = n0;
|
||||
for k in 0..(i - lm0) {
|
||||
mb <<= 1;
|
||||
let offset = ((m.log_n[j] + ((lm0 + k) << BITRES)) >> 1) - QTHETA_OFFSET;
|
||||
let num = 459 * ((2 * n - 1) * offset + mb);
|
||||
let den = ((2 * n - 1) << 9) - 459;
|
||||
let qb = ((num + (den >> 1)) / den).min(57);
|
||||
mb += qb;
|
||||
n <<= 1;
|
||||
}
|
||||
if c == 2 {
|
||||
mb <<= 1;
|
||||
let offset = ((m.log_n[j] + (i << BITRES)) >> 1) - if n == 2 { QTHETA_OFFSET_TWOPHASE } else { QTHETA_OFFSET };
|
||||
let ndof = 2 * n - 1 - (n == 2) as i32;
|
||||
let p = if n == 2 { 512 } else { 487 };
|
||||
let num = p * (mb + ndof * offset);
|
||||
let den = (ndof << 9) - p;
|
||||
let qb = ((num + (den >> 1)) / den).min(if n == 2 { 64 } else { 61 });
|
||||
mb += qb;
|
||||
}
|
||||
let ndof = c * n + if c == 2 && n > 2 { 1 } else { 0 };
|
||||
let mut offset = ((m.log_n[j] + (i << BITRES)) >> 1) - FINE_OFFSET;
|
||||
if n == 2 {
|
||||
offset += (1 << BITRES) >> 2;
|
||||
}
|
||||
let num = mb + ndof * offset;
|
||||
let den = (ndof - 1) << BITRES;
|
||||
let qb = ((num + (den >> 1)) / den).min(MAX_FINE_BITS);
|
||||
mb += (c * qb) << BITRES;
|
||||
mb
|
||||
};
|
||||
let w = c * ((EBANDS[j + 1] - EBANDS[j]) << i);
|
||||
let v = (4 * max_bits / w) - 64;
|
||||
caps[((i * 2 + c - 1) as usize) * nb + j] = v.clamp(0, 255) as u8;
|
||||
}
|
||||
}
|
||||
}
|
||||
caps
|
||||
}
|
||||
|
||||
/// Shared allocation search before encoder/decoder signalling diverges.
|
||||
pub struct Bracket {
|
||||
pub skip_start: usize,
|
||||
pub bits1: [i32; NB_EBANDS],
|
||||
pub bits2: [i32; NB_EBANDS],
|
||||
pub thresh: [i32; NB_EBANDS],
|
||||
pub total: i32,
|
||||
pub skip_rsv: i32,
|
||||
pub intensity_rsv: i32,
|
||||
pub dual_stereo_rsv: i32,
|
||||
}
|
||||
|
||||
pub fn bracket(
|
||||
start: usize,
|
||||
end: usize,
|
||||
offsets: &[i32; NB_EBANDS],
|
||||
cap: &[i32; NB_EBANDS],
|
||||
alloc_trim: i32,
|
||||
total: i32,
|
||||
c: usize,
|
||||
lm: usize,
|
||||
) -> Bracket {
|
||||
let ci = c as i32;
|
||||
let lmi = lm as i32;
|
||||
let mut total = total.max(0);
|
||||
let mut skip_start = start;
|
||||
let skip_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 };
|
||||
total -= skip_rsv;
|
||||
let mut intensity_rsv = 0;
|
||||
let mut dual_stereo_rsv = 0;
|
||||
if c == 2 {
|
||||
intensity_rsv = LOG2_FRAC_TABLE[end - start];
|
||||
if intensity_rsv > total {
|
||||
intensity_rsv = 0;
|
||||
} else {
|
||||
total -= intensity_rsv;
|
||||
dual_stereo_rsv = if total >= 1 << BITRES { 1 << BITRES } else { 0 };
|
||||
total -= dual_stereo_rsv;
|
||||
}
|
||||
}
|
||||
let mut bits1 = [0i32; NB_EBANDS];
|
||||
let mut bits2 = [0i32; NB_EBANDS];
|
||||
let mut thresh = [0i32; NB_EBANDS];
|
||||
let mut trim_offset = [0i32; NB_EBANDS];
|
||||
for j in start..end {
|
||||
let w = EBANDS[j + 1] - EBANDS[j];
|
||||
thresh[j] = (ci << BITRES).max(((3 * w) << lmi << BITRES) >> 4);
|
||||
trim_offset[j] = (ci * w * (alloc_trim - 5 - lmi) * (end as i32 - j as i32 - 1) * (1 << (lmi + BITRES as i32))) >> 6;
|
||||
if w << lmi == 1 {
|
||||
trim_offset[j] -= ci << BITRES;
|
||||
}
|
||||
}
|
||||
let nb_alloc = ALLOC.len() as i32;
|
||||
let mut lo = 1;
|
||||
let mut hi = nb_alloc - 1;
|
||||
loop {
|
||||
let mut done = false;
|
||||
let mut psum = 0;
|
||||
let mid = (lo + hi) >> 1;
|
||||
for j in (start..end).rev() {
|
||||
let n = EBANDS[j + 1] - EBANDS[j];
|
||||
let mut bitsj = (ci * n * (ALLOC[mid as usize][j] as i32)) << lmi >> 2;
|
||||
if bitsj > 0 {
|
||||
bitsj = (bitsj + trim_offset[j]).max(0);
|
||||
}
|
||||
bitsj += offsets[j];
|
||||
if bitsj >= thresh[j] || done {
|
||||
done = true;
|
||||
psum += bitsj.min(cap[j]);
|
||||
} else if bitsj >= ci << BITRES {
|
||||
psum += ci << BITRES;
|
||||
}
|
||||
}
|
||||
if psum > total {
|
||||
hi = mid - 1;
|
||||
} else {
|
||||
lo = mid + 1;
|
||||
}
|
||||
if lo > hi {
|
||||
break;
|
||||
}
|
||||
}
|
||||
hi = lo;
|
||||
lo -= 1;
|
||||
for j in start..end {
|
||||
let n = EBANDS[j + 1] - EBANDS[j];
|
||||
let mut bits1j = (ci * n * (ALLOC[lo as usize][j] as i32)) << lmi >> 2;
|
||||
let mut bits2j = if hi >= nb_alloc { cap[j] } else { (ci * n * (ALLOC[hi as usize][j] as i32)) << lmi >> 2 };
|
||||
if bits1j > 0 {
|
||||
bits1j = (bits1j + trim_offset[j]).max(0);
|
||||
}
|
||||
if bits2j > 0 {
|
||||
bits2j = (bits2j + trim_offset[j]).max(0);
|
||||
}
|
||||
if lo > 0 {
|
||||
bits1j += offsets[j];
|
||||
}
|
||||
bits2j += offsets[j];
|
||||
if offsets[j] > 0 {
|
||||
skip_start = j;
|
||||
}
|
||||
bits2j = (bits2j - bits1j).max(0);
|
||||
bits1[j] = bits1j;
|
||||
bits2[j] = bits2j;
|
||||
}
|
||||
Bracket { skip_start, bits1, bits2, thresh, total, skip_rsv, intensity_rsv, dual_stereo_rsv }
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
//! Audio-only files: WAV (RIFF, little-endian PCM; RF64 past 4 GiB) and AIFF (big-endian PCM),
|
||||
//! 16, 24 or 32-bit integer or 32-bit float ([`SampleFormat`]), for streaming export.
|
||||
//! Float WAV is `WAVE_FORMAT_IEEE_FLOAT` with a `fact` chunk; float AIFF is AIFF-C (`fl32`).
|
||||
//!
|
||||
//! The header is written first from the known length (`header`), then the samples in chunks
|
||||
//! (`encode`) and the pad byte (`pad`), so an export streams to disk instead of building the
|
||||
//! file in memory.
|
||||
|
||||
/// PCM representation in an exported file.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum SampleFormat {
|
||||
/// 16-bit signed integer.
|
||||
Int16,
|
||||
/// 24-bit signed integer.
|
||||
Int24,
|
||||
/// 32-bit signed integer.
|
||||
Int32,
|
||||
/// 32-bit IEEE float.
|
||||
Float32,
|
||||
}
|
||||
|
||||
impl SampleFormat {
|
||||
/// Bits per sample.
|
||||
pub fn bits(self) -> u16 {
|
||||
match self {
|
||||
Self::Int16 => 16,
|
||||
Self::Int24 => 24,
|
||||
Self::Int32 | Self::Float32 => 32,
|
||||
}
|
||||
}
|
||||
/// Bytes per sample.
|
||||
pub fn bytes(self) -> usize {
|
||||
usize::from(self.bits() / 8)
|
||||
}
|
||||
/// Whether this is a floating point format.
|
||||
pub fn is_float(self) -> bool {
|
||||
self == Self::Float32
|
||||
}
|
||||
/// Clip and quantize one sample with symmetric integer full scale; NaN becomes silence.
|
||||
pub fn quantise(self, s: f32) -> i32 {
|
||||
let s = s.clamp(-1.0, 1.0);
|
||||
match self {
|
||||
Self::Int16 => (s * 32_767.0).round() as i32,
|
||||
Self::Int24 => (s * 8_388_607.0).round() as i32,
|
||||
Self::Int32 => (f64::from(s) * 2_147_483_647.0).round() as i32,
|
||||
Self::Float32 => 0,
|
||||
}
|
||||
}
|
||||
/// Sanitize a floating sample: NaN becomes silence, infinity becomes the largest finite value.
|
||||
pub fn float(s: f32) -> f32 {
|
||||
if s.is_nan() { 0.0 } else { s.clamp(f32::MIN, f32::MAX) }
|
||||
}
|
||||
}
|
||||
|
||||
/// The container of an audio-only export.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum PcmContainer {
|
||||
/// RIFF/WAVE or RF64.
|
||||
Wav,
|
||||
/// AIFF or AIFF-C.
|
||||
Aiff,
|
||||
}
|
||||
|
||||
/// Bytes of sample data for `frames` frames, or None when that does not fit 64 bits.
|
||||
fn data_len(frames: u64, channels: u16, format: SampleFormat) -> Option<u64> {
|
||||
frames.checked_mul(u64::from(channels))?.checked_mul(format.bytes() as u64)
|
||||
}
|
||||
|
||||
/// The pad byte after an odd-sized data chunk (RIFF and AIFF chunks are word-aligned).
|
||||
pub fn pad(frames: u64, channels: u16, format: SampleFormat) -> &'static [u8] {
|
||||
if data_len(frames, channels, format).is_some_and(|n| n & 1 == 1) { &[0] } else { &[] }
|
||||
}
|
||||
|
||||
/// `KSDATAFORMAT_SUBTYPE_PCM` / `_IEEE_FLOAT` after their first two bytes (the format tag).
|
||||
const SUBTYPE_TAIL: [u8; 14] = [0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71];
|
||||
|
||||
/// AIFF-C format version 1 (`FVER`).
|
||||
const AIFC_VERSION_1: u32 = 0xA280_5140;
|
||||
|
||||
/// The AIFF-C compression name of `fl32`, as a Pascal string of even length.
|
||||
const FL32_NAME: &[u8] = b"\x1532-bit floating point";
|
||||
|
||||
/// Everything before the samples of a file of `frames` frames. A WAV whose RIFF size would not
|
||||
/// fit 32 bits (4 GiB: 24-bit 5.1 at 96 kHz after about 41 minutes) is written as RF64 (EBU Tech
|
||||
/// 3306), with the 64-bit sizes in a `ds64` chunk. AIFF has no 64-bit form, so a longer AIFF is
|
||||
/// an error naming WAV instead.
|
||||
pub fn header(container: PcmContainer, channels: u16, sample_rate: u32, format: SampleFormat, frames: u64) -> Result<Vec<u8>, String> {
|
||||
let too_long = || "the audio is too long to write".to_string();
|
||||
if channels == 0 {
|
||||
return Err("audio needs at least one channel".into());
|
||||
}
|
||||
if sample_rate == 0 {
|
||||
return Err("audio needs a sample rate".into());
|
||||
}
|
||||
let bits = format.bits();
|
||||
let bps = bits / 8;
|
||||
let float = format.is_float();
|
||||
let data = data_len(frames, channels, format).ok_or_else(too_long)?;
|
||||
let block_align = channels.checked_mul(bps).ok_or("too many audio channels")?;
|
||||
match container {
|
||||
PcmContainer::Wav => {
|
||||
let byte_rate =
|
||||
u32::try_from(u64::from(sample_rate) * u64::from(block_align)).map_err(|_| "the audio sample rate is too high".to_string())?;
|
||||
let ext = channels > 2;
|
||||
// plain PCM has no cbSize; IEEE float has an empty one
|
||||
let fmt_len: u32 = if ext {
|
||||
40
|
||||
} else if float {
|
||||
18
|
||||
} else {
|
||||
16
|
||||
};
|
||||
// every format but integer PCM has a `fact` chunk (the frame count; RF64: in `ds64`)
|
||||
let fact_len: u64 = if float { 12 } else { 0 };
|
||||
let padded = data.checked_add(data & 1).ok_or_else(too_long)?;
|
||||
// "WAVE" + fmt chunk + fact chunk + data chunk header + samples
|
||||
let riff = (4 + 8 + u64::from(fmt_len) + fact_len + 8).checked_add(padded).ok_or_else(too_long)?;
|
||||
let rf64 = riff > u64::from(u32::MAX);
|
||||
let mut v = Vec::with_capacity(100);
|
||||
if rf64 {
|
||||
const DS64: u64 = 8 + 28;
|
||||
v.extend_from_slice(b"RF64");
|
||||
v.extend_from_slice(&u32::MAX.to_le_bytes());
|
||||
v.extend_from_slice(b"WAVEds64");
|
||||
v.extend_from_slice(&28u32.to_le_bytes());
|
||||
v.extend_from_slice(&riff.checked_add(DS64).ok_or_else(too_long)?.to_le_bytes());
|
||||
v.extend_from_slice(&data.to_le_bytes());
|
||||
v.extend_from_slice(&frames.to_le_bytes());
|
||||
v.extend_from_slice(&0u32.to_le_bytes()); // no table entries
|
||||
} else {
|
||||
v.extend_from_slice(b"RIFF");
|
||||
v.extend_from_slice(&(riff as u32).to_le_bytes());
|
||||
v.extend_from_slice(b"WAVE");
|
||||
}
|
||||
let tag: u16 = if float { 3 } else { 1 };
|
||||
v.extend_from_slice(b"fmt ");
|
||||
v.extend_from_slice(&fmt_len.to_le_bytes());
|
||||
v.extend_from_slice(&(if ext { 0xFFFE } else { tag }).to_le_bytes());
|
||||
v.extend_from_slice(&channels.to_le_bytes());
|
||||
v.extend_from_slice(&sample_rate.to_le_bytes());
|
||||
v.extend_from_slice(&byte_rate.to_le_bytes());
|
||||
v.extend_from_slice(&block_align.to_le_bytes());
|
||||
v.extend_from_slice(&bits.to_le_bytes());
|
||||
if ext {
|
||||
// cbSize, valid bits, channel mask, KSDATAFORMAT_SUBTYPE_PCM / _IEEE_FLOAT
|
||||
v.extend_from_slice(&22u16.to_le_bytes());
|
||||
v.extend_from_slice(&bits.to_le_bytes());
|
||||
let mask: u32 = if channels == 6 { 0x3F } else { 0 };
|
||||
v.extend_from_slice(&mask.to_le_bytes());
|
||||
v.extend_from_slice(&tag.to_le_bytes());
|
||||
v.extend_from_slice(&SUBTYPE_TAIL);
|
||||
} else if float {
|
||||
v.extend_from_slice(&0u16.to_le_bytes());
|
||||
}
|
||||
if float {
|
||||
v.extend_from_slice(b"fact");
|
||||
v.extend_from_slice(&4u32.to_le_bytes());
|
||||
v.extend_from_slice(&(if rf64 { u32::MAX } else { u32::try_from(frames).unwrap_or(u32::MAX) }).to_le_bytes());
|
||||
}
|
||||
v.extend_from_slice(b"data");
|
||||
v.extend_from_slice(&(if rf64 { u32::MAX } else { data as u32 }).to_le_bytes());
|
||||
Ok(v)
|
||||
}
|
||||
PcmContainer::Aiff => {
|
||||
let aiff_too_long = || "AIFF holds at most 4 GB of audio: export WAV, which has no such limit".to_string();
|
||||
let frames32 = u32::try_from(frames).map_err(|_| aiff_too_long())?;
|
||||
// SSND: offset + block size + samples
|
||||
let ssnd = u32::try_from(data.checked_add(8).ok_or_else(aiff_too_long)?).map_err(|_| aiff_too_long())?;
|
||||
// floats need AIFF-C: a version chunk, and the compression type and name in COMM
|
||||
let comm: u32 = if float { 18 + 4 + FL32_NAME.len() as u32 } else { 18 };
|
||||
let fver: u32 = if float { 8 + 4 } else { 0 };
|
||||
let form = (4u32 + fver + (8 + comm))
|
||||
.checked_add(8)
|
||||
.and_then(|n| n.checked_add(ssnd))
|
||||
.and_then(|n| n.checked_add(ssnd & 1))
|
||||
.ok_or_else(aiff_too_long)?;
|
||||
let mut v = Vec::with_capacity(90);
|
||||
v.extend_from_slice(b"FORM");
|
||||
v.extend_from_slice(&form.to_be_bytes());
|
||||
if float {
|
||||
v.extend_from_slice(b"AIFCFVER");
|
||||
v.extend_from_slice(&4u32.to_be_bytes());
|
||||
v.extend_from_slice(&AIFC_VERSION_1.to_be_bytes());
|
||||
} else {
|
||||
v.extend_from_slice(b"AIFF");
|
||||
}
|
||||
v.extend_from_slice(b"COMM");
|
||||
v.extend_from_slice(&comm.to_be_bytes());
|
||||
v.extend_from_slice(&channels.to_be_bytes());
|
||||
v.extend_from_slice(&frames32.to_be_bytes());
|
||||
v.extend_from_slice(&bits.to_be_bytes());
|
||||
v.extend_from_slice(&extended(sample_rate));
|
||||
if float {
|
||||
v.extend_from_slice(b"fl32");
|
||||
v.extend_from_slice(FL32_NAME);
|
||||
}
|
||||
v.extend_from_slice(b"SSND");
|
||||
v.extend_from_slice(&ssnd.to_be_bytes());
|
||||
v.extend_from_slice(&0u32.to_be_bytes()); // offset
|
||||
v.extend_from_slice(&0u32.to_be_bytes()); // block size
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Append interleaved samples in `format` (little-endian for WAV, big-endian for AIFF).
|
||||
pub fn encode(container: PcmContainer, interleaved: &[f32], format: SampleFormat, out: &mut Vec<u8>) {
|
||||
let bps = format.bytes();
|
||||
out.reserve(interleaved.len().saturating_mul(bps));
|
||||
for &s in interleaved {
|
||||
let q = if format.is_float() { SampleFormat::float(s).to_bits() as i32 } else { format.quantise(s) };
|
||||
match container {
|
||||
PcmContainer::Wav => out.extend_from_slice(q.to_le_bytes().get(..bps).unwrap_or_default()),
|
||||
PcmContainer::Aiff => out.extend_from_slice(q.to_be_bytes().get(4 - bps..).unwrap_or_default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A whole file in memory (short audio; exports stream with [`header`] and [`encode`]).
|
||||
fn write_whole(container: PcmContainer, interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result<Vec<u8>, String> {
|
||||
let per_frame = usize::from(channels.max(1));
|
||||
if !interleaved.len().is_multiple_of(per_frame) {
|
||||
return Err("the samples do not make whole frames".into());
|
||||
}
|
||||
let frames = (interleaved.len() / per_frame) as u64;
|
||||
let mut v = header(container, channels, sample_rate, format, frames)?;
|
||||
encode(container, interleaved, format, &mut v);
|
||||
v.extend_from_slice(pad(frames, channels, format));
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// A RIFF/WAVE file of interleaved samples. More than two channels are written as
|
||||
/// `WAVE_FORMAT_EXTENSIBLE` with the speaker mask of the layout (6 channels: L, R, C, LFE, Ls, Rs
|
||||
/// = `0x3F`).
|
||||
pub fn write_wav(interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result<Vec<u8>, String> {
|
||||
write_whole(PcmContainer::Wav, interleaved, channels, sample_rate, format)
|
||||
}
|
||||
|
||||
/// IEEE 754 80-bit extended encoding of a sample rate (AIFF `COMM`).
|
||||
pub fn extended(rate: u32) -> [u8; 10] {
|
||||
let mut out = [0u8; 10];
|
||||
if rate == 0 {
|
||||
return out;
|
||||
}
|
||||
let shift = rate.leading_zeros();
|
||||
let mant = (rate as u64) << (32 + shift);
|
||||
let exp = 16383 + 31 - shift as u16;
|
||||
out[..2].copy_from_slice(&exp.to_be_bytes());
|
||||
out[2..].copy_from_slice(&mant.to_be_bytes());
|
||||
out
|
||||
}
|
||||
|
||||
/// An AIFF file (`FORM`/`AIFF`, `COMM` + `SSND`; AIFF-C for floats) of interleaved samples.
|
||||
pub fn write_aiff(interleaved: &[f32], channels: u16, sample_rate: u32, format: SampleFormat) -> Result<Vec<u8>, String> {
|
||||
write_whole(PcmContainer::Aiff, interleaved, channels, sample_rate, format)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn streaming_formats_preserve_samples_and_bounds() {
|
||||
let samples = [1.0, -1.0, f32::NAN];
|
||||
let wav = write_wav(&samples, 1, 48_000, SampleFormat::Int24).unwrap();
|
||||
assert_eq!(&wav[..4], b"RIFF");
|
||||
assert_eq!(&wav[44..53], &[255, 255, 127, 1, 0, 128, 0, 0, 0]);
|
||||
let h = header(PcmContainer::Wav, 6, 96_000, SampleFormat::Int24, 96_000 * 3600).unwrap();
|
||||
assert_eq!(&h[..4], b"RF64");
|
||||
assert!(header(PcmContainer::Aiff, 2, 48_000, SampleFormat::Int24, u64::MAX).is_err());
|
||||
let aiff = write_aiff(&[0.25, -3.0], 1, 44_100, SampleFormat::Float32).unwrap();
|
||||
assert_eq!(&aiff[8..12], b"AIFC");
|
||||
assert_eq!(&aiff[aiff.len() - 8..], [0.25f32.to_be_bytes(), (-3.0f32).to_be_bytes()].concat());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_channels_rates_and_partial_frames_are_refused() {
|
||||
for container in [PcmContainer::Wav, PcmContainer::Aiff] {
|
||||
assert!(header(container, 0, 48_000, SampleFormat::Int16, 1).is_err());
|
||||
assert!(header(container, 1, 0, SampleFormat::Int16, 1).is_err());
|
||||
assert!(header(container, 2, 48_000, SampleFormat::Int16, 0).is_ok(), "zero frames stay valid");
|
||||
}
|
||||
assert!(write_wav(&[0.5], 0, 48_000, SampleFormat::Int16).is_err());
|
||||
assert!(write_wav(&[0.1, 0.2, 0.3], 2, 48_000, SampleFormat::Int16).is_err());
|
||||
assert!(write_aiff(&[0.1, 0.2, 0.3], 2, 48_000, SampleFormat::Int16).is_err());
|
||||
assert!(write_aiff(&[0.1, 0.2], 2, 48_000, SampleFormat::Int16).is_ok());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
//! Interleaved PCM packet decoding into caller-owned planar channels.
|
||||
|
||||
/// The sample representation in an interleaved PCM packet.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Format {
|
||||
/// Bits per sample.
|
||||
pub bits: u16,
|
||||
/// IEEE floating-point samples.
|
||||
pub float: bool,
|
||||
/// Big-endian sample bytes.
|
||||
pub big_endian: bool,
|
||||
/// Signed 8-bit integers (wider integer PCM is signed).
|
||||
pub signed: bool,
|
||||
}
|
||||
|
||||
/// Append complete interleaved frames to caller-owned planar channels. Empty channel slices and
|
||||
/// zero-bit formats append nothing. Incomplete frames are ignored; unsupported widths produce
|
||||
/// silent samples, matching the container adapters that use this API.
|
||||
pub fn decode_into(data: &[u8], cfg: Format, out: &mut [Vec<f32>]) {
|
||||
let ch = out.len();
|
||||
if cfg.bits == 0 || ch == 0 {
|
||||
return;
|
||||
}
|
||||
let bps = usize::from(cfg.bits).div_ceil(8);
|
||||
let Some(frame_bytes) = bps.checked_mul(ch).filter(|&n| n > 0) else { return };
|
||||
let n = data.len() / frame_bytes;
|
||||
for channel in out.iter_mut() {
|
||||
channel.reserve(n);
|
||||
}
|
||||
// `n` is whole frames within `data`, so every sample slice below is in bounds.
|
||||
for i in 0..n {
|
||||
for (c, o) in out.iter_mut().enumerate() {
|
||||
let off = (i * ch + c) * bps;
|
||||
let b = &data[off..off + bps];
|
||||
let v = match (cfg.bits, cfg.float, cfg.big_endian) {
|
||||
(32, true, false) => f32::from_le_bytes([b[0], b[1], b[2], b[3]]),
|
||||
(32, true, true) => f32::from_be_bytes([b[0], b[1], b[2], b[3]]),
|
||||
(64, true, false) => f64::from_le_bytes(b.try_into().unwrap_or([0; 8])) as f32,
|
||||
(64, true, true) => f64::from_be_bytes(b.try_into().unwrap_or([0; 8])) as f32,
|
||||
(8, _, _) => {
|
||||
if cfg.signed {
|
||||
b[0] as i8 as f32 / 128.0
|
||||
} else {
|
||||
(b[0] as f32 - 128.0) / 128.0
|
||||
}
|
||||
}
|
||||
(16, _, false) => i16::from_le_bytes([b[0], b[1]]) as f32 / 32768.0,
|
||||
(16, _, true) => i16::from_be_bytes([b[0], b[1]]) as f32 / 32768.0,
|
||||
(24, _, false) => (i32::from_le_bytes([0, b[0], b[1], b[2]]) >> 8) as f32 / 8_388_608.0,
|
||||
(24, _, true) => (i32::from_be_bytes([b[0], b[1], b[2], 0]) >> 8) as f32 / 8_388_608.0,
|
||||
(32, false, false) => i32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f32 / 2_147_483_648.0,
|
||||
(32, false, true) => i32::from_be_bytes([b[0], b[1], b[2], b[3]]) as f32 / 2_147_483_648.0,
|
||||
_ => 0.0,
|
||||
};
|
||||
o.push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn decodes_endian_and_interleaving_without_touching_partial_frames() {
|
||||
let mut channels = vec![Vec::new(), Vec::new()];
|
||||
let fmt = Format { bits: 16, float: false, big_endian: true, signed: true };
|
||||
decode_into(&[0x40, 0, 0xc0, 0, 0xff], fmt, &mut channels);
|
||||
assert_eq!(channels, [vec![0.5], vec![-0.5]]);
|
||||
decode_into(&[0; 8], Format { bits: 0, ..fmt }, &mut channels);
|
||||
assert_eq!(channels, [vec![0.5], vec![-0.5]]);
|
||||
}
|
||||
#[test]
|
||||
fn decodes_sample_representations_and_ignores_invalid_inputs() {
|
||||
let base = Format { bits: 8, float: false, big_endian: false, signed: false };
|
||||
let cases: &[(Format, &[u8], f32)] = &[
|
||||
(base, &[192], 0.5),
|
||||
(Format { signed: true, ..base }, &[192], -0.5),
|
||||
(Format { bits: 16, ..base }, &[0, 64], 0.5),
|
||||
(Format { bits: 24, ..base }, &[0, 0, 64], 0.5),
|
||||
(Format { bits: 24, big_endian: true, ..base }, &[0xc0, 0, 0], -0.5),
|
||||
(Format { bits: 32, ..base }, &[0, 0, 0, 64], 0.5),
|
||||
(Format { bits: 32, big_endian: true, ..base }, &[0xc0, 0, 0, 0], -0.5),
|
||||
(Format { bits: 32, float: true, ..base }, &0.5_f32.to_le_bytes(), 0.5),
|
||||
(Format { bits: 32, float: true, big_endian: true, ..base }, &(-0.5_f32).to_be_bytes(), -0.5),
|
||||
(Format { bits: 64, float: true, ..base }, &0.5_f64.to_le_bytes(), 0.5),
|
||||
(Format { bits: 64, float: true, big_endian: true, ..base }, &(-0.5_f64).to_be_bytes(), -0.5),
|
||||
(Format { bits: 12, ..base }, &[0xff, 0xff], 0.0),
|
||||
];
|
||||
for &(format, bytes, expected) in cases {
|
||||
let mut out = [vec![1.0]];
|
||||
decode_into(bytes, format, &mut out);
|
||||
assert_eq!(out[0], [1.0, expected], "{format:?}");
|
||||
}
|
||||
decode_into(&[1], base, &mut []);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,411 @@
|
||||
//! QOA ("Quite OK Audio"): a decoder (feature `qoa-decode`) and an encoder (feature `qoa-encode`)
|
||||
//! for whole `.qoa` files, 16-bit, 1 to 8 channels.
|
||||
//!
|
||||
//! Written from the published QOA specification (qoaformat.org). A file is an 8-byte header (the
|
||||
//! magic `qoaf` and the number of samples per channel) followed by frames of up to 5120 samples
|
||||
//! per channel. Each frame starts with its channel count, sample rate, sample count and size, then
|
||||
//! the state of one 4-tap sign-sign LMS predictor per channel, then 64-bit slices of 20 samples:
|
||||
//! a 4-bit scale factor and twenty 3-bit quantised residuals, the channels interleaved slice by
|
||||
//! slice.
|
||||
//!
|
||||
//! Streaming files (a sample count of 0 in the header) are not supported.
|
||||
//! Samples are `i16`, one `Vec` per channel.
|
||||
|
||||
#[cfg(feature = "qoa-decode")]
|
||||
const MAX_FRAME_SAMPLES: usize = 5120;
|
||||
const SLICE_SAMPLES: usize = 20;
|
||||
const LMS_LEN: usize = 4;
|
||||
const MAGIC: &[u8; 4] = b"qoaf";
|
||||
/// The most channels a file may have.
|
||||
pub const MAX_CHANNELS: usize = 8;
|
||||
|
||||
/// `dequant[scale factor][quantised residual]`: `round(round((sf + 1)^2.75) * [0.75, -0.75, 2.5,
|
||||
/// -2.5, 4.5, -4.5, 7, -7])`.
|
||||
const DEQUANT: [[i32; 8]; 16] = [
|
||||
[1, -1, 3, -3, 5, -5, 7, -7],
|
||||
[5, -5, 18, -18, 32, -32, 49, -49],
|
||||
[16, -16, 53, -53, 95, -95, 147, -147],
|
||||
[34, -34, 113, -113, 203, -203, 315, -315],
|
||||
[63, -63, 210, -210, 378, -378, 588, -588],
|
||||
[104, -104, 345, -345, 621, -621, 966, -966],
|
||||
[158, -158, 528, -528, 950, -950, 1477, -1477],
|
||||
[228, -228, 760, -760, 1368, -1368, 2128, -2128],
|
||||
[316, -316, 1053, -1053, 1895, -1895, 2947, -2947],
|
||||
[422, -422, 1405, -1405, 2529, -2529, 3934, -3934],
|
||||
[548, -548, 1828, -1828, 3290, -3290, 5117, -5117],
|
||||
[696, -696, 2320, -2320, 4176, -4176, 6496, -6496],
|
||||
[868, -868, 2893, -2893, 5207, -5207, 8099, -8099],
|
||||
[1064, -1064, 3548, -3548, 6386, -6386, 9933, -9933],
|
||||
[1286, -1286, 4288, -4288, 7718, -7718, 12005, -12005],
|
||||
[1536, -1536, 5120, -5120, 9216, -9216, 14336, -14336],
|
||||
];
|
||||
|
||||
/// What went wrong reading or writing a QOA file.
|
||||
#[non_exhaustive]
|
||||
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
|
||||
pub enum Error {
|
||||
/// The input is not a QOA file, or is damaged or truncated.
|
||||
#[error("malformed QOA: {0}")]
|
||||
Malformed(&'static str),
|
||||
/// A valid file this module does not handle (a streaming file).
|
||||
#[error("unsupported QOA: {0}")]
|
||||
Unsupported(&'static str),
|
||||
/// The samples or settings given to the encoder cannot be written as QOA.
|
||||
#[error("QOA cannot encode: {0}")]
|
||||
InvalidInput(&'static str),
|
||||
}
|
||||
|
||||
/// A decoded file.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Audio {
|
||||
/// Sample rate in Hz (1 to 16 777 215).
|
||||
pub sample_rate: u32,
|
||||
/// One `Vec` of samples per channel, all the same length.
|
||||
pub channels: Vec<Vec<i16>>,
|
||||
}
|
||||
|
||||
/// The predictor: the last four samples and four weights.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Lms {
|
||||
history: [i32; LMS_LEN],
|
||||
weights: [i32; LMS_LEN],
|
||||
}
|
||||
|
||||
impl Lms {
|
||||
fn new() -> Self {
|
||||
Lms { history: [0; LMS_LEN], weights: [0, 0, -(1 << 13), 1 << 14] }
|
||||
}
|
||||
|
||||
fn predict(&self) -> i32 {
|
||||
let mut p = 0i32;
|
||||
for i in 0..LMS_LEN {
|
||||
p = p.wrapping_add(self.weights[i].wrapping_mul(self.history[i]));
|
||||
}
|
||||
p >> 13
|
||||
}
|
||||
|
||||
fn update(&mut self, sample: i32, residual: i32) {
|
||||
let delta = residual >> 4;
|
||||
for i in 0..LMS_LEN {
|
||||
self.weights[i] += if self.history[i] < 0 { -delta } else { delta };
|
||||
}
|
||||
self.history.copy_within(1.., 0);
|
||||
self.history[LMS_LEN - 1] = sample;
|
||||
}
|
||||
}
|
||||
|
||||
fn clamp16(v: i32) -> i32 {
|
||||
v.clamp(-32768, 32767)
|
||||
}
|
||||
|
||||
#[cfg(feature = "qoa-decode")]
|
||||
mod decode {
|
||||
use super::{Audio, DEQUANT, Error, LMS_LEN, Lms, MAGIC, MAX_CHANNELS, MAX_FRAME_SAMPLES, SLICE_SAMPLES, clamp16};
|
||||
|
||||
fn be16(b: &[u8], i: usize) -> u16 {
|
||||
u16::from_be_bytes([b[i], b[i + 1]])
|
||||
}
|
||||
|
||||
/// Decode a whole `.qoa` file.
|
||||
///
|
||||
/// The declared length is checked against the input before anything is allocated: a slice of
|
||||
/// 8 bytes holds at most 20 samples, so a file cannot declare more samples than that.
|
||||
pub fn decode(bytes: &[u8]) -> Result<Audio, Error> {
|
||||
if bytes.len() < 8 || &bytes[..4] != MAGIC {
|
||||
return Err(Error::Malformed("missing the qoaf magic"));
|
||||
}
|
||||
let total = u32::from_be_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]) as usize;
|
||||
if total == 0 {
|
||||
return Err(Error::Unsupported("streaming files (a sample count of 0)"));
|
||||
}
|
||||
if bytes.len() < 16 {
|
||||
return Err(Error::Malformed("truncated frame header"));
|
||||
}
|
||||
let channels = usize::from(bytes[8]);
|
||||
let sample_rate = u32::from_be_bytes([0, bytes[9], bytes[10], bytes[11]]);
|
||||
if channels == 0 || channels > MAX_CHANNELS || sample_rate == 0 {
|
||||
return Err(Error::Malformed("bad channel count or sample rate"));
|
||||
}
|
||||
// 8 bytes (20 samples) per channel at the very least, plus the frame headers.
|
||||
let room = (bytes.len() - 8) / (8 * channels) * SLICE_SAMPLES;
|
||||
if total > room {
|
||||
return Err(Error::Malformed("the file is too short for its declared length"));
|
||||
}
|
||||
let mut out: Vec<Vec<i16>> = Vec::new();
|
||||
for _ in 0..channels {
|
||||
let mut v = Vec::new();
|
||||
v.try_reserve_exact(total).map_err(|_| Error::Malformed("too many samples"))?;
|
||||
out.push(v);
|
||||
}
|
||||
let mut pos = 8;
|
||||
let mut done = 0usize;
|
||||
while done < total {
|
||||
let head = bytes.get(pos..pos + 8).ok_or(Error::Malformed("truncated frame header"))?;
|
||||
let rate = u32::from_be_bytes([0, head[1], head[2], head[3]]);
|
||||
if usize::from(head[0]) != channels || rate != sample_rate {
|
||||
return Err(Error::Malformed("the channel count or sample rate changes between frames"));
|
||||
}
|
||||
let samples = usize::from(be16(head, 4));
|
||||
let size = usize::from(be16(head, 6));
|
||||
let slices = samples.div_ceil(SLICE_SAMPLES);
|
||||
if samples == 0 || samples > MAX_FRAME_SAMPLES || samples > total - done {
|
||||
return Err(Error::Malformed("bad frame sample count"));
|
||||
}
|
||||
if size != 8 + 16 * channels + 8 * slices * channels {
|
||||
return Err(Error::Malformed("frame size does not match its sample count"));
|
||||
}
|
||||
let frame = bytes.get(pos..pos + size).ok_or(Error::Malformed("truncated frame"))?;
|
||||
let mut lms = [Lms::new(); MAX_CHANNELS];
|
||||
for (c, state) in lms.iter_mut().take(channels).enumerate() {
|
||||
let at = 8 + c * 16;
|
||||
for i in 0..LMS_LEN {
|
||||
state.history[i] = i32::from(be16(frame, at + 2 * i) as i16);
|
||||
state.weights[i] = i32::from(be16(frame, at + 8 + 2 * i) as i16);
|
||||
}
|
||||
}
|
||||
let mut at = 8 + 16 * channels;
|
||||
for s in 0..slices {
|
||||
let n = (samples - s * SLICE_SAMPLES).min(SLICE_SAMPLES);
|
||||
for (c, state) in lms.iter_mut().take(channels).enumerate() {
|
||||
let mut slice = u64::from_be_bytes([
|
||||
frame[at],
|
||||
frame[at + 1],
|
||||
frame[at + 2],
|
||||
frame[at + 3],
|
||||
frame[at + 4],
|
||||
frame[at + 5],
|
||||
frame[at + 6],
|
||||
frame[at + 7],
|
||||
]);
|
||||
at += 8;
|
||||
let table = &DEQUANT[(slice >> 60) as usize & 15];
|
||||
for _ in 0..n {
|
||||
let dequantised = table[(slice >> 57) as usize & 7];
|
||||
slice <<= 3;
|
||||
let sample = clamp16(state.predict() + dequantised);
|
||||
out[c].push(sample as i16);
|
||||
state.update(sample, dequantised);
|
||||
}
|
||||
}
|
||||
}
|
||||
pos += size;
|
||||
done += samples;
|
||||
}
|
||||
Ok(Audio { sample_rate, channels: out })
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "qoa-decode")]
|
||||
pub use decode::decode;
|
||||
|
||||
#[cfg(feature = "qoa-encode")]
|
||||
mod encode {
|
||||
use super::{DEQUANT, Error, Lms, MAGIC, MAX_CHANNELS, SLICE_SAMPLES, clamp16};
|
||||
|
||||
/// The quantised residual (0..8) whose dequantised value is nearest `residual`.
|
||||
fn quantise(table: &[i32; 8], residual: i32) -> usize {
|
||||
let mut best = 0;
|
||||
let mut best_err = i64::MAX;
|
||||
for (q, &d) in table.iter().enumerate() {
|
||||
let e = i64::from(residual - d).pow(2);
|
||||
if e < best_err {
|
||||
best = q;
|
||||
best_err = e;
|
||||
}
|
||||
}
|
||||
best
|
||||
}
|
||||
|
||||
/// Encode one slice of one channel with every scale factor and keep the one with the least
|
||||
/// squared error. Returns the 64-bit slice; `lms` is advanced to the chosen result.
|
||||
fn encode_slice(lms: &mut Lms, samples: &[i16]) -> u64 {
|
||||
let mut best: Option<(i64, u64, Lms)> = None;
|
||||
for (sf, table) in DEQUANT.iter().enumerate() {
|
||||
let mut trial = *lms;
|
||||
let mut slice = sf as u64;
|
||||
let mut err = 0i64;
|
||||
for k in 0..SLICE_SAMPLES {
|
||||
let mut q = 0;
|
||||
if let Some(&s) = samples.get(k) {
|
||||
let predicted = trial.predict();
|
||||
q = quantise(table, i32::from(s) - predicted);
|
||||
let sample = clamp16(predicted + table[q]);
|
||||
err += i64::from(i32::from(s) - sample).pow(2);
|
||||
trial.update(sample, table[q]);
|
||||
}
|
||||
slice = slice << 3 | q as u64;
|
||||
}
|
||||
// 4 + 60 bits are in `slice`; the scale factor belongs on top.
|
||||
let slice = slice & ((1 << 60) - 1) | (sf as u64) << 60;
|
||||
if best.as_ref().is_none_or(|b| err < b.0) {
|
||||
best = Some((err, slice, trial));
|
||||
}
|
||||
}
|
||||
match best {
|
||||
Some((_, slice, state)) => {
|
||||
*lms = state;
|
||||
slice
|
||||
}
|
||||
None => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode `channels` (one `Vec` of samples per channel, all the same length) as a `.qoa` file.
|
||||
pub fn encode(sample_rate: u32, channels: &[Vec<i16>]) -> Result<Vec<u8>, Error> {
|
||||
let n = channels.len();
|
||||
if n == 0 || n > MAX_CHANNELS {
|
||||
return Err(Error::InvalidInput("QOA stores 1 to 8 channels"));
|
||||
}
|
||||
if sample_rate == 0 || sample_rate > 0x00ff_ffff {
|
||||
return Err(Error::InvalidInput("the sample rate must be 1 to 16777215 Hz"));
|
||||
}
|
||||
let total = channels[0].len();
|
||||
if channels.iter().any(|c| c.len() != total) {
|
||||
return Err(Error::InvalidInput("the channels differ in length"));
|
||||
}
|
||||
let total32 = u32::try_from(total).map_err(|_| Error::InvalidInput("too many samples"))?;
|
||||
if total == 0 {
|
||||
return Err(Error::InvalidInput("QOA cannot store an empty file"));
|
||||
}
|
||||
let mut out = Vec::with_capacity(8 + total * n / 2);
|
||||
out.extend_from_slice(MAGIC);
|
||||
out.extend_from_slice(&total32.to_be_bytes());
|
||||
let mut lms = [Lms::new(); MAX_CHANNELS];
|
||||
let mut start = 0;
|
||||
while start < total {
|
||||
let len = (total - start).min(5120);
|
||||
let slices = len.div_ceil(SLICE_SAMPLES);
|
||||
let size = 8 + 16 * n + 8 * slices * n;
|
||||
out.push(n as u8);
|
||||
out.extend_from_slice(&sample_rate.to_be_bytes()[1..]);
|
||||
out.extend_from_slice(&(len as u16).to_be_bytes());
|
||||
out.extend_from_slice(&(size as u16).to_be_bytes());
|
||||
// The state a frame starts from is written as 16-bit values, so the predictor
|
||||
// continues from exactly what a decoder reads.
|
||||
for state in lms.iter_mut().take(n) {
|
||||
for v in state.history.iter_mut().chain(state.weights.iter_mut()) {
|
||||
*v = i32::from(*v as i16);
|
||||
out.extend_from_slice(&(*v as i16).to_be_bytes());
|
||||
}
|
||||
}
|
||||
for s in 0..slices {
|
||||
let from = start + s * SLICE_SAMPLES;
|
||||
let to = (from + SLICE_SAMPLES).min(start + len);
|
||||
for (c, state) in lms.iter_mut().take(n).enumerate() {
|
||||
out.extend_from_slice(&encode_slice(state, &channels[c][from..to]).to_be_bytes());
|
||||
}
|
||||
}
|
||||
start += len;
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "qoa-encode")]
|
||||
pub use encode::encode;
|
||||
|
||||
#[cfg(all(test, feature = "qoa-decode", feature = "qoa-encode"))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn tone(frames: usize, ch: usize) -> Vec<Vec<i16>> {
|
||||
(0..ch)
|
||||
.map(|c| (0..frames).map(|i| (((i as f64) * (0.03 + 0.01 * c as f64)).sin() * 12000.0 + ((i * 7919) % 200) as f64) as i16).collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn snr(a: &[i16], b: &[i16]) -> f64 {
|
||||
let (mut s, mut e) = (0f64, 0f64);
|
||||
for (&x, &y) in a.iter().zip(b) {
|
||||
s += f64::from(x).powi(2);
|
||||
e += (f64::from(x) - f64::from(y)).powi(2);
|
||||
}
|
||||
10.0 * (s / e.max(1.0)).log10()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dequant_table_matches_its_formula() {
|
||||
for (sf, row) in DEQUANT.iter().enumerate() {
|
||||
let scale = ((sf as f64 + 1.0).powf(2.75)).round();
|
||||
for (q, base) in [0.75, -0.75, 2.5, -2.5, 4.5, -4.5, 7.0, -7.0].into_iter().enumerate() {
|
||||
assert_eq!(f64::from(row[q]), (scale * base).round(), "sf {sf} q {q}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trips_with_a_good_signal_to_noise_ratio() {
|
||||
for (frames, ch) in [(1, 1), (19, 2), (20, 1), (21, 2), (5120, 1), (5121, 2), (12_345, 8)] {
|
||||
let src = tone(frames, ch);
|
||||
let bytes = encode(44_100, &src).unwrap();
|
||||
let back = decode(&bytes).unwrap();
|
||||
assert_eq!((back.sample_rate, back.channels.len(), back.channels[0].len()), (44_100, ch, frames));
|
||||
if frames > 1000 {
|
||||
for (c, (a, b)) in src.iter().zip(&back.channels).enumerate() {
|
||||
assert!(snr(a, b) > 25.0, "{frames}x{ch} channel {c}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn silence_and_full_scale_extremes_survive() {
|
||||
let silent = vec![vec![0i16; 3000]];
|
||||
// No residual value is zero, so silence decodes to a tiny residue, never exactly 0.
|
||||
let quiet = decode(&encode(8000, &silent).unwrap()).unwrap();
|
||||
assert!(quiet.channels[0].iter().all(|&s| s.abs() <= 2), "silence stays within 2 LSB");
|
||||
let square: Vec<i16> = (0..4000).map(|i| if (i / 50) % 2 == 0 { i16::MAX } else { i16::MIN }).collect();
|
||||
let back = decode(&encode(48_000, std::slice::from_ref(&square)).unwrap()).unwrap();
|
||||
assert_eq!(back.channels[0].len(), square.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_file_has_the_documented_layout() {
|
||||
let bytes = encode(48_000, &tone(100, 2)).unwrap();
|
||||
assert_eq!(&bytes[..4], b"qoaf");
|
||||
assert_eq!(u32::from_be_bytes(bytes[4..8].try_into().unwrap()), 100);
|
||||
assert_eq!(bytes[8], 2);
|
||||
assert_eq!(&bytes[9..12], &48_000u32.to_be_bytes()[1..]);
|
||||
assert_eq!(u16::from_be_bytes([bytes[12], bytes[13]]), 100);
|
||||
// 8 header + 2 channels * 16 state + 5 slices * 2 channels * 8
|
||||
assert_eq!(usize::from(u16::from_be_bytes([bytes[14], bytes[15]])), 8 + 32 + 80);
|
||||
assert_eq!(bytes.len(), 8 + 8 + 32 + 80);
|
||||
// The first frame starts from the specified predictor state.
|
||||
assert_eq!(&bytes[16 + 8..16 + 16], &[0, 0, 0, 0, 0xe0, 0, 0x40, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_input_to_the_encoder_is_refused() {
|
||||
assert!(encode(44_100, &[]).is_err());
|
||||
assert!(encode(0, &[vec![0; 10]]).is_err());
|
||||
assert!(encode(1 << 24, &[vec![0; 10]]).is_err());
|
||||
assert!(encode(44_100, &[vec![0; 10], vec![0; 9]]).is_err());
|
||||
assert!(encode(44_100, &[vec![]]).is_err());
|
||||
assert!(encode(44_100, &vec![vec![0; 10]; 9]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_input_is_an_error_not_a_panic() {
|
||||
let good = encode(44_100, &tone(6000, 2)).unwrap();
|
||||
for len in 0..good.len().min(600) {
|
||||
let _ = decode(&good[..len]);
|
||||
}
|
||||
assert!(decode(&good[..good.len() - 1]).is_err());
|
||||
// A header that declares more samples than the bytes can hold.
|
||||
let mut huge = good.clone();
|
||||
huge[4..8].copy_from_slice(&u32::MAX.to_be_bytes());
|
||||
assert!(decode(&huge).is_err());
|
||||
// A streaming file, and a frame whose size field lies.
|
||||
let mut streaming = good.clone();
|
||||
streaming[4..8].fill(0);
|
||||
assert_eq!(decode(&streaming), Err(Error::Unsupported("streaming files (a sample count of 0)")));
|
||||
let mut lying = good.clone();
|
||||
lying[14] ^= 1;
|
||||
assert!(decode(&lying).is_err());
|
||||
let mut zero_rate = good.clone();
|
||||
zero_rate[9..12].fill(0);
|
||||
assert!(decode(&zero_rate).is_err());
|
||||
let mut changed = good;
|
||||
let second = 8 + usize::from(u16::from_be_bytes([changed[14], changed[15]]));
|
||||
changed[second] = 1;
|
||||
assert!(decode(&changed).is_err());
|
||||
}
|
||||
}
|
||||
@@ -413,7 +413,7 @@ impl<'a> Native<'a> {
|
||||
let Some(next) = self.packets.next() else {
|
||||
// A track that declares no frames (an empty WAV or AIFF) is empty, not broken.
|
||||
let empty = self.frames_known && self.info.frames == 0 && self.errors == 0;
|
||||
if !self.decoded && !empty && !(self.seeked && self.errors == 0) {
|
||||
if !(self.decoded || empty || self.seeked && self.errors == 0) {
|
||||
return Err(self.no_audio("stream contains no audio"));
|
||||
}
|
||||
return Ok(None);
|
||||
|
||||
@@ -115,6 +115,50 @@ fn parse_bext(c: &[u8]) -> Option<BwfInfo> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Cap optional metadata independently of audio length, including on range-backed sources.
|
||||
const IXML_MAX: usize = 1 << 20;
|
||||
|
||||
/// iXML's BEXT mirror is a fallback only: a real `bext` always wins, including timestamp zero.
|
||||
/// Read the two unsigned 32-bit words, not arbitrary text that merely contains their tag names.
|
||||
fn ixml_time_reference(bytes: &[u8]) -> Option<u64> {
|
||||
let options = craft_xml::Options {
|
||||
doctype: craft_xml::Doctype::Reject,
|
||||
..craft_xml::Options::default().strict().with_limits(craft_xml::Limits {
|
||||
max_input: IXML_MAX,
|
||||
max_depth: 32,
|
||||
max_nodes: 8192,
|
||||
max_attrs: 64,
|
||||
max_attrs_total: 8192,
|
||||
..craft_xml::Limits::default()
|
||||
})
|
||||
};
|
||||
let root = options.parse(bytes).ok()?;
|
||||
if root.name != "BWFXML" {
|
||||
return None;
|
||||
}
|
||||
let mut bexts = root.children("BEXT");
|
||||
let bext = bexts.next()?;
|
||||
if bexts.next().is_some() {
|
||||
return None;
|
||||
}
|
||||
let word = |name: &str| -> Option<Option<u32>> {
|
||||
let mut fields = bext.children(name);
|
||||
let Some(field) = fields.next() else { return Some(None) };
|
||||
if fields.next().is_some() || field.children.iter().any(|n| matches!(n, craft_xml::Node::El(_))) {
|
||||
return None;
|
||||
}
|
||||
let text = field.text();
|
||||
let text = text.trim();
|
||||
if text.is_empty() || !text.bytes().all(|b| b.is_ascii_digit()) {
|
||||
return None;
|
||||
}
|
||||
Some(Some(text.parse().ok()?))
|
||||
};
|
||||
let low = word("BWF_TIME_REFERENCE_LOW")??;
|
||||
let high = word("BWF_TIME_REFERENCE_HIGH")?.unwrap_or(0);
|
||||
Some((u64::from(high) << 32) | u64::from(low))
|
||||
}
|
||||
|
||||
/// Refuse the WAVE file at `at`, which symphonia reads (A-law, ADPCM, ...), if an INFO entry
|
||||
/// claims more bytes than the file holds: symphonia allocates each entry's length. Chunks are
|
||||
/// walked where symphonia reads them: after the bytes a chunk's parser used, not its length.
|
||||
@@ -239,7 +283,7 @@ fn pcm_layout(fmt: &Fmt) -> Result<Option<(PcmKind, SampleFormat)>> {
|
||||
}
|
||||
|
||||
/// Parse a WAV file's chunks: its format, `bext` metadata and where its samples are. Only chunk
|
||||
/// headers and the small `fmt `, `bext` and `ds64` bodies are read.
|
||||
/// headers, small `fmt `, `bext` and `ds64` bodies, and at most one 1 MiB iXML document are read.
|
||||
pub(crate) fn parse(src: &Src<'_>) -> Result<WavParse> {
|
||||
if !is_wav(&src.bytes_upto(0, 12)) {
|
||||
return Err(AudioError::Malformed("not a RIFF/RF64 WAVE file".into()));
|
||||
@@ -249,6 +293,8 @@ pub(crate) fn parse(src: &Src<'_>) -> Result<WavParse> {
|
||||
let mut ds64_data: Option<u64> = None;
|
||||
let mut fmt: Option<Fmt> = None;
|
||||
let mut bwf: Option<BwfInfo> = None;
|
||||
let mut ixml_time = None;
|
||||
let mut ixml_seen = false;
|
||||
let mut data: Option<(u64, u64)> = None;
|
||||
|
||||
let mut pos: u64 = 12;
|
||||
@@ -288,7 +334,18 @@ pub(crate) fn parse(src: &Src<'_>) -> Result<WavParse> {
|
||||
let small = |n: u64| src.bytes(body, usize::try_from(declared.min(n)).unwrap_or(0)).unwrap_or_default();
|
||||
match &id {
|
||||
b"fmt " => fmt = Some(parse_fmt(&small(256))?),
|
||||
b"bext" => bwf = parse_bext(&small(4096)),
|
||||
b"bext" => {
|
||||
if let Some(info) = parse_bext(&small(4096)) {
|
||||
bwf = Some(info);
|
||||
}
|
||||
}
|
||||
b"iXML" if !ixml_seen => {
|
||||
// Inspect at most one bounded document, not 100,000 individually bounded trees.
|
||||
ixml_seen = true;
|
||||
if declared <= IXML_MAX as u64 {
|
||||
ixml_time = ixml_time_reference(&small(IXML_MAX as u64));
|
||||
}
|
||||
}
|
||||
b"ds64" => ds64_data = le_u64(&small(64), 8),
|
||||
_ => {}
|
||||
}
|
||||
@@ -297,6 +354,9 @@ pub(crate) fn parse(src: &Src<'_>) -> Result<WavParse> {
|
||||
pos = next;
|
||||
}
|
||||
|
||||
if bwf.is_none() {
|
||||
bwf = ixml_time.map(|time_reference| BwfInfo { time_reference, ..BwfInfo::default() });
|
||||
}
|
||||
let fmt = fmt.ok_or_else(|| AudioError::Malformed("WAV has no fmt chunk".into()))?;
|
||||
if fmt.channels == 0 {
|
||||
return Err(AudioError::Malformed("WAV declares zero channels".into()));
|
||||
|
||||
+20
-28
@@ -24,8 +24,10 @@ pub const LOOKAHEAD_PACKETS: usize = 8;
|
||||
enum Inner {
|
||||
#[cfg(feature = "aac-decode")]
|
||||
Aac {
|
||||
/// The decoder, with SBR on for HE-AAC.
|
||||
dec: Box<aac::Decoder>,
|
||||
up: Option<aac::Upsample2x>,
|
||||
/// HE-AAC: 2048 samples per access unit, at twice the core rate.
|
||||
sbr: bool,
|
||||
order: Option<&'static [usize]>,
|
||||
/// The format ADTS packets must keep, when their headers gave the configuration.
|
||||
adts_format: Option<AdtsFormat>,
|
||||
@@ -74,7 +76,7 @@ pub(crate) type PacketFrames = fn(&[u8], u64) -> Option<u64>;
|
||||
///
|
||||
/// It takes the track's [`CodecParams`] (from `isobmff::Track::codec_params`,
|
||||
/// `matroska::Track::codec_params`, `ogg::Stream::codec_params`, or built by hand for another
|
||||
/// container) and decodes AAC (LC; HE-AAC as its upsampled LC core), ALAC, Opus, AC-3/E-AC-3,
|
||||
/// container) and decodes AAC (LC, and HE-AAC with its SBR high band), ALAC, Opus, AC-3/E-AC-3,
|
||||
/// MP1/MP2, MP3 and PCM. Container timing (AAC priming, Opus pre-skip, end trimming) is the caller's:
|
||||
/// every decoded sample is returned.
|
||||
///
|
||||
@@ -168,17 +170,18 @@ impl TrackDecoder {
|
||||
params.extradata.clone()
|
||||
};
|
||||
let asc = asc.as_slice();
|
||||
let dec = aac::Decoder::new(asc)?;
|
||||
let mut dec = aac::Decoder::new(asc)?;
|
||||
let out = aac::output_rate(asc, payloads.iter().copied()).unwrap_or(dec.sample_rate());
|
||||
let up = (dec.sample_rate().checked_mul(2) == Some(out)).then(|| aac::Upsample2x::new(dec.channels()));
|
||||
let codec = if up.is_some() { CodecId::HeAac } else { CodecId::Aac };
|
||||
// Only twice the core rate is upsampled to; any other signalled rate plays the core.
|
||||
let out = if up.is_some() { out } else { dec.sample_rate() };
|
||||
let sbr = dec.sample_rate().checked_mul(2) == Some(out);
|
||||
let codec = if sbr { CodecId::HeAac } else { CodecId::Aac };
|
||||
// Only SBR at twice the core rate is decoded; any other signalled rate plays the core.
|
||||
dec.set_sbr(sbr);
|
||||
let out = dec.sample_rate();
|
||||
// a program config element that spells out a configuration has its speakers
|
||||
let cfg = aac::speaker_config(dec.config());
|
||||
let layout = aac_layout(cfg, dec.channels());
|
||||
let order = layout.is_ordered().then(|| aac::wav_order(cfg)).flatten();
|
||||
(Inner::Aac { dec: Box::new(dec), up, order, adts_format }, codec, out, layout, SampleFormat::Compressed)
|
||||
(Inner::Aac { dec: Box::new(dec), sbr, order, adts_format }, codec, out, layout, SampleFormat::Compressed)
|
||||
}
|
||||
#[cfg(feature = "alac-decode")]
|
||||
CodecId::Alac => {
|
||||
@@ -320,7 +323,7 @@ impl TrackDecoder {
|
||||
let mut partial = false;
|
||||
let channels = match &mut self.inner {
|
||||
#[cfg(feature = "aac-decode")]
|
||||
Inner::Aac { dec, up, order, adts_format } => {
|
||||
Inner::Aac { dec, order, adts_format, .. } => {
|
||||
let mut out: Vec<Vec<f32>> = Vec::new();
|
||||
let units = match adts_frames(packet) {
|
||||
Some(frames) => adts_units(frames, *adts_format)?,
|
||||
@@ -328,17 +331,11 @@ impl TrackDecoder {
|
||||
};
|
||||
// A failed frame leaves the decoder as it was, so the frames before it in the
|
||||
// packet must too: their audio is replaced by the packet's concealment.
|
||||
let before = (units.len() > 1).then(|| (dec.clone(), up.clone()));
|
||||
let decoded = units.into_iter().try_for_each(|unit| {
|
||||
let mut block = dec.decode(unit)?;
|
||||
if let Some(up) = up.as_mut() {
|
||||
block = up.process(block);
|
||||
}
|
||||
append(&mut out, block)
|
||||
});
|
||||
let before = (units.len() > 1).then(|| dec.clone());
|
||||
let decoded = units.into_iter().try_for_each(|unit| append(&mut out, dec.decode(unit)?));
|
||||
if let Err(e) = decoded {
|
||||
if let Some(before) = before {
|
||||
(*dec, *up) = before;
|
||||
*dec = before;
|
||||
}
|
||||
return Err(e);
|
||||
}
|
||||
@@ -440,8 +437,8 @@ impl TrackDecoder {
|
||||
None
|
||||
}
|
||||
#[cfg(feature = "aac-decode")]
|
||||
Inner::Aac { up, .. } => {
|
||||
let per_unit = aac::FRAME_LEN * if up.is_some() { 2 } else { 1 };
|
||||
Inner::Aac { sbr, .. } => {
|
||||
let per_unit = aac::FRAME_LEN * if *sbr { 2 } else { 1 };
|
||||
match adts_frames(data) {
|
||||
Some(f) => Some(f.iter().map(|(h, _)| usize::from(h.raw_data_blocks)).sum::<usize>().min(MAX_ADTS_FRAMES_PER_PACKET) * per_unit),
|
||||
// A packet lost whole: as long as the last one, in a stream of multi-frame
|
||||
@@ -488,7 +485,7 @@ impl TrackDecoder {
|
||||
pub fn frames_per_packet(&self) -> Option<usize> {
|
||||
match &self.inner {
|
||||
#[cfg(feature = "aac-decode")]
|
||||
Inner::Aac { up, .. } => Some(aac::FRAME_LEN * if up.is_some() { 2 } else { 1 }),
|
||||
Inner::Aac { sbr, .. } => Some(aac::FRAME_LEN * if *sbr { 2 } else { 1 }),
|
||||
#[cfg(feature = "alac-decode")]
|
||||
Inner::Alac { .. } => None,
|
||||
#[cfg(feature = "ac3-decode")]
|
||||
@@ -544,7 +541,7 @@ impl TrackDecoder {
|
||||
pub fn preroll(&self) -> usize {
|
||||
match &self.inner {
|
||||
#[cfg(feature = "aac-decode")]
|
||||
Inner::Aac { up, .. } => 2 * aac::FRAME_LEN * if up.is_some() { 2 } else { 1 },
|
||||
Inner::Aac { sbr, .. } => 2 * aac::FRAME_LEN * if *sbr { 2 } else { 1 },
|
||||
#[cfg(feature = "alac-decode")]
|
||||
Inner::Alac { .. } => 0,
|
||||
#[cfg(feature = "ac3-decode")]
|
||||
@@ -570,12 +567,7 @@ impl TrackDecoder {
|
||||
self.last_frames = 0;
|
||||
match &mut self.inner {
|
||||
#[cfg(feature = "aac-decode")]
|
||||
Inner::Aac { dec, up, .. } => {
|
||||
dec.reset();
|
||||
if let Some(up) = up {
|
||||
up.reset();
|
||||
}
|
||||
}
|
||||
Inner::Aac { dec, .. } => dec.reset(),
|
||||
#[cfg(feature = "alac-decode")]
|
||||
Inner::Alac { .. } => {}
|
||||
#[cfg(feature = "ac3-decode")]
|
||||
|
||||
Loaded 100 of 1006 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user