From 75710cfd20a11fecf1a192db43485c61a5f0c59b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 07:18:59 -0500 Subject: [PATCH 01/29] ci: lock workflow actions through an actions.lock lockfile Mutable action tags are a supply-chain risk: a moved tag runs different code without a diff. Every workflow action is now pinned through .github/workflows/actions.lock, and CI verifies the lockfile so an out-of-sync or tampered one fails instead of running. --- .github/workflows/actions.lock | 170 +++++++++++++++++++++++ .github/workflows/ai-core.yml | 6 +- .github/workflows/atomic.yml | 6 +- .github/workflows/audio.yml | 32 ++--- .github/workflows/crw.yml | 26 ++-- .github/workflows/dialogs.yml | 6 +- .github/workflows/doc.yml | 6 +- .github/workflows/docpath.yml | 6 +- .github/workflows/ecproj-format.yml | 6 +- .github/workflows/font-store.yml | 6 +- .github/workflows/formats.yml | 8 +- .github/workflows/i18n.yml | 6 +- .github/workflows/jpeg.yml | 8 +- .github/workflows/keymap.yml | 28 ++-- .github/workflows/media.yml | 16 +-- .github/workflows/pcraft-format.yml | 6 +- .github/workflows/pdf.yml | 40 +++--- .github/workflows/print.yml | 6 +- .github/workflows/proof.yml | 10 +- .github/workflows/ui.yml | 16 +-- .github/workflows/update.yml | 6 +- .github/workflows/vectorcraft-format.yml | 6 +- .github/workflows/video.yml | 16 +-- .github/workflows/web-api.yml | 8 +- .github/workflows/web-host.yml | 10 +- .github/workflows/window.yml | 6 +- sonar-project.properties | 10 +- 27 files changed, 327 insertions(+), 149 deletions(-) create mode 100644 .github/workflows/actions.lock diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock new file mode 100644 index 000000000..0d6c3b8f9 --- /dev/null +++ b/.github/workflows/actions.lock @@ -0,0 +1,170 @@ +# 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@v7.0.1' + - '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/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/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/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' +dependencies: + 'actions/cache@v6.1.0': + ref: 'v6.1.0' + commit: 'sha1-55cc8345863c7cc4c66a329aec7e433d2d1c52a9' + owner_id: 44036562 + repo_id: 215566462 + '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-b725eda525362e7cf34af9a7243cac1e3f01a7ec' + 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 diff --git a/.github/workflows/ai-core.yml b/.github/workflows/ai-core.yml index cc8d738ac..5471bf1ec 100644 --- a/.github/workflows/ai-core.yml +++ b/.github/workflows/ai-core.yml @@ -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 diff --git a/.github/workflows/atomic.yml b/.github/workflows/atomic.yml index 1471832d2..24f59bc2b 100644 --- a/.github/workflows/atomic.yml +++ b/.github/workflows/atomic.yml @@ -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 diff --git a/.github/workflows/audio.yml b/.github/workflows/audio.yml index 0104f848e..3944d3591 100644 --- a/.github/workflows/audio.yml +++ b/.github/workflows/audio.yml @@ -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' }} @@ -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). diff --git a/.github/workflows/crw.yml b/.github/workflows/crw.yml index 8fec98661..54be7ba3b 100644 --- a/.github/workflows/crw.yml +++ b/.github/workflows/crw.yml @@ -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') }} diff --git a/.github/workflows/dialogs.yml b/.github/workflows/dialogs.yml index 97a931aec..c8d43d725 100644 --- a/.github/workflows/dialogs.yml +++ b/.github/workflows/dialogs.yml @@ -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 diff --git a/.github/workflows/doc.yml b/.github/workflows/doc.yml index a0c619c1e..6caa4f0ee 100644 --- a/.github/workflows/doc.yml +++ b/.github/workflows/doc.yml @@ -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 diff --git a/.github/workflows/docpath.yml b/.github/workflows/docpath.yml index 571b65eca..e0115f6f5 100644 --- a/.github/workflows/docpath.yml +++ b/.github/workflows/docpath.yml @@ -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 diff --git a/.github/workflows/ecproj-format.yml b/.github/workflows/ecproj-format.yml index 16bab05c7..24a3350af 100644 --- a/.github/workflows/ecproj-format.yml +++ b/.github/workflows/ecproj-format.yml @@ -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 diff --git a/.github/workflows/font-store.yml b/.github/workflows/font-store.yml index f246f621a..0578f18ec 100644 --- a/.github/workflows/font-store.yml +++ b/.github/workflows/font-store.yml @@ -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 diff --git a/.github/workflows/formats.yml b/.github/workflows/formats.yml index e19902e8c..84d2c0d7e 100644 --- a/.github/workflows/formats.yml +++ b/.github/workflows/formats.yml @@ -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' }} diff --git a/.github/workflows/i18n.yml b/.github/workflows/i18n.yml index 5f717aeff..7e6903188 100644 --- a/.github/workflows/i18n.yml +++ b/.github/workflows/i18n.yml @@ -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 diff --git a/.github/workflows/jpeg.yml b/.github/workflows/jpeg.yml index 900baf678..21db0ab79 100644 --- a/.github/workflows/jpeg.yml +++ b/.github/workflows/jpeg.yml @@ -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' }} diff --git a/.github/workflows/keymap.yml b/.github/workflows/keymap.yml index aba535265..41fcc0502 100644 --- a/.github/workflows/keymap.yml +++ b/.github/workflows/keymap.yml @@ -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' }} diff --git a/.github/workflows/media.yml b/.github/workflows/media.yml index cb3912d54..4814e35ca 100644 --- a/.github/workflows/media.yml +++ b/.github/workflows/media.yml @@ -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' }} diff --git a/.github/workflows/pcraft-format.yml b/.github/workflows/pcraft-format.yml index 177bb4593..9c66b5a10 100644 --- a/.github/workflows/pcraft-format.yml +++ b/.github/workflows/pcraft-format.yml @@ -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 diff --git a/.github/workflows/pdf.yml b/.github/workflows/pdf.yml index c72ac7f72..25cf71d4e 100644 --- a/.github/workflows/pdf.yml +++ b/.github/workflows/pdf.yml @@ -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 diff --git a/.github/workflows/print.yml b/.github/workflows/print.yml index 3838e6c42..8e2108e09 100644 --- a/.github/workflows/print.yml +++ b/.github/workflows/print.yml @@ -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 diff --git a/.github/workflows/proof.yml b/.github/workflows/proof.yml index 0035b1c04..10365a230 100644 --- a/.github/workflows/proof.yml +++ b/.github/workflows/proof.yml @@ -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' }} diff --git a/.github/workflows/ui.yml b/.github/workflows/ui.yml index 0892f5a2c..f15ef6ddc 100644 --- a/.github/workflows/ui.yml +++ b/.github/workflows/ui.yml @@ -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' }} diff --git a/.github/workflows/update.yml b/.github/workflows/update.yml index f9c8e1498..9a7139026 100644 --- a/.github/workflows/update.yml +++ b/.github/workflows/update.yml @@ -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 diff --git a/.github/workflows/vectorcraft-format.yml b/.github/workflows/vectorcraft-format.yml index b2b20463a..74144f30f 100644 --- a/.github/workflows/vectorcraft-format.yml +++ b/.github/workflows/vectorcraft-format.yml @@ -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 diff --git a/.github/workflows/video.yml b/.github/workflows/video.yml index ec1c16eda..fa667bdfc 100644 --- a/.github/workflows/video.yml +++ b/.github/workflows/video.yml @@ -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) diff --git a/.github/workflows/web-api.yml b/.github/workflows/web-api.yml index 2460fb569..4bf1a30fe 100644 --- a/.github/workflows/web-api.yml +++ b/.github/workflows/web-api.yml @@ -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 diff --git a/.github/workflows/web-host.yml b/.github/workflows/web-host.yml index b780ee76b..76e6701e5 100644 --- a/.github/workflows/web-host.yml +++ b/.github/workflows/web-host.yml @@ -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 diff --git a/.github/workflows/window.yml b/.github/workflows/window.yml index b07bce384..f14b24038 100644 --- a/.github/workflows/window.yml +++ b/.github/workflows/window.yml @@ -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 diff --git a/sonar-project.properties b/sonar-project.properties index d08c71698..516e4d5cc 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -15,6 +15,14 @@ sonar.cpd.exclusions=**/*tables.rs # S4790 (weak hash): the ICC specification defines the profile ID as the MD5 of the profile, so # no other algorithm produces an ID other software accepts. It identifies, it does not protect. -sonar.issue.ignore.multicriteria=icc_profile_id +sonar.issue.ignore.multicriteria=icc_profile_id,gha-lockfile sonar.issue.ignore.multicriteria.icc_profile_id.ruleKey=rust:S4790 sonar.issue.ignore.multicriteria.icc_profile_id.resourceKey=crates/cms/src/write.rs + +# githubactions:S7637 asks for every action to be pinned to a commit SHA. A workflow covered by +# .github/workflows/actions.lock runs the commit the lockfile records, so its tag refs are pinned +# and the lockfile covers the actions those actions use too. Sonar does not read lockfiles yet, so +# the rule flags the tag refs anyway: +# https://community.sonarsource.com/t/githubactions-s7637-false-positives-for-actions-pinned-through-native-actions-lock/190814 +sonar.issue.ignore.multicriteria.gha-lockfile.ruleKey=githubactions:S7637 +sonar.issue.ignore.multicriteria.gha-lockfile.resourceKey=.github/workflows/** From 5ba5b82fad19ac0baa1058e0cb1535af656a34a8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Thu, 12 Nov 2026 07:22:55 -0500 Subject: [PATCH 02/29] ci: verify the actions lockfile in CI Fails the build when .github/workflows/actions.lock stops matching the workflows, or when a locked pin no longer verifies upstream, so a stale or tampered lockfile cannot go unnoticed between dependency updates. --- .github/workflows/actions.lock | 3 +++ .github/workflows/lockfile.yml | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 .github/workflows/lockfile.yml diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock index 0d6c3b8f9..c2e897943 100644 --- a/.github/workflows/actions.lock +++ b/.github/workflows/actions.lock @@ -63,6 +63,9 @@ workflows: - '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' diff --git a/.github/workflows/lockfile.yml b/.github/workflows/lockfile.yml new file mode 100644 index 000000000..e630c9a8f --- /dev/null +++ b/.github/workflows/lockfile.yml @@ -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 From c298ac8bd28d5d7191a23eee979ca658f8a923c1 Mon Sep 17 00:00:00 2001 From: alehel Date: Fri, 13 Nov 2026 13:38:33 +0100 Subject: [PATCH 03/29] craft-video: MPEG-2 encoder, second step: B pictures (#1133) B pictures between anchors (b_pictures 0..=4, default 2) with reordering: pictures go in in display order, packets come out in decoding order with dts one picture interval before the presentation times. Closed or open GOPs; the leading B pictures of a closed GOP predict backward only. B macroblocks are forward, backward or interpolated, from a search per direction seeded with the P picture's vectors scaled to the distance, or skipped (previous directions with the predictors, kept inside the reference). Rate control adds TM5's B complexity (K_b 1.4); a constant quantiser codes B pictures at 1.25 times it. --- crates/video/README.md | 2 +- crates/video/src/lib.rs | 2 +- crates/video/src/mpeg2/README.md | 56 ++-- crates/video/src/mpeg2/encode/mod.rs | 307 +++++++++++++++---- crates/video/src/mpeg2/encode/motion.rs | 5 +- crates/video/src/mpeg2/encode/picture.rs | 369 ++++++++++++++++++----- crates/video/src/mpeg2/encode/rate.rs | 116 ++++--- crates/video/src/mpeg2/encode/tables.rs | 27 +- crates/video/src/mpeg2/encode/tests.rs | 158 +++++++++- crates/video/src/mpeg2/mod.rs | 2 +- crates/video/tests/mpeg2/encode.rs | 59 ++-- 11 files changed, 876 insertions(+), 227 deletions(-) diff --git a/crates/video/README.md b/crates/video/README.md index 74b8c6414..7de348bb6 100644 --- a/crates/video/README.md +++ b/crates/video/README.md @@ -21,7 +21,7 @@ No codec feature is on by default. | `dnx` | [`dnx`](src/dnx/README.md) | Avid DNxHD / DNxHR (SMPTE ST 2019-1 VC-3): decoder and encoder | | `apv` | [`apv`](src/apv/README.md) | APV (Advanced Professional Video, RFC 9924): decoder (`apv-decode` alone) and encoder (`apv-encode` alone) | | `vp8` | `vp8` | VP8 (RFC 6386): decoder | -| `mpeg2` | [`mpeg2`](src/mpeg2/README.md) | MPEG-2 video (ITU-T H.262) and MPEG-1 video (ISO/IEC 11172-2): decoder (`mpeg2-decode` alone); MPEG-2 encoder with I and P pictures (`mpeg2-encode`, which also turns on the decoder) | +| `mpeg2` | [`mpeg2`](src/mpeg2/README.md) | MPEG-2 video (ITU-T H.262) and MPEG-1 video (ISO/IEC 11172-2): decoder (`mpeg2-decode` alone); MPEG-2 encoder with I, P and B pictures (`mpeg2-encode`, which also turns on the decoder) | | `mpeg4` | [`mpeg4`](src/mpeg4/README.md) | MPEG-4 Part 2 video (ISO/IEC 14496-2, the Xvid/DivX format): Simple profile encoder (`mpeg4-encode`) | | `prores` | `prores` | Apple ProRes (SMPTE RDD 36): decoder and encoder | | `h264` | [`h264`](src/h264/decode/README.md) | ITU-T H.264 / AVC: decoder (`h264-decode` alone) and encoder (`h264-encode` alone) | diff --git a/crates/video/src/lib.rs b/crates/video/src/lib.rs index d9723a81d..0d8625bbd 100644 --- a/crates/video/src/lib.rs +++ b/crates/video/src/lib.rs @@ -8,7 +8,7 @@ //! | `dnx` | [`dnx`] | Avid DNxHD / DNxHR (SMPTE ST 2019-1): decoder and encoder | //! | `apv` | [`apv`] | APV (RFC 9924): decoder (`apv-decode`) and encoder (`apv-encode`) | //! | `vp8` | [`vp8`] | VP8 (RFC 6386): decoder (`vp8-decode`) and key-frame encoder (`vp8-encode`) | -//! | `mpeg2` | [`mpeg2`] | MPEG-2 video (ITU-T H.262) and MPEG-1 video: decoder (`mpeg2-decode`) and MPEG-2 I/P encoder (`mpeg2-encode`) | +//! | `mpeg2` | [`mpeg2`] | MPEG-2 video (ITU-T H.262) and MPEG-1 video: decoder (`mpeg2-decode`) and MPEG-2 I/P/B encoder (`mpeg2-encode`) | //! | `mpeg4` | [`mpeg4`] | MPEG-4 Part 2 video (ISO/IEC 14496-2, Xvid/DivX): Simple profile encoder (`mpeg4-encode`) | //! | `prores` | [`prores`] | Apple ProRes (SMPTE RDD 36): decoder and encoder | //! | `h264` | [`h264`] | ITU-T H.264 / AVC: decoder (`h264-decode`) and encoder (`h264-encode`) | diff --git a/crates/video/src/mpeg2/README.md b/crates/video/src/mpeg2/README.md index b68dc800b..a8502f696 100644 --- a/crates/video/src/mpeg2/README.md +++ b/crates/video/src/mpeg2/README.md @@ -1,7 +1,7 @@ # MPEG-2 and MPEG-1 video decoder, MPEG-2 encoder (`craft_video::mpeg2`, features `mpeg2-decode`, `mpeg2-encode`) -Clean-room MPEG-2 video and MPEG-1 video decoder, and the first step of an MPEG-2 video encoder -(I and P pictures, see "Encoder"), in pure Rust. Depends only on +Clean-room MPEG-2 video and MPEG-1 video decoder, and the first steps of an MPEG-2 video encoder +(I, P and B pictures, see "Encoder"), in pure Rust. Depends only on `thiserror` and (optional, feature `threads`) `rayon`. Builds for `wasm32-unknown-unknown`; no `unsafe`. No GPL/LGPL code (FFmpeg, libmpeg2, libavcodec) was consulted; FFmpeg is used only as an external fixture generator and test oracle. @@ -77,8 +77,8 @@ the synthetic-stream tests confirmed against FFmpeg: ## Encoder (feature `mpeg2-encode`) -The encoder is the first step of MPEG-2 encoding in this crate, tracked in -storytold/craft-libs#1133. It writes valid MPEG-2 elementary streams, but not yet DVD or +The encoder is built in steps, tracked in storytold/craft-libs#1133: I and P pictures came in +#1136, B pictures in the second step. It writes valid MPEG-2 elementary streams, but not yet DVD or broadcast deliverables: those need the later steps listed below. ```rust @@ -86,13 +86,15 @@ use craft_video::mpeg2::{Encoder, EncoderConfig, RateControl, YuvFrame}; let mut cfg = EncoderConfig::new(720, 576, 25, 1); cfg.rate = RateControl::Abr { kbps: 6000 }; // or RateControl::Quantiser(1..=31) -cfg.gop_size = 12; // one I picture, then P pictures +cfg.gop_size = 12; // pictures from one I picture to the next +cfg.b_pictures = 2; // I B B P B B P ...; 0 for I and P only +cfg.closed_gop = true; // or open GOPs let mut enc = Encoder::new(cfg)?; for (i, f) in frames.iter().enumerate() { let frame = YuvFrame { y: &f.y, cb: &f.cb, cr: &f.cr, y_stride: 720, c_stride: 360 }; for p in enc.encode(&frame, i as i64)? { /* p.data, p.pts, p.dts, p.keyframe, p.picture_type */ } } -for p in enc.flush() { /* none yet: no B pictures */ } +for p in enc.flush() { /* the pictures held for reordering */ } es.extend_from_slice(enc.end_of_sequence()); ``` @@ -101,9 +103,16 @@ What it does: - Main Profile (4:2:0) at Low, Main, High-1440 and High Level, and 4:2:2 Profile at Main and High Level. The lowest level that holds the size, frame rate and (average) bitrate is chosen; sizes beyond High Level are refused. Any even picture size; edges are padded by repetition. -- Progressive frame pictures, I and P, in closed GOPs. Every GOP starts with a sequence header, - sequence extension and GOP header (with a time code), so every I picture is a random access - point. Packets come out in decoding order, which equals display order without B pictures. +- Progressive frame pictures, I, P and B. Every GOP starts with a sequence header, sequence + extension and GOP header (with a time code), so every I picture is a random access point. +- B pictures (0-4 between anchors) and reordering: pictures go in in display order and packets + come out in decoding order; a B picture is held until the anchor after it is coded, and + `flush` codes the last held picture as a P picture. With B pictures, `dts` is the presentation + times in decoding order one picture interval earlier, so presentation times must step evenly. + `low_delay` is set only without B pictures. +- GOPs are closed or open. The B pictures shown before a GOP's I picture (coded after it) predict + backward only in a closed GOP, and from the previous GOP's last anchor too in an open one. + temporal_reference counts display order from the GOP's first picture. - Forward DCT on the transpose of the decoder's IDCT basis; intra quantisation with a small dead zone, non-intra quantisation by truncation; default or custom quantiser matrices (sequence header); linear or non-linear quantiser scale; intra DC precision 8-10 bits; @@ -112,9 +121,14 @@ What it does: predictor, the previous picture's vectors), then half-sample refinement; cost SAD + λ·bits. Each macroblock is coded intra, motion-compensated with or without residual, "no MC" with residual, or skipped. The search range sets f_code (1-4). +- B pictures: a search per direction (candidates include the P picture's vectors scaled to the + B picture's distance), then forward, backward, interpolated or the skip prediction (the + previous macroblock's directions with the predictors), whichever costs least, or intra. + Interpolated predictions are averaged in the decoder's order. - Adaptive quantisation by macroblock activity (Test Model 5 §5.2.3). -- Rate control: a constant quantiser, or single-pass average bitrate with Test Model 5's - target bit allocation at picture level. Streams are signalled as variable bitrate +- Rate control: a constant quantiser (B pictures at 1.25 times it), or single-pass average + bitrate with Test Model 5's target bit allocation at picture level (B pictures weighted by + K_b = 1.4). Streams are signalled as variable bitrate (`vbv_delay` 0xFFFF, the level's maximum bitrate and VBV size in the sequence header); the VBV buffer is not modelled. - The reconstruction uses the decoder's own dequantisation, mismatch control, IDCT and motion @@ -122,7 +136,7 @@ What it does: - Slices (one per macroblock row) are coded in parallel on rayon with `threads`; the stream is identical either way. -Later steps (#1133): B pictures and reordering; CBR and VBR with a VBV guarantee; DVD presets +Later steps (#1133): CBR and VBR with a VBV guarantee; DVD presets (sizes, aspect, GOP length, 9.8 Mb/s, 23.976 soft telecine); interlaced coding (field/frame DCT, field prediction, alternate scan) and 4:2:2 High Level CBR for XDCAM HD422. MPEG-2 essence in the MXF writer and a program stream muxer belong to `craft-media`. @@ -191,9 +205,15 @@ fell to 77–150 fps). These are lower bounds. rate control converges on its target. `encode/tests.rs`: our decoder outputs exactly the encoder's reconstruction (intra only, a 30-picture P GOP, 4:2:2, linear and non-linear quantisers, quantisers 1 and 31, DC precision 8 and 10, custom matrices, odd macroblock counts, - average bitrate); quality and size follow the quantiser; repeated pictures skip; levels and - aspect in the sequence header; bad configurations and short or inconsistent planes are refused. -- `tests/mpeg2/encode.rs`: FFmpeg decodes our streams of `testsrc2` (CIF and 576-line 4:2:2) - without errors and within the decoder suite's tolerance of our reconstruction, ffprobe reports - the profile, level, pixel format and I/P picture types, and average bitrate lands within 10 % of - 800 and 2500 kbit/s targets. + average bitrate, each with and without B pictures; 1-4 B pictures with closed and open GOPs, + GOP lengths that are and are not multiples of the anchor distance, and streams that end inside + a group of B pictures); decoding order, decoding times and temporal_reference; a closed GOP + decodes from its I picture with its leading B pictures and an open one without them; B pictures + are smaller than P pictures; uneven presentation times are refused with B pictures; quality and + size follow the quantiser; repeated pictures skip; levels and aspect in the sequence header; + bad configurations and short or inconsistent planes are refused. +- `tests/mpeg2/encode.rs`: FFmpeg decodes our streams of `testsrc2` (CIF and 576-line 4:2:2; + without B pictures, with 2 and 4, closed and open GOPs) without errors and within the decoder + suite's tolerance of our reconstruction, ffprobe reports the profile, level, pixel format and + picture types in display order, and average bitrate lands within 10 % of 800 and 2500 kbit/s + targets. diff --git a/crates/video/src/mpeg2/encode/mod.rs b/crates/video/src/mpeg2/encode/mod.rs index fcd1bd8d1..073847f8c 100644 --- a/crates/video/src/mpeg2/encode/mod.rs +++ b/crates/video/src/mpeg2/encode/mod.rs @@ -1,9 +1,9 @@ //! MPEG-2 video encoder (ITU-T H.262 | ISO/IEC 13818-2), feature `mpeg2-encode`. //! -//! This is the first step of MPEG-2 encoding in this crate (storytold/craft-libs#1133): I and P -//! pictures, progressive frame pictures, 4:2:0 (Main Profile) and 4:2:2 (4:2:2 Profile), closed -//! GOPs, constant quantiser or single-pass average bitrate. B pictures, a VBV guarantee, -//! interlaced coding and DVD / broadcast presets come later. +//! MPEG-2 encoding in this crate is built in steps (storytold/craft-libs#1133). So far: I, P and B +//! pictures, progressive frame pictures, 4:2:0 (Main Profile) and 4:2:2 (4:2:2 Profile), closed or +//! open GOPs, constant quantiser or single-pass average bitrate. A VBV guarantee, interlaced +//! coding and DVD / broadcast presets come later. //! //! ```no_run //! use craft_video::mpeg2::encode::{Encoder, EncoderConfig, RateControl, YuvFrame}; @@ -36,6 +36,8 @@ mod transform; #[cfg(test)] mod tests; +use std::collections::VecDeque; + use crate::mpeg2::ChromaFormat; use crate::mpeg2::headers::{DEFAULT_INTRA, PictureType, SCAN}; use crate::mpeg2::mc::Frame; @@ -50,6 +52,10 @@ pub enum EncodeError { Config(String), #[error("bad input frame: {0}")] BadFrame(String), + /// With B pictures, presentation times must increase by a constant step (the decoding times + /// are the presentation times one step earlier). + #[error("bad presentation time: {0}")] + BadTimestamp(String), } /// aspect_ratio_information (H.262 Table 6-3). @@ -97,8 +103,15 @@ pub struct EncoderConfig { /// 4:2:0 (Main Profile) or 4:2:2 (4:2:2 Profile). pub chroma: ChromaFormat, pub aspect: AspectRatio, - /// Pictures per GOP: one I picture, then P pictures. 1 codes every picture as I. 1..=1023. + /// Pictures from one I picture to the next (display order). 1 codes every picture as I. + /// 1..=1023, and at most 1024 with `b_pictures` added. pub gop_size: u32, + /// B pictures between two anchor (I or P) pictures, 0..=4. 0 gives I and P pictures only and a + /// low-delay stream (no reordering). + pub b_pictures: u32, + /// Closed GOPs: the B pictures shown before a GOP's I picture predict from it alone, so the + /// GOP decodes on its own. Open GOPs let them predict from the previous GOP too. + pub closed_gop: bool, pub rate: RateControl, /// q_scale_type: the non-linear quantiser scale (finer steps at low quantisers). pub nonlinear_quant: bool, @@ -125,6 +138,8 @@ impl EncoderConfig { chroma: ChromaFormat::Yuv420, aspect: AspectRatio::Square, gop_size: 12, + b_pictures: 2, + closed_gop: true, rate: RateControl::Quantiser(4), nonlinear_quant: true, adaptive_quant: true, @@ -155,9 +170,11 @@ pub struct Packet { /// the picture. pub data: Vec, pub pts: i64, - /// Decoding timestamp; equal to `pts` while there are no B pictures. + /// Decoding timestamp: `pts` without B pictures; with them, the presentation times in + /// decoding order one picture interval earlier, so `dts <= pts`. pub dts: i64, - /// An I picture starting a closed GOP (random access point). + /// An I picture (random access point; in an open GOP the B pictures after it in decoding + /// order also need the previous GOP). pub keyframe: bool, pub picture_type: PictureType, /// Average quantiser_scale of the picture's macroblocks. @@ -260,6 +277,13 @@ fn validate(cfg: &EncoderConfig) -> Result<(), EncodeError> { if !(1..=1023).contains(&cfg.gop_size) { return bad(format!("GOP size {} (1..=1023)", cfg.gop_size)); } + if cfg.b_pictures > 4 { + return bad(format!("{} B pictures between anchors (0..=4)", cfg.b_pictures)); + } + // temporal_reference (10 bits) counts a GOP's leading B pictures too + if cfg.gop_size + cfg.b_pictures > 1024 { + return bad(format!("GOP size {} with {} B pictures: more than 1024 pictures to number", cfg.gop_size, cfg.b_pictures)); + } match cfg.rate { RateControl::Quantiser(q) if !(1..=31).contains(&q) => return bad(format!("quantiser_scale_code {q} (1..=31)")), RateControl::Abr { kbps: 0 } => return bad("bitrate 0".into()), @@ -284,6 +308,26 @@ fn put_matrix(w: &mut BitWriter, m: &[u8; 64]) { } } +/// A source picture waiting for the anchor after it (a B picture). +struct Held { + src: Frame, + pts: i64, + /// Display index. + n: u64, +} + +/// A coded picture before rate control and timestamps. +struct Coded { + packet: Packet, + recon: Frame, + mvs: Vec<[i32; 2]>, + avg_q: f64, +} + +/// Quantiser ratio of B to P pictures with a constant quantiser (TM5 uses 1.4 for its rate +/// allocation; a constant quantiser keeps B pictures a little closer to the anchors). +const B_QUANT_FACTOR: f64 = 1.25; + /// A stateful encoder. Feed pictures in display order; packets come out in decoding order. pub struct Encoder { cfg: EncoderConfig, @@ -297,16 +341,32 @@ pub struct Encoder { inter_matrix: [u8; 64], /// Sequence header and sequence extension, sent before every GOP. seq_header: Vec, - /// The reconstruction of the previous picture. - reference: Option, - prev_mvs: Vec<[i32; 2]>, - /// Pictures coded so far. + /// The reconstructions of the last two anchor pictures and their display indices: `latest` + /// is the backward reference of the B pictures held, and the forward reference of the next + /// P picture. + older: Option<(Frame, u64)>, + latest: Option<(Frame, u64)>, + /// Forward vectors of the latest anchor when it is a P picture (search candidates). + anchor_mvs: Vec<[i32; 2]>, + /// B pictures waiting for their backward reference. + held: Vec, + /// Display index of the next picture. count: u64, - /// Position of the next picture in its GOP. - gop_pos: u32, + /// Display indices of the current GOP's I picture and of its first picture in display order + /// (a leading B picture in front of the I picture, or the I picture). + gop_start: u64, + gop_first: u64, + /// The current GOP is closed: its leading B pictures predict backward only. + gop_closed: bool, + /// Presentation times not yet given to a coded picture as its decoding time, in input order. + display_pts: VecDeque, + last_pts: Option, + pts_step: Option, + /// Packets waiting for the presentation time step (the decoding time offset). + out: Vec, rc: Option, recon_capture: bool, - recon: Vec, + recon: Vec<(u64, ReconFrame)>, } impl Encoder { @@ -330,10 +390,18 @@ impl Encoder { intra_matrix: cfg.intra_matrix.unwrap_or(DEFAULT_INTRA), inter_matrix: cfg.non_intra_matrix.unwrap_or([16; 64]), seq_header: Vec::new(), - reference: None, - prev_mvs: Vec::new(), + older: None, + latest: None, + anchor_mvs: Vec::new(), + held: Vec::new(), count: 0, - gop_pos: 0, + gop_start: 0, + gop_first: 0, + gop_closed: true, + display_pts: VecDeque::new(), + last_pts: None, + pts_step: None, + out: Vec::new(), rc, recon_capture: false, recon: Vec::new(), @@ -367,13 +435,24 @@ impl Encoder { self.recon_capture = on; } + /// The reconstructions captured so far, in display order. pub fn take_recon(&mut self) -> Vec { - std::mem::take(&mut self.recon) + let mut r = std::mem::take(&mut self.recon); + r.sort_by_key(|(n, _)| *n); + r.into_iter().map(|(_, f)| f).collect() } - /// Pictures held for reordering (none yet: there are no B pictures). + /// Code the pictures held for reordering (the last of them becomes a P picture) and return + /// every packet not yet returned. The next picture starts a new GOP. pub fn flush(&mut self) -> Vec { - Vec::new() + let mut out = Vec::new(); + if let Some(last) = self.held.pop() { + self.code_anchor(last, PictureType::P, &mut out); + } + self.older = None; + self.latest = None; + self.anchor_mvs.clear(); + self.release(out, true) } fn sequence_header_bytes(&self) -> Vec { @@ -406,16 +485,15 @@ impl Encoder { w.put((bit_rate >> 18) as u32 & 0xFFF, 12); w.bit(true); // marker_bit w.put((vbv >> 10) as u32 & 0xFF, 8); - w.bit(true); // low_delay: no B pictures + w.bit(c.b_pictures == 0); // low_delay: no B pictures, no reordering w.put(0, 2); // frame_rate_extension_n w.put(0, 5); // frame_rate_extension_d w.finish() } - /// group_of_pictures_header with the time code of picture `self.count`. - fn gop_header(&self, w: &mut BitWriter) { + /// group_of_pictures_header with the time code of display picture `n`. + fn gop_header(&self, w: &mut BitWriter, n: u64, closed: bool) { let fps = u64::from(self.cfg.fps_num.div_ceil(self.cfg.fps_den)).max(1); - let n = self.count; let secs = n / fps; w.start_code(0xB8); w.bit(false); // drop_frame_flag @@ -424,24 +502,40 @@ impl Encoder { w.bit(true); // marker_bit w.put((secs % 60) as u32, 6); w.put((n % fps) as u32, 6); - w.bit(true); // closed_gop + w.bit(closed); // closed_gop w.bit(false); // broken_link } - fn picture_header(&self, w: &mut BitWriter, intra: bool) { + fn picture_header(&self, w: &mut BitWriter, ptype: PictureType, temporal_reference: u64) { w.start_code(0x00); - w.put(self.gop_pos & 0x3FF, 10); // temporal_reference: display order within the GOP - w.put(if intra { 1 } else { 2 }, 3); + w.put((temporal_reference & 0x3FF) as u32, 10); // display order within the GOP + w.put( + match ptype { + PictureType::P => 2, + PictureType::B => 3, + _ => 1, + }, + 3, + ); w.put(0xFFFF, 16); // vbv_delay: variable bitrate - if !intra { - w.bit(false); // full_pel_forward_vector - w.put(7, 3); // forward_f_code (MPEG-2: in the extension) + for dir in [PictureType::P, PictureType::B] { + if ptype == dir || ptype == PictureType::B { + // full_pel_{forward,backward}_vector and {forward,backward}_f_code: MPEG-2 + // carries them in the extension + w.bit(false); + w.put(7, 3); + } } w.bit(false); // extra_bit_picture w.start_code(0xB5); w.put(8, 4); // picture coding extension - let fc = if intra { 15 } else { u32::from(self.f_code) }; - for v in [fc, fc, 15, 15] { + let fc = u32::from(self.f_code); + let f_codes = match ptype { + PictureType::P => [fc, fc, 15, 15], + PictureType::B => [fc; 4], + _ => [15; 4], + }; + for v in f_codes { w.put(v, 4); } w.put(u32::from(self.cfg.intra_dc_bits - 8), 2); @@ -502,28 +596,107 @@ impl Encoder { acts.iter().map(|&a| qcode_for(nl, base * (2.0 * a + avg) / (a + 2.0 * avg))).collect() } - /// Encode one picture. Returns the packets that became ready (one per picture for now). + /// With B pictures, presentation times must step by a constant amount. Checked before any + /// state changes. + fn check_pts(&self, pts: i64) -> Result, EncodeError> { + if self.cfg.b_pictures == 0 { + return Ok(None); + } + let Some(last) = self.last_pts else { return Ok(None) }; + let step = pts.checked_sub(last).filter(|&d| d > 0).ok_or_else(|| EncodeError::BadTimestamp(format!("{pts} after {last}")))?; + match self.pts_step { + Some(s) if s != step => Err(EncodeError::BadTimestamp(format!("{pts} after {last}: step {step}, earlier {s}"))), + _ => Ok(Some(step)), + } + } + + /// Encode one picture (display order). Returns the packets that became ready, in decoding + /// order: with B pictures, a B picture is held until the anchor after it is coded. pub fn encode(&mut self, frame: &YuvFrame, pts: i64) -> Result, EncodeError> { let src = self.load(frame)?; - let reference = if self.gop_pos == 0 { None } else { self.reference.take() }; - let intra = reference.is_none(); + if let Some(step) = self.check_pts(pts)? { + self.pts_step = Some(step); + } + self.last_pts = Some(pts); + let n = self.count; + self.count += 1; + self.display_pts.push_back(pts); + let held = Held { src, pts, n }; + let mut out = Vec::new(); + let m = u64::from(self.cfg.b_pictures) + 1; + if self.latest.is_none() || n - self.gop_start >= u64::from(self.cfg.gop_size) { + self.code_anchor(held, PictureType::I, &mut out); + } else if (n - self.gop_start).is_multiple_of(m) { + self.code_anchor(held, PictureType::P, &mut out); + } else { + self.held.push(held); + } + Ok(self.release(out, false)) + } + + /// Code an anchor picture, then the B pictures held in front of it. + fn code_anchor(&mut self, h: Held, ptype: PictureType, out: &mut Vec) { + let intra = ptype == PictureType::I; if intra { - self.gop_pos = 0; + self.gop_start = h.n; + self.gop_first = self.held.first().map_or(h.n, |b| b.n); + self.gop_closed = self.cfg.closed_gop || self.held.is_empty(); + let n = u64::from(self.cfg.gop_size); + let m = u64::from(self.cfg.b_pictures) + 1; + let np = (n - 1) / m; if let Some(rc) = self.rc.as_mut() { - rc.start_gop(self.cfg.gop_size); + rc.start_gop(np as u32, (n - 1 - np) as u32); } } + let fwd = if intra { None } else { self.latest.as_ref().map(|r| &r.0) }; + let cand: &[[i32; 2]] = if intra { &[] } else { &self.anchor_mvs }; + let c = self.code_picture(&h, ptype, (fwd, None), [cand, &[]]); + let (recon, mvs) = self.finish(c, h.n, out); + self.older = self.latest.take(); + self.latest = Some((recon, h.n)); + self.anchor_mvs = if intra { Vec::new() } else { mvs }; + for b in std::mem::take(&mut self.held) { + self.code_b(b, out); + } + } + + /// Code a B picture between the two latest anchors. The leading B pictures of a closed GOP + /// (shown before its I picture) predict backward only. + fn code_b(&mut self, b: Held, out: &mut Vec) { + let Some((bwd, bn)) = self.latest.as_ref() else { return }; + let leading = b.n < self.gop_start; + let fwd = self.older.as_ref().filter(|_| !(leading && self.gop_closed)); + // search candidates: the latest anchor's vectors (which span the two anchors) scaled to + // this picture's distance from each + let span = self.older.as_ref().map_or(1, |o| bn.saturating_sub(o.1).max(1)) as i64; + let scaled = |d: i64| -> Vec<[i32; 2]> { + self.anchor_mvs.iter().map(|v| [(i64::from(v[0]) * d / span) as i32, (i64::from(v[1]) * d / span) as i32]).collect() + }; + let cf = fwd.map_or(Vec::new(), |f| scaled(b.n as i64 - f.1 as i64)); + let cb = scaled(b.n as i64 - *bn as i64); + let c = self.code_picture(&b, PictureType::B, (fwd.map(|f| &f.0), Some(bwd)), [&cf, &cb]); + self.finish(c, b.n, out); + } + + /// Code one picture with the given references and search candidates. + fn code_picture(&self, h: &Held, ptype: PictureType, (fwd, bwd): (Option<&Frame>, Option<&Frame>), cand: [&[[i32; 2]]; 2]) -> Coded { let base = match (&self.rc, self.cfg.rate) { - (Some(rc), _) => rc.qscale(intra), - (None, RateControl::Quantiser(q)) => f64::from(qscale(self.cfg.nonlinear_quant, q)), + (Some(rc), _) => rc.qscale(ptype), + (None, RateControl::Quantiser(q)) => { + let qs = f64::from(qscale(self.cfg.nonlinear_quant, q)); + if ptype == PictureType::B { qs * B_QUANT_FACTOR } else { qs } + } (None, _) => 8.0, }; - let mb_qcode = self.mb_quant(&src, base); + let src = &h.src; + let mb_qcode = self.mb_quant(src, base); let mut recon = Frame::new(src.w, src.h, src.cw, src.ch); let ctx = PicCtx { - src: &src, - reference: reference.as_ref(), - prev_mvs: &self.prev_mvs, + ptype, + src, + fwd, + bwd, + cand, mb_w: self.mb_w, shx: self.shx, shy: self.shy, @@ -536,11 +709,12 @@ impl Encoder { mb_qcode: &mb_qcode, }; let rows = encode_rows(&ctx, &mut recon, self.cfg.threads); + let intra = ptype == PictureType::I; let mut w = BitWriter::new(); if intra { - self.gop_header(&mut w); + self.gop_header(&mut w, self.gop_first, self.gop_closed); } - self.picture_header(&mut w, intra); + self.picture_header(&mut w, ptype, h.n - self.gop_first); let mut data = if intra { self.seq_header.clone() } else { Vec::new() }; data.extend_from_slice(&w.finish()); let mut qsum = 0u64; @@ -551,18 +725,43 @@ impl Encoder { mvs.extend_from_slice(&r.mvs); } let avg_q = qsum as f64 / (self.mb_w * self.mb_h).max(1) as f64; + let packet = Packet { data, pts: h.pts, dts: h.pts, keyframe: intra, picture_type: ptype, quantiser_scale: avg_q as f32 }; + Coded { packet, recon, mvs, avg_q } + } + + /// Account for a coded picture (display index `n`): rate control, its decoding time, the + /// reconstruction capture. Returns the reconstruction and the picture's vectors. + fn finish(&mut self, c: Coded, n: u64, out: &mut Vec) -> (Frame, Vec<[i32; 2]>) { + let Coded { mut packet, recon, mvs, avg_q } = c; if let Some(rc) = self.rc.as_mut() { - rc.update(intra, data.len() as u64 * 8, avg_q); + rc.update(packet.picture_type, packet.data.len() as u64 * 8, avg_q); } + // the k-th coded picture decodes at the k-th presentation time (less the offset that + // `release` subtracts); one time is queued per input picture + packet.dts = self.display_pts.pop_front().unwrap_or(packet.pts); if self.recon_capture { - self.recon.push(self.crop(&recon)); + self.recon.push((n, self.crop(&recon))); } - self.reference = Some(recon); - self.prev_mvs = mvs; - self.count += 1; - self.gop_pos = (self.gop_pos + 1) % self.cfg.gop_size; - let picture_type = if intra { PictureType::I } else { PictureType::P }; - Ok(vec![Packet { data, pts, dts: pts, keyframe: intra, picture_type, quantiser_scale: avg_q as f32 }]) + out.push(packet); + (recon, mvs) + } + + /// Hand out packets once their decoding times are known: with B pictures they are the + /// presentation times one step earlier, so packets wait for the second picture's time. + fn release(&mut self, out: Vec, flushing: bool) -> Vec { + self.out.extend(out); + if self.cfg.b_pictures == 0 { + return std::mem::take(&mut self.out); + } + if self.pts_step.is_none() && !flushing { + return Vec::new(); + } + let off = self.pts_step.unwrap_or(0); + let mut res = std::mem::take(&mut self.out); + for p in &mut res { + p.dts -= off; + } + res } fn crop(&self, f: &Frame) -> ReconFrame { diff --git a/crates/video/src/mpeg2/encode/motion.rs b/crates/video/src/mpeg2/encode/motion.rs index a7bedc80b..8242faafe 100644 --- a/crates/video/src/mpeg2/encode/motion.rs +++ b/crates/video/src/mpeg2/encode/motion.rs @@ -47,7 +47,7 @@ impl Search<'_> { } /// A half-sample vector the bitstream can carry whose prediction stays inside the reference. - fn valid(&self, mv: [i32; 2]) -> bool { + pub(crate) fn valid(&self, mv: [i32; 2]) -> bool { let f = 1i32 << self.f_code.saturating_sub(1); let (ox, oy) = self.origin(); let size = [self.reference.w as i32, self.reference.h as i32]; @@ -58,7 +58,8 @@ impl Search<'_> { }) } - fn mv_cost(&self, mv: [i32; 2]) -> u32 { + /// λ·(bits of `mv` against the predictor). + pub(crate) fn mv_cost(&self, mv: [i32; 2]) -> u32 { let t = enc_tables(); let bits: u32 = (0..2).map(|c| t.motion_bits(wrap_delta(mv[c], self.pmv[c], self.f_code), self.f_code)).sum(); self.lambda * bits diff --git a/crates/video/src/mpeg2/encode/picture.rs b/crates/video/src/mpeg2/encode/picture.rs index e7bb84b7e..b6c177eec 100644 --- a/crates/video/src/mpeg2/encode/picture.rs +++ b/crates/video/src/mpeg2/encode/picture.rs @@ -1,11 +1,11 @@ //! Slices and macroblocks of one frame picture (H.262 §6.2.4-§6.2.6), with a reconstruction //! that matches the decoder exactly. One slice per macroblock row; slices only depend on the -//! reference picture, so rows can be coded in parallel. +//! reference pictures, so rows can be coded in parallel. -use crate::mpeg2::headers::SCAN; +use crate::mpeg2::headers::{PictureType, SCAN}; use crate::mpeg2::mc::{Block, Frame, predict}; use crate::mpeg2::slice::NON_LINEAR_Q; -use crate::mpeg2::vlc::{MB_FWD, MB_INTRA, MB_PATTERN, MB_QUANT}; +use crate::mpeg2::vlc::{MB_BWD, MB_FWD, MB_INTRA, MB_PATTERN, MB_QUANT}; use super::bitwriter::BitWriter; use super::motion::Search; @@ -35,12 +35,17 @@ pub(crate) fn qcode_for(nonlinear: bool, qs: f64) -> u8 { /// What every slice of one picture shares (read-only). pub(crate) struct PicCtx<'a> { + /// I, P or B. + pub ptype: PictureType, /// The padded source picture. pub src: &'a Frame, - /// The reconstructed previous picture (P pictures); `None` codes an I picture. - pub reference: Option<&'a Frame>, - /// Vectors of the previous picture (motion search candidates), one per macroblock. - pub prev_mvs: &'a [[i32; 2]], + /// The reconstructed reference pictures: forward (P and B pictures) and backward (B + /// pictures). A B picture without a forward reference (the leading B pictures of a closed + /// GOP) predicts backward only. + pub fwd: Option<&'a Frame>, + pub bwd: Option<&'a Frame>, + /// Motion search candidates per macroblock, forward and backward (empty: none). + pub cand: [&'a [[i32; 2]]; 2], pub mb_w: usize, pub shx: u32, pub shy: u32, @@ -166,11 +171,22 @@ impl MbPix { struct SliceSt { qcode: u8, dc: [i32; 3], - pmv: [i32; 2], + /// Motion vector predictors, forward and backward. + pmv: [[i32; 2]; 2], /// Skipped macroblocks not yet covered by an address increment. skipped: usize, - /// The vector of the macroblock to the left (motion search candidate). - left_mv: [i32; 2], + /// The vectors of the macroblock to the left (motion search candidates). + left_mv: [[i32; 2]; 2], + /// Prediction directions (`MB_FWD` / `MB_BWD`) of the previous macroblock, or `MB_INTRA`: + /// a skipped macroblock in a B picture repeats them. + last_dirs: i16, +} + +/// A B macroblock's prediction: its directions and a vector per direction. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct BPred { + dirs: i16, + mvs: [[i32; 2]; 2], } /// One macroblock being coded. @@ -188,7 +204,7 @@ pub(crate) fn encode_row(ctx: &PicCtx, row: usize, out: &mut RowPlanes) -> RowOu let q0 = ctx.mb_qcode.get(row * ctx.mb_w).copied().unwrap_or(1); w.put(u32::from(q0), 5); w.bit(false); // extra_bit_slice - let mut st = SliceSt { qcode: q0, dc: [ctx.dc_reset(); 3], pmv: [0; 2], skipped: 0, left_mv: [0; 2] }; + let mut st = SliceSt { qcode: q0, dc: [ctx.dc_reset(); 3], pmv: [[0; 2]; 2], skipped: 0, left_mv: [[0; 2]; 2], last_dirs: MB_INTRA }; let mut ro = RowOut { bytes: Vec::new(), mvs: vec![[0; 2]; ctx.mb_w], stats: RowStats::default() }; for mbx in 0..ctx.mb_w { macroblock(ctx, &mut st, &mut w, (mbx, row), out, &mut ro); @@ -205,31 +221,105 @@ fn macroblock(ctx: &PicCtx, st: &mut SliceSt, w: &mut BitWriter, (mbx, mby): (us let qs = qscale(ctx.nonlinear, qcode); ro.stats.qsum += qs as u64; let planes = (ctx.src.w, ctx.src.cw); - let Some(reference) = ctx.reference else { - intra_mb(ctx, st, w, &mb).put(out, mbx, ctx.cmb(), planes); - ro.stats.intra += 1; - return; + let lambda = ((qs * 3 + 4) / 8).max(1) as u32; + let rec = match (ctx.ptype, ctx.fwd) { + (PictureType::P, Some(reference)) => { + let search = Search { reference, src: &src.y, mbx, mby, f_code: ctx.f_code, range: ctx.search, pmv: st.pmv[0], lambda }; + let found = search.run(&candidates(ctx, st, addr, 0)); + if intra_cost(&src.y).saturating_add(INTRA_BIAS) < found.sad { + None + } else { + let (rec, skipped) = inter_mb(ctx, st, w, &mb, reference, found.mv); + ro.stats.skipped += skipped as u32; + ro.mvs[mbx] = found.mv; + st.left_mv[0] = found.mv; + Some(rec) + } + } + (PictureType::B, _) if ctx.bwd.is_some() => b_decision(ctx, st, &mb, lambda).map(|pred| { + let (rec, skipped) = b_mb(ctx, st, w, &mb, pred); + ro.stats.skipped += skipped as u32; + st.left_mv = pred.mvs; + rec + }), + _ => None, }; - let mut candidates = vec![st.left_mv, st.pmv]; + let rec = rec.unwrap_or_else(|| { + ro.stats.intra += 1; + st.left_mv = [[0; 2]; 2]; + intra_mb(ctx, st, w, &mb) + }); + rec.put(out, mbx, ctx.cmb(), planes); +} + +/// Motion search candidates for direction `s` of macroblock `addr`: the left neighbour's vector, +/// the predictor, and the co-located and neighbouring candidates of the picture. +fn candidates(ctx: &PicCtx, st: &SliceSt, addr: usize, s: usize) -> Vec<[i32; 2]> { + let mut c = vec![st.left_mv[s], st.pmv[s]]; for a in [Some(addr), addr.checked_sub(ctx.mb_w), Some(addr + 1), Some(addr + ctx.mb_w)].into_iter().flatten() { - if let Some(&v) = ctx.prev_mvs.get(a) { - candidates.push(v); + if let Some(&v) = ctx.cand[s].get(a) { + c.push(v); } } - let lambda = ((qs * 3 + 4) / 8).max(1) as u32; - let search = Search { reference, src: &src.y, mbx, mby, f_code: ctx.f_code, range: ctx.search, pmv: st.pmv, lambda }; - let found = search.run(&candidates); - if intra_cost(&src.y).saturating_add(INTRA_BIAS) < found.sad { - intra_mb(ctx, st, w, &mb).put(out, mbx, ctx.cmb(), planes); - ro.stats.intra += 1; - st.left_mv = [0; 2]; - return; + c +} + +/// Luma SAD of a prediction against the source. +fn sad(a: &[u8; 256], b: &[u8; 256]) -> u32 { + a.iter().zip(b).map(|(x, y)| u32::from(x.abs_diff(*y))).sum() +} + +/// Choose a B macroblock's prediction: forward, backward or interpolated, from a motion search +/// per direction, or the skip prediction (the previous macroblock's directions with the +/// predictors). `None`: code it intra. +fn b_decision(ctx: &PicCtx, st: &SliceSt, mb: &MbIn, lambda: u32) -> Option { + let addr = mb.mby * ctx.mb_w + mb.mbx; + let mut found = [None, None]; + for (s, r) in [ctx.fwd, ctx.bwd].into_iter().enumerate() { + if let Some(reference) = r { + let search = + Search { reference, src: &mb.src.y, mbx: mb.mbx, mby: mb.mby, f_code: ctx.f_code, range: ctx.search, pmv: st.pmv[s], lambda }; + let f = search.run(&candidates(ctx, st, addr, s)); + found[s] = Some((f.mv, f.sad, search.mv_cost(f.mv))); + } } - let (rec, skipped) = inter_mb(ctx, st, w, &mb, reference, found.mv); - rec.put(out, mbx, ctx.cmb(), planes); - ro.stats.skipped += skipped as u32; - ro.mvs[mbx] = found.mv; - st.left_mv = found.mv; + let mut best: Option<(BPred, u32)> = None; + let mut consider = |pred: BPred, cost: u32| { + if best.is_none_or(|(_, c)| cost < c) { + best = Some((pred, cost)); + } + }; + for (s, dir) in [(0, MB_FWD), (1, MB_BWD)] { + if let Some((mv, sad, bits)) = found[s] { + let mut mvs = [[0; 2]; 2]; + mvs[s] = mv; + consider(BPred { dirs: dir, mvs }, sad.saturating_add(bits)); + } + } + if let (Some((f, _, bits_f)), Some((b, _, bits_b))) = (found[0], found[1]) { + let pred = BPred { dirs: MB_FWD | MB_BWD, mvs: [f, b] }; + let p = predict_b(ctx, pred, mb.mbx, mb.mby); + consider(pred, sad(&p.y, &mb.src.y).saturating_add(bits_f).saturating_add(bits_b)); + } + // the skip prediction costs no vector bits; the predictors came from the macroblock to the + // left, so they must be checked to stay inside the reference from here + let edge = mb.mbx == 0 || mb.mbx + 1 == ctx.mb_w; + if !edge && st.last_dirs & MB_INTRA == 0 && st.last_dirs != 0 { + let pred = BPred { dirs: st.last_dirs, mvs: st.pmv }; + let usable = [(0, MB_FWD, ctx.fwd), (1, MB_BWD, ctx.bwd)].into_iter().all(|(s, dir, r)| { + st.last_dirs & dir == 0 + || r.is_some_and(|reference| { + let search = + Search { reference, src: &mb.src.y, mbx: mb.mbx, mby: mb.mby, f_code: ctx.f_code, range: ctx.search, pmv: st.pmv[s], lambda }; + search.valid(st.pmv[s]) + }) + }); + if usable { + consider(pred, sad(&predict_b(ctx, pred, mb.mbx, mb.mby).y, &mb.src.y)); + } + } + let (pred, cost) = best?; + (intra_cost(&mb.src.y).saturating_add(INTRA_BIAS) >= cost).then_some(pred) } /// Σ |sample − block mean| over the four luma blocks: the cost of coding them intra. @@ -270,8 +360,12 @@ fn put_coefficients(w: &mut BitWriter, t: &EncTables, levels: &[i32; 64], table: t.dct_eob[table].put(w); } -fn put_mb_type(w: &mut BitWriter, t: &EncTables, predicted: bool, flags: i16) { - let table = if predicted { &t.mbtype_p } else { &t.mbtype_i }; +fn put_mb_type(w: &mut BitWriter, t: &EncTables, ptype: PictureType, flags: i16) { + let table = match ptype { + PictureType::P => &t.mbtype_p, + PictureType::B => &t.mbtype_b, + _ => &t.mbtype_i, + }; if let Some(c) = table.get(flags as usize).copied().flatten() { c.put(w); } @@ -283,12 +377,13 @@ fn intra_mb(ctx: &PicCtx, st: &mut SliceSt, w: &mut BitWriter, mb: &MbIn) -> MbP let quant = mb.qcode != st.qcode; put_increment(w, t, st.skipped + 1); st.skipped = 0; - put_mb_type(w, t, ctx.reference.is_some(), if quant { MB_INTRA | MB_QUANT } else { MB_INTRA }); + put_mb_type(w, t, ctx.ptype, if quant { MB_INTRA | MB_QUANT } else { MB_INTRA }); if quant { w.put(u32::from(mb.qcode), 5); st.qcode = mb.qcode; } - st.pmv = [0; 2]; + st.pmv = [[0; 2]; 2]; + st.last_dirs = MB_INTRA; let qs = qscale(ctx.nonlinear, st.qcode); let dc_mult = 8 >> ctx.dc_prec; let dc_max = (1 << (8 + ctx.dc_prec)) - 1; @@ -321,28 +416,16 @@ fn intra_mb(ctx: &PicCtx, st: &mut SliceSt, w: &mut BitWriter, mb: &MbIn) -> MbP /// whether it was skipped. fn inter_mb(ctx: &PicCtx, st: &mut SliceSt, w: &mut BitWriter, mb: &MbIn, reference: &Frame, mv: [i32; 2]) -> (MbPix, bool) { let t = enc_tables(); - let mut rec = predict_mb(ctx, reference, mv, mb.mbx, mb.mby); + let mut rec = predict_mb(ctx, Some(reference), mv, mb.mbx, mb.mby); let qs = qscale(ctx.nonlinear, mb.qcode); - let (cbw, _) = ctx.cmb(); - let n = ctx.blocks(); - let mut levels = [[0i32; 64]; 8]; - let mut cbp = 0u32; - for i in 0..n { - let (s, p) = (mb.src.block(i, cbw), rec.block(i, cbw)); - let f = fdct(&std::array::from_fn(|k| s[k] - p[k])); - for pos in 0..64 { - levels[i][pos] = quant_inter(f[pos], ctx.inter_matrix[pos], qs); - } - if levels[i].iter().any(|&l| l != 0) { - cbp |= 1 << (n - 1 - i); - } - } + let (levels, cbp) = residual(ctx, mb, &rec, qs); st.dc = [ctx.dc_reset(); 3]; + st.last_dirs = MB_FWD; // the first and last macroblock of a slice are always coded let edge = mb.mbx == 0 || mb.mbx + 1 == ctx.mb_w; if mv == [0, 0] && cbp == 0 && !edge { st.skipped += 1; - st.pmv = [0; 2]; + st.pmv = [[0; 2]; 2]; return (rec, true); } put_increment(w, t, st.skipped + 1); @@ -358,44 +441,140 @@ fn inter_mb(ctx: &PicCtx, st: &mut SliceSt, w: &mut BitWriter, mb: &MbIn, refere if quant { flags |= MB_QUANT; } - put_mb_type(w, t, true, flags); + put_mb_type(w, t, PictureType::P, flags); if quant { w.put(u32::from(mb.qcode), 5); st.qcode = mb.qcode; } if flags & MB_FWD != 0 { for c in 0..2 { - t.put_motion(w, wrap_delta(mv[c], st.pmv[c], ctx.f_code), ctx.f_code); + t.put_motion(w, wrap_delta(mv[c], st.pmv[0][c], ctx.f_code), ctx.f_code); } - st.pmv = mv; + st.pmv[0] = mv; } else { - st.pmv = [0; 2]; // "No MC" resets the predictors - } - if cbp != 0 { - if n == 8 { - t.cbp[(cbp >> 2) as usize].put(w); - w.put(cbp & 3, 2); - } else { - t.cbp[cbp as usize].put(w); - } - for i in 0..n { - if cbp & (1 << (n - 1 - i)) != 0 { - put_coefficients(w, t, &levels[i], 0, 0); - rec.store(i, cbw, &reconstruct(&levels[i], ctx.inter_matrix, qs, false, 0), true); - } - } + st.pmv = [[0; 2]; 2]; // "No MC" resets the predictors } + put_residual(ctx, w, &levels, cbp, &mut rec, qs); (rec, false) } -/// The frame prediction of a macroblock from `f` with vector `mv`, formed as the decoder forms -/// it (§7.6.4: chroma vectors are the luma ones halved towards zero). -fn predict_mb(ctx: &PicCtx, f: &Frame, mv: [i32; 2], mbx: usize, mby: usize) -> MbPix { - let (cbw, cbh) = ctx.cmb(); +/// Quantised residual of every block of `mb` against `pred`, and the coded_block_pattern. +fn residual(ctx: &PicCtx, mb: &MbIn, pred: &MbPix, qs: i32) -> ([[i32; 64]; 8], u32) { + let (cbw, _) = ctx.cmb(); + let n = ctx.blocks(); + let mut levels = [[0i32; 64]; 8]; + let mut cbp = 0u32; + for i in 0..n { + let (s, p) = (mb.src.block(i, cbw), pred.block(i, cbw)); + let f = fdct(&std::array::from_fn(|k| s[k] - p[k])); + for pos in 0..64 { + levels[i][pos] = quant_inter(f[pos], ctx.inter_matrix[pos], qs); + } + if levels[i].iter().any(|&l| l != 0) { + cbp |= 1 << (n - 1 - i); + } + } + (levels, cbp) +} + +/// Write coded_block_pattern and the coded blocks, adding their reconstruction to `rec`. +fn put_residual(ctx: &PicCtx, w: &mut BitWriter, levels: &[[i32; 64]; 8], cbp: u32, rec: &mut MbPix, qs: i32) { + if cbp == 0 { + return; + } + let t = enc_tables(); + let (cbw, _) = ctx.cmb(); + let n = ctx.blocks(); + if n == 8 { + t.cbp[(cbp >> 2) as usize].put(w); + w.put(cbp & 3, 2); + } else { + t.cbp[cbp as usize].put(w); + } + for i in 0..n { + if cbp & (1 << (n - 1 - i)) != 0 { + put_coefficients(w, t, &levels[i], 0, 0); + rec.store(i, cbw, &reconstruct(&levels[i], ctx.inter_matrix, qs, false, 0), true); + } + } +} + +/// Code a B macroblock with prediction `pred` (or skip it); returns its reconstruction and +/// whether it was skipped. +fn b_mb(ctx: &PicCtx, st: &mut SliceSt, w: &mut BitWriter, mb: &MbIn, pred: BPred) -> (MbPix, bool) { + let t = enc_tables(); + let mut rec = predict_b(ctx, pred, mb.mbx, mb.mby); + let qs = qscale(ctx.nonlinear, mb.qcode); + let (levels, cbp) = residual(ctx, mb, &rec, qs); + st.dc = [ctx.dc_reset(); 3]; + // a skipped B macroblock repeats the previous one's directions with the predictors as its + // vectors (§7.6.6.3); the first and last macroblock of a slice are always coded + let edge = mb.mbx == 0 || mb.mbx + 1 == ctx.mb_w; + let same = |s: usize, dir: i16| pred.dirs & dir == 0 || pred.mvs[s] == st.pmv[s]; + if cbp == 0 && !edge && st.last_dirs & MB_INTRA == 0 && pred.dirs == st.last_dirs && same(0, MB_FWD) && same(1, MB_BWD) { + st.skipped += 1; + return (rec, true); + } + put_increment(w, t, st.skipped + 1); + st.skipped = 0; + let quant = cbp != 0 && mb.qcode != st.qcode; + let mut flags = pred.dirs; + if cbp != 0 { + flags |= MB_PATTERN; + } + if quant { + flags |= MB_QUANT; + } + put_mb_type(w, t, PictureType::B, flags); + if quant { + w.put(u32::from(mb.qcode), 5); + st.qcode = mb.qcode; + } + for (s, dir) in [(0, MB_FWD), (1, MB_BWD)] { + if pred.dirs & dir != 0 { + for c in 0..2 { + t.put_motion(w, wrap_delta(pred.mvs[s][c], st.pmv[s][c], ctx.f_code), ctx.f_code); + } + st.pmv[s] = pred.mvs[s]; + } + } + st.last_dirs = pred.dirs; + put_residual(ctx, w, &levels, cbp, &mut rec, qs); + (rec, false) +} + +/// The prediction of a B macroblock: forward, backward, or the average of both, formed in the +/// decoder's order (forward first, then the backward prediction averaged in). +fn predict_b(ctx: &PicCtx, pred: BPred, mbx: usize, mby: usize) -> MbPix { let mut p = MbPix { y: [0; 256], c: [[0; 256]; 2] }; + let mut avg = false; + for (s, dir, r) in [(0, MB_FWD, ctx.fwd), (1, MB_BWD, ctx.bwd)] { + if pred.dirs & dir != 0 { + if let Some(f) = r { + predict_into(ctx, &mut p, f, pred.mvs[s], (mbx, mby), avg); + } + avg = true; + } + } + p +} + +/// The frame prediction of a macroblock from `f` with vector `mv` (gray without a reference). +fn predict_mb(ctx: &PicCtx, f: Option<&Frame>, mv: [i32; 2], mbx: usize, mby: usize) -> MbPix { + let mut p = MbPix { y: [128; 256], c: [[128; 256]; 2] }; + if let Some(f) = f { + predict_into(ctx, &mut p, f, mv, (mbx, mby), false); + } + p +} + +/// Form the frame prediction from `f` with vector `mv` into `p`, or average it with what `p` +/// holds, as the decoder forms it (§7.6.4: chroma vectors are the luma ones halved towards zero). +fn predict_into(ctx: &PicCtx, p: &mut MbPix, f: &Frame, mv: [i32; 2], (mbx, mby): (usize, usize), avg: bool) { + let (cbw, cbh) = ctx.cmb(); let blk = Block { x: (mbx * 16) as i32 + (mv[0] >> 1), y: (mby * 16) as i32 + (mv[1] >> 1), hx: mv[0] & 1 != 0, hy: mv[1] & 1 != 0, bw: 16, bh: 16 }; - predict(&mut p.y, 0, 16, f.plane(0, None), blk, false); + predict(&mut p.y, 0, 16, f.plane(0, None), blk, avg); let cmv = [if ctx.shx == 1 { mv[0] / 2 } else { mv[0] }, if ctx.shy == 1 { mv[1] / 2 } else { mv[1] }]; let cb = Block { x: (mbx * cbw) as i32 + (cmv[0] >> 1), @@ -406,7 +585,45 @@ fn predict_mb(ctx: &PicCtx, f: &Frame, mv: [i32; 2], mbx: usize, mby: usize) -> bh: cbh, }; for c in 0..2 { - predict(&mut p.c[c], 0, cbw, f.plane(1 + c, None), cb, false); + predict(&mut p.c[c], 0, cbw, f.plane(1 + c, None), cb, avg); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_skip_prediction_never_leaves_the_reference() { + // three macroblocks across; the left neighbour's forward vector (+17 samples) fits its + // own position but points past the right edge from the middle macroblock + let mut f = Frame::new(48, 16, 24, 8); + for (i, v) in f.planes[0].iter_mut().enumerate() { + *v = ((i % 48) * 5 + (i / 48) * 11) as u8; + } + let ctx = PicCtx { + ptype: PictureType::B, + src: &f, + fwd: Some(&f), + bwd: Some(&f), + cand: [&[], &[]], + mb_w: 3, + shx: 1, + shy: 1, + intra_matrix: &crate::mpeg2::headers::DEFAULT_INTRA, + inter_matrix: &[16; 64], + nonlinear: true, + dc_prec: 1, + f_code: 2, + search: 15, + mb_qcode: &[4; 3], + }; + let bad = BPred { dirs: MB_FWD, mvs: [[34, 0], [0, 0]] }; + // a source that the out-of-range (edge-clamped) prediction would match exactly + let src = predict_b(&ctx, bad, 1, 0); + let st = SliceSt { qcode: 4, dc: [256; 3], pmv: bad.mvs, skipped: 0, left_mv: bad.mvs, last_dirs: MB_FWD }; + let mb = MbIn { src: &src, mbx: 1, mby: 0, qcode: 4 }; + let pred = b_decision(&ctx, &st, &mb, 1); + assert_ne!(pred, Some(bad)); } - p } diff --git a/crates/video/src/mpeg2/encode/rate.rs b/crates/video/src/mpeg2/encode/rate.rs index 65c23efc3..cd7de9680 100644 --- a/crates/video/src/mpeg2/encode/rate.rs +++ b/crates/video/src/mpeg2/encode/rate.rs @@ -1,18 +1,26 @@ //! Single-pass average bitrate control at picture level, after the target bit allocation of the //! MPEG-2 Test Model 5: each picture type keeps a complexity `X = bits · average quantiser`, the -//! bits left for the GOP are shared out by complexity, and a picture's quantiser is `X / target`. -//! There is no VBV model: the average converges on the target, single pictures may not. +//! bits left for the GOP are shared out by complexity (B pictures weighted down by `KB`), and a +//! picture's quantiser is `X / target`. There is no VBV model: the average converges on the +//! target, single pictures may not. + +use crate::mpeg2::headers::PictureType; + +/// TM5's quantiser ratio of B to P pictures (K_b / K_p). +const KB: f64 = 1.4; pub(crate) struct RateCtl { bps: f64, fps: f64, - /// Complexity of I and P pictures. + /// Complexity of I, P and B pictures. xi: f64, xp: f64, + xb: f64, /// Bits left for the current GOP (carries over between GOPs). remaining: f64, - /// P pictures left in the current GOP. + /// P and B pictures left in the current GOP. np: u32, + nb: u32, /// Range of quantiser_scale values. q_min: f64, q_max: f64, @@ -21,34 +29,52 @@ pub(crate) struct RateCtl { impl RateCtl { pub fn new(bps: f64, fps: f64, q_range: (f64, f64)) -> Self { // TM5's starting complexities - Self { bps, fps, xi: 160.0 * bps / 115.0, xp: 60.0 * bps / 115.0, remaining: 0.0, np: 0, q_min: q_range.0, q_max: q_range.1 } + Self { + bps, + fps, + xi: 160.0 * bps / 115.0, + xp: 60.0 * bps / 115.0, + xb: 42.0 * bps / 115.0, + remaining: 0.0, + np: 0, + nb: 0, + q_min: q_range.0, + q_max: q_range.1, + } } - /// A GOP of `pictures` pictures (one I, the rest P) starts. - pub fn start_gop(&mut self, pictures: u32) { - self.remaining += self.bps * f64::from(pictures) / self.fps; - self.np = pictures.saturating_sub(1); + /// A GOP of one I, `np` P and `nb` B pictures starts. + pub fn start_gop(&mut self, np: u32, nb: u32) { + self.remaining += self.bps * f64::from(1 + np + nb) / self.fps; + self.np = np; + self.nb = nb; } - /// quantiser_scale for the next picture. - pub fn qscale(&self, intra: bool) -> f64 { + /// quantiser_scale for the next picture of type `ptype`. + pub fn qscale(&self, ptype: PictureType) -> f64 { let floor = self.bps / (8.0 * self.fps); - let (target, x) = if intra { - (self.remaining / (1.0 + f64::from(self.np) * self.xp / self.xi), self.xi) - } else { - (self.remaining / f64::from(self.np.max(1)), self.xp) + let (np, nb) = (f64::from(self.np), f64::from(self.nb)); + let (target, x) = match ptype { + PictureType::I => (self.remaining / (1.0 + np * self.xp / self.xi + nb * self.xb / (self.xi * KB)), self.xi), + PictureType::B => (self.remaining / (nb.max(1.0) + np * KB * self.xp / self.xb), self.xb), + _ => (self.remaining / (np.max(1.0) + nb * self.xb / (KB * self.xp)), self.xp), }; (x / target.max(floor)).clamp(self.q_min, self.q_max) } - /// The picture used `bits` at average quantiser_scale `avg_q`. - pub fn update(&mut self, intra: bool, bits: u64, avg_q: f64) { - let x = bits as f64 * avg_q; - if intra { - self.xi = x.max(1.0); - } else { - self.xp = x.max(1.0); - self.np = self.np.saturating_sub(1); + /// A picture of type `ptype` used `bits` at average quantiser_scale `avg_q`. + pub fn update(&mut self, ptype: PictureType, bits: u64, avg_q: f64) { + let x = (bits as f64 * avg_q).max(1.0); + match ptype { + PictureType::I => self.xi = x, + PictureType::B => { + self.xb = x; + self.nb = self.nb.saturating_sub(1); + } + _ => { + self.xp = x; + self.np = self.np.saturating_sub(1); + } } self.remaining -= bits as f64; } @@ -58,18 +84,26 @@ impl RateCtl { mod tests { use super::*; - /// A picture whose size is `complexity / q` (the model the controller assumes). - fn run(complexity_i: f64, complexity_p: f64, bps: f64, gops: u32) -> f64 { + /// A picture whose size is `complexity / q` (the model the controller assumes), in GOPs of + /// 12 pictures with `b` B pictures between anchors. + fn run(c: [f64; 3], b: usize, bps: f64, gops: u32) -> f64 { let fps = 25.0; + let types: Vec = (0..12) + .map(|k| match k { + 0 => PictureType::I, + k if k % (b + 1) == 0 => PictureType::P, + _ => PictureType::B, + }) + .collect(); + let np = types.iter().filter(|&&t| t == PictureType::P).count() as u32; let mut rc = RateCtl::new(bps, fps, (1.0, 112.0)); let mut total = 0.0; for _ in 0..gops { - rc.start_gop(12); - for k in 0..12 { - let intra = k == 0; - let q = rc.qscale(intra); - let bits = (if intra { complexity_i } else { complexity_p }) / q; - rc.update(intra, bits as u64, q); + rc.start_gop(np, 11 - np); + for &t in &types { + let q = rc.qscale(t); + let bits = c[t as usize] / q; + rc.update(t, bits as u64, q); total += bits; } } @@ -79,24 +113,24 @@ mod tests { #[test] fn converges_on_the_target() { // complexities whose target quantiser is inside the allowed range - for (ci, cp) in [(4e7, 1e7), (8e7, 2e6), (1e7, 9e6)] { - let got = run(ci, cp, 4e6, 20); - assert!((got / 4e6 - 1.0).abs() < 0.03, "{ci} {cp}: {got}"); + for (c, b) in [([4e7, 1e7, 7e6], 0), ([8e7, 2e6, 1e6], 0), ([1e7, 9e6, 8e6], 0), ([4e7, 1e7, 7e6], 2), ([6e7, 2e7, 5e6], 1)] { + let got = run(c, b, 4e6, 20); + assert!((got / 4e6 - 1.0).abs() < 0.03, "{c:?} b {b}: {got}"); } } #[test] fn the_quantiser_stays_in_range() { let mut rc = RateCtl::new(1e6, 25.0, (2.0, 62.0)); - rc.start_gop(2); + rc.start_gop(1, 0); // a picture far larger than its budget pushes the next quantiser to the top - rc.update(true, 50_000_000, 10.0); - rc.start_gop(2); - assert_eq!(rc.qscale(true), 62.0); + rc.update(PictureType::I, 50_000_000, 10.0); + rc.start_gop(1, 0); + assert_eq!(rc.qscale(PictureType::I), 62.0); // and a tiny one to the bottom let mut rc = RateCtl::new(1e6, 25.0, (2.0, 62.0)); - rc.start_gop(2); - rc.update(true, 10, 2.0); - assert_eq!(rc.qscale(true), 2.0); + rc.start_gop(1, 0); + rc.update(PictureType::I, 10, 2.0); + assert_eq!(rc.qscale(PictureType::I), 2.0); } } diff --git a/crates/video/src/mpeg2/encode/tables.rs b/crates/video/src/mpeg2/encode/tables.rs index b79dd92ea..e23929450 100644 --- a/crates/video/src/mpeg2/encode/tables.rs +++ b/crates/video/src/mpeg2/encode/tables.rs @@ -4,7 +4,7 @@ use std::sync::OnceLock; use crate::mpeg2::vlc::{ - CBP, DC_CHROMA, DC_LUMA, DCT_EOB, DCT_ESCAPE, DCT_ONE, DCT_ZERO, DCT_ZERO_LONG, MBA, MBA_ESCAPE, MBTYPE_I, MBTYPE_P, MOTION, dct_table, + CBP, DC_CHROMA, DC_LUMA, DCT_EOB, DCT_ESCAPE, DCT_ONE, DCT_ZERO, DCT_ZERO_LONG, MBA, MBA_ESCAPE, MBTYPE_B, MBTYPE_I, MBTYPE_P, MOTION, dct_table, parse_code, }; @@ -38,9 +38,10 @@ pub(crate) struct EncTables { /// macroblock_address_increment 1..=33 (index 0 unused) and the escape. pub mba: [Code; 34], pub mba_escape: Code, - /// macroblock_type by flags: I pictures, P pictures (index: flags, 0..32). + /// macroblock_type by flags: I, P and B pictures (index: flags, 0..32). pub mbtype_i: [Option; 32], pub mbtype_p: [Option; 32], + pub mbtype_b: [Option; 32], /// coded_block_pattern_420 (the first six blocks). pub cbp: [Code; 64], /// motion_code -16..=16 at index code + 16. @@ -105,6 +106,7 @@ pub(crate) fn enc_tables() -> &'static EncTables { mba_escape, mbtype_i: mb_types(MBTYPE_I), mbtype_p: mb_types(MBTYPE_P), + mbtype_b: mb_types(MBTYPE_B), cbp: by_value(CBP, 0), motion: by_value(MOTION, 16), dc_luma: by_value(DC_LUMA, 0), @@ -185,11 +187,25 @@ mod tests { use super::*; use crate::mpeg2::bits::Bits; use crate::mpeg2::slice::wrap_vector; - use crate::mpeg2::vlc::{MB_FWD, MB_INTRA, MB_PATTERN, MB_QUANT, rl, tables}; + use crate::mpeg2::vlc::{MB_BWD, MB_FWD, MB_INTRA, MB_PATTERN, MB_QUANT, rl, tables}; - /// Macroblock types the encoder sends (frame pictures, frame prediction, no B pictures). + /// Macroblock types the encoder sends in P pictures (frame pictures, frame prediction). const ENC_MB_TYPES: [i16; 7] = [MB_INTRA, MB_INTRA | MB_QUANT, MB_FWD | MB_PATTERN, MB_PATTERN, MB_FWD, MB_QUANT | MB_FWD | MB_PATTERN, MB_QUANT | MB_PATTERN]; + /// And in B pictures: every combination of directions, with and without a residual. + const ENC_MB_TYPES_B: [i16; 11] = [ + MB_INTRA, + MB_INTRA | MB_QUANT, + MB_FWD, + MB_BWD, + MB_FWD | MB_BWD, + MB_FWD | MB_PATTERN, + MB_BWD | MB_PATTERN, + MB_FWD | MB_BWD | MB_PATTERN, + MB_QUANT | MB_FWD | MB_PATTERN, + MB_QUANT | MB_BWD | MB_PATTERN, + MB_QUANT | MB_FWD | MB_BWD | MB_PATTERN, + ]; #[test] fn every_code_decodes_to_its_value() { @@ -223,6 +239,9 @@ mod tests { } check(t.mbtype_p[f as usize].unwrap_or_default(), &d.mbtype[1], f); } + for f in ENC_MB_TYPES_B { + check(t.mbtype_b[f as usize].unwrap_or_default(), &d.mbtype[2], f); + } for (k, vlc) in [&d.dct_zero, &d.dct_one].into_iter().enumerate() { for run in 0..=MAX_RUN { for level in 1..=MAX_LEVEL { diff --git a/crates/video/src/mpeg2/encode/tests.rs b/crates/video/src/mpeg2/encode/tests.rs index 5352161b3..c8dfc1205 100644 --- a/crates/video/src/mpeg2/encode/tests.rs +++ b/crates/video/src/mpeg2/encode/tests.rs @@ -3,7 +3,7 @@ //! comparison is in `tests/mpeg2/encode.rs`. use super::*; -use crate::mpeg2::{Decoder, access_units, probe}; +use crate::mpeg2::{Decoder, probe}; /// A moving, textured test sequence: a gradient with a sharp pattern drifting by a fraction of a /// sample per picture, and a block moving across it. @@ -54,16 +54,21 @@ fn encode(cfg: EncoderConfig, frames: usize) -> (Vec, Vec, Vec

Vec { +/// Decode packets (each tagged with its presentation time); returns the pictures in display +/// order. +fn decode(packets: &[Packet]) -> Vec { let mut d = Decoder::new(); let mut out = Vec::new(); - for (i, r) in access_units(es).into_iter().enumerate() { - out.extend(d.decode(&es[r], i as i64).unwrap()); + for p in packets { + out.extend(d.decode(&p.data, p.pts).unwrap()); } out.extend(d.flush()); assert_eq!(d.errors(), 0, "decoder errors: {:?}", d.last_error()); @@ -71,8 +76,9 @@ fn decode(es: &[u8]) -> Vec { } fn assert_decodes_to_recon(name: &str, cfg: EncoderConfig, frames: usize) -> Vec { - let (es, recon, packets) = encode(cfg, frames); - let pics = decode(&es); + let (_, recon, packets) = encode(cfg, frames); + assert!(packets.windows(2).all(|w| w[0].dts < w[1].dts) && packets.iter().all(|p| p.dts <= p.pts), "{name}: decoding times"); + let pics = decode(&packets); assert_eq!(pics.len(), frames, "{name}: picture count"); for (i, (p, r)) in pics.iter().zip(&recon).enumerate() { assert_eq!(p.pts, i as i64, "{name}: display order"); @@ -101,6 +107,7 @@ fn intra_only_decodes_to_the_reconstruction() { fn long_p_gop_does_not_drift() { let mut cfg = EncoderConfig::new(96, 64, 25, 1); cfg.gop_size = 30; + cfg.b_pictures = 0; let packets = assert_decodes_to_recon("long gop", cfg, 30); assert_eq!(packets.iter().filter(|p| p.keyframe).count(), 1); assert!(packets[1..].iter().all(|p| p.picture_type == PictureType::P)); @@ -127,7 +134,131 @@ fn every_coding_option_decodes_to_the_reconstruction() { ("422 odd size", EncoderConfig { width: 70, height: 30, chroma: ChromaFormat::Yuv422, ..base.clone() }), ]; for (name, cfg) in cases { - assert_decodes_to_recon(name, cfg, 8); + assert_decodes_to_recon(name, cfg.clone(), 8); + assert_decodes_to_recon(name, EncoderConfig { b_pictures: 0, ..cfg }, 8); + } +} + +#[test] +fn b_pictures_decode_to_the_reconstruction() { + let base = EncoderConfig::new(96, 64, 25, 1); + for b in 1..=4 { + for closed in [true, false] { + for (gop, frames) in [(12, 30), (7, 17), (2, 6), (1, 4), (15, 16)] { + let name = format!("b {b} closed {closed} gop {gop} frames {frames}"); + let cfg = EncoderConfig { b_pictures: b, closed_gop: closed, gop_size: gop, ..base.clone() }; + let packets = assert_decodes_to_recon(&name, cfg, frames); + if gop > 1 { + assert!(packets.iter().any(|p| p.picture_type == PictureType::B), "{name}: no B pictures"); + } + } + } + } + let cases = [ + ("422", EncoderConfig { chroma: ChromaFormat::Yuv422, ..base.clone() }), + ("abr", EncoderConfig { rate: RateControl::Abr { kbps: 400 }, ..base.clone() }), + ("odd size", EncoderConfig { width: 50, height: 34, ..base.clone() }), + ("small range", EncoderConfig { search_range: 2, ..base.clone() }), + ("linear coarse", EncoderConfig { nonlinear_quant: false, rate: RateControl::Quantiser(25), ..base.clone() }), + ]; + for (name, cfg) in cases { + assert_decodes_to_recon(name, cfg, 14); + } +} + +#[test] +fn pictures_are_reordered_into_decoding_order() { + let cfg = EncoderConfig { gop_size: 12, b_pictures: 2, ..EncoderConfig::new(64, 48, 25, 1) }; + let (_, _, packets) = encode(cfg, 15); + let order: Vec<(PictureType, i64)> = packets.iter().map(|p| (p.picture_type, p.pts)).collect(); + use PictureType::{B, I, P}; + #[rustfmt::skip] + let want = [(I, 0), (P, 3), (B, 1), (B, 2), (P, 6), (B, 4), (B, 5), (P, 9), (B, 7), (B, 8), + // B10 and B11 lead the second GOP; at the end the last held picture becomes a P picture + (I, 12), (B, 10), (B, 11), (P, 14), (B, 13)]; + assert_eq!(order, want); + // decoding times: the presentation times in order, one interval early + let dts: Vec = packets.iter().map(|p| p.dts).collect(); + assert_eq!(dts, (-1..14).collect::>()); + // temporal_reference counts display order from each GOP's first picture + let pics = decode(&packets); + let tr: Vec = pics.iter().map(|p| p.temporal_reference).collect(); + assert_eq!(tr, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]); +} + +#[test] +fn a_closed_gop_decodes_on_its_own() { + for closed in [true, false] { + let cfg = EncoderConfig { gop_size: 6, b_pictures: 2, closed_gop: closed, ..EncoderConfig::new(64, 48, 25, 1) }; + let (_, recon, packets) = encode(cfg, 12); + // start decoding at the second GOP's I picture (display 6): B4 and B5 lead it + let at = packets.iter().position(|p| p.pts == 6).unwrap(); + assert!(packets[at].keyframe); + let mut d = Decoder::new(); + let mut pics = Vec::new(); + for p in &packets[at..] { + pics.extend(d.decode(&p.data, p.pts).unwrap()); + } + pics.extend(d.flush()); + let shown: Vec = pics.iter().map(|p| p.pts).collect(); + if closed { + assert_eq!(shown, (4..12).collect::>()); + for p in &pics { + assert_eq!(p.y, recon[p.pts as usize].y, "closed GOP: picture {}", p.pts); + } + } else { + // the leading B pictures need the previous GOP's last P picture + assert_eq!(shown, (6..12).collect::>()); + } + } +} + +#[test] +fn b_pictures_are_smaller_than_p_pictures() { + let cfg = EncoderConfig { gop_size: 12, b_pictures: 2, ..EncoderConfig::new(128, 96, 25, 1) }; + let (_, _, packets) = encode(cfg, 12); + let avg = |t: PictureType| { + let s: Vec = packets.iter().filter(|p| p.picture_type == t).map(|p| p.data.len()).collect(); + s.iter().sum::() as f64 / s.len().max(1) as f64 + }; + let (p, b) = (avg(PictureType::P), avg(PictureType::B)); + assert!(b * 1.5 < p, "P {p:.0} bytes, B {b:.0} bytes"); + // static content: the B pictures skip every macroblock but the first and last of a slice + let mut enc = Encoder::new(EncoderConfig { gop_size: 12, b_pictures: 2, ..EncoderConfig::new(128, 64, 25, 1) }).unwrap(); + let (y, cb, cr) = scene(128, 64, 64, 32, 0); + let f = YuvFrame { y: &y, cb: &cb, cr: &cr, y_stride: 128, c_stride: 64 }; + let mut packets = Vec::new(); + for t in 0..7 { + packets.extend(enc.encode(&f, t).unwrap()); + } + let i = packets[0].data.len(); + let bs: Vec<&Packet> = packets.iter().filter(|p| p.picture_type == PictureType::B).collect(); + assert_eq!(bs.len(), 4); + for p in bs { + // a picture header, four slice headers and eight edge macroblocks + assert!(p.data.len() < 64, "static B picture {} of {} bytes (I {i})", p.pts, p.data.len()); + } +} + +#[test] +fn presentation_times_must_step_evenly_with_b_pictures() { + let mut enc = Encoder::new(EncoderConfig::new(32, 32, 25, 1)).unwrap(); + let (y, c) = (vec![0u8; 32 * 32], vec![0u8; 16 * 16]); + let f = YuvFrame { y: &y, cb: &c, cr: &c, y_stride: 32, c_stride: 16 }; + // the I picture waits for the second time, which sets the step + assert!(enc.encode(&f, 10).unwrap().is_empty()); + let mut packets = enc.encode(&f, 12).unwrap(); + for bad in [12, 11, 15] { + assert!(matches!(enc.encode(&f, bad), Err(EncodeError::BadTimestamp(_))), "{bad}"); + } + packets.extend(enc.encode(&f, 14).unwrap()); + packets.extend(enc.flush()); + assert_eq!(packets.iter().map(|p| (p.pts, p.dts)).collect::>(), [(10, 8), (14, 10), (12, 12)]); + // without B pictures any increasing (or not) times pass through + let mut enc = Encoder::new(EncoderConfig { b_pictures: 0, ..EncoderConfig::new(32, 32, 25, 1) }).unwrap(); + for pts in [5, 3, 9] { + let p = enc.encode(&f, pts).unwrap().remove(0); + assert_eq!((p.pts, p.dts), (pts, pts)); } } @@ -161,7 +292,7 @@ fn quality_follows_the_quantiser() { #[test] fn p_pictures_are_smaller_and_static_content_skips() { - let cfg = EncoderConfig { gop_size: 8, ..EncoderConfig::new(128, 64, 25, 1) }; + let cfg = EncoderConfig { gop_size: 8, b_pictures: 0, ..EncoderConfig::new(128, 64, 25, 1) }; let mut enc = Encoder::new(cfg).unwrap(); let (y, cb, cr) = scene(128, 64, 64, 32, 0); let f = YuvFrame { y: &y, cb: &cb, cr: &cr, y_stride: 128, c_stride: 64 }; @@ -191,7 +322,7 @@ fn the_sequence_header_describes_the_stream() { let enc = Encoder::new(cfg).unwrap(); assert_eq!(enc.profile_and_level(), pli, "{name}"); let info = probe(enc.sequence_header()).unwrap(); - assert_eq!((info.width, info.height, info.mpeg2, info.progressive_sequence, info.low_delay), (w, h, true, true, true)); + assert_eq!((info.width, info.height, info.mpeg2, info.progressive_sequence, info.low_delay), (w, h, true, true, false)); assert_eq!(info.codec_name(), format!("MPEG-2 Video ({name})")); assert_eq!(aspect, AspectRatio::Square); assert_eq!(info.sar, (1, 1)); @@ -199,6 +330,9 @@ fn the_sequence_header_describes_the_stream() { let enc = Encoder::new(EncoderConfig { aspect: AspectRatio::Dar16x9, ..EncoderConfig::new(720, 576, 30000, 1001) }).unwrap(); let info = probe(enc.sequence_header()).unwrap(); assert_eq!((info.dar, info.frame_rate), (Some((16, 9)), Some((30000, 1001)))); + // without B pictures the stream is low delay + let enc = Encoder::new(EncoderConfig { b_pictures: 0, ..EncoderConfig::new(720, 576, 25, 1) }).unwrap(); + assert!(probe(enc.sequence_header()).unwrap().low_delay); } #[test] @@ -214,6 +348,8 @@ fn bad_configurations_are_refused() { EncoderConfig { fps_den: 0, ..ok.clone() }, EncoderConfig { gop_size: 0, ..ok.clone() }, EncoderConfig { gop_size: 1024, ..ok.clone() }, + EncoderConfig { b_pictures: 5, ..ok.clone() }, + EncoderConfig { gop_size: 1023, b_pictures: 2, ..ok.clone() }, EncoderConfig { rate: RateControl::Quantiser(0), ..ok.clone() }, EncoderConfig { rate: RateControl::Quantiser(32), ..ok.clone() }, EncoderConfig { rate: RateControl::Abr { kbps: 0 }, ..ok.clone() }, @@ -231,7 +367,7 @@ fn bad_configurations_are_refused() { #[test] fn short_or_inconsistent_planes_are_refused() { - let mut enc = Encoder::new(EncoderConfig::new(32, 32, 25, 1)).unwrap(); + let mut enc = Encoder::new(EncoderConfig { b_pictures: 0, ..EncoderConfig::new(32, 32, 25, 1) }).unwrap(); let (y, c) = (vec![0u8; 32 * 32], vec![0u8; 16 * 16]); let ok = YuvFrame { y: &y, cb: &c, cr: &c, y_stride: 32, c_stride: 16 }; assert!(enc.encode(&ok, 0).is_ok()); diff --git a/crates/video/src/mpeg2/mod.rs b/crates/video/src/mpeg2/mod.rs index ae2bb1682..4fd59dced 100644 --- a/crates/video/src/mpeg2/mod.rs +++ b/crates/video/src/mpeg2/mod.rs @@ -1,5 +1,5 @@ //! Clean-room MPEG-2 video (ITU-T H.262 | ISO/IEC 13818-2) and MPEG-1 video (ISO/IEC 11172-2) -//! decoder, and the start of an MPEG-2 encoder ([`encode`], feature `mpeg2-encode`: I and P +//! decoder, and the start of an MPEG-2 encoder ([`encode`], feature `mpeg2-encode`: I, P and B //! pictures). //! //! - Main Profile at Low/Main/High-1440/High Level, 4:2:2 Profile (XDCAM HD422, IMX / D-10), diff --git a/crates/video/tests/mpeg2/encode.rs b/crates/video/tests/mpeg2/encode.rs index 5cb2377dc..eb4ec947a 100644 --- a/crates/video/tests/mpeg2/encode.rs +++ b/crates/video/tests/mpeg2/encode.rs @@ -6,7 +6,7 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; -use craft_video::mpeg2::{ChromaFormat, Encoder, EncoderConfig, RateControl, ReconFrame, YuvFrame}; +use craft_video::mpeg2::{ChromaFormat, Encoder, EncoderConfig, Packet, RateControl, ReconFrame, YuvFrame}; use crate::common::{Stats, fixture_dir, pix_fmt_of}; use crate::oracle::{MAX_DIFFERING, MIN_PSNR, TOL_MAX}; @@ -39,24 +39,22 @@ fn sizes(cfg: &EncoderConfig) -> (usize, usize, usize) { (w * h, w / 2 * ch, w / 2) } -/// Encode `raw` frames; returns the elementary stream and the reconstruction. -fn encode(cfg: &EncoderConfig, raw: &[u8]) -> (Vec, Vec) { +/// Encode `raw` frames; returns the elementary stream, the reconstruction (display order) and +/// the packets (decoding order). +fn encode(cfg: &EncoderConfig, raw: &[u8]) -> (Vec, Vec, Vec) { let (ys, cs, cw) = sizes(cfg); let fsize = ys + 2 * cs; let mut enc = Encoder::new(cfg.clone()).expect("config"); enc.set_recon_capture(true); - let mut es = Vec::new(); + let mut packets = Vec::new(); for (i, f) in raw.chunks_exact(fsize).enumerate() { let frame = YuvFrame { y: &f[..ys], cb: &f[ys..ys + cs], cr: &f[ys + cs..], y_stride: cfg.width as usize, c_stride: cw }; - for p in enc.encode(&frame, i as i64).expect("encode") { - es.extend_from_slice(&p.data); - } - } - for p in enc.flush() { - es.extend_from_slice(&p.data); + packets.extend(enc.encode(&frame, i as i64).expect("encode")); } + packets.extend(enc.flush()); + let mut es: Vec = packets.iter().flat_map(|p| p.data.iter().copied()).collect(); es.extend_from_slice(enc.end_of_sequence()); - (es, enc.take_recon()) + (es, enc.take_recon(), packets) } /// ffmpeg's decode of `file`, failing on any decoding error. @@ -121,7 +119,7 @@ fn check(ff: &Path, c: &Case) -> (Vec, f64) { let raw = source(ff, c.cfg.width, c.cfg.height, c.frames, pix_fmt); let (ys, cs, _) = sizes(&c.cfg); let fsize = ys + 2 * cs; - let (es, recon) = encode(&c.cfg, &raw); + let (es, recon, packets) = encode(&c.cfg, &raw); let path = write(c.name, &es); let decoded = ffmpeg_decode(ff, &path, pix_fmt); assert_eq!(decoded.len(), c.frames * fsize, "{}: ffmpeg's frame count", c.name); @@ -139,15 +137,21 @@ fn check(ff: &Path, c: &Case) -> (Vec, f64) { assert!(quality >= c.min_psnr, "{}: luma PSNR {quality:.2} dB against the source", c.name); if let Some((stream, types)) = ffprobe(&path) { assert_eq!(stream, c.probe, "{}: ffprobe", c.name); - let gop = c.cfg.gop_size as usize; - let want: Vec<&str> = (0..c.frames).map(|i| if i % gop == 0 { "I" } else { "P" }).collect(); + // ffprobe lists the pictures in display order + let mut shown: Vec<&Packet> = packets.iter().collect(); + shown.sort_by_key(|p| p.pts); + let want: Vec = shown.iter().map(|p| format!("{:?}", p.picture_type)).collect(); assert_eq!(types, want, "{}: picture types", c.name); } + let count = |t: &str| packets.iter().filter(|p| format!("{:?}", p.picture_type) == t).count(); let kbps = es.len() as f64 * 8.0 * 25.0 / c.frames as f64 / 1000.0; println!( - "{}: {} frames, {kbps:.0} kbit/s, luma PSNR {quality:.2} dB, max diff to ffmpeg {}, {:.3}% differ", + "{}: {} frames ({} I, {} P, {} B), {kbps:.0} kbit/s, luma PSNR {quality:.2} dB, max diff to ffmpeg {}, {:.3}% differ", c.name, c.frames, + count("I"), + count("P"), + count("B"), all.max, 100.0 * all.diff as f64 / all.n as f64 ); @@ -160,6 +164,21 @@ fn ffmpeg_decodes_our_streams() { let cif = EncoderConfig { gop_size: 12, ..EncoderConfig::new(352, 288, 25, 1) }; let cases = [ Case { name: "cif_q4", cfg: cif.clone(), frames: 25, probe: "mpeg2video,Main,10,yuv420p", min_psnr: 36.0 }, + Case { name: "cif_ip", cfg: EncoderConfig { b_pictures: 0, ..cif.clone() }, frames: 25, probe: "mpeg2video,Main,10,yuv420p", min_psnr: 36.0 }, + Case { + name: "cif_open_gop", + cfg: EncoderConfig { closed_gop: false, gop_size: 15, ..cif.clone() }, + frames: 31, + probe: "mpeg2video,Main,10,yuv420p", + min_psnr: 35.0, + }, + Case { + name: "cif_b4", + cfg: EncoderConfig { b_pictures: 4, gop_size: 10, ..cif.clone() }, + frames: 23, + probe: "mpeg2video,Main,10,yuv420p", + min_psnr: 34.0, + }, Case { name: "cif_intra", cfg: EncoderConfig { gop_size: 1, rate: RateControl::Quantiser(8), ..cif.clone() }, @@ -191,10 +210,14 @@ fn ffmpeg_decodes_our_streams() { #[test] fn average_bitrate_reaches_its_target() { let Some(ff) = craft_media_testkit::ffmpeg_or_skip("mpeg2 encoder bitrate") else { return }; - for kbps in [800u32, 2500] { + for (kbps, b) in [(800u32, 2), (2500, 2), (800, 0)] { let c = Case { - name: if kbps == 800 { "cif_abr_800" } else { "cif_abr_2500" }, - cfg: EncoderConfig { rate: RateControl::Abr { kbps }, gop_size: 12, ..EncoderConfig::new(352, 288, 25, 1) }, + name: match (kbps, b) { + (800, 0) => "cif_abr_800_ip", + (800, _) => "cif_abr_800", + _ => "cif_abr_2500", + }, + cfg: EncoderConfig { rate: RateControl::Abr { kbps }, gop_size: 12, b_pictures: b, ..EncoderConfig::new(352, 288, 25, 1) }, frames: 72, probe: "mpeg2video,Main,10,yuv420p", min_psnr: 26.0, From b9c1dfa18f42757755f7cc7770255afc5cdefcab Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 08:50:02 -0500 Subject: [PATCH 04/29] feat(audio): migrate Ogg Vorbis encoding to craft-audio (PR #26) - Add vorbis-encode feature with dep:vorbis_rs = "0.5" - Implement encode_ogg() via VorbisEncoderBuilder matching original behavior: - Fixed stream serial 0x4C43_5246, VBR target bitrate (64-500 kbit/s) - 4096-frame blocks, panic catch with QualityVbr fallback (target_quality: 0.6) - Add EncodeFormat::Ogg variant (cfg-gated on vorbis-encode) - Update ENCODE_EXTENSIONS and encode_format_for() to include .ogg --- Cargo.lock | 34 +++++++++++++++++++ crates/audio/Cargo.toml | 3 ++ crates/audio/src/lib.rs | 20 ++++++++++- crates/audio/src/write/mod.rs | 56 ++++++++++++++++++++++++++---- crates/audio/src/write/vorbis.rs | 58 ++++++++++++++++++++++++++++++++ 5 files changed, 164 insertions(+), 7 deletions(-) create mode 100644 crates/audio/src/write/vorbis.rs diff --git a/Cargo.lock b/Cargo.lock index a1b1ccab3..1314c7745 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -222,6 +222,16 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "aotuv_lancer_vorbis_sys" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bc4fd1a61860d2f1198b60bedd30910eaffa978f1ee6214dfb24ac70d589225" +dependencies = [ + "cc", + "ogg_next_sys", +] + [[package]] name = "arbitrary" version = "1.5.0" @@ -1349,6 +1359,7 @@ dependencies = [ "serde_json", "symphonia", "thiserror 2.0.21", + "vorbis_rs", ] [[package]] @@ -6563,6 +6574,15 @@ dependencies = [ "phf 0.11.3", ] +[[package]] +name = "ogg_next_sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2d7a48e247c2bb07e633aefb65a38648ea58c7eedd4e4408a5861721ab049b" +dependencies = [ + "cc", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -10279,6 +10299,20 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "vorbis_rs" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49c5da94d280f7a27e8c937e9b73df2da3e23a2583f48471fd8fb4c72f9c1933" +dependencies = [ + "aotuv_lancer_vorbis_sys", + "errno", + "getrandom 0.4.3", + "ogg_next_sys", + "thiserror 2.0.21", + "tinyvec", +] + [[package]] name = "wait-timeout" version = "0.2.1" diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index 9b6200198..63e55d232 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -30,6 +30,8 @@ lossless-encode = ["pcm-encode", "flac-encode"] pcm-encode = [] # Encode FLAC files. flac-encode = ["craft-media/bitstream"] +# Encode Ogg Vorbis files (desktop only: builds the reference libvorbis from its C sources). +vorbis-encode = ["dep:vorbis_rs"] # Codec modules on their own, for packet-level use. aac-decode = ["craft-media/bitstream"] aac-encode = ["craft-media/bitstream"] @@ -59,6 +61,7 @@ thiserror = { workspace = true } serde = { workspace = true, optional = true } log = { version = "0.4", optional = true } rayon = { workspace = true, optional = true } +vorbis_rs = { version = "0.5", optional = true } # symphonia keeps its AAC decoder for AAC in CAF, a container craft-media does not read. symphonia = { version = "0.6.1", optional = true, default-features = false, features = ["mp3", "flac", "vorbis", "ogg", "wav", "pcm", "aiff", "isomp4", "aac", "alac", "adpcm", "caf", "mkv"] } diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index f8441a51d..04b813c30 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -54,8 +54,12 @@ mod read; mod track; #[cfg(any(feature = "decode-native", feature = "pcm-encode"))] mod wav; -#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] +#[cfg(any(feature = "pcm-encode", feature = "flac-encode", feature = "vorbis-encode"))] mod write; +#[cfg(feature = "vorbis-encode")] +pub use write::encode_ogg; +#[cfg(feature = "vorbis-encode")] +pub use write::OGG_BITRATES; /// 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 @@ -226,12 +230,20 @@ pub enum EncodeFormat { Aiff, /// FLAC (integer depths). Flac, + /// Ogg Vorbis (variable bitrate). Whole-buffer only: use [`encode_ogg`] directly. + #[cfg(feature = "vorbis-encode")] + Ogg, } impl EncodeFormat { /// Every format, in the order menus list them. Whether [`encode`] writes one depends on its /// feature. + #[cfg(not(feature = "vorbis-encode"))] pub const ALL: &[EncodeFormat] = &[EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac]; + /// Every format, in the order menus list them. Whether [`encode`] writes one depends on its + /// feature. + #[cfg(feature = "vorbis-encode")] + pub const ALL: &[EncodeFormat] = &[EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac, 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. @@ -240,6 +252,8 @@ impl EncodeFormat { EncodeFormat::Wav => "wav", EncodeFormat::Aiff => "aif", EncodeFormat::Flac => "flac", + #[cfg(feature = "vorbis-encode")] + EncodeFormat::Ogg => "ogg", } } @@ -249,6 +263,8 @@ impl EncodeFormat { EncodeFormat::Wav => "audio/wav", EncodeFormat::Aiff => "audio/aiff", EncodeFormat::Flac => "audio/flac", + #[cfg(feature = "vorbis-encode")] + EncodeFormat::Ogg => "audio/ogg", } } @@ -258,6 +274,8 @@ impl EncodeFormat { EncodeFormat::Wav => Container::Wav, EncodeFormat::Aiff => Container::Aiff, EncodeFormat::Flac => Container::Flac, + #[cfg(feature = "vorbis-encode")] + EncodeFormat::Ogg => Container::Ogg, } } } diff --git a/crates/audio/src/write/mod.rs b/crates/audio/src/write/mod.rs index 2355a7b05..4a82fdcbe 100644 --- a/crates/audio/src/write/mod.rs +++ b/crates/audio/src/write/mod.rs @@ -10,6 +10,10 @@ mod pcm; mod raw; #[cfg(feature = "pcm-encode")] mod wav; +#[cfg(feature = "vorbis-encode")] +pub(crate) mod vorbis; +#[cfg(feature = "vorbis-encode")] +pub use vorbis::OGG_BITRATES; use crate::{AudioBuffer, AudioError, ChannelLayout, EncodeFormat, EncodeOptions, Result}; #[cfg(feature = "pcm-encode")] @@ -47,20 +51,54 @@ pub fn encode(buf: &AudioBuffer, opts: &EncodeOptions) -> Result> { /// Refuses a format whose writer is not built, before anything else about the request is checked. fn check_built(format: EncodeFormat) -> Result<()> { - let (built, feature) = - if format == EncodeFormat::Flac { (cfg!(feature = "flac-encode"), "flac-encode") } else { (cfg!(feature = "pcm-encode"), "pcm-encode") }; + let (built, feature) = match format { + EncodeFormat::Flac => (cfg!(feature = "flac-encode"), "flac-encode"), + #[cfg(feature = "vorbis-encode")] + EncodeFormat::Ogg => (cfg!(feature = "vorbis-encode"), "vorbis-encode"), + _ => (cfg!(feature = "pcm-encode"), "pcm-encode"), + }; if built { Ok(()) } else { Err(AudioError::Unsupported(format!("writing {format:?} needs the `{feature}` feature"))) } } /// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "pcm-encode", feature = "flac-encode"))] +#[cfg(all(feature = "pcm-encode", feature = "flac-encode", feature = "vorbis-encode"))] +pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc", "flac", "ogg"]; +/// The file extensions of the formats [`encode`] can write (those whose features are on). +#[cfg(all(feature = "pcm-encode", feature = "flac-encode", not(feature = "vorbis-encode")))] pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc", "flac"]; /// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "pcm-encode", not(feature = "flac-encode")))] +#[cfg(all(feature = "pcm-encode", feature = "vorbis-encode", not(feature = "flac-encode")))] +pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc", "ogg"]; +/// The file extensions of the formats [`encode`] can write (those whose features are on). +#[cfg(all(feature = "pcm-encode", not(feature = "flac-encode"), not(feature = "vorbis-encode")))] pub const ENCODE_EXTENSIONS: &[&str] = &["wav", "wave", "bwf", "aif", "aiff", "aifc"]; /// The file extensions of the formats [`encode`] can write (those whose features are on). -#[cfg(all(feature = "flac-encode", not(feature = "pcm-encode")))] +#[cfg(all(feature = "flac-encode", feature = "vorbis-encode", not(feature = "pcm-encode")))] +pub const ENCODE_EXTENSIONS: &[&str] = &["flac", "ogg"]; +/// The file extensions of the formats [`encode`] can write (those whose features are on). +#[cfg(all(feature = "flac-encode", not(feature = "pcm-encode"), not(feature = "vorbis-encode")))] pub const ENCODE_EXTENSIONS: &[&str] = &["flac"]; +/// The file extensions of the formats [`encode`] can write (those whose features are on). +#[cfg(all(feature = "vorbis-encode", not(feature = "pcm-encode"), not(feature = "flac-encode")))] +pub const ENCODE_EXTENSIONS: &[&str] = &["ogg"]; + +/// Encode `buf` as an Ogg Vorbis stream at about `kbps` (variable, as Vorbis is): stereo, or mono +/// for a mono buffer. A bitrate the encoder has no setup for at that rate and channel count falls +/// back to a quality that suits it. Whole-buffer only: no streaming encoder is provided for Vorbis. +/// +/// ``` +/// # #[cfg(feature = "vorbis-encode")] { +/// use craft_audio::{AudioBuffer, encode_ogg}; +/// +/// let buf = AudioBuffer::new(48_000, 2, 48_000); // one second of stereo silence +/// let ogg = encode_ogg(&buf, 192).unwrap(); +/// assert_eq!(&ogg[..4], b"OggS"); +/// # } +/// ``` +#[cfg(feature = "vorbis-encode")] +pub fn encode_ogg(buf: &AudioBuffer, kbps: u32) -> Result> { + vorbis::encode(buf, kbps) +} /// The format [`encode`] writes for a file extension or format name (`wav`, `aiff`, `flac`, /// case-insensitive, with or without the dot), or `None` when it cannot write it. `aifc` is @@ -82,6 +120,8 @@ pub fn encode_format_for(name: &str) -> Option { "aif" | "aiff" | "aifc" => Some(EncodeFormat::Aiff), #[cfg(feature = "flac-encode")] "flac" => Some(EncodeFormat::Flac), + #[cfg(feature = "vorbis-encode")] + "ogg" => Some(EncodeFormat::Ogg), _ => None, } } @@ -123,11 +163,13 @@ fn failed_write_error() -> AudioError { /// assert_eq!(&flac[..4], b"fLaC"); /// # } /// ``` +#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] pub struct FileEncoder { writer: Writer, channels: usize, } +#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] impl std::fmt::Debug for FileEncoder { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let format = match &self.writer { @@ -142,6 +184,7 @@ impl std::fmt::Debug for FileEncoder { } } +#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] enum Writer { #[cfg(feature = "pcm-encode")] Wav(wav::Writer), @@ -151,6 +194,7 @@ enum Writer { Flac(flac::Writer), } +#[cfg(any(feature = "pcm-encode", feature = "flac-encode"))] impl FileEncoder { /// Starts a file of `channels` channels at `sample_rate` at the current position of `out`. /// `opts.layout`, when set, must have `channels` channels. @@ -252,7 +296,7 @@ pub fn wav_float_header(sample_rate: u32, channels: u16, frames: u64) -> Result< // With only one of `pcm-encode` and `flac-encode`, the other's formats are refused, not silently // written as something else. -#[cfg(all(test, not(all(feature = "pcm-encode", feature = "flac-encode"))))] +#[cfg(all(test, any(feature = "pcm-encode", feature = "flac-encode"), not(all(feature = "pcm-encode", feature = "flac-encode"))))] mod tests { use super::*; diff --git a/crates/audio/src/write/vorbis.rs b/crates/audio/src/write/vorbis.rs new file mode 100644 index 000000000..6b33e8981 --- /dev/null +++ b/crates/audio/src/write/vorbis.rs @@ -0,0 +1,58 @@ +//! Ogg Vorbis encoding (RFC 5334): the reference libvorbis encoder, built from its C sources by +//! `vorbis_rs`. Stereo or mono, variable bitrate at the requested kbit/s (64–500), with a +//! fallback to a quality preset when the bitrate is not supported for the rate and channel count. + +use crate::{AudioBuffer, AudioError, Result}; + +/// The bitrates an Ogg Vorbis export is asked for, in kbit/s. +pub const OGG_BITRATES: (u32, u32) = (64, 500); + +/// The block size the encoder processes at a time: small enough that memory stays bounded +/// regardless of file length. +const BLOCK: usize = 4096; + +fn encode_error(e: vorbis_rs::VorbisError) -> AudioError { + AudioError::Encode(e.to_string()) +} + +/// Encode `buf` as an Ogg Vorbis stream at about `kbps` (variable, as Vorbis is): stereo, or mono +/// for a mono buffer. A bitrate the encoder has no setup for at that rate and channel count falls +/// back to a quality that suits it. `Err` says why there is nothing. +#[cfg(not(target_arch = "wasm32"))] +pub fn encode(buf: &AudioBuffer, kbps: u32) -> Result> { + use std::num::{NonZeroU8, NonZeroU32}; + + let channels = buf.channels.len().clamp(1, 2); + let (Some(rate), Some(count)) = (NonZeroU32::new(buf.sample_rate), NonZeroU8::new(channels as u8)) else { + return Err(AudioError::Encode("there is no audio to encode".into())); + }; + let target = NonZeroU32::new(kbps.clamp(OGG_BITRATES.0, OGG_BITRATES.1) * 1000).unwrap_or(NonZeroU32::MIN); + + let encode = |strategy: vorbis_rs::VorbisBitrateManagementStrategy| -> Result> { + let mut encoder = vorbis_rs::VorbisEncoderBuilder::new_with_serial(rate, count, Vec::new(), 0x4C43_5246) + .bitrate_management_strategy(strategy) + .build() + .map_err(encode_error)?; + let frames = buf.frames(); + let mut at = 0usize; + while at < frames { + let end = (at + BLOCK).min(frames); + let block: Vec<&[f32]> = buf.channels.iter().take(channels).map(|side| side.get(at..end).unwrap_or_default()).collect(); + encoder.encode_audio_block(&block).map_err(encode_error)?; + at = end; + } + encoder.finish().map_err(encode_error) + }; + + // The library is C: whatever it does wrong ends here as an error, never as a crash of the app. + let attempt = |strategy| std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| encode(strategy))).unwrap_or_else(|_| Err(AudioError::Encode("the Vorbis encoder failed".into()))); + match attempt(vorbis_rs::VorbisBitrateManagementStrategy::Vbr { target_bitrate: target }) { + Ok(bytes) => Ok(bytes), + Err(_) => attempt(vorbis_rs::VorbisBitrateManagementStrategy::QualityVbr { target_quality: 0.6 }).map_err(|e| AudioError::Encode(format!("the Vorbis encoder refused the audio: {e}"))), + } +} + +#[cfg(target_arch = "wasm32")] +pub fn encode(_buf: &AudioBuffer, _kbps: u32) -> Result> { + Err("Ogg Vorbis files are exported by the desktop app".into()) +} From ebece3ff5c0d5bdc382dd3e03d78ab17f538d6f4 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 10:05:44 -0500 Subject: [PATCH 05/29] ci: lock the actions main added while this branch was open Merging main in brought cms.yml, menu.yml and the wasm job back, and their action refs had no lockfile entries, so `gh actions-lock --verify` failed. Regenerated with `gh actions-lock`; the four refs it normalized resolve to the commits already in use. --- .github/workflows/actions.lock | 15 +++++++++++++++ .github/workflows/atomic.yml | 2 +- .github/workflows/cms.yml | 2 +- .github/workflows/menu.yml | 6 +++--- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.github/workflows/actions.lock b/.github/workflows/actions.lock index c2e897943..acbdeff60 100644 --- a/.github/workflows/actions.lock +++ b/.github/workflows/actions.lock @@ -8,7 +8,9 @@ workflows: - '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': @@ -17,6 +19,9 @@ workflows: - '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' @@ -71,6 +76,10 @@ workflows: - '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' @@ -125,12 +134,18 @@ workflows: - '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' diff --git a/.github/workflows/atomic.yml b/.github/workflows/atomic.yml index 9caf6c17c..f72a64f1e 100644 --- a/.github/workflows/atomic.yml +++ b/.github/workflows/atomic.yml @@ -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 diff --git a/.github/workflows/cms.yml b/.github/workflows/cms.yml index a15a48c9d..74ce0815f 100644 --- a/.github/workflows/cms.yml +++ b/.github/workflows/cms.yml @@ -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 diff --git a/.github/workflows/menu.yml b/.github/workflows/menu.yml index 6d0e082cb..8d1058731 100644 --- a/.github/workflows/menu.yml +++ b/.github/workflows/menu.yml @@ -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 From bcd09376d0e14a8231827a2d580682f3a9ae7885 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 08:27:34 -0500 Subject: [PATCH 06/29] sonar: ignore the weak hashes the encrypted formats require S4790 reports the MD5 and SHA-1 in the xls, pdf and odf cryptography modules. Each hash is the one the format specifies: a workbook's FilePass verifier, a PDF revision 2-4 file key, and an OpenDocument start key or file checksum. A stronger hash derives a different value, so the files would no longer open. The three files are excluded with the reason recorded, as the ICC profile ID already is. --- sonar-project.properties | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/sonar-project.properties b/sonar-project.properties index 516e4d5cc..f30f3ae5f 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -15,9 +15,24 @@ sonar.cpd.exclusions=**/*tables.rs # S4790 (weak hash): the ICC specification defines the profile ID as the MD5 of the profile, so # no other algorithm produces an ID other software accepts. It identifies, it does not protect. -sonar.issue.ignore.multicriteria=icc_profile_id,gha-lockfile +# +# The encrypted packages below are the same case: each format derives its keys from MD5 or SHA-1, +# so substituting a stronger hash stops the files the module reads and writes from opening. +sonar.issue.ignore.multicriteria=icc_profile_id,xls_filepass_hash,pdf_standard_security_hash,odf_package_hash,gha-lockfile sonar.issue.ignore.multicriteria.icc_profile_id.ruleKey=rust:S4790 sonar.issue.ignore.multicriteria.icc_profile_id.resourceKey=crates/cms/src/write.rs +# xls: [MS-OFFCRYPTO] 2.3.6.1-2.3.6.2, where RC4 1.1 takes its key and verifier from MD5, and +# 2.3.5.1-2.3.5.2, where RC4 CryptoAPI takes them from SHA-1. +sonar.issue.ignore.multicriteria.xls_filepass_hash.ruleKey=rust:S4790 +sonar.issue.ignore.multicriteria.xls_filepass_hash.resourceKey=crates/xls/src/crypt.rs +# pdf: ISO 32000-1 7.6.3.3, Algorithms 2-7. Revisions 2-4 derive the file key and the /O and /U +# values from MD5; revisions 5 and 6 use SHA-256 and SHA-512 (Algorithm 2.B). +sonar.issue.ignore.multicriteria.pdf_standard_security_hash.ruleKey=rust:S4790 +sonar.issue.ignore.multicriteria.pdf_standard_security_hash.resourceKey=crates/pdf/src/crypt.rs +# odf: OpenDocument 4.16.6 and 4.16.3, a start key and a file checksum made with SHA-1, each used +# only where the package's manifest names it. +sonar.issue.ignore.multicriteria.odf_package_hash.ruleKey=rust:S4790 +sonar.issue.ignore.multicriteria.odf_package_hash.resourceKey=crates/odf/src/crypt.rs # githubactions:S7637 asks for every action to be pinned to a commit SHA. A workflow covered by # .github/workflows/actions.lock runs the commit the lockfile records, so its tag refs are pinned From 630c57078931ceb58f9d3d61281872261d667785 Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 14 Nov 2026 10:16:33 -0500 Subject: [PATCH 07/29] fix: update tests for vorbis-encode feature (ogg support) --- crates/audio/src/lib.rs | 4 ++-- crates/audio/src/write/mod.rs | 4 ++-- crates/audio/src/write/vorbis.rs | 8 ++++++-- crates/audio/tests/audio/decode_extensions.rs | 2 +- crates/audio/tests/audio/encode_format.rs | 2 +- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 04b813c30..3c578242f 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -57,9 +57,9 @@ mod wav; #[cfg(any(feature = "pcm-encode", feature = "flac-encode", feature = "vorbis-encode"))] mod write; #[cfg(feature = "vorbis-encode")] -pub use write::encode_ogg; -#[cfg(feature = "vorbis-encode")] pub use write::OGG_BITRATES; +#[cfg(feature = "vorbis-encode")] +pub use write::encode_ogg; /// 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 diff --git a/crates/audio/src/write/mod.rs b/crates/audio/src/write/mod.rs index 4a82fdcbe..d95b8fe0f 100644 --- a/crates/audio/src/write/mod.rs +++ b/crates/audio/src/write/mod.rs @@ -8,10 +8,10 @@ pub(crate) mod flac; mod pcm; #[cfg(feature = "pcm-encode")] mod raw; -#[cfg(feature = "pcm-encode")] -mod wav; #[cfg(feature = "vorbis-encode")] pub(crate) mod vorbis; +#[cfg(feature = "pcm-encode")] +mod wav; #[cfg(feature = "vorbis-encode")] pub use vorbis::OGG_BITRATES; diff --git a/crates/audio/src/write/vorbis.rs b/crates/audio/src/write/vorbis.rs index 6b33e8981..5466a5b92 100644 --- a/crates/audio/src/write/vorbis.rs +++ b/crates/audio/src/write/vorbis.rs @@ -45,10 +45,14 @@ pub fn encode(buf: &AudioBuffer, kbps: u32) -> Result> { }; // The library is C: whatever it does wrong ends here as an error, never as a crash of the app. - let attempt = |strategy| std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| encode(strategy))).unwrap_or_else(|_| Err(AudioError::Encode("the Vorbis encoder failed".into()))); + let attempt = |strategy| { + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| encode(strategy))) + .unwrap_or_else(|_| Err(AudioError::Encode("the Vorbis encoder failed".into()))) + }; match attempt(vorbis_rs::VorbisBitrateManagementStrategy::Vbr { target_bitrate: target }) { Ok(bytes) => Ok(bytes), - Err(_) => attempt(vorbis_rs::VorbisBitrateManagementStrategy::QualityVbr { target_quality: 0.6 }).map_err(|e| AudioError::Encode(format!("the Vorbis encoder refused the audio: {e}"))), + Err(_) => attempt(vorbis_rs::VorbisBitrateManagementStrategy::QualityVbr { target_quality: 0.6 }) + .map_err(|e| AudioError::Encode(format!("the Vorbis encoder refused the audio: {e}"))), } } diff --git a/crates/audio/tests/audio/decode_extensions.rs b/crates/audio/tests/audio/decode_extensions.rs index e8fe22d58..c7c8615e4 100644 --- a/crates/audio/tests/audio/decode_extensions.rs +++ b/crates/audio/tests/audio/decode_extensions.rs @@ -67,6 +67,6 @@ fn every_encode_format_has_an_extension_it_is_written_and_read_under() { assert!(DECODE_EXTENSIONS.contains(&ext), "{ext}: what is written reads back"); assert!(format.mime_type().starts_with("audio/"), "{format:?}"); } - assert_eq!(EncodeFormat::ALL.iter().map(|f| f.extension()).collect::>(), ["wav", "aif", "flac"]); + assert_eq!(EncodeFormat::ALL.iter().map(|f| f.extension()).collect::>(), ["wav", "aif", "flac", "ogg"]); assert_eq!(EncodeFormat::Aiff.mime_type(), "audio/aiff"); } diff --git a/crates/audio/tests/audio/encode_format.rs b/crates/audio/tests/audio/encode_format.rs index 30b992685..098dc025a 100644 --- a/crates/audio/tests/audio/encode_format.rs +++ b/crates/audio/tests/audio/encode_format.rs @@ -14,7 +14,7 @@ fn only_formats_the_encoder_writes_are_named() { for name in craft_audio::ENCODE_EXTENSIONS { assert!(encode_format_for(name).is_some(), "{name}"); } - for other in ["mp3", "ogg", "m4a", "xyz", ""] { + for other in ["mp3", "m4a", "xyz", ""] { assert_eq!(encode_format_for(other), None, "{other}"); } } From bac1a19d933472ea2a07ad7ff883f8e135a52681 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 17:10:03 +0000 Subject: [PATCH 08/29] fix(xml): harden DTD validation bounds and parsing --- crates/xml/src/dtd.rs | 421 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 373 insertions(+), 48 deletions(-) diff --git a/crates/xml/src/dtd.rs b/crates/xml/src/dtd.rs index 6bbca3554..9dc0790c5 100644 --- a/crates/xml/src/dtd.rs +++ b/crates/xml/src/dtd.rs @@ -3,11 +3,19 @@ //! Document Type Definitions: element declarations (content models) and required attributes, and //! validation of an XML document against them (Structure ▸ Validate). -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; /// How much text parameter entities may add during expansion (a pass count is not an output /// bound: each pass replaces whole strings, so references can inflate multiplicatively). const MAX_EXPANDED_BYTES: usize = 1024 * 1024; +const MAX_DTD_BYTES: usize = 1024 * 1024; +const MAX_VALIDATION_XML_BYTES: usize = 16 * 1024 * 1024; +const MAX_VALIDATION_NODES: usize = 100_000; +const MAX_VALIDATION_DEPTH: usize = 256; +const MAX_VALIDATION_ATTRIBUTES: usize = 1_000_000; +const MAX_MATCH_WORK: usize = 20_000_000; +const MAX_MATCH_POSITIONS: usize = 5_000_000; +const MAX_MATCH_STATES: usize = 100_000; /// A content particle: a name, a sequence, a choice, or one of those repeated (`?`, `*`, `+`). #[derive(Clone, Debug, PartialEq)] @@ -54,32 +62,38 @@ impl Dtd { } /// Markup declarations (``) with comments removed and quoted strings kept whole. -fn declarations(src: &str) -> Vec { +fn declarations(src: &str) -> Result, String> { let mut out = Vec::new(); let mut rest = src; while let Some(i) = rest.find("").map_or("", |j| &rest[j + 3..]); + let Some(j) = rest.find("-->") else { + return Err("unterminated DTD comment".into()); + }; + rest = &rest[j + 3..]; continue; } let mut quote = None; - let mut end = rest.len(); + let mut end = None; for (k, c) in rest.char_indices().skip(2) { match (quote, c) { (Some(q), c) if c == q => quote = None, (None, '"' | '\'') => quote = Some(c), (None, '>') => { - end = k; + end = Some(k); break; } _ => {} } } + let Some(end) = end else { + return Err("unterminated DTD declaration".into()); + }; out.push(rest[2..end].to_string()); - rest = rest.get(end + 1..).unwrap_or(""); + rest = &rest[end + 1..]; } - out + Ok(out) } fn tokens(s: &str) -> Vec { @@ -159,8 +173,48 @@ fn parse_model(s: &str) -> Result { _ => {} } if s.contains("#PCDATA") { - let t = tokens(&s.replace("#PCDATA", " #PCDATA ")); - return Ok(Model::Mixed(t.into_iter().filter(|x| !"()|*".contains(x.as_str()) && x != "#PCDATA").collect())); + let t = tokens(s); + if t.first().map(String::as_str) != Some("(") || t.get(1).map(String::as_str) != Some("#PCDATA") { + return Err("malformed mixed content model".into()); + } + let mut i = 2; + let mut allowed = Vec::new(); + if t.get(i).map(String::as_str) == Some(")") { + i += 1; + if t.get(i).map(String::as_str) == Some("*") { + i += 1; + } + if i != t.len() { + return Err("unexpected token after mixed content model".into()); + } + return Ok(Model::Mixed(allowed)); + } + loop { + if t.get(i).map(String::as_str) != Some("|") { + return Err("expected `|` in mixed content model".into()); + } + i += 1; + let Some(name) = t.get(i).filter(|name| !"(),|?*+".contains(name.as_str())) else { + return Err("expected element name in mixed content model".into()); + }; + if allowed.contains(name) { + return Err(format!("duplicate `{name}` in mixed content model")); + } + allowed.push(name.clone()); + i += 1; + match t.get(i).map(String::as_str) { + Some("|") => continue, + Some(")") => { + i += 1; + break; + } + _ => return Err("expected `|` or `)` in mixed content model".into()), + } + } + if t.get(i).map(String::as_str) != Some("*") || i + 1 != t.len() { + return Err("mixed content with element names must end in `*`".into()); + } + return Ok(Model::Mixed(allowed)); } let t = tokens(s); let mut i = 0; @@ -196,7 +250,33 @@ fn expand_parameter_entities(src: &str, ents: &[(String, String)]) -> Result") else { + return Err("unterminated DTD comment".into()); + }; + append(&mut out, &comment_tail[..end + 3])?; + rest = &comment_tail[end + 3..]; + continue; + } let (prefix, reference) = rest.split_at(i); append(&mut out, prefix)?; // `find` points to an ASCII `%`, so both split positions are UTF-8 boundaries. @@ -231,9 +311,12 @@ fn expand_parameter_entities(src: &str, ents: &[(String, String)]) -> Result Result { + if src.len() > MAX_DTD_BYTES { + return Err(format!("DTD exceeds the {MAX_DTD_BYTES} byte parsing limit")); + } // Parameter entities first, then expand references and read the declarations again. let mut ents: Vec<(String, String)> = Vec::new(); - for d in declarations(src) { + for d in declarations(src)? { if let Some(rest) = d.strip_prefix("ENTITY") { let rest = rest.trim_start(); if let Some(rest) = rest.strip_prefix('%') { @@ -249,7 +332,7 @@ pub fn parse(src: &str) -> Result { } let text = expand_parameter_entities(src, &ents)?; let mut dtd = Dtd::default(); - for d in declarations(&text) { + for d in declarations(&text)? { if let Some(rest) = d.strip_prefix("ELEMENT") { let rest = rest.trim_start(); let name: String = rest.chars().take_while(|c| !c.is_whitespace()).collect(); @@ -293,13 +376,31 @@ pub fn parse(src: &str) -> Result { } let Some((el, attrs)) = words.split_first() else { continue }; let mut k = 0; - while k + 2 < attrs.len() { + while k < attrs.len() { let name = &attrs[k]; - let default = attrs.get(k + 2).map(String::as_str).unwrap_or(""); + let Some(ty) = attrs.get(k + 1) else { + return Err(format!("incomplete ATTLIST for `{el}`")); + }; + k += 2; + // NOTATION is a two-token type: the keyword followed by its enumeration. + if ty == "NOTATION" { + if attrs.get(k).is_none_or(|group| !group.starts_with('(') || !group.ends_with(')')) { + return Err(format!("malformed NOTATION type for `{name}`")); + } + k += 1; + } + let Some(default) = attrs.get(k).map(String::as_str) else { + return Err(format!("missing default declaration for `{name}`")); + }; + k += 1; if default == "#REQUIRED" { dtd.required.entry(el.clone()).or_default().push(name.clone()); + } else if default == "#FIXED" { + if k >= attrs.len() { + return Err(format!("missing #FIXED value for `{name}`")); + } + k += 1; } - k += if default == "#FIXED" { 4 } else { 3 }; } } } @@ -313,18 +414,61 @@ pub fn parse(src: &str) -> Result { /// address of its node, which lives as long as the model) from a position. type EndsMemo = HashMap<(usize, usize), Vec>; +#[derive(Default)] +struct MatchBudget { + work: usize, + positions: usize, + states: usize, + exhausted: bool, + reported: bool, +} + +impl MatchBudget { + fn spend(&mut self, work: usize, positions: usize) -> Result<(), &'static str> { + if self.exhausted { + return Err("content model matching exceeded its resource budget"); + } + self.work = self.work.saturating_add(work); + self.positions = self.positions.saturating_add(positions); + if self.work > MAX_MATCH_WORK { + self.exhausted = true; + return Err("content model matching exceeded its work limit"); + } + if self.positions > MAX_MATCH_POSITIONS { + self.exhausted = true; + return Err("content model matching exceeded its memory limit"); + } + Ok(()) + } + + fn retain_state(&mut self) -> Result<(), &'static str> { + self.states = self.states.saturating_add(1); + if self.states > MAX_MATCH_STATES { + self.exhausted = true; + return Err("content model matching exceeded its state limit"); + } + Ok(()) + } +} + /// End positions after `cp` matches a prefix of `seq[i..]`. /// /// The positions of each group are worked out once per start position (`memo`): nested repetitions /// such as `((a*)*)*` ask the inner group for the same positions again and again, which is /// exponential in their depth without it. -fn ends(cp: &Cp, seq: &[&str], i: usize, memo: &mut EndsMemo) -> Vec { +fn ends(cp: &Cp, seq: &[&str], i: usize, memo: &mut EndsMemo, budget: &mut MatchBudget) -> Result, &'static str> { + budget.spend(1, 0)?; if let Cp::Name(n) = cp { - return if seq.get(i) == Some(&n.as_str()) { vec![i + 1] } else { vec![] }; + if seq.get(i) == Some(&n.as_str()) { + budget.spend(0, 1)?; + return Ok(vec![i + 1]); + } + return Ok(vec![]); } let key = (std::ptr::from_ref(cp) as usize, i); if let Some(done) = memo.get(&key) { - return done.clone(); + budget.spend(0, done.len())?; + return Ok(done.clone()); } let mut out: Vec = match cp { Cp::Name(_) => vec![], @@ -334,7 +478,9 @@ fn ends(cp: &Cp, seq: &[&str], i: usize, memo: &mut EndsMemo) -> Vec { for c in items { let mut next = Vec::new(); for p in at { - next.extend(ends(c, seq, p, memo)); + let found = ends(c, seq, p, memo, budget)?; + budget.spend(found.len(), found.len())?; + next.extend(found); } next.sort_unstable(); next.dedup(); @@ -345,22 +491,41 @@ fn ends(cp: &Cp, seq: &[&str], i: usize, memo: &mut EndsMemo) -> Vec { Cp::Choice(items) => { let mut all = Vec::new(); for c in items { - all.extend(ends(c, seq, i, memo)); + let found = ends(c, seq, i, memo, budget)?; + budget.spend(found.len(), found.len())?; + all.extend(found); } all } Cp::Rep(c, r) => { let mut all = if *r == '+' { vec![] } else { vec![i] }; - let mut frontier = ends(c, seq, i, memo); + let mut seen: HashSet = all.iter().copied().collect(); + let mut frontier = ends(c, seq, i, memo, budget)?; + budget.spend(frontier.len(), frontier.len())?; if *r == '?' { all.extend(frontier); } else { while !frontier.is_empty() { - let new: Vec = frontier.iter().copied().filter(|p| !all.contains(p)).collect(); + let mut new = Vec::new(); + for p in frontier.iter().copied() { + budget.spend(1, 0)?; + if seen.insert(p) { + budget.spend(0, 1)?; + new.push(p); + } + } + budget.spend(new.len(), new.len())?; all.extend(&new); let mut next = Vec::new(); + let mut next_seen = HashSet::new(); for p in new.into_iter().filter(|&p| p > i) { - next.extend(ends(c, seq, p, memo).into_iter().filter(|p| !all.contains(p))); + for found in ends(c, seq, p, memo, budget)? { + budget.spend(1, 0)?; + if !seen.contains(&found) && next_seen.insert(found) { + budget.spend(0, 1)?; + next.push(found); + } + } } // Two paths to one position are one position: kept apart they multiply per round. next.sort_unstable(); @@ -373,8 +538,10 @@ fn ends(cp: &Cp, seq: &[&str], i: usize, memo: &mut EndsMemo) -> Vec { }; out.sort_unstable(); out.dedup(); + budget.spend(out.len(), out.len())?; + budget.retain_state()?; memo.insert(key, out.clone()); - out + Ok(out) } fn show(cp: &Cp) -> String { @@ -392,60 +559,140 @@ struct Node { attrs: Vec, children: Vec, text: bool, + content: bool, } fn read_tree(xml: &str) -> Result { use quick_xml::events::Event; + if xml.len() > MAX_VALIDATION_XML_BYTES { + return Err(format!("XML exceeds the {} byte validation limit", MAX_VALIDATION_XML_BYTES)); + } let mut r = quick_xml::Reader::from_str(xml); let mut stack: Vec = Vec::new(); - let node = |e: &quick_xml::events::BytesStart| Node { - name: String::from_utf8_lossy(e.name().as_ref()).to_string(), - attrs: e.attributes().flatten().map(|a| String::from_utf8_lossy(a.key.as_ref()).to_string()).collect(), - children: vec![], - text: false, + let mut root = None; + let mut nodes = 0usize; + let mut attributes = 0usize; + let node = |e: &quick_xml::events::BytesStart| -> Result { + let mut attrs = Vec::new(); + for attr in e.attributes().with_checks(true) { + let attr = attr.map_err(|err| format!("invalid attribute: {err}"))?; + attrs.push(String::from_utf8_lossy(attr.key.as_ref()).to_string()); + } + Ok(Node { name: String::from_utf8_lossy(e.name().as_ref()).to_string(), attrs, children: vec![], text: false, content: false }) }; loop { match r.read_event().map_err(|e| e.to_string())? { - Event::Start(e) => stack.push(node(&e)), + Event::Start(e) => { + if stack.len() >= MAX_VALIDATION_DEPTH { + return Err(format!("XML nesting exceeds the {MAX_VALIDATION_DEPTH} level validation limit")); + } + if stack.is_empty() && root.is_some() { + return Err("multiple root elements".into()); + } + nodes = nodes.saturating_add(1); + if nodes > MAX_VALIDATION_NODES { + return Err(format!("XML exceeds the {MAX_VALIDATION_NODES} node validation limit")); + } + let n = node(&e)?; + attributes = attributes.saturating_add(n.attrs.len()); + if attributes > MAX_VALIDATION_ATTRIBUTES { + return Err(format!("XML exceeds the {MAX_VALIDATION_ATTRIBUTES} attribute validation limit")); + } + if let Some(parent) = stack.last_mut() { + parent.content = true; + } + stack.push(n); + } Event::Empty(e) => { - let n = node(&e); + if stack.len() >= MAX_VALIDATION_DEPTH { + return Err(format!("XML nesting exceeds the {MAX_VALIDATION_DEPTH} level validation limit")); + } + if stack.is_empty() && root.is_some() { + return Err("multiple root elements".into()); + } + nodes = nodes.saturating_add(1); + if nodes > MAX_VALIDATION_NODES { + return Err(format!("XML exceeds the {MAX_VALIDATION_NODES} node validation limit")); + } + let n = node(&e)?; + attributes = attributes.saturating_add(n.attrs.len()); + if attributes > MAX_VALIDATION_ATTRIBUTES { + return Err(format!("XML exceeds the {MAX_VALIDATION_ATTRIBUTES} attribute validation limit")); + } match stack.last_mut() { - Some(p) => p.children.push(n), - None => return Ok(n), + Some(p) => { + p.content = true; + p.children.push(n); + } + None => root = Some(n), } } - Event::End(_) => { + Event::End(e) => { let n = stack.pop().ok_or("unbalanced end tag")?; + if n.name.as_bytes() != e.name().as_ref() { + return Err("mismatched end tag".into()); + } match stack.last_mut() { Some(p) => p.children.push(n), - None => return Ok(n), + None => root = Some(n), } } Event::Text(t) => { - if let Some(p) = stack.last_mut() - && !t.iter().all(u8::is_ascii_whitespace) - { - p.text = true; + if let Some(p) = stack.last_mut() { + p.content = true; + if !t.iter().all(u8::is_ascii_whitespace) { + p.text = true; + } + } else if !t.iter().all(u8::is_ascii_whitespace) { + return Err("text outside the root element".into()); } } - Event::CData(_) | Event::GeneralRef(_) => { + Event::CData(_) => { if let Some(p) = stack.last_mut() { p.text = true; + p.content = true; } } - Event::Eof => return Err("no root element".into()), + Event::GeneralRef(reference) => { + let name = reference.decode().map_err(|e| e.to_string())?; + let accepted = if let Some(character) = reference.resolve_char_ref().map_err(|e| e.to_string())? { + crate::is_xml_char(character) + } else { + matches!(name.as_ref(), "amp" | "lt" | "gt" | "apos" | "quot") + }; + if !accepted { + return Err(format!("unsupported general entity reference `&{name};`")); + } + if let Some(p) = stack.last_mut() { + p.text = true; + p.content = true; + } else { + return Err("reference outside the root element".into()); + } + } + Event::Comment(_) | Event::PI(_) => { + if let Some(parent) = stack.last_mut() { + parent.content = true; + } + } + Event::Eof => { + if !stack.is_empty() { + return Err("unclosed element".into()); + } + return root.ok_or_else(|| "no root element".into()); + } _ => {} } } } -fn check(dtd: &Dtd, n: &Node, path: &str, out: &mut Vec) { +fn check(dtd: &Dtd, n: &Node, path: &str, out: &mut Vec, budget: &mut MatchBudget) { let mut problem = |m: String| out.push(Problem { path: path.to_string(), message: m }); match dtd.model(&n.name) { None => problem(format!("`{}` isn't declared in the DTD", n.name)), Some(Model::Any) => {} Some(Model::Empty) => { - if n.text || !n.children.is_empty() { + if n.content { problem(format!("`{}` should be empty", n.name)); } } @@ -461,9 +708,19 @@ fn check(dtd: &Dtd, n: &Node, path: &str, out: &mut Vec) { problem(format!("`{}` can't contain text", n.name)); } let seq: Vec<&str> = n.children.iter().map(|c| c.name.as_str()).collect(); - if !ends(cp, &seq, 0, &mut EndsMemo::new()).contains(&seq.len()) { - let got = if seq.is_empty() { "nothing".to_string() } else { seq.join(", ") }; - problem(format!("`{}` contains {got}; the DTD asks for {}", n.name, show(cp))); + let matched = ends(cp, &seq, 0, &mut EndsMemo::new(), budget); + match matched { + Ok(positions) if positions.contains(&seq.len()) => {} + Ok(_) => { + let got = if seq.is_empty() { "nothing".to_string() } else { seq.join(", ") }; + problem(format!("`{}` contains {got}; the DTD asks for {}", n.name, show(cp))); + } + Err(error) => { + if !budget.reported { + problem(format!("`{}`: {error}", n.name)); + budget.reported = true; + } + } } } } @@ -476,17 +733,21 @@ fn check(dtd: &Dtd, n: &Node, path: &str, out: &mut Vec) { for c in &n.children { let k = seen.entry(c.name.as_str()).or_default(); *k += 1; - check(dtd, c, &format!("{path}/{}[{k}]", c.name), out); + check(dtd, c, &format!("{path}/{}[{k}]", c.name), out, budget); } } /// Validate `xml` against `dtd`; an empty list when it's valid. +/// +/// Validation rejects input above the internal byte, depth, node, attribute and content-model +/// matching budgets. Only predefined entities and numeric character references are resolved; a +/// general entity reference is reported as unsupported rather than expanded or fetched. pub fn validate(dtd: &Dtd, xml: &str) -> Vec { match read_tree(xml) { Ok(root) => { let mut out = Vec::new(); let path = root.name.clone(); - check(dtd, &root, &path, &mut out); + check(dtd, &root, &path, &mut out, &mut MatchBudget::default()); out } Err(e) => vec![Problem { path: String::new(), message: format!("not well-formed XML: {e}") }], @@ -745,4 +1006,68 @@ mod tests { assert_eq!(problems.len(), 1, "{problems:?}"); assert!(problems[0].message.starts_with("`r` contains a, a, a"), "{problems:?}"); } + + #[test] + fn incomplete_declarations_and_malformed_mixed_models_are_rejected() { + assert!(parse("")).is_err(), "{model}"); + } + assert!(parse("").is_ok()); + assert!(parse("").is_ok()); + assert!(parse("").is_ok()); + } + + #[test] + fn parameter_entities_are_not_expanded_inside_comments() { + let dtd = parse( + r#" + +"#, + ) + .unwrap(); + assert_eq!(dtd.model("r"), Some(&Model::Empty)); + assert!(!validate(&dtd, "").is_empty()); + } + + #[test] + fn notation_attribute_type_keeps_following_required_attribute_aligned() { + let dtd = parse("").unwrap(); + assert!(validate(&dtd, "").iter().any(|p| p.message.contains("required attribute `id`"))); + assert!(validate(&dtd, "").is_empty()); + } + + #[test] + fn malformed_or_unbounded_xml_is_rejected_before_validation() { + let dtd = parse("").unwrap(); + for xml in ["", ""] { + assert!(!validate(&dtd, xml).is_empty(), "{xml}"); + } + let deep = format!("{}{}", "".repeat(MAX_VALIDATION_DEPTH + 1), "".repeat(MAX_VALIDATION_DEPTH + 1)); + let problems = validate(&dtd, &deep); + assert!(problems.iter().any(|p| p.message.contains("nesting exceeds")), "{problems:?}"); + } + + #[test] + fn references_must_be_declared_and_empty_elements_must_have_no_content() { + let dtd = parse("").unwrap(); + assert!(validate(&dtd, "&missing;").iter().any(|p| p.message.contains("unsupported general entity"))); + assert!(validate(&dtd, "&known;").iter().any(|p| p.message.contains("unsupported general entity"))); + assert!(validate(&dtd, "A&").is_empty()); + assert!(!validate(&dtd, "�").is_empty()); + for xml in [" ", "", "", "&"] { + assert!(validate(&dtd, xml).iter().any(|p| p.message.contains("should be empty")), "{xml}"); + } + assert!(validate(&dtd, "").is_empty()); + } + + #[test] + fn ambiguous_content_matching_stops_at_its_budget() { + let dtd = parse("").unwrap(); + let xml = format!("{}", "".repeat(10_000)); + let problems = validate(&dtd, &xml); + assert!(problems.iter().any(|p| p.message.contains("matching exceeded its ")), "{}", problems.len()); + } } From f5720d8f6925b2218cb6adc0bc2289801d8ca437 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 17:36:14 +0000 Subject: [PATCH 09/29] refactor(xml): share bounded tree-node validation --- crates/xml/src/dtd.rs | 54 ++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/crates/xml/src/dtd.rs b/crates/xml/src/dtd.rs index 9dc0790c5..9fa1dd497 100644 --- a/crates/xml/src/dtd.rs +++ b/crates/xml/src/dtd.rs @@ -562,6 +562,26 @@ struct Node { content: bool, } +fn bounded_node(e: &quick_xml::events::BytesStart<'_>, depth: usize, nodes: &mut usize, attributes: &mut usize) -> Result { + if depth >= MAX_VALIDATION_DEPTH { + return Err(format!("XML nesting exceeds the {MAX_VALIDATION_DEPTH} level validation limit")); + } + *nodes = nodes.saturating_add(1); + if *nodes > MAX_VALIDATION_NODES { + return Err(format!("XML exceeds the {MAX_VALIDATION_NODES} node validation limit")); + } + let mut attrs = Vec::new(); + for attr in e.attributes().with_checks(true) { + let attr = attr.map_err(|err| format!("invalid attribute: {err}"))?; + attrs.push(String::from_utf8_lossy(attr.key.as_ref()).to_string()); + } + *attributes = attributes.saturating_add(attrs.len()); + if *attributes > MAX_VALIDATION_ATTRIBUTES { + return Err(format!("XML exceeds the {MAX_VALIDATION_ATTRIBUTES} attribute validation limit")); + } + Ok(Node { name: String::from_utf8_lossy(e.name().as_ref()).to_string(), attrs, children: vec![], text: false, content: false }) +} + fn read_tree(xml: &str) -> Result { use quick_xml::events::Event; if xml.len() > MAX_VALIDATION_XML_BYTES { @@ -572,53 +592,23 @@ fn read_tree(xml: &str) -> Result { let mut root = None; let mut nodes = 0usize; let mut attributes = 0usize; - let node = |e: &quick_xml::events::BytesStart| -> Result { - let mut attrs = Vec::new(); - for attr in e.attributes().with_checks(true) { - let attr = attr.map_err(|err| format!("invalid attribute: {err}"))?; - attrs.push(String::from_utf8_lossy(attr.key.as_ref()).to_string()); - } - Ok(Node { name: String::from_utf8_lossy(e.name().as_ref()).to_string(), attrs, children: vec![], text: false, content: false }) - }; loop { match r.read_event().map_err(|e| e.to_string())? { Event::Start(e) => { - if stack.len() >= MAX_VALIDATION_DEPTH { - return Err(format!("XML nesting exceeds the {MAX_VALIDATION_DEPTH} level validation limit")); - } if stack.is_empty() && root.is_some() { return Err("multiple root elements".into()); } - nodes = nodes.saturating_add(1); - if nodes > MAX_VALIDATION_NODES { - return Err(format!("XML exceeds the {MAX_VALIDATION_NODES} node validation limit")); - } - let n = node(&e)?; - attributes = attributes.saturating_add(n.attrs.len()); - if attributes > MAX_VALIDATION_ATTRIBUTES { - return Err(format!("XML exceeds the {MAX_VALIDATION_ATTRIBUTES} attribute validation limit")); - } + let n = bounded_node(&e, stack.len(), &mut nodes, &mut attributes)?; if let Some(parent) = stack.last_mut() { parent.content = true; } stack.push(n); } Event::Empty(e) => { - if stack.len() >= MAX_VALIDATION_DEPTH { - return Err(format!("XML nesting exceeds the {MAX_VALIDATION_DEPTH} level validation limit")); - } if stack.is_empty() && root.is_some() { return Err("multiple root elements".into()); } - nodes = nodes.saturating_add(1); - if nodes > MAX_VALIDATION_NODES { - return Err(format!("XML exceeds the {MAX_VALIDATION_NODES} node validation limit")); - } - let n = node(&e)?; - attributes = attributes.saturating_add(n.attrs.len()); - if attributes > MAX_VALIDATION_ATTRIBUTES { - return Err(format!("XML exceeds the {MAX_VALIDATION_ATTRIBUTES} attribute validation limit")); - } + let n = bounded_node(&e, stack.len(), &mut nodes, &mut attributes)?; match stack.last_mut() { Some(p) => { p.content = true; From b6b222afb07274e98a952f81759125047a149a09 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 17:45:53 +0000 Subject: [PATCH 10/29] fix(prepress): address PDF/X review findings --- crates/pdf/src/write/svg.rs | 13 ++ crates/prepress/src/pdfx1a.rs | 215 ++++++++++++++++++++++++++------ vendor/hayro-syntax/src/pdf.rs | 10 ++ vendor/hayro-syntax/src/xref.rs | 18 +++ 4 files changed, 219 insertions(+), 37 deletions(-) diff --git a/crates/pdf/src/write/svg.rs b/crates/pdf/src/write/svg.rs index 2eaf055ec..eb6542c0a 100644 --- a/crates/pdf/src/write/svg.rs +++ b/crates/pdf/src/write/svg.rs @@ -112,6 +112,7 @@ fn shared_limits(bytes: usize, limits: Limits) -> craft_svg::tree::Limits { craft_svg::tree::Limits { bytes, depth: limits.depth, + tree_depth: limits.depth, refuse_entity_markup: true, xml_nodes: limits.xml_nodes, embedded_bytes: limits.embedded_svg_bytes, @@ -161,3 +162,15 @@ fn image_resolver(limits: Limits) -> usvg::ImageHrefResolver<'static> { resolve_string: Box::new(|_, _| None), } } + +#[cfg(test)] +mod tests { + use super::{Limits, shared_limits}; + + #[test] + fn shared_depth_limit_also_bounds_svg_references() { + let limits = shared_limits(1024, Limits { depth: 7, ..Limits::default() }); + assert_eq!(limits.depth, 7); + assert_eq!(limits.tree_depth, 7); + } +} diff --git a/crates/prepress/src/pdfx1a.rs b/crates/prepress/src/pdfx1a.rs index 0755424be..92110166b 100644 --- a/crates/prepress/src/pdfx1a.rs +++ b/crates/prepress/src/pdfx1a.rs @@ -25,6 +25,10 @@ const CONFORMANCE: &str = "PDF/X-1a:2003"; /// `/RegistryName` of the ICC's registry of characterized printing conditions. An output intent /// naming one of them needs no `/DestOutputProfile` in PDF/X. const ICC_REGISTRY: &str = "http://www.color.org"; +const REGISTERED_CONDITIONS: &[&str] = &["FOGRA39"]; +/// Largest classic cross-reference size accepted by the public finalizer. The rewritten table +/// emits one entry per object number, so this is also an output-allocation bound. +const MAX_XREF_SIZE: usize = 1 << 20; /// The graphics placed on one output page, to name in errors: the page's name and the graphics' /// names. @@ -114,7 +118,6 @@ pub fn check_pdfx1a(pdf: &[u8]) -> Vec { /// [`check_pdfx1a`]'s findings: transparency and color spaces first, then the rest. fn checks(pdf: &[u8]) -> (Vec, Vec) { let mut issues = Vec::new(); - let text = String::from_utf8_lossy(pdf); let file = craft_pdf::syntax::Pdf::new(pdf.to_vec()).ok(); if !pdf.starts_with(b"%PDF-1.") || pdf.get(7).is_none_or(|v| *v > b'4') { issues.push("the PDF version must be 1.4 or lower".into()); @@ -122,41 +125,46 @@ fn checks(pdf: &[u8]) -> (Vec, Vec) { if file.as_ref().is_none_or(|f| pdfx_intents(f).is_empty()) { issues.push("no PDF/X output intent".into()); } - let info = |key: &[u8]| file.as_ref().and_then(|f| info_text(pdf, f, key)); + let info = |key: &[u8]| file.as_ref().and_then(|f| info_text(f, key)); if info(b"GTS_PDFXVersion").as_deref() != Some(VERSION) { issues.push(format!("the document information doesn't name {VERSION}")); } if info(b"GTS_PDFXConformance").as_deref() != Some(CONFORMANCE) { issues.push(format!("the document information doesn't name the conformance level {CONFORMANCE}")); } - if !text.contains("/Trapped/False") && !text.contains("/Trapped/True") && !text.contains("/Trapped /False") && !text.contains("/Trapped /True") { + let trapped = file.as_ref().and_then(|f| f.info()).and_then(|info| info.get::>(b"Trapped")); + if !trapped.as_ref().is_some_and(|value| matches!(value.as_ref(), b"False" | b"True")) { issues.push("the document information needs /Trapped True or False".into()); } - if text.contains("/Interpolate true") { - issues.push("an image asks for interpolation".into()); - } - if text.contains("/Encrypt") { + if file.as_ref().is_some_and(|f| f.is_encrypted()) { issues.push("the file is encrypted".into()); } - let pages = text.matches("/Type/Page/").count() + text.matches("/Type/Page>").count(); - let trims = text.matches("/TrimBox").count(); - if pages > 0 && trims < pages { - issues.push("every page needs a trim box".into()); + if let Some(file) = file.as_ref() { + for (index, page) in file.pages().iter().enumerate() { + let raw = page.raw(); + let trim = raw.contains_key(b"TrimBox"); + let art = raw.contains_key(b"ArtBox"); + if trim == art { + issues.push(format!("page {} needs either a trim box or an art box, but not both", index + 1)); + } + } } - let (content, fonts) = content(file.as_ref()); + let (content, fonts, scan_issues) = content(file.as_ref()); issues.extend(fonts); + issues.extend(scan_issues); (content, issues) } /// Transparency and color spaces, then the output intent and fonts, walked through every page's /// content and what it uses. -fn content(file: Option<&craft_pdf::syntax::Pdf>) -> (Vec, Vec) { +fn content(file: Option<&craft_pdf::syntax::Pdf>) -> (Vec, Vec, Vec) { let Some(file) = file else { - return (vec!["the file couldn't be read to check its transparency and color spaces".into()], Vec::new()); + return (vec!["the file couldn't be read to check its transparency and color spaces".into()], Vec::new(), Vec::new()); }; let mut fonts = intent_issues(file); let mut found: Vec<(String, Vec)> = Vec::new(); let mut font_found: Vec<(String, Vec)> = Vec::new(); + let mut scan_found: Vec<(String, Vec)> = Vec::new(); for (i, page) in file.pages().iter().enumerate() { let mut scan = Scan::default(); let res = page.resources(); @@ -166,7 +174,7 @@ fn content(file: Option<&craft_pdf::syntax::Pdf>) -> (Vec, Vec) if let Some(content) = page.page_stream() { scan.content(content, res, 0); } - for (list, items) in [(&mut found, scan.found), (&mut font_found, scan.font_issues)] { + for (list, items) in [(&mut found, scan.found), (&mut font_found, scan.font_issues), (&mut scan_found, scan.other_issues)] { for what in items { match list.iter_mut().find(|(w, _)| *w == what) { Some((_, pages)) => pages.push(i + 1), @@ -182,7 +190,7 @@ fn content(file: Option<&craft_pdf::syntax::Pdf>) -> (Vec, Vec) format!("{what} on page{s} {}{more}", list.join(", ")) }; fonts.extend(font_found.into_iter().map(on_pages)); - (found.into_iter().map(on_pages).collect(), fonts) + (found.into_iter().map(on_pages).collect(), fonts, scan_found.into_iter().map(on_pages).collect()) } /// A `GTS_PDFX` output intent, as PDF/X-1a judges it. @@ -230,7 +238,8 @@ fn pdfx_intents(file: &craft_pdf::syntax::Pdf) -> Vec { Intent { obj: oi.obj_id().and_then(|id| number(id.obj_number)), name: text(b"OutputCondition").or_else(|| identifier.clone()).unwrap_or_else(|| "?".into()), - registered: registry.is_some_and(|r| r.trim_end_matches('/') == ICC_REGISTRY) && identifier.as_deref().is_some_and(|i| i != "Custom"), + registered: registry.is_some_and(|r| r.trim_end_matches('/') == ICC_REGISTRY) + && identifier.as_deref().is_some_and(|identifier| REGISTERED_CONDITIONS.contains(&identifier)), has_profile: oi.contains_key(b"DestOutputProfile"), profile_obj: oi.get_ref(b"DestOutputProfile").and_then(|r| number(r.obj_number)), channels: profile.as_ref().and_then(|p| p.dict().get::(b"N")), @@ -247,14 +256,8 @@ fn raw_pdfx_intents<'a>(file: &'a craft_pdf::syntax::Pdf) -> Vec> { intents.iter::>().filter(|oi| oi.get::>(b"S").is_some_and(|s| s.as_ref() == b"GTS_PDFX")).collect() } -fn info_text(pdf: &[u8], file: &craft_pdf::syntax::Pdf, key: &[u8]) -> Option { - let layout = Layout::read(pdf)?; - let at = layout.trailer.find("/Info")? + "/Info".len(); - let tail = layout.trailer.get(at..)?.trim_start(); - let (number, _) = tail.split_once(' ')?; - let id = number.parse::().ok()?; - let dict = file.xref().get::>(craft_pdf::syntax::object::ObjectIdentifier { obj_number: id, gen_number: 0 })?; - dict.get::>(key).map(|s| String::from_utf8_lossy(s.as_bytes()).trim().to_string()) +fn info_text(file: &craft_pdf::syntax::Pdf, key: &[u8]) -> Option { + file.info()?.get::>(key).map(|s| String::from_utf8_lossy(s.as_bytes()).trim().to_string()) } /// What PDF/X-1a finds wrong with the file's output intent: a CMYK profile, ICC v2 (PDF 1.4 @@ -285,7 +288,7 @@ pub fn finish_x1a(pdf: &[u8]) -> Result, String> { let layout = Layout::read(pdf).ok_or_else(unreadable)?; let file = craft_pdf::syntax::Pdf::new(pdf.to_vec()).map_err(|_| unreadable())?; let mut bodies: HashMap> = HashMap::new(); - let info = int_after(&layout.trailer, "/Info").ok_or_else(unreadable)?; + let info = file.info().and_then(|info| info.obj_id()).and_then(|id| usize::try_from(id.obj_number).ok()).ok_or_else(unreadable)?; let body = layout.body(pdf, info).ok_or_else(unreadable)?; let (head, close) = body.split_at_checked(rfind(body, b">>").ok_or_else(unreadable)?).ok_or_else(unreadable)?; bodies.insert(info, [head, format!("/GTS_PDFXConformance({CONFORMANCE})").as_bytes(), close].concat()); @@ -404,6 +407,9 @@ impl Layout { return None; } let size = int_after(&trailer, "/Size")?; + if size > MAX_XREF_SIZE { + return None; + } let mut tokens = std::str::from_utf8(section.get(..t)?).ok()?.split_whitespace(); if tokens.next()? != "xref" { return None; @@ -526,8 +532,8 @@ fn disallowed(cs: &Object<'_>, res: &Resources<'_>, depth: usize) -> Option { let _names = items.next(); let alt = items.next()?; - if let Some(why) = disallowed(&alt, res, depth + 1) { - return Some(format!("a spot color whose alternate space is {why}")); + if !is_process_space(&alt, res, depth + 1) { + return Some("a spot color whose alternate space isn't a CMYK or gray process space".into()); } let _tint = items.next(); // NChannel attributes are PDF 1.6, past the 1.4 that PDF/X-1a:2003 allows. @@ -546,6 +552,26 @@ fn disallowed(cs: &Object<'_>, res: &Resources<'_>, depth: usize) -> Option, res: &Resources<'_>, depth: usize) -> bool { + if depth > 4 { + return false; + } + match cs { + Object::Name(name) => match name.as_ref() { + b"DeviceCMYK" | b"CMYK" | b"DeviceGray" | b"G" => true, + other => match res.get_color_space(&Name::new_unescaped(other)) { + Some(Object::Name(alias)) if alias.as_ref() != other => is_process_space(&Object::Name(alias), res, depth + 1), + Some(named) if !matches!(named, Object::Name(_)) => is_process_space(&named, res, depth + 1), + _ => false, + }, + }, + Object::Array(array) => { + array.iter::>().next().is_some_and(|family| matches!(family.as_ref(), b"DeviceCMYK" | b"CMYK" | b"DeviceGray" | b"G")) + } + _ => false, + } +} + /// What one page has that PDF/X-1a doesn't allow, in the order found. #[derive(Default)] struct Scan { @@ -553,6 +579,7 @@ struct Scan { found: Vec, known: HashSet, font_issues: Vec, + other_issues: Vec, seen: HashSet, fonts: HashSet, } @@ -572,6 +599,7 @@ impl Scan { fn content(&mut self, data: &[u8], res: &Resources<'_>, depth: usize) { if depth > MAX_DEPTH { + self.add("content was not fully checked because nested resources exceed the scan limit".into()); return; } let mut ops = UntypedIter::new(data); @@ -617,10 +645,12 @@ impl Scan { } } b"BI" | b"EI" => { - if let Some(Object::Stream(img)) = last - && let Some(cs) = img.dict().get::>(b"CS").or_else(|| img.dict().get::>(b"ColorSpace")) - { - self.space(&cs, res); + if let Some(Object::Stream(img)) = last { + let dict = img.dict(); + if let Some(cs) = dict.get::>(b"CS").or_else(|| dict.get::>(b"ColorSpace")) { + self.space(&cs, res); + } + self.interpolation(dict); } } _ => {} @@ -664,6 +694,7 @@ impl Scan { return; } let d = img.dict(); + self.interpolation(d); if d.get::>(b"SMask").is_some() { self.add(format!("an image with a soft mask (object {}, transparency)", img.obj_id().obj_number)); } @@ -685,16 +716,30 @@ impl Scan { } } + fn interpolation(&mut self, dict: &Dict<'_>) { + if dict.get::(b"Interpolate") == Some(true) { + let message = "an image asks for interpolation".to_string(); + if !self.other_issues.contains(&message) { + self.other_issues.push(message); + } + } + } + fn form(&mut self, form: &Stream<'_>, parent: &Resources<'_>, depth: usize) { - if depth >= MAX_DEPTH || !self.seen.insert(form.obj_id()) { + if depth >= MAX_DEPTH { + self.add("content was not fully checked because nested forms exceed the scan limit".into()); + return; + } + if !self.seen.insert(form.obj_id()) { return; } let res = Resources::from_parent(form.dict().get::>(b"Resources").unwrap_or_default(), parent.clone()); if let Some(group) = form.dict().get::>(b"Group") { self.group(&group); } - if let Ok(data) = form.decoded() { - self.content(&data, &res, depth + 1); + match form.decoded() { + Ok(data) => self.content(&data, &res, depth + 1), + Err(_) => self.add(format!("a form XObject could not be decoded (object {})", form.obj_id().obj_number)), } } @@ -756,6 +801,7 @@ impl Scan { /// A Type 3 font's glyph procedures, with the font's resources over `parent`'s. fn type3(&mut self, font: &Dict<'_>, parent: &Resources<'_>, depth: usize) { if depth >= MAX_DEPTH { + self.add("content was not fully checked because nested Type 3 glyphs exceed the scan limit".into()); return; } let res = Resources::from_parent(font.get::>(b"Resources").unwrap_or_default(), parent.clone()); @@ -763,10 +809,105 @@ impl Scan { for key in procs.keys() { if let Some(glyph) = procs.get::>(key.as_ref()) && self.seen.insert(glyph.obj_id()) - && let Ok(data) = glyph.decoded() { - self.content(&data, &res, depth + 1); + match glyph.decoded() { + Ok(data) => self.content(&data, &res, depth + 1), + Err(_) => self.add(format!("a Type 3 glyph stream could not be decoded (object {})", glyph.obj_id().obj_number)), + } } } } } + +#[cfg(test)] +mod review_regressions { + use super::{Layout, check_pdfx1a, finish_x1a}; + fn pdf(page_boxes: &str, info: &str, content: &[u8], extra: &[(&str, &str)]) -> Vec { + let mut objects = vec![ + b"<< /Type /Catalog /Pages 2 0 R /OutputIntents [3 0 R] >>".to_vec(), + b"<< /Type /Pages /Kids [4 0 R] /Count 1 >>".to_vec(), + b"<< /Type /OutputIntent /S /GTS_PDFX /OutputConditionIdentifier (FOGRA39) /RegistryName (http://www.color.org) >>".to_vec(), + format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] {page_boxes} /Resources <<>> /Contents 5 0 R >>").into_bytes(), + [format!("<< /Length {} >>\nstream\n", content.len()).as_bytes(), content, b"\nendstream"].concat(), + format!("<< {info} >>").into_bytes(), + ]; + for (key, body) in extra { + objects.push(format!("<< {key} {body} >>").into_bytes()); + } + let mut out = b"%PDF-1.4\n% parser regression fixture\n".to_vec(); + let mut offsets = Vec::new(); + for (index, body) in objects.iter().enumerate() { + offsets.push(out.len()); + out.extend_from_slice(format!("{} 0 obj\n", index + 1).as_bytes()); + out.extend_from_slice(body); + out.extend_from_slice(b"\nendobj\n"); + } + let xref = out.len(); + out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objects.len() + 1).as_bytes()); + for offset in offsets { + out.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes()); + } + out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R /Info 6 0 R >>\nstartxref\n{xref}\n%%EOF\n", objects.len() + 1).as_bytes()); + out + } + + fn compliant_info() -> &'static str { + "/GTS_PDFXVersion (PDF/X-1:2003) /GTS_PDFXConformance (PDF/X-1a:2003) /Trapped /False" + } + + #[test] + fn parsed_info_values_are_required_even_when_comment_contains_expected_values() { + let bytes = pdf( + "/TrimBox [0 0 100 100]", + "/GTS_PDFXVersion (wrong) /GTS_PDFXConformance (wrong) /Trapped /False", + b"% /GTS_PDFXVersion (PDF/X-1:2003) /GTS_PDFXConformance (PDF/X-1a:2003)\n", + &[], + ); + let issues = check_pdfx1a(&bytes); + assert!(issues.iter().any(|issue| issue.contains("doesn't name PDF/X-1:2003")), "{issues:?}"); + assert!(issues.iter().any(|issue| issue.contains("doesn't name the conformance level")), "{issues:?}"); + } + + #[test] + fn either_trim_or_art_box_is_accepted_but_not_both() { + let art_only = pdf("/ArtBox [0 0 100 100]", compliant_info(), b"", &[]); + let both = pdf("/TrimBox [0 0 100 100] /ArtBox [0 0 100 100]", compliant_info(), b"", &[]); + assert!(!check_pdfx1a(&art_only).iter().any(|issue| issue.contains("either a trim box or an art box"))); + assert!(check_pdfx1a(&both).iter().any(|issue| issue.contains("either a trim box or an art box"))); + } + + #[test] + fn an_unknown_registry_identifier_cannot_replace_a_destination_profile() { + let mut bytes = pdf("/TrimBox [0 0 100 100]", compliant_info(), b"", &[]); + let known = b"FOGRA39"; + let at = bytes.windows(known.len()).position(|window| window == known); + assert!(at.is_some()); + if let Some(at) = at { + bytes[at..at + known.len()].copy_from_slice(b"NOTREAL"); + } + let issues = check_pdfx1a(&bytes); + assert!(issues.iter().any(|issue| issue.contains("doesn't name a printing condition in the ICC registry")), "{issues:?}"); + } + + #[test] + fn finalizer_rejects_unbounded_xref_size_before_allocating_the_table() { + let mut bytes = pdf("/TrimBox [0 0 100 100]", compliant_info(), b"", &[]); + let size = b"/Size 7"; + let at = bytes.windows(size.len()).position(|window| window == size); + assert!(at.is_some()); + if let Some(at) = at { + bytes.splice(at..at + size.len(), b"/Size 1048577".iter().copied()); + } + assert!(Layout::read(&bytes).is_none()); + assert!(finish_x1a(&bytes).is_err()); + } + + #[test] + fn trapped_must_be_a_pdf_name_with_a_boolean_name_value() { + for value in ["(False)", "true", "(True)"] { + let info = format!("/GTS_PDFXVersion (PDF/X-1:2003) /GTS_PDFXConformance (PDF/X-1a:2003) /Trapped {value}"); + let bytes = pdf("/TrimBox [0 0 100 100]", &info, b"", &[]); + assert!(check_pdfx1a(&bytes).iter().any(|issue| issue.contains("needs /Trapped True or False")), "{value}"); + } + } +} diff --git a/vendor/hayro-syntax/src/pdf.rs b/vendor/hayro-syntax/src/pdf.rs index a13e3f669..88b881b7f 100644 --- a/vendor/hayro-syntax/src/pdf.rs +++ b/vendor/hayro-syntax/src/pdf.rs @@ -186,6 +186,16 @@ impl Pdf { pub fn metadata(&self) -> &Metadata { self.xref.metadata() } + + /// Return the document information dictionary resolved through the final trailer and xref. + pub fn info(&self) -> Option> { + self.xref.info_id().and_then(|id| self.xref.get(id)) + } + + /// Whether the resolved trailer declares the document encrypted. + pub fn is_encrypted(&self) -> bool { + self.xref.is_encrypted() + } } fn find_version(data: &[u8]) -> Option { diff --git a/vendor/hayro-syntax/src/xref.rs b/vendor/hayro-syntax/src/xref.rs index 5fced8a88..0d2dadb64 100644 --- a/vendor/hayro-syntax/src/xref.rs +++ b/vendor/hayro-syntax/src/xref.rs @@ -449,6 +449,8 @@ impl XRef { let td = TrailerData { pages_ref: pages_ref.into(), root_ref: root_ref.into(), + info_ref: trailer_dict.get_ref(INFO).map(Into::into), + encrypted: trailer_dict.contains_key(ENCRYPT), version, }; @@ -461,6 +463,8 @@ impl XRef { let td = TrailerData { pages_ref: pages_ref.into(), root_ref, + info_ref: None, + encrypted: false, version: None, }; @@ -526,6 +530,16 @@ impl XRef { self.trailer_data().root_ref } + /// Return the document information dictionary reference from the resolved trailer, if any. + pub fn info_id(&self) -> Option { + self.trailer_data().info_ref + } + + /// Whether the resolved trailer has an `/Encrypt` entry. + pub fn is_encrypted(&self) -> bool { + self.trailer_data().encrypted + } + /// Whether the PDF has optional content groups. pub fn has_optional_content_groups(&self) -> bool { match &self.0 { @@ -885,6 +899,8 @@ struct MapRepr { pub(crate) struct TrailerData { pub(crate) pages_ref: ObjectIdentifier, pub(crate) root_ref: ObjectIdentifier, + pub(crate) info_ref: Option, + pub(crate) encrypted: bool, pub(crate) version: Option, } @@ -893,6 +909,8 @@ impl TrailerData { Self { pages_ref: ObjectIdentifier::new(0, 0), root_ref: ObjectIdentifier::new(0, 0), + info_ref: None, + encrypted: false, version: None, } } From d74e60b2d59ac6af7263c4b6a3c3444e0851319d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 12:43:40 -0500 Subject: [PATCH 11/29] craft-pdf: refuse to create RC4 encryption without an explicit legacy call The create() API exposed RC4-40 and RC4-128 behind a doc comment, so an app could write files whose encryption protects nothing. create() now returns Unsupported for both schemes; create_legacy() and Document::set_encryption_legacy() are the explicit ways to write a file that must stay readable by an old reader. Tests and the encrypt example, which read and write every algorithm, use the legacy entry points. --- crates/pdf/README.md | 9 ++++-- crates/pdf/examples/cos_protect.rs | 3 +- crates/pdf/src/cos/document/open.rs | 16 ++++++++++- crates/pdf/src/crypt.rs | 28 +++++++++++++++++-- crates/pdf/src/crypt/tests.rs | 24 +++++++++++++++- crates/pdf/tests/cos/bounded_writer.rs | 3 +- crates/pdf/tests/cos/encryption.rs | 10 ++++--- crates/pdf/tests/cos/file_identifier.rs | 5 ++-- .../pdf/tests/crypt/permissions_revision.rs | 6 ++-- crates/pdf/tests/crypt/proptests.rs | 12 +++++--- 10 files changed, 94 insertions(+), 22 deletions(-) diff --git a/crates/pdf/README.md b/crates/pdf/README.md index 8cd2c3f25..210b79849 100644 --- a/crates/pdf/README.md +++ b/crates/pdf/README.md @@ -202,9 +202,12 @@ PDF files. The reader decrypts on its own and does not use it. It covers revisio block is ignored and bad padding is left in. - `permissions()` gives the `/P` flags; for R5/R6 a permission counts only when `/Perms` grants it too. Owners have every permission. -- `create(&NewEncryption, id0)` builds the `/Encrypt` values for RC4-40, RC4-128, AES-128 or - AES-256. The caller supplies 32 bytes of entropy (`seed`), so the module needs no OS access - and builds for the web. `file_id` derives a file identifier from the same seed. +- `create(&NewEncryption, id0)` builds the `/Encrypt` values for AES-128 or AES-256. It refuses + the legacy RC4-40 and RC4-128 schemes, because RC4 is broken (RFC 7465); `create_legacy` + writes those, for a file that must stay readable by an old reader. `Document::set_encryption` + refuses them too, with `set_encryption_legacy` as the explicit way in. The caller supplies 32 + bytes of entropy (`seed`), so the module needs no OS access and builds for the web. `file_id` + derives a file identifier from the same seed. - `EncryptDict` holds the dictionary's values. The caller reads them from, and writes them to, its own object model; the module does not parse PDF syntax. Keys and prepared passwords are zeroized when dropped. diff --git a/crates/pdf/examples/cos_protect.rs b/crates/pdf/examples/cos_protect.rs index c40bccdce..b12c700cf 100644 --- a/crates/pdf/examples/cos_protect.rs +++ b/crates/pdf/examples/cos_protect.rs @@ -22,7 +22,8 @@ fn main() { _ => craft_pdf::cos::Algorithm::Aes256, }; let mut doc = craft_pdf::cos::Document::open(std::sync::Arc::new(std::fs::read(input).unwrap())).unwrap(); - doc.set_encryption(&craft_pdf::cos::NewEncryption { + // `rc4-40` and `rc4-128` are the legacy schemes, which `set_encryption` refuses. + doc.set_encryption_legacy(&craft_pdf::cos::NewEncryption { algorithm, user_password: user, owner_password: owner, diff --git a/crates/pdf/src/cos/document/open.rs b/crates/pdf/src/cos/document/open.rs index 46a6423a9..4991412ce 100644 --- a/crates/pdf/src/cos/document/open.rs +++ b/crates/pdf/src/cos/document/open.rs @@ -414,7 +414,20 @@ impl Document { /// Protect the document with a password (§7.6.4). Takes effect on the next save, which is /// always a full rewrite. Returns the handler (authenticated as owner). + /// + /// The legacy RC4 schemes are refused (RC4 is broken, `RFC 7465`): [`Self::set_encryption_legacy`] + /// writes them for a file an old reader must open. pub fn set_encryption(&mut self, params: &crate::crypt::NewEncryption) -> Result<(), CosError> { + self.set_encryption_with(params, false) + } + + /// [`Self::set_encryption`] with the legacy RC4-40 and RC4-128 schemes allowed. Pass it only + /// for a file that must stay readable by a reader without AES. + pub fn set_encryption_legacy(&mut self, params: &crate::crypt::NewEncryption) -> Result<(), CosError> { + self.set_encryption_with(params, true) + } + + fn set_encryption_with(&mut self, params: &crate::crypt::NewEncryption, legacy: bool) -> Result<(), CosError> { // The file identifier is part of the key; make sure it exists and keep it. let id = self.trailer.get(b"ID").map(|id| self.resolve(id)); let id0 = match id.as_deref() { @@ -426,7 +439,8 @@ impl Document { id } }; - let h = crate::crypt::create(params, &id0).map_err(|e| CosError::Security(e.to_string()))?; + let built = if legacy { crate::crypt::create_legacy(params, &id0) } else { crate::crypt::create(params, &id0) }; + let h = built.map_err(|e| CosError::Security(e.to_string()))?; let d = h.dict(); let s = |b: &[u8]| Object::String(crate::cos::PdfString { bytes: b.to_vec(), hex: true }); let mut e = Dict::new(); diff --git a/crates/pdf/src/crypt.rs b/crates/pdf/src/crypt.rs index 069627db9..fff09b7a7 100644 --- a/crates/pdf/src/crypt.rs +++ b/crates/pdf/src/crypt.rs @@ -651,9 +651,9 @@ fn key_r6(d: &EncryptDict, pw: &[u8], owner: bool) -> Result bool { + matches!(self, Algorithm::Rc4_40 | Algorithm::Rc4_128) + } +} + /// A password as [`create`] prepares it: for R2–R4 its PDFDocEncoding (characters it lacks /// dropped), at most 32 bytes; for R6 its SASLprep (RFC 4013: soft hyphens and the like removed, /// other spaces made plain, then NFKC; the password as given if SASLprep refuses it), in UTF-8, @@ -725,7 +733,23 @@ pub fn file_id(seed: &[u8; 32]) -> Vec { } /// Build the `/Encrypt` values and an authenticated (owner) handler for a new encryption. +/// +/// The legacy RC4 schemes are refused, because RC4 is broken (`RFC 7465`) and a file written with +/// it protects nothing: [`create_legacy`] is the explicit way to write them. pub fn create(params: &NewEncryption, id0: &[u8]) -> Result { + if params.algorithm.is_legacy() { + return Err(CryptError::Unsupported("RC4 encryption is legacy (RFC 7465); use create_legacy for a file an old reader must open".into())); + } + create_any(params, id0) +} + +/// [`create`] without the RC4 gate: writes RC4-40 and RC4-128 too. For a file that must stay +/// readable by a reader without AES, and for tests that read and write every algorithm. +pub fn create_legacy(params: &NewEncryption, id0: &[u8]) -> Result { + create_any(params, id0) +} + +fn create_any(params: &NewEncryption, id0: &[u8]) -> Result { // R2 reserves bits 7-32; later revisions reserve bits 7-8 and 13-32. // Reserved bits must be 1, and bits 1-2 must be 0 (§7.6.4.2). let reserved = if params.algorithm == Algorithm::Rc4_40 { !0x3F } else { !0xFFF | 0xC0 }; diff --git a/crates/pdf/src/crypt/tests.rs b/crates/pdf/src/crypt/tests.rs index 04bb7c61e..59fc20e44 100644 --- a/crates/pdf/src/crypt/tests.rs +++ b/crates/pdf/src/crypt/tests.rs @@ -52,13 +52,35 @@ fn aes_padding_is_added_and_tolerantly_removed() { const ID: &[u8] = b"0123456789abcdef"; fn new(alg: Algorithm, user: &str, owner: &str, perms: i32) -> SecurityHandler { - create( + // These tests read and write every algorithm, RC4 included. + create_legacy( &NewEncryption { algorithm: alg, user_password: user, owner_password: owner, permissions: perms, encrypt_metadata: true, seed: [42; 32] }, ID, ) .unwrap() } +/// New files are not written with RC4: `create` refuses it, and the explicitly named +/// `create_legacy` is the only way to write it. +#[test] +fn create_refuses_rc4_unless_asked() { + let params = |algorithm| NewEncryption { + algorithm, + user_password: "user", + owner_password: "owner", + permissions: -1, + encrypt_metadata: true, + seed: [42; 32], + }; + for (alg, revision) in [(Algorithm::Rc4_40, 2), (Algorithm::Rc4_128, 3)] { + assert!(matches!(create(¶ms(alg), ID), Err(CryptError::Unsupported(_))), "{alg:?}"); + assert_eq!(create_legacy(¶ms(alg), ID).unwrap().dict().r, revision, "{alg:?}"); + } + for alg in [Algorithm::Aes128, Algorithm::Aes256] { + assert!(create(¶ms(alg), ID).is_ok(), "AES is not legacy: {alg:?}"); + } +} + const ALL: [Algorithm; 4] = [Algorithm::Rc4_40, Algorithm::Rc4_128, Algorithm::Aes128, Algorithm::Aes256]; #[test] diff --git a/crates/pdf/tests/cos/bounded_writer.rs b/crates/pdf/tests/cos/bounded_writer.rs index b8fe79370..5677ca542 100644 --- a/crates/pdf/tests/cos/bounded_writer.rs +++ b/crates/pdf/tests/cos/bounded_writer.rs @@ -139,7 +139,8 @@ fn full_rewrites_preserve_graph_across_multiple_object_streams_and_evictions() { } fn protect(doc: &mut Document, algorithm: Algorithm) { - doc.set_encryption(&NewEncryption { + // Every algorithm, RC4 included, has to survive a rewrite. + doc.set_encryption_legacy(&NewEncryption { algorithm, user_password: "user", owner_password: "owner", diff --git a/crates/pdf/tests/cos/encryption.rs b/crates/pdf/tests/cos/encryption.rs index 923b04e39..2ac53cb69 100644 --- a/crates/pdf/tests/cos/encryption.rs +++ b/crates/pdf/tests/cos/encryption.rs @@ -39,9 +39,11 @@ fn fixture_with(extra: &str) -> Vec { out } +/// Every algorithm, RC4-40 and RC4-128 included: those two are the legacy schemes, which +/// `set_encryption` refuses. fn protect(alg: Algorithm, user: &str, owner: &str, permissions: i32) -> Vec { let mut doc = Document::open(Arc::new(fixture())).unwrap(); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm: alg, user_password: user, owner_password: owner, @@ -287,7 +289,7 @@ fn signature_contents_stay_unencrypted() { c.set(b"Test".to_vec(), Object::Array(vec![Object::Ref(sig), Object::Ref(untyped), Object::Ref(note)])) }) .unwrap(); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm: alg, user_password: "", owner_password: "owner", @@ -423,7 +425,7 @@ fn an_earlier_revision_opens_with_the_documents_own_credentials() { // Another file, encrypted with other keys: the document's key is no use, and it says so. let other = { let mut doc = Document::open(Arc::new(fixture())).unwrap(); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm: alg, user_password: "user", owner_password: "owner", @@ -444,7 +446,7 @@ fn an_earlier_revision_opens_with_the_documents_own_credentials() { /// instead of as an indirect object. fn protect_inline(alg: Algorithm, user: &str, owner: &str) -> Vec { let mut doc = Document::open(Arc::new(fixture())).unwrap(); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm: alg, user_password: user, owner_password: owner, diff --git a/crates/pdf/tests/cos/file_identifier.rs b/crates/pdf/tests/cos/file_identifier.rs index 551db4ebd..5ca0f0f54 100644 --- a/crates/pdf/tests/cos/file_identifier.rs +++ b/crates/pdf/tests/cos/file_identifier.rs @@ -122,7 +122,8 @@ fn caller_entropy_makes_the_new_identifier_reproducible() { fn an_encrypted_rewrite_keeps_the_first_identifier_string_its_key_depends_on() { for algorithm in [Algorithm::Rc4_128, Algorithm::Aes128, Algorithm::Aes256] { let mut doc = reopen(source(true)); - doc.set_encryption(&NewEncryption { + // The loop covers RC4-128, which `set_encryption` refuses. + doc.set_encryption_legacy(&NewEncryption { algorithm, user_password: "user", owner_password: "owner", @@ -160,7 +161,7 @@ fn unusable_ids() -> Vec<(&'static str, Option)> { /// An encrypted file whose key is derived from the empty string, with its trailer `/ID` replaced by `id`. fn protected_with_id(algorithm: Algorithm, id: Option) -> Vec { let mut doc = reopen(source_with(Some("/ID [<> <>]"))); - doc.set_encryption(&NewEncryption { + doc.set_encryption_legacy(&NewEncryption { algorithm, user_password: "user", owner_password: "owner", diff --git a/crates/pdf/tests/crypt/permissions_revision.rs b/crates/pdf/tests/crypt/permissions_revision.rs index 06a5a5b98..a58868637 100644 --- a/crates/pdf/tests/crypt/permissions_revision.rs +++ b/crates/pdf/tests/crypt/permissions_revision.rs @@ -3,7 +3,7 @@ //! //! Bit n has the mask `1 << (n - 1)`. Readers use bits 3 to 6 and 9 to 12 and ignore the rest. -use craft_pdf::crypt::{Algorithm, Auth, NewEncryption, Permissions, SecurityHandler, create}; +use craft_pdf::crypt::{Algorithm, Auth, NewEncryption, Permissions, SecurityHandler, create_legacy}; const ID: &[u8] = b"0123456789abcdef"; const T: bool = true; @@ -18,7 +18,7 @@ fn cells(p: &Permissions) -> [bool; 8] { /// Creates a file with `flags` at revision `revision`, opens it with the user password, and /// returns the user's permissions. fn user_view(alg: Algorithm, revision: i64, flags: i32) -> Permissions { - let made = create( + let made = create_legacy( &NewEncryption { algorithm: alg, user_password: "user", owner_password: "owner", permissions: flags, encrypt_metadata: true, seed: [42; 32] }, ID, ) @@ -56,7 +56,7 @@ fn revision_three_rows_hold_through_a_created_and_reopened_file() { #[test] fn owner_access_gives_every_flag() { - let made = create( + let made = create_legacy( &NewEncryption { algorithm: Algorithm::Rc4_128, user_password: "user", diff --git a/crates/pdf/tests/crypt/proptests.rs b/crates/pdf/tests/crypt/proptests.rs index a81808ab9..7b9ede4d3 100644 --- a/crates/pdf/tests/crypt/proptests.rs +++ b/crates/pdf/tests/crypt/proptests.rs @@ -19,7 +19,7 @@ use craft_pdf::crypt::{ Algorithm, Auth, CryptError, Method, NewEncryption, PADDING, SecurityHandler, StreamKind, aes_cbc_decrypt, aes_cbc_encrypt, create, - effective_password, pdfdoc_byte, rc4, + create_legacy, effective_password, pdfdoc_byte, rc4, }; use proptest::prelude::*; @@ -92,8 +92,12 @@ fn expected_auth(algorithm: Algorithm, user_target: &[u8], owner_target: &[u8], } fn make(algorithm: Algorithm, user: &str, owner: &str) -> SecurityHandler { - create(&NewEncryption { algorithm, user_password: user, owner_password: owner, permissions: -1, encrypt_metadata: true, seed: [0x5e; 32] }, ID) - .unwrap() + // These properties read and write every algorithm, RC4 included. + create_legacy( + &NewEncryption { algorithm, user_password: user, owner_password: owner, permissions: -1, encrypt_metadata: true, seed: [0x5e; 32] }, + ID, + ) + .unwrap() } proptest! { @@ -457,7 +461,7 @@ proptest! { } // Revisions below 4 always encrypt metadata, whatever the caller asked for. - let legacy = create(&NewEncryption { algorithm: Algorithm::Rc4_128, user_password: "user", owner_password: "owner", permissions: -1, encrypt_metadata: false, seed: [0x5e; 32] }, ID).unwrap(); + let legacy = create_legacy(&NewEncryption { algorithm: Algorithm::Rc4_128, user_password: "user", owner_password: "owner", permissions: -1, encrypt_metadata: false, seed: [0x5e; 32] }, ID).unwrap(); prop_assert!(legacy.dict().encrypt_metadata && legacy.encrypts_metadata(), "V<4 always encrypts metadata, as create() documents"); } From 81e1dcec5aa97361c0f6c10f39a0fb3ef2779381 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 12:46:47 -0500 Subject: [PATCH 12/29] craft-dwg: refuse to convert without a worker instead of running acadrust here The bridge fell back to decoding in the calling process whenever no worker was configured, or the worker failed to start, which is exactly the case the audit called out: acadrust allocates from sizes a hostile file declares, and an allocation failure or stack overflow ends the process. Conversions are now refused without a worker, a worker that cannot start is an error, and allow_in_process() is the explicit opt-in for input known to be trusted. --- crates/dwg/README.md | 4 ++- crates/dwg/src/lib.rs | 12 ++++++- crates/dwg/src/worker.rs | 73 +++++++++++++++++++++++++++++++++------- 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/crates/dwg/README.md b/crates/dwg/README.md index ed52bdcfc..80088a89e 100644 --- a/crates/dwg/README.md +++ b/crates/dwg/README.md @@ -11,7 +11,9 @@ allocation failure inside the decoder. On native applications, call `use_own_wor startup and call `serve_if_asked()` before normal argument handling. Subsequent `decode` and `encode_*` calls then run in a child process with a 30-minute timeout; a crash or stack overflow is contained to that child. The worker has no operating-system memory limit yet. Without a -configured worker, conversions run in the current process. +configured worker, conversions are refused rather than run in the current process: call +`allow_in_process()` only for input that is known to be trusted (tests, or bytes this process +produced itself). Coordinates, once a worker is configured but cannot start, also fail closed. The bridge is not built for `wasm32` or FreeBSD/aarch64. Those targets retain the public API and return a clear unsupported-build error. The `acadrust` 0.6.6 dependency is used unmodified under diff --git a/crates/dwg/src/lib.rs b/crates/dwg/src/lib.rs index 2db7afeae..05c1a673c 100644 --- a/crates/dwg/src/lib.rs +++ b/crates/dwg/src/lib.rs @@ -21,6 +21,11 @@ //! These bound the input- and output-size variants. A small file that declares a huge internal //! table or section can still make the dependency allocate before any size is known, so the app //! runs both directions in a child process ([`worker`]): what that kills is the child. +//! +//! Isolation is not optional for untrusted input. With no worker configured the conversion is +//! refused rather than run here, and a worker that cannot start is an error, so a missing worker +//! never turns into decoding a hostile file in the caller's process. [`worker::allow_in_process`] +//! is the explicit exception, for input the caller knows is trusted. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] #![forbid(unsafe_code)] @@ -65,7 +70,10 @@ fn mb(n: u64) -> String { #[cfg(not(no_dwg_bridge))] pub mod worker; #[cfg(not(no_dwg_bridge))] -pub use worker::{WORKER_ARG, crash_worker_for_test, decode, encode_as, encode_kept_noted, encode_noted, serve_if_asked, use_own_worker, use_worker}; +pub use worker::{ + WORKER_ARG, allow_in_process, crash_worker_for_test, decode, encode_as, encode_kept_noted, encode_noted, serve_if_asked, use_own_worker, + use_worker, +}; #[cfg(not(no_dwg_bridge))] mod proxy_transport; @@ -1569,6 +1577,8 @@ pub fn serve_if_asked() -> Option { #[cfg(no_dwg_bridge)] pub fn use_own_worker() {} #[cfg(no_dwg_bridge)] +pub fn allow_in_process() {} +#[cfg(no_dwg_bridge)] pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec, Vec), String> { dwg_to_dxf_noted(bytes, limits) } diff --git a/crates/dwg/src/worker.rs b/crates/dwg/src/worker.rs index 0c273257b..1d16f5701 100644 --- a/crates/dwg/src/worker.rs +++ b/crates/dwg/src/worker.rs @@ -6,9 +6,11 @@ //! that reads one request from standard input and writes one reply to standard output. A file that //! kills the worker then costs that one open or save, and every other open drawing stays safe. //! -//! Without a worker (libraries, tests, a build that can't start one) [`decode`] and [`encode_as`] -//! run in the calling process, as they always did. There's no memory limit on the worker yet: that -//! takes per-platform system calls, which `unsafe_code = "forbid"` keeps out of this crate. +//! Without a worker [`decode`] and [`encode_as`] refuse rather than run the conversion in the +//! calling process, because the third-party code is the part that can end a process. +//! [`allow_in_process`] is the explicit exception, for input the caller knows is trusted (tests, or +//! bytes this process produced itself). There's no memory limit on the worker yet: that takes +//! per-platform system calls, which `unsafe_code = "forbid"` keeps out of this crate. //! //! A reply comes from a process that just handled a possibly hostile file, so it is parsed as //! untrusted input. @@ -18,6 +20,7 @@ use std::io::{Read, Write}; use std::path::PathBuf; use std::process::{Command, Stdio}; use std::sync::Mutex; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{RecvTimeoutError, channel}; use std::time::Duration; @@ -38,6 +41,9 @@ const MAX_NOTES_BYTES: u64 = 64 << 20; const MAX_DWG_REPLY: u64 = 2 << 30; /// Most bytes one request holds: the largest input, whichever the direction, and its header. const MAX_REQUEST: u64 = (2 << 30) + 256; +/// What a conversion says when it has no worker and is not allowed to run here. +const NO_WORKER: &str = "DWG: no worker is configured, so this file was not converted; \ + call use_worker or use_own_worker at startup, or allow_in_process for input that is trusted"; struct Worker { program: PathBuf, @@ -45,6 +51,8 @@ struct Worker { } static WORKER: Mutex> = Mutex::new(None); +/// Whether this process may run the conversion itself, set only by [`allow_in_process`]. +static IN_PROCESS: AtomicBool = AtomicBool::new(false); /// Start `program args` as the worker from now on. `program` must call [`serve_if_asked`]. pub fn use_worker(program: PathBuf, args: Vec) { @@ -62,12 +70,27 @@ pub fn use_own_worker() { } } +/// Let [`decode`] and [`encode_as`] run the conversion in this process when no worker is set. +/// +/// This opts out of the isolation the crate exists for: the third-party code allocates from sizes +/// a hostile file declares, and a failed allocation ends the process. Call it for input the caller +/// knows is trusted (tests, or bytes this process produced), never for a file that arrived from +/// outside. +pub fn allow_in_process() { + IN_PROCESS.store(true, Ordering::Relaxed); +} + +fn in_process_allowed() -> bool { + IN_PROCESS.load(Ordering::Relaxed) +} + /// Serve one request and return the exit code if this process was started as a worker. pub fn serve_if_asked() -> Option { (std::env::args_os().nth(1)? == WORKER_ARG).then(|| serve(std::io::stdin().lock(), std::io::stdout().lock())) } -/// [`super::dwg_to_dxf_noted`] in the worker, when there is one. +/// [`super::dwg_to_dxf_noted`] in the worker. With no worker configured this is refused unless +/// [`allow_in_process`] was called; a configured worker that cannot start is an error. pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec, Vec), String> { // Before starting a process, and so the same limit holds with or without one. if bytes.len() > limits.max_dwg_bytes { @@ -76,17 +99,19 @@ pub fn decode(bytes: &[u8], limits: Limits) -> Result<(Vec, Vec), St let reply_limit = (limits.max_dxf_bytes as u64).saturating_add(MAX_NOTES_BYTES); match call(&request(DECODE, limits, ""), bytes, reply_limit, "reading this file") { Some(reply) => reply?, - None => super::dwg_to_dxf_noted(bytes, limits), + None if in_process_allowed() => super::dwg_to_dxf_noted(bytes, limits), + None => Err(NO_WORKER.to_string()), } } -/// [`super::dxf_to_dwg_as`] in the worker, when there is one. +/// [`super::dxf_to_dwg_as`] in the worker. Refused without a worker unless [`allow_in_process`] +/// was called. pub fn encode_as(dxf: &[u8], version: &str) -> Result, String> { encode_noted(dxf, version).map(|(dwg, _)| dwg) } -/// [`super::dxf_to_dwg_noted`] in the worker, when there is one. What the writer left out comes -/// back as notes of `TYPEcount`. +/// [`super::dxf_to_dwg_noted`] in the worker. What the writer left out comes back as notes of +/// `TYPEcount`. pub fn encode_noted(dxf: &[u8], version: &str) -> Result<(Vec, super::Unwritten), String> { encode_kept_noted(dxf, version, None) } @@ -103,7 +128,8 @@ pub fn encode_kept_noted(dxf: &[u8], version: &str, kept: Option<&str>) -> Resul push_str(&mut header, kept.unwrap_or("")); match call(&header, dxf, MAX_DWG_REPLY, "writing this file") { Some(reply) => reply?.map(|(dwg, notes)| (dwg, unwritten_from(¬es))), - None => encode_here(dxf, version, kept), + None if in_process_allowed() => encode_here(dxf, version, kept), + None => Err(NO_WORKER.to_string()), } } @@ -147,8 +173,9 @@ fn push_str(r: &mut Vec, text: &str) { type Reply = Result<(Vec, Vec), String>; -/// Run one request in a worker. `None`: none could be started, so the caller does the work. -/// `Err`: the worker went wrong, and `doing` says at what, for the user. +/// Run one request in a worker. `None`: no worker is configured, so the caller decides what to do +/// (convert here only with [`allow_in_process`], or refuse). `Err`: a configured worker could not +/// start or went wrong, and `doing` says at what, for the user. fn call(header: &[u8], payload: &[u8], reply_limit: u64, doing: &str) -> Option> { let mut command = { let worker = WORKER.lock().ok()?; @@ -167,14 +194,15 @@ fn call(header: &[u8], payload: &[u8], reply_limit: u64, doing: &str) -> Option< let mut child = match command.spawn() { Ok(c) => c, Err(e) => { + // A worker is configured but cannot run: never fall back to decoding here. log::warn!("DWG worker: can't start it: {e}"); - return None; + return Some(Err(format!("the DWG worker could not start ({e}); nothing else is affected"))); } }; let (Some(mut to_child), Some(from_child)) = (child.stdin.take(), child.stdout.take()) else { let _ = child.kill(); let _ = child.wait(); - return None; + return Some(Err("the DWG worker could not start (no pipes); nothing else is affected".to_string())); }; let received = std::thread::scope(|scope| { // Written beside the read: a worker that replies before it has read everything, or dies, @@ -367,6 +395,25 @@ mod tests { } } + /// A conversion is refused rather than run here, and a worker that cannot start is an error: + /// neither case may turn into decoding a (possibly hostile) file in this process. + #[test] + fn a_conversion_is_refused_without_a_worker_and_never_falls_back_to_this_process() { + // No worker is configured and nothing opted in. + assert_eq!(decode(b"AC1032", Limits::DEFAULT).unwrap_err(), NO_WORKER); + assert_eq!(encode_as(b"not a dxf", "AC1032").unwrap_err(), NO_WORKER); + // Trusted input may run here, and the reader still reports a file it cannot read. + allow_in_process(); + assert!(in_process_allowed()); + assert!(decode(b"AC1032", Limits::DEFAULT).is_err()); + assert!(encode_as(b"not a dxf", "AC1032").is_err()); + // A configured worker that cannot start is an error even after the opt-in: the conversion + // never runs here instead. + use_worker(PathBuf::from("/nonexistent/craft-dwg-worker"), Vec::new()); + assert!(decode(b"AC1032", Limits::DEFAULT).unwrap_err().contains("could not start")); + assert!(encode_as(b"not a dxf", "AC1032").unwrap_err().contains("could not start")); + } + #[test] fn serving_reports_a_decode_failure_as_a_reply() { let mut request = self::request(DECODE, Limits::DEFAULT, ""); From abe4aca26252eeee32fdb63adbdf445371c3046e Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 14:44:00 -0500 Subject: [PATCH 13/29] vendor: record the hayro-syntax transmute as an audited exception --- vendor/README.craft-reader.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/vendor/README.craft-reader.md b/vendor/README.craft-reader.md index 3439657b5..8fc625fb9 100644 --- a/vendor/README.craft-reader.md +++ b/vendor/README.craft-reader.md @@ -444,3 +444,18 @@ a decoder's limit from damage). Not ported from those commits: the page-wide all refused-mask handling in `hayro-syntax` and `hayro-interpret` (0ed33990, 4aa732ea, e2464e14), which rest on the decoded-image cache of 558ffd7f (a performance change). Tests: those that came with the patches in `hayro-jbig2`, and PdfCraft's raster tests adapted in `hayro-jbig2/src/pdfcraft_tests.rs`. + +## Craft review note: the one direct `unsafe` + +`hayro-syntax/src/page.rs` holds the snapshot's only direct `unsafe` block: a +`core::mem::transmute` of `&XRef` to `&'static XRef` in the page cache. The vendored crates are +outside the workspace's `forbid(unsafe_code)`, so it compiles. Its safety argument is the field +order of the owning struct together with the `Arc` outliving the borrow. Treat it as an +audited exception, not a pattern to copy: if the caching code is re-synced, re-read that +invariant and re-run the PDF tests that drop pages while a cache entry is live. + +`hayro-syntax` also has an `unsafe` cargo feature that pulls in `flate2`, `memchr` and +SIMD-capable decoders. `craft-pdf/read` depends on it with `default-features = false, +features = ["std"]`, so the feature stays off; a review of the resolved graph should keep it +that way. The CI check for that resolved feature set is a CI change under AGENTS.md rule 11 and +is proposed separately rather than in a fix pull request. From 25abb43bde9e96dd99032b3c93cf19f12a099ca6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 14:44:05 -0500 Subject: [PATCH 14/29] craft-psd: pin that a length not fitting 32 bits is refused, not truncated --- crates/psd/src/io.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/psd/src/io.rs b/crates/psd/src/io.rs index d8aa4b51d..8ca8353b9 100644 --- a/crates/psd/src/io.rs +++ b/crates/psd/src/io.rs @@ -440,6 +440,18 @@ pub(crate) fn write_unicode_units(out: &mut Vec, units: &[u16]) { #[cfg(test)] mod tests { + /// A section length that does not fit the file's 32-bit field is refused, not truncated: + /// this is what keeps every `as u32` on a length inside a serialized file from ever wrapping, + /// and it is why an over-large PSD fails the write instead of producing a short length. + #[test] + fn a_length_that_does_not_fit_32_bits_is_refused() { + let mut out = Vec::new(); + assert!(out.put_len(u64::from(u32::MAX), false).is_ok(), "the largest 32-bit length fits"); + assert!(matches!(out.put_len(u64::from(u32::MAX) + 1, false), Err(PsdError::LimitExceeded(_)))); + // A PSB length field is 64 bits, so the same value is written there. + assert!(out.put_len(u64::from(u32::MAX) + 1, true).is_ok(), "PSB has room for it"); + } + use super::*; #[test] From fae9569718633972e168b75791a91798de850d30 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 14:53:16 -0500 Subject: [PATCH 15/29] craft-docpath: prove a multi-megabyte UNC path parses iteratively --- crates/docpath/tests/long_path.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 crates/docpath/tests/long_path.rs diff --git a/crates/docpath/tests/long_path.rs b/crates/docpath/tests/long_path.rs new file mode 100644 index 000000000..2ecf08ba1 --- /dev/null +++ b/crates/docpath/tests/long_path.rs @@ -0,0 +1,29 @@ +//! Paths long enough that a per-level recursive descent would exhaust the stack. Parsing is +//! iterative — `resolve` and `classify` reach `parse_share` at a fixed depth — so these come +//! back; a refusal is fine, a crash is not. + +use craft_docpath::{classify, resolve}; + +/// Half a megabyte of components under a UNC share: one component per level for a parser that +/// recursed on each separator. +#[test] +fn a_half_megabyte_unc_path_comes_back() { + let mut path = String::from(r"\\?\UNC\host\share"); + for _ in 0..(1 << 18) { + path.push_str(r"\d"); + } + let _ = resolve(&path, None); + let _ = classify(&path); +} + +/// Sixteen thousand nested verbatim prefixes: each level was a candidate recursive call when the +/// UNC prefixes were first reviewed. +#[test] +fn nested_verbatim_prefixes_come_back() { + let mut path = String::new(); + for _ in 0..(1 << 14) { + path.push_str(r"\\?\UNC\"); + } + let _ = resolve(&path, None); + let _ = classify(&path); +} From 30209caad802de6744a916358862bc128722deb8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 15:13:21 -0500 Subject: [PATCH 16/29] feat(gif,image-codecs): add indexed/interlaced GIF, jpeg_progressive_scans, and JP2 decode (#1358) * feat(gif,image-codecs): add indexed/interlaced GIF, jpeg_progressive_scans, and JP2 decode * chore: update Cargo.lock for craft-image-codecs jp2 dep * fix(image-codecs,svg): deref jp2 ch and re-export resvg from craft_svg::tree::draw --- Cargo.lock | 1 + crates/gif/src/write.rs | 52 ++++++ crates/image-codecs/Cargo.toml | 1 + crates/image-codecs/src/codecs.rs | 1 + crates/image-codecs/src/codecs/jp2.rs | 242 +++++++++++++++++++++++++ crates/image-codecs/src/codecs/jpeg.rs | 3 + crates/image-codecs/src/format.rs | 14 +- crates/image-codecs/src/lib.rs | 4 +- crates/image-codecs/src/options.rs | 4 + crates/svg/src/tree/draw.rs | 2 + 10 files changed, 322 insertions(+), 2 deletions(-) create mode 100644 crates/image-codecs/src/codecs/jp2.rs diff --git a/Cargo.lock b/Cargo.lock index 3d8cf4a7b..52b28f0bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1852,6 +1852,7 @@ dependencies = [ "exr", "flate2", "half", + "hayro-jpeg2000", "image", "image-webp", "jpeg-encoder 0.6.1", diff --git a/crates/gif/src/write.rs b/crates/gif/src/write.rs index bc8a4be97..259aa78f1 100644 --- a/crates/gif/src/write.rs +++ b/crates/gif/src/write.rs @@ -194,6 +194,45 @@ pub fn animation(width: u32, height: u32, frames: &[RgbaFrame<'_>], options: Opt writer.finish() } +/// Encode a single pre-quantized indexed-color frame as a GIF89a image. +/// +/// `palette` holds 1 to 256 RGB triplets; `indices` is `width * height` row-major palette indices. +/// When `interlaced` is true, rows are reordered into GIF's 4-pass interlaced order before writing. +pub fn encode_indexed( + width: u32, + height: u32, + palette: &[[u8; 3]], + indices: &[u8], + transparent: Option, + interlaced: bool, +) -> Result, Error> { + let (w, h) = dimensions(width, height)?; + let expected = usize::from(w) * usize::from(h); + if indices.len() != expected { + return Err(Error::FrameLength { index: 0, actual: indices.len(), expected }); + } + if palette.is_empty() || palette.len() > 256 { + return Err(Error::Encode("GIF palette must hold 1 to 256 entries".into())); + } + let flat_palette: Vec = palette.iter().flat_map(|c| *c).collect(); + let buffer = if interlaced { std::borrow::Cow::Owned(interlace(indices, usize::from(w))) } else { std::borrow::Cow::Borrowed(indices) }; + let mut enc = gif::Encoder::new(Vec::new(), w, h, &flat_palette).map_err(Error::from)?; + let frame = gif::Frame { width: w, height: h, buffer, transparent, interlaced, ..gif::Frame::default() }; + enc.write_frame(&frame).map_err(Error::from)?; + enc.into_inner().map_err(Error::from) +} + +/// The rows of a `width`-wide image in GIF's interlaced order: every 8th row from 0, every 8th +/// from 4, every 4th from 2, then every 2nd from 1. +fn interlace(px: &[u8], width: usize) -> Vec { + let rows: Vec<&[u8]> = px.chunks(width.max(1)).collect(); + [(0, 8), (4, 8), (2, 4), (1, 2)] + .into_iter() + .flat_map(|(start, step)| rows.iter().skip(start).step_by(step)) + .flat_map(|r| r.iter().copied()) + .collect() +} + #[cfg(test)] mod tests { #![allow(clippy::unwrap_used)] @@ -288,4 +327,17 @@ mod tests { assert_eq!(decoded.frames[0].rgba, red); assert_eq!(decoded.frames[1].rgba, blue); } + + #[test] + fn encode_indexed_validates_and_writes_gif() { + let palette = [[255, 0, 0], [0, 255, 0]]; + let indices = [0u8, 1, 1, 0, 0, 1, 1, 0, 0]; + let bytes = super::encode_indexed(3, 3, &palette, &indices, Some(1), true).unwrap(); + let info = crate::scan(&bytes, 4).unwrap(); + assert_eq!((info.width, info.height), (3, 3)); + assert_eq!(info.frames.len(), 1); + assert_eq!(info.frames[0].transparent, Some(1)); + assert!(super::encode_indexed(2, 2, &[], &[0; 4], None, false).is_err()); + assert!(super::encode_indexed(2, 2, &palette, &[0; 3], None, false).is_err()); + } } diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index b893c347a..3cf6109c5 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -33,6 +33,7 @@ craft-jpeg = { workspace = true } craft-metadata = { workspace = true } zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] } craft-heif = { workspace = true, optional = true } +hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["std", "simd"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] rayon = { workspace = true } diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index 4c4c22388..da856aaad 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -10,6 +10,7 @@ pub(crate) mod gif_frames; pub(crate) mod gif_palette; pub(crate) mod heif; pub(crate) mod iff; +pub(crate) mod jp2; pub(crate) mod jpeg; pub(crate) mod jxl; pub(crate) mod pcx; diff --git a/crates/image-codecs/src/codecs/jp2.rs b/crates/image-codecs/src/codecs/jp2.rs new file mode 100644 index 000000000..763ac9c9c --- /dev/null +++ b/crates/image-codecs/src/codecs/jp2.rs @@ -0,0 +1,242 @@ +//! JPEG 2000 (JP2 container and raw J2K codestream) decoding via `hayro-jpeg2000`. +//! +//! Decodes within `Limits`, rejecting unsupported JPX multi-layer/composition boxes and +//! checking declared SIZ/tile/palette/channel-definition bounds before pixel allocation. + +use hayro_jpeg2000::{ColorSpace, DecodeSettings, DecoderContext, Image as Jp2Image}; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Jp2; +const JP2_SIGNATURE: &[u8] = b"\0\0\0\x0cjP \r\n\x87\n"; +const MAX_PROFILE: usize = 4 * 1024 * 1024; +const MAX_TILES: u64 = 65_535; +const TILE_COMPONENT_BYTES: u64 = 256; + +/// Boxes at one JP2 nesting level. Invalid lengths stop the walk at that level. +fn boxes(b: &[u8]) -> impl Iterator { + let mut at = 0usize; + std::iter::from_fn(move || { + let header = b.get(at..at.checked_add(8)?)?; + let size = u32::from_be_bytes(header.get(..4)?.try_into().ok()?); + let (length, head) = match size { + 0 => (b.len().checked_sub(at)?, 8usize), + 1 => (usize::try_from(u64::from_be_bytes(b.get(at.checked_add(8)?..at.checked_add(16)?)?.try_into().ok()?)).ok()?, 16usize), + n => (n as usize, 8usize), + }; + if length < head { + return None; + } + let end = at.checked_add(length)?; + let data = b.get(at.checked_add(head)?..end)?; + let ty = header.get(4..8)?; + at = end; + Some((ty, data)) + }) +} + +fn unsupported_jpx_content(bytes: &[u8]) -> bool { + fn association_has_content(data: &[u8], depth: usize) -> bool { + if depth >= 16 { + return true; + } + boxes(data).any(|(ty, child)| { + matches!(ty, b"jp2c" | b"jpch" | b"jplh" | b"comp" | b"ftbl") || (ty == b"asoc" && association_has_content(child, depth + 1)) + }) + } + let mut codestreams = 0u8; + for (ty, data) in boxes(bytes) { + match ty { + b"jp2c" => { + codestreams = codestreams.saturating_add(1); + if codestreams > 1 { + return true; + } + } + b"jpch" | b"jplh" | b"comp" | b"ftbl" => return true, + b"asoc" if association_has_content(data, 0) => return true, + _ => {} + } + } + false +} + +fn codestream_size(bytes: &[u8]) -> Option<(u64, u32, u32, u64)> { + let stream = if bytes.starts_with(b"\xff\x4f") { bytes } else { boxes(bytes).find_map(|(ty, data)| (ty == b"jp2c").then_some(data))? }; + if stream.get(..4)? != b"\xff\x4f\xff\x51" { + return None; + } + let length = u16::from_be_bytes(stream.get(4..6)?.try_into().ok()?) as usize; + let count = u16::from_be_bytes(stream.get(40..42)?.try_into().ok()?) as usize; + if count == 0 || length != 38usize.checked_add(count.checked_mul(3)?)? || stream.get(4..4usize.checked_add(length)?)?.len() != length { + return None; + } + let be32 = |range: std::ops::Range| stream.get(range).and_then(|v| v.try_into().ok()).map(u32::from_be_bytes); + let width = be32(8..12)?.checked_sub(be32(16..20)?)?; + let height = be32(12..16)?.checked_sub(be32(20..24)?)?; + let tiles = |grid: u32, offset: u32, tile: u32| match (grid.checked_sub(offset), tile) { + (Some(span), 1..) => Some(u64::from(span).div_ceil(u64::from(tile))), + _ => None, + }; + let across = tiles(be32(8..12)?, be32(32..36)?, be32(24..28)?)?; + let down = tiles(be32(12..16)?, be32(36..40)?, be32(28..32)?)?; + Some((count as u64, width, height, across.checked_mul(down)?)) +} + +fn single_header_boxes(bytes: &[u8]) -> bool { + let mut headers = boxes(bytes).filter(|(ty, _)| *ty == b"jp2h"); + let (Some((_, header)), None) = (headers.next(), headers.next()) else { + return false; + }; + [b"pclr", b"cmap", b"cdef"].iter().all(|kind| boxes(header).filter(|(ty, _)| ty == kind).count() <= 1) +} + +fn channel_definitions(bytes: &[u8]) -> Option { + if bytes.starts_with(b"\xff\x4f") { + return None; + } + let header = boxes(bytes).find_map(|(ty, data)| (ty == b"jp2h").then_some(data))?; + let cdef = boxes(header).find_map(|(ty, data)| (ty == b"cdef").then_some(data))?; + let count = cdef.get(..2).and_then(|n| n.try_into().ok()).map_or(0, |n: [u8; 2]| usize::from(u16::from_be_bytes(n))); + let Some(entries) = cdef.get(2..).filter(|rest| count > 0 && rest.len() == count * 6) else { + return Some(0); + }; + let mut defs: Vec<[u16; 3]> = + entries.as_chunks::<6>().0.iter().map(|&[a, b, c, d, e, f]| [[a, b], [c, d], [e, f]].map(u16::from_be_bytes)).collect(); + defs.sort_unstable(); + let opacity = defs.last().is_some_and(|&[_, kind, association]| kind == 1 && association == 0); + let colors = defs.len() - usize::from(opacity); + let mut seen = vec![false; colors]; + let layout_ok = defs.iter().enumerate().all(|(i, &[channel, kind, association])| { + usize::from(channel) == i + && (i >= colors + || (kind == 0 + && usize::from(association).checked_sub(1).and_then(|slot| seen.get_mut(slot)).is_some_and(|s| !std::mem::replace(s, true)))) + }); + Some(if layout_ok { defs.len() as u64 } else { 0 }) +} + +fn palette_columns(bytes: &[u8], components: u64) -> Result, ()> { + if bytes.starts_with(b"\xff\x4f") { + return Ok(None); + } + let Some(header) = boxes(bytes).find_map(|(ty, data)| (ty == b"jp2h").then_some(data)) else { + return Ok(None); + }; + let Some(palette) = boxes(header).find_map(|(ty, data)| (ty == b"pclr").then_some(data)) else { + return Ok(None); + }; + let columns = palette.get(2).copied().ok_or(())?; + let depths = palette.get(3..3 + usize::from(columns)).ok_or(())?; + if columns == 0 || depths.iter().any(|&d| d & 0x80 != 0 || (d & 0x7f) + 1 > 31) { + return Err(()); + } + let Some(mapping) = boxes(header).find_map(|(ty, data)| (ty == b"cmap").then_some(data)) else { + return Ok(Some(u64::from(columns))); + }; + if mapping.len() != usize::from(columns) * 4 { + return Err(()); + } + for &[c0, c1, kind, column] in mapping.as_chunks::<4>().0 { + let valid = u64::from(u16::from_be_bytes([c0, c1])) < components + && match kind { + 0 => true, + 1 => column < columns, + _ => false, + }; + if !valid { + return Err(()); + } + } + Ok(Some(u64::from(columns))) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let malformed = |msg: &str| CodecError::malformed(F, msg); + if bytes.starts_with(JP2_SIGNATURE) && unsupported_jpx_content(bytes) { + return Err(CodecError::unsupported(F, "unsupported color space or JPX content")); + } + if bytes.starts_with(JP2_SIGNATURE) && !single_header_boxes(bytes) { + return Err(malformed("invalid JP2 header boxes")); + } + let (codestream_channels, declared_w, declared_h, tile_count) = codestream_size(bytes).ok_or_else(|| malformed("invalid codestream SIZ"))?; + if tile_count == 0 || tile_count > MAX_TILES { + return Err(malformed("invalid tile count")); + } + let max_alloc = limits.max_alloc; + let declared_pixels = u64::from(declared_w).checked_mul(u64::from(declared_h)); + if declared_w == 0 + || declared_h == 0 + || declared_w > limits.max_width + || declared_h > limits.max_height + || declared_pixels.is_none_or(|n| n > limits.max_pixels) + || declared_pixels.and_then(|n| n.checked_mul(codestream_channels)).and_then(|n| n.checked_mul(4)).is_none_or(|n| n > max_alloc) + || tile_count.checked_mul(codestream_channels).and_then(|n| n.checked_mul(TILE_COMPONENT_BYTES)).is_none_or(|n| n > max_alloc) + { + return Err(CodecError::LimitExceeded(format!("dimensions {declared_w}x{declared_h} exceed decode limits"))); + } + let header = boxes(bytes).find_map(|(ty, data)| (ty == b"jp2h").then_some(data)).unwrap_or_default(); + if boxes(header).any(|(ty, data)| ty == b"colr" && data.len() > MAX_PROFILE + 3) { + return Err(CodecError::unsupported(F, "ICC profile exceeds 4 MiB limit")); + } + let palette = palette_columns(bytes, codestream_channels).map_err(|()| malformed("invalid JP2 palette"))?; + let palette_rows = boxes(header).find_map(|(ty, data)| (ty == b"pclr").then(|| data.get(..2)).flatten()); + if let (Some(columns), Some(&[r0, r1])) = (palette, palette_rows) + && u64::from(u16::from_be_bytes([r0, r1])) * (columns * 8 + 24) > max_alloc + { + return Err(CodecError::LimitExceeded("JP2 palette exceeds max_alloc".into())); + } + let image = Jp2Image::new(bytes, &DecodeSettings::default()).map_err(|e| malformed(&format!("{e:?}")))?; + let (w, h) = (image.width(), image.height()); + let color = image.color_space(); + let (channels, icc) = match color { + ColorSpace::Gray => (1u8, None), + ColorSpace::RGB => (3u8, None), + ColorSpace::CMYK => (4u8, None), + ColorSpace::Icc { num_channels: ch @ (1 | 3 | 4), profile } if profile.len() <= MAX_PROFILE => (*ch, Some(profile.to_vec())), + _ => return Err(CodecError::unsupported(F, "unsupported JPEG 2000 color space")), + }; + let has_alpha = image.has_alpha(); + let stride = u64::from(channels) + u64::from(has_alpha); + if palette.is_some_and(|columns| columns != stride) { + return Err(malformed("JP2 palette columns do not match output channels")); + } + if channel_definitions(bytes).is_some_and(|count| count != stride || count != palette.unwrap_or(codestream_channels)) { + return Err(malformed("JP2 channel definitions do not match output channels")); + } + let layout = match (channels, has_alpha) { + (1, false) => ChannelLayout::Gray, + (1, true) => ChannelLayout::GrayA, + (3, false) => ChannelLayout::Rgb, + (3, true) => ChannelLayout::Rgba, + (4, false) => ChannelLayout::Cmyk, + (4, true) => ChannelLayout::CmykA, + _ => return Err(CodecError::unsupported(F, "unsupported channel count")), + }; + limits.check(w, h, layout, SampleType::U8)?; + let plane_channels = codestream_channels.max(stride).max(palette.unwrap_or(0)); + let pixels = u64::from(w).checked_mul(u64::from(h)).ok_or_else(|| CodecError::LimitExceeded("pixel count overflow".into()))?; + let size = pixels.checked_mul(stride).ok_or_else(|| CodecError::LimitExceeded("sample count overflow".into()))?; + let plane_bytes = + pixels.checked_mul(plane_channels).and_then(|n| n.checked_mul(4)).ok_or_else(|| CodecError::LimitExceeded("plane overflow".into()))?; + let scratch_bytes = plane_bytes + .checked_mul(2) + .and_then(|n| n.checked_add(pixels.checked_mul(8)?)) + .ok_or_else(|| CodecError::LimitExceeded("scratch overflow".into()))?; + let working = size + .checked_add(plane_bytes) + .and_then(|n| n.checked_add(scratch_bytes)) + .ok_or_else(|| CodecError::LimitExceeded("memory overflow".into()))?; + if working > max_alloc { + return Err(CodecError::LimitExceeded(format!("working set {working} exceeds max_alloc {max_alloc}"))); + } + let len = usize::try_from(size).map_err(|_| CodecError::LimitExceeded("buffer does not fit in memory".into()))?; + let mut samples = vec![0u8; len]; + image.decode_into(&mut samples, &mut DecoderContext::default()).map_err(|e| malformed(&format!("{e:?}")))?; + let mut out = Image::from_raw(w, h, layout, SampleType::U8, samples)?; + out.icc = icc; + Ok(out) +} diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index 173b4b740..bcd86ad5a 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -479,6 +479,9 @@ pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result "Cineon", Format::Dicom => "DICOM", Format::Iff => "Amiga IFF", + Format::Jp2 => "JPEG 2000", } } @@ -201,6 +206,7 @@ impl Format { Format::Cineon => &["cin"], Format::Dicom => &["dcm", "dicom", "dc3"], Format::Iff => &["iff", "lbm", "ilbm"], + Format::Jp2 => &["jp2", "j2k", "j2c", "jpf", "jpx"], } } @@ -229,6 +235,7 @@ impl Format { Format::Cineon => "image/x-cin", Format::Dicom => "application/dicom", Format::Iff => "image/x-iff", + Format::Jp2 => "image/jp2", } } @@ -303,6 +310,7 @@ pub fn caps(format: Format) -> FormatCaps { Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base }, } } @@ -320,6 +328,10 @@ pub fn detect(bytes: &[u8]) -> Option { if b.starts_with(&[0xFF, 0x0A]) || b.starts_with(b"\0\0\0\x0cJXL \r\n\x87\n") { return Some(Format::Jxl); } + // JPEG 2000 container signature box or raw J2K codestream (SOC + SIZ). + if b.starts_with(b"\0\0\0\x0cjP \r\n\x87\n") || b.starts_with(b"\xff\x4f\xff\x51") { + return Some(Format::Jp2); + } if b.starts_with(b"II*\0") || b.starts_with(b"MM\0*") || b.starts_with(b"II+\0") || b.starts_with(b"MM\0+") { return Some(Format::Tiff); } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index b4358bba1..50292cbf4 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -61,7 +61,7 @@ pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_ pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; -use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -102,6 +102,7 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::Iff => iff::decode(bytes, l), Format::OpenExr => exr::decode(bytes, l), Format::Heif => heif::decode(bytes, l, opts.keep_orientation), + Format::Jp2 => jp2::decode(bytes, l), Format::Jxl => Err(CodecError::unsupported(format, "JPEG XL reading is not yet available; export is supported")), Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::decode(format, bytes, l), }?; @@ -446,6 +447,7 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result iff::encode(image, plan), Format::OpenExr => exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), + Format::Jp2 => Err(CodecError::unsupported(format, "JPEG 2000 is import-only; save as PNG or TIFF instead")), Format::Jxl => jxl::encode(image, plan), Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => { via_image::encode(format, image, plan, opts) diff --git a/crates/image-codecs/src/options.rs b/crates/image-codecs/src/options.rs index 9674b4edf..ed96b8733 100644 --- a/crates/image-codecs/src/options.rs +++ b/crates/image-codecs/src/options.rs @@ -299,6 +299,9 @@ pub struct EncodeOptions { pub jpeg_chroma_subsampling: bool, /// JPEG scan layout (baseline, baseline optimized or progressive). pub jpeg_format: JpegFormat, + /// Optional number of progressive JPEG scans (typically 3..=5). Ignored unless + /// `jpeg_format` is [`JpegFormat::Progressive`]. + pub jpeg_progressive_scans: Option, pub png_compression: PngCompression, /// Write Adam7-interlaced PNG. pub png_interlaced: bool, @@ -336,6 +339,7 @@ impl Default for EncodeOptions { jpeg_quality: 90, jpeg_chroma_subsampling: true, jpeg_format: JpegFormat::Baseline, + jpeg_progressive_scans: None, png_compression: PngCompression::Default, png_interlaced: false, png_stereo: None, diff --git a/crates/svg/src/tree/draw.rs b/crates/svg/src/tree/draw.rs index a7bdfd80f..f83653284 100644 --- a/crates/svg/src/tree/draw.rs +++ b/crates/svg/src/tree/draw.rs @@ -24,6 +24,8 @@ use std::collections::HashMap; use usvg::Transform; +#[cfg(feature = "tree-render")] +pub use resvg; #[cfg(feature = "tree-render")] pub use resvg::tiny_skia; From acaa353f29f61e38b782dd90f304ea9387c9da5a Mon Sep 17 00:00:00 2001 From: Dylan Taylor Date: Sat, 14 Nov 2026 15:28:11 -0500 Subject: [PATCH 17/29] chore: trigger CI re-run From f215740d1dab55ef8aa1a0fa69bf30bbdffb1a87 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 20:44:52 +0000 Subject: [PATCH 18/29] Add shared XFL document schema model --- Cargo.lock | 1 + crates/xfl/Cargo.toml | 1 + crates/xfl/src/document.rs | 414 +++++++++++++++++++++++++++++++++++++ crates/xfl/src/lib.rs | 2 + 4 files changed, 418 insertions(+) create mode 100644 crates/xfl/src/document.rs diff --git a/Cargo.lock b/Cargo.lock index 3d8cf4a7b..651fdf49b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2799,6 +2799,7 @@ dependencies = [ name = "craft-xfl" version = "0.1.0" dependencies = [ + "craft-xml", "miniz_oxide 0.8.9", "thiserror 2.0.21", ] diff --git a/crates/xfl/Cargo.toml b/crates/xfl/Cargo.toml index ddf2b3a36..9c8a40e3c 100644 --- a/crates/xfl/Cargo.toml +++ b/crates/xfl/Cargo.toml @@ -11,5 +11,6 @@ repository.workspace = true workspace = true [dependencies] +craft-xml = { workspace = true } miniz_oxide = "0.8" thiserror = { workspace = true } diff --git a/crates/xfl/src/document.rs b/crates/xfl/src/document.rs new file mode 100644 index 000000000..f3f3194d8 --- /dev/null +++ b/crates/xfl/src/document.rs @@ -0,0 +1,414 @@ +//! Neutral views of the native XFL document schema. +//! +//! This layer recognizes the document, stage, scene, layer and keyframe fields defined by XFL. +//! It does not construct a consumer's animation model. More detailed payloads remain available +//! through [`Document::root`] for the application adapter. +use craft_xml::{Doctype, El, Error as XmlError, Limits, Options, UnknownEntity}; + +/// Maximum bytes of XML in one XFL document or library item. +pub const MAX_XML_BYTES: usize = 256 * 1024 * 1024; +/// Maximum depth accepted by AniCraft's existing XFL XML adapter (root depth is one). +pub const MAX_XML_DEPTH: usize = 257; +/// Maximum XFL document elements accepted by the existing adapter. +pub const MAX_XML_ELEMENTS: usize = 4_000_000; + +/// An XFL `DOMDocument.xml` tree plus typed, zero-copy views over its native structure. +#[derive(Clone, Debug)] +pub struct Document { + root: El, +} + +/// Stage metadata stored on the root `DOMDocument` element. +#[derive(Clone, Copy, Debug)] +pub struct Stage<'a> { + pub width: Option, + pub height: Option, + pub frame_rate: Option, + pub background_color: Option<&'a str>, +} + +/// Bounds used when projecting native schema fields into a compact model. +#[derive(Clone, Copy, Debug)] +pub struct ModelLimits { + pub max_timelines: usize, + pub max_layers_per_timeline: usize, +} + +/// Compact XFL metadata that does not retain or duplicate the parsed XML tree. +#[derive(Clone, Debug, PartialEq)] +pub struct DocumentModel { + pub stage: StageModel, + pub timelines: Vec, + /// Present for a library `DOMSymbolItem`; absent for normal `DOMDocument` files. + pub symbol_timeline: Option, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct StageModel { + pub width: Option, + pub height: Option, + pub frame_rate: Option, + pub has_background_color: bool, +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct TimelineModel { + pub layers: Vec, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum LayerType { + #[default] + Normal, + Folder, + Guide, + Mask, + Other, +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct LayerModel { + pub layer_type: LayerType, + pub parent_layer_index: Option, + pub visible: Option, + pub locked: Option, + pub outline: Option, + pub height_multiplier: Option, + pub frames: Vec, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum TweenType { + Motion, + Shape, + MotionObject, + #[default] + Other, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct FrameModel { + pub index: Option, + pub duration: Option, + pub is_comment: bool, + pub tween_type: TweenType, +} + +/// A timeline (`DOMTimeline`) listed under `DOMDocument/timelines`. +#[derive(Clone, Copy, Debug)] +pub struct Timeline<'a> { + element: &'a El, +} + +/// A native `DOMLayer` element. +#[derive(Clone, Copy, Debug)] +pub struct Layer<'a> { + element: &'a El, +} + +/// A native `DOMFrame` element. +#[derive(Clone, Copy, Debug)] +pub struct Frame<'a> { + element: &'a El, +} + +fn attr_last<'a>(element: &'a El, name: &str) -> Option<&'a str> { + element.attrs.iter().rev().find(|(key, _)| key == name).map(|(_, value)| value.as_str()) +} + +fn number_f32(element: &El, name: &str) -> Option { + attr_last(element, name).and_then(|value| value.trim().parse::().ok()).filter(|value| value.is_finite()) +} + +fn number_u32(element: &El, name: &str) -> Option { + attr_last(element, name).and_then(|value| value.trim().parse::().ok()) +} + +fn boolean(element: &El, name: &str) -> Option { + attr_last(element, name).map(|value| value.trim() == "true") +} + +impl Document { + /// Parse XFL XML with AniCraft's existing size, depth, element, attribute, entity, doctype + /// and malformed-end-tag behavior. The shared XML parser owns byte parsing; this crate owns + /// the XFL-specific limits and schema views. + pub fn parse(bytes: &[u8]) -> Result { + let defaults = Limits::DEFAULT; + let limits = Limits { + max_input: MAX_XML_BYTES, + max_depth: MAX_XML_DEPTH, + max_nodes: MAX_XML_ELEMENTS.saturating_mul(2).saturating_add(1), + max_elements: MAX_XML_ELEMENTS, + max_attrs: defaults.max_attrs, + max_attrs_total: defaults.max_attrs_total, + }; + let options = + Options { limits, strict_end_tags: true, unknown_entity: UnknownEntity::Literal, doctype: Doctype::Ignore, ..Options::default() }; + Ok(Self { root: options.parse(bytes)? }) + } + + /// The lossless XML tree used by consumer adapters for schema details this view does not + /// interpret. Text and child order come directly from `craft-xml`. + pub fn root(&self) -> &El { + &self.root + } + + /// The root's local element name (normally `DOMDocument`). + pub fn root_name(&self) -> &str { + &self.root.name + } + + /// The XFL stage metadata, applying finite-number checks but leaving product defaults and + /// clamping to the consumer. + pub fn stage(&self) -> Stage<'_> { + Stage { + width: number_f32(&self.root, "width"), + height: number_f32(&self.root, "height"), + frame_rate: number_f32(&self.root, "frameRate"), + background_color: attr_last(&self.root, "backgroundColor"), + } + } + + /// Timelines in document order. Non-`DOMTimeline` extension nodes are skipped. + pub fn timelines(&self) -> impl Iterator> { + self.root + .child("timelines") + .into_iter() + .flat_map(El::elements) + .filter(|element| element.name == "DOMTimeline") + .map(|element| Timeline { element }) + } + + /// The timeline nested under `DOMSymbolItem/timeline`, if this is library symbol XML. + pub fn symbol_timeline(&self) -> Option> { + self.root.child("timeline")?.child("DOMTimeline").map(|element| Timeline { element }) + } + + /// Projects bounded schema metadata and returns the XML tree by ownership. Consumers can + /// convert that tree into their compatibility representation without keeping two full trees + /// alive during import. + pub fn into_parts(self, limits: ModelLimits) -> (DocumentModel, El) { + let stage = StageModel { + width: number_f32(&self.root, "width"), + height: number_f32(&self.root, "height"), + frame_rate: number_f32(&self.root, "frameRate"), + has_background_color: attr_last(&self.root, "backgroundColor").is_some(), + }; + let timelines = self.timelines().take(limits.max_timelines).map(|timeline| timeline.into_model(limits.max_layers_per_timeline)).collect(); + let symbol_timeline = self.symbol_timeline().map(|timeline| timeline.into_model(limits.max_layers_per_timeline)); + (DocumentModel { stage, timelines, symbol_timeline }, self.root) + } +} + +impl<'a> Timeline<'a> { + pub fn name(self) -> Option<&'a str> { + attr_last(self.element, "name") + } + + /// Raw timeline XML for application-specific payload conversion. + pub fn element(self) -> &'a El { + self.element + } + + /// Layers in native order. Non-`DOMLayer` extension nodes are skipped. + pub fn layers(self) -> impl Iterator> { + self.element.child("layers").into_iter().flat_map(El::elements).filter(|element| element.name == "DOMLayer").map(|element| Layer { element }) + } + + fn into_model(self, max_layers: usize) -> TimelineModel { + TimelineModel { layers: self.layers().take(max_layers).map(Layer::into_model).collect() } + } +} + +impl<'a> Layer<'a> { + pub fn name(self) -> Option<&'a str> { + attr_last(self.element, "name") + } + + pub fn layer_type(self) -> Option<&'a str> { + attr_last(self.element, "layerType") + } + + pub fn parent_layer_index(self) -> Option { + number_u32(self.element, "parentLayerIndex") + } + + pub fn visible(self) -> Option { + boolean(self.element, "visible") + } + + pub fn locked(self) -> Option { + boolean(self.element, "locked") + } + + pub fn outline(self) -> Option { + boolean(self.element, "outline") + } + + pub fn color(self) -> Option<&'a str> { + attr_last(self.element, "color") + } + + pub fn height_multiplier(self) -> Option { + number_u32(self.element, "heightMultiplier") + } + + /// Frames in native order. Non-`DOMFrame` extension nodes are skipped. + pub fn frames(self) -> impl Iterator> { + self.element.child("frames").into_iter().flat_map(El::elements).filter(|element| element.name == "DOMFrame").map(|element| Frame { element }) + } + + fn into_model(self) -> LayerModel { + let layer_type = match attr_last(self.element, "layerType") { + Some("folder") => LayerType::Folder, + Some("guide") => LayerType::Guide, + Some("mask") => LayerType::Mask, + Some("normal") | None => LayerType::Normal, + Some(_) => LayerType::Other, + }; + LayerModel { + layer_type, + parent_layer_index: number_u32(self.element, "parentLayerIndex"), + visible: boolean(self.element, "visible"), + locked: boolean(self.element, "locked"), + outline: boolean(self.element, "outline"), + height_multiplier: number_u32(self.element, "heightMultiplier"), + frames: self.frames().map(Frame::into_model).collect(), + } + } +} + +impl<'a> Frame<'a> { + pub fn index(self) -> Option { + number_u32(self.element, "index") + } + + pub fn duration(self) -> Option { + number_u32(self.element, "duration") + } + + pub fn label_type(self) -> Option<&'a str> { + attr_last(self.element, "labelType") + } + + pub fn name(self) -> Option<&'a str> { + attr_last(self.element, "name") + } + + pub fn tween_type(self) -> Option<&'a str> { + attr_last(self.element, "tweenType") + } + + /// Raw frame XML for application-specific shape, action, sound and tween conversion. + pub fn element(self) -> &'a El { + self.element + } + + fn into_model(self) -> FrameModel { + let tween_type = match attr_last(self.element, "tweenType") { + Some("motion") => TweenType::Motion, + Some("shape") => TweenType::Shape, + Some("motion object") => TweenType::MotionObject, + _ => TweenType::Other, + }; + FrameModel { + index: number_u32(self.element, "index"), + duration: number_u32(self.element, "duration"), + is_comment: attr_last(self.element, "labelType").is_some_and(|value| value.trim() == "comment"), + tween_type, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_stage_scene_layer_and_frame_schema_without_building_an_app_model() { + let bytes = br##"]> + + + + + + + + + + +"##; + let document = Document::parse(bytes).unwrap(); + assert_eq!(document.root_name(), "DOMDocument"); + let stage = document.stage(); + assert_eq!((stage.width, stage.height, stage.frame_rate), (Some(640.0), Some(480.0), Some(29.97))); + assert_eq!(stage.background_color, Some("#123456")); + let mut timelines = document.timelines(); + let timeline = timelines.next().unwrap(); + assert_eq!(timeline.name(), Some("Scene &custom;"), "declared entities stay literal"); + assert!(timelines.next().is_none()); + let mut layers = timeline.layers(); + let layer = layers.next().unwrap(); + assert_eq!(layer.name(), Some("Background")); + assert_eq!(layer.layer_type(), Some("normal")); + assert_eq!(layer.parent_layer_index(), Some(0)); + assert_eq!((layer.visible(), layer.locked(), layer.outline()), (Some(false), Some(true), Some(true))); + assert_eq!(layer.color(), Some("#abcdef")); + assert_eq!(layer.height_multiplier(), Some(2)); + let frame = layer.frames().next().unwrap(); + assert_eq!((frame.index(), frame.duration()), (Some(4), Some(3))); + assert_eq!((frame.name(), frame.label_type(), frame.tween_type()), (Some("Intro"), Some("name"), Some("motion"))); + assert_eq!(frame.element().child("elements").unwrap().child("Shape").unwrap().name, "Shape"); + } + + #[test] + fn uses_last_duplicate_attribute_like_the_legacy_xfl_adapter() { + let document = Document::parse(br##""##).unwrap(); + assert_eq!(document.stage().width, Some(2.0)); + } + + #[test] + fn preserves_lenient_doctype_entities_and_strict_end_tag_behavior() { + let document = Document::parse(br##"tail"##).unwrap(); + assert_eq!(document.stage().width, None); + assert!(Document::parse(b"").is_err()); + } + + #[test] + fn applies_the_existing_xfl_depth_and_attribute_budgets() { + let too_deep = format!("{}{}", "".repeat(MAX_XML_DEPTH + 1), "".repeat(MAX_XML_DEPTH + 1)); + assert!(matches!(Document::parse(too_deep.as_bytes()), Err(XmlError::TooDeep(_)))); + + let mut too_many_attrs = String::from(""); + assert!(matches!(Document::parse(too_many_attrs.as_bytes()), Err(XmlError::TooManyAttrs(_)))); + } + + #[test] + fn projects_neutral_schema_fields_and_returns_the_xml_tree_by_move() { + let document = Document::parse( + br##""##, + ) + .unwrap(); + let (model, root) = document.into_parts(ModelLimits { max_timelines: 1, max_layers_per_timeline: 1 }); + assert_eq!(model.stage, StageModel { width: Some(640.0), height: None, frame_rate: None, has_background_color: true }); + assert_eq!(model.timelines.len(), 1); + let layer = &model.timelines[0].layers[0]; + assert_eq!(layer.layer_type, LayerType::Mask); + assert_eq!(layer.visible, Some(false)); + assert_eq!(layer.frames[0], FrameModel { index: Some(7), duration: Some(2), is_comment: true, tween_type: TweenType::Shape }); + assert_eq!(root.attr("width"), Some("640")); + assert_eq!(root.child("timelines").unwrap().child("DOMTimeline").unwrap().attr("name"), Some("Scene")); + } + + #[test] + fn projects_symbol_timeline_under_the_native_symbol_wrapper() { + let document = Document::parse(b"").unwrap(); + let (model, _) = document.into_parts(ModelLimits { max_timelines: 10, max_layers_per_timeline: 10 }); + assert!(model.timelines.is_empty()); + assert_eq!(model.symbol_timeline.unwrap().layers[0].frames[0].index, Some(1)); + } +} diff --git a/crates/xfl/src/lib.rs b/crates/xfl/src/lib.rs index 83fdad40b..9fce43eac 100644 --- a/crates/xfl/src/lib.rs +++ b/crates/xfl/src/lib.rs @@ -23,6 +23,8 @@ use std::cell::Cell; use std::collections::HashMap; +/// Neutral views of the XFL document, timeline, layer and frame schema. +pub mod document; /// XFL's edge-attribute grammar, independent of any consuming application's geometry types. pub mod edges; From d6cd7836d364177661b0ae17f48a0805e83a5f32 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 16:15:44 -0500 Subject: [PATCH 19/29] feat(video,image-codecs,stream-codecs): add encode_with_key, encode_avif_hdr, and decode_ccitt_raw (#1369) --- Cargo.lock | 3 + crates/image-codecs/Cargo.toml | 5 +- crates/image-codecs/src/codecs.rs | 1 + crates/image-codecs/src/codecs/avif.rs | 211 +++++++++++++++++++++++++ crates/image-codecs/src/lib.rs | 1 + crates/stream-codecs/src/ccitt.rs | 70 ++++++++ crates/stream-codecs/src/lib.rs | 2 +- crates/video/src/av1/encode/mod.rs | 7 +- 8 files changed, 297 insertions(+), 3 deletions(-) create mode 100644 crates/image-codecs/src/codecs/avif.rs diff --git a/Cargo.lock b/Cargo.lock index 52b28f0bd..c11a134ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1846,6 +1846,7 @@ dependencies = [ name = "craft-image-codecs" version = "0.1.0" dependencies = [ + "avif-serialize", "craft-heif", "craft-jpeg", "craft-metadata", @@ -1859,6 +1860,8 @@ dependencies = [ "jxl-oxide", "png 0.18.1", "proptest", + "rav1e", + "ravif", "rayon", "serde_json", "thiserror 2.0.21", diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index 3cf6109c5..2dc9426ac 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -13,7 +13,7 @@ workspace = true [features] corpus = [] default = [] -avif = ["image/avif"] +avif = ["image/avif", "dep:ravif", "dep:rav1e", "dep:avif-serialize"] heif = ["dep:craft-heif"] [dependencies] @@ -33,6 +33,9 @@ craft-jpeg = { workspace = true } craft-metadata = { workspace = true } zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] } craft-heif = { workspace = true, optional = true } +ravif = { version = "0.13", default-features = false, optional = true } +rav1e = { version = "0.8", default-features = false, optional = true } +avif-serialize = { version = "0.8", optional = true } hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["std", "simd"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index da856aaad..0288ae22b 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -1,3 +1,4 @@ +pub(crate) mod avif; pub(crate) mod dds; pub(crate) mod dds_bc; pub(crate) mod deep_exr; diff --git a/crates/image-codecs/src/codecs/avif.rs b/crates/image-codecs/src/codecs/avif.rs new file mode 100644 index 000000000..d27112fa1 --- /dev/null +++ b/crates/image-codecs/src/codecs/avif.rs @@ -0,0 +1,211 @@ +//! AVIF encoding helpers (8/16-bit SDR via `ravif` and 10-bit 4:4:4 BT.2020 HDR via `rav1e` + `avif-serialize`). + +use crate::{CodecError, Format, Image}; + +/// Transfer function for [`encode_avif_hdr`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HdrTransfer { + Pq, + Hlg, +} + +/// Absolute luminance of SDR white in an HDR file (ITU-R BT.2408 reference white), cd/m^2. +pub const HDR_REFERENCE_WHITE_NITS: f32 = 203.0; + +#[cfg(all(feature = "avif", not(target_arch = "wasm32")))] +fn pq_encode(nits: f32) -> f32 { + const M1: f32 = 2610.0 / 16384.0; + const M2: f32 = 2523.0 / 4096.0 * 128.0; + const C1: f32 = 3424.0 / 4096.0; + const C2: f32 = 2413.0 / 4096.0 * 32.0; + const C3: f32 = 2392.0 / 4096.0 * 32.0; + let y = (nits / 10_000.0).clamp(0.0, 1.0).powf(M1); + ((C1 + C2 * y) / (1.0 + C3 * y)).powf(M2) +} + +#[cfg(all(feature = "avif", not(target_arch = "wasm32")))] +fn hlg_signal(rgb: [f32; 3]) -> [f32; 3] { + const A: f32 = 0.17883277; + const B: f32 = 0.28466892; + const C: f32 = 0.559_910_7; + rgb.map(|v| { + let s = if v.is_finite() { v.clamp(0.0, 12.0) / 12.0 } else { 0.0 }; + if s <= 1.0 / 12.0 { (3.0 * s).sqrt() } else { A * (12.0 * s - B).ln() + C } + }) +} + +/// Encode an 8-bit or 16-bit SDR `Image` as AVIF (`4:4:4`, 10-bit AV1) with `quality` (`1..=100`) +/// and `speed` (`1..=10`). +pub fn encode_avif_with_speed(img: &Image, quality: u8, speed: u8) -> Result, CodecError> { + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + { + let mut enc = + ravif::Encoder::new().with_quality(f32::from(quality.clamp(1, 100))).with_speed(speed.clamp(1, 10)).with_bit_depth(ravif::BitDepth::Ten); + let alpha_mode = if img.layout().has_alpha() { ravif::AlphaColorMode::UnassociatedClean } else { ravif::AlphaColorMode::UnassociatedDirty }; + enc = enc.with_alpha_color_mode(alpha_mode); + let w = img.width() as usize; + let h = img.height() as usize; + if let Some(s16) = img.to_u16_samples() { + let ch = img.layout().channels(); + let mut planes = Vec::with_capacity(w * h); + let mut a_plane = if img.layout().has_alpha() { Some(Vec::with_capacity(w * h)) } else { None }; + for px in s16.chunks_exact(ch) { + let (r, g, b, a) = match ch { + 1 => (px[0], px[0], px[0], u16::MAX), + 2 => (px[0], px[0], px[0], px[1]), + 3 => (px[0], px[1], px[2], u16::MAX), + _ => (px[0], px[1], px[2], px[3]), + }; + let (rf, gf, bf) = (f32::from(r) / 65535.0, f32::from(g) / 65535.0, f32::from(b) / 65535.0); + let y = 0.299 * rf + 0.587 * gf + 0.114 * bf; + let cb = (bf - y) * (0.5 / (1.0 - 0.114)) + 0.5; + let cr = (rf - y) * (0.5 / (1.0 - 0.299)) + 0.5; + let q10 = |v: f32| (v * 1023.0).round().clamp(0.0, 1023.0) as u16; + planes.push([q10(y), q10(cb), q10(cr)]); + if let Some(ap) = &mut a_plane { + ap.push(q10(f32::from(a) / 65535.0)); + } + } + let alpha = a_plane.map(Vec::into_iter); + let res = enc + .encode_raw_planes_10_bit(w, h, planes, alpha, ravif::PixelRange::Full, ravif::MatrixCoefficients::BT601) + .map_err(|e| CodecError::encode(Format::Avif, e))?; + return Ok(res.avif_file); + } + let rgba = img.to_rgba8(); + let px: Vec = rgba.as_chunks::<4>().0.iter().map(|c| ravif::RGBA8::new(c[0], c[1], c[2], c[3])).collect(); + let res = enc.encode_rgba(ravif::Img::new(&px[..], w, h)).map_err(|e| CodecError::encode(Format::Avif, e))?; + Ok(res.avif_file) + } + #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] + { + let _ = (img, quality, speed); + Err(CodecError::unsupported(Format::Avif, "AVIF encoding is not available in this build")) + } +} + +/// Encode linear BT.2020 HDR `rgb` (`width * height * 3` floats, `1.0` = `203` cd/m^2) as +/// 10-bit 4:4:4 BT.2020 PQ or HLG AVIF. +pub fn encode_avif_hdr( + width: u32, + height: u32, + rgb: &[f32], + quality: u8, + speed: u8, + exif: Option<&[u8]>, + transfer: HdrTransfer, +) -> Result, CodecError> { + let (w, h) = (width as usize, height as usize); + let n = w.checked_mul(h).ok_or_else(|| CodecError::encode(Format::Avif, "image too large"))?; + if n == 0 || n.checked_mul(3).is_none_or(|need| rgb.len() < need) { + return Err(CodecError::encode(Format::Avif, "HDR AVIF: sample buffer too short")); + } + #[cfg(all(feature = "avif", not(target_arch = "wasm32")))] + { + use rav1e::prelude::*; + const K: [f32; 3] = [0.2627, 0.6780, 0.0593]; + let mut max_nits = 0f32; + let mut sum_nits = 0f64; + let planes: Vec<[u16; 3]> = rgb + .as_chunks::<3>() + .0 + .iter() + .take(n) + .map(|c| { + let nits = [c[0], c[1], c[2]].map(|v| (if v.is_finite() { v.max(0.0) } else { 0.0 }) * HDR_REFERENCE_WHITE_NITS); + let y_nits = K[0] * nits[0] + K[1] * nits[1] + K[2] * nits[2]; + max_nits = max_nits.max(nits[0].max(nits[1]).max(nits[2])); + sum_nits += f64::from(y_nits); + let [r, g, b] = match transfer { + HdrTransfer::Pq => nits.map(pq_encode), + HdrTransfer::Hlg => hlg_signal([c[0], c[1], c[2]]), + }; + let y = K[0] * r + K[1] * g + K[2] * b; + let cb = (b - y) / (2.0 * (1.0 - K[2])) + 0.5; + let cr = (r - y) / (2.0 * (1.0 - K[0])) + 0.5; + [y, cb, cr].map(|v| (v * 1023.0).round().clamp(0.0, 1023.0) as u16) + }) + .collect(); + let q = f32::from(quality.clamp(1, 100)) / 100.0; + let x = if q >= 0.82 { + (1.0 - q) * 2.6 + } else if q > 0.25 { + 1.0 - 0.125 - q * 0.5 + } else { + 1.0 - q + }; + let quantizer = (x * 255.0).round() as usize; + let mut ec = EncoderConfig::with_speed_preset(speed.clamp(1, 10)); + ec.width = w; + ec.height = h; + ec.time_base = Rational::new(1, 1); + ec.bit_depth = 10; + ec.chroma_sampling = ChromaSampling::Cs444; + ec.pixel_range = PixelRange::Full; + ec.color_description = Some(ColorDescription { + color_primaries: ColorPrimaries::BT2020, + transfer_characteristics: match transfer { + HdrTransfer::Pq => TransferCharacteristics::SMPTE2084, + HdrTransfer::Hlg => TransferCharacteristics::HLG, + }, + matrix_coefficients: MatrixCoefficients::BT2020NCL, + }); + let cll = (max_nits.ceil().clamp(1.0, 10_000.0) as u16, (sum_nits / n as f64).ceil().clamp(1.0, 10_000.0) as u16); + ec.content_light = + (transfer == HdrTransfer::Pq).then_some(ContentLight { max_content_light_level: cll.0, max_frame_average_light_level: cll.1 }); + ec.still_picture = true; + ec.quantizer = quantizer; + ec.min_quantizer = quantizer.min(255) as u8; + ec.speed_settings.multiref = false; + ec.speed_settings.rdo_lookahead_frames = 1; + ec.speed_settings.scene_detection_mode = SceneDetectionSpeed::None; + let mut ctx: Context = Config::new().with_encoder_config(ec).new_context().map_err(|e| CodecError::encode(Format::Avif, e))?; + let mut frame = ctx.new_frame(); + { + let mut it = planes.iter(); + let mut fp = frame.planes.iter_mut(); + let (Some(py), Some(pu), Some(pv)) = (fp.next(), fp.next(), fp.next()) else { + return Err(CodecError::encode(Format::Avif, "AV1: frame has no planes")); + }; + let (mut sy, mut su, mut sv) = (py.mut_slice(Default::default()), pu.mut_slice(Default::default()), pv.mut_slice(Default::default())); + for ((ry, ru), rv) in sy.rows_iter_mut().zip(su.rows_iter_mut()).zip(sv.rows_iter_mut()).take(h) { + for ((y, u), v) in ry.iter_mut().zip(ru.iter_mut()).zip(rv.iter_mut()).take(w) { + let Some(p) = it.next() else { break }; + (*y, *u, *v) = (p[0], p[1], p[2]); + } + } + } + ctx.send_frame(frame).map_err(|e| CodecError::encode(Format::Avif, e))?; + ctx.flush(); + let mut av1 = Vec::new(); + loop { + match ctx.receive_packet() { + Ok(mut p) if p.frame_type == FrameType::KEY => av1.append(&mut p.data), + Ok(_) => {} + Err(EncoderStatus::Encoded | EncoderStatus::LimitReached) => break, + Err(e) => return Err(CodecError::encode(Format::Avif, e)), + } + } + use avif_serialize::constants as c; + let mut a = avif_serialize::Aviffy::new(); + a.set_matrix_coefficients(c::MatrixCoefficients::Bt2020Ncl) + .set_transfer_characteristics(match transfer { + HdrTransfer::Pq => c::TransferCharacteristics::Smpte2084, + HdrTransfer::Hlg => c::TransferCharacteristics::Hlg, + }) + .set_color_primaries(c::ColorPrimaries::Bt2020) + .set_full_color_range(true); + if transfer == HdrTransfer::Pq { + a.set_content_light_level(cll.0, cll.1); + } + if let Some(exif) = exif { + a.set_exif(exif.to_vec()); + } + Ok(a.to_vec(&av1, None, width, height, 10)) + } + #[cfg(not(all(feature = "avif", not(target_arch = "wasm32"))))] + { + let _ = (width, height, rgb, quality, speed, exif, transfer); + Err(CodecError::unsupported(Format::Avif, "AVIF encoding is not available in this build")) + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 50292cbf4..61109faa1 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -33,6 +33,7 @@ pub mod web; pub use crate::allocation::probe_allocation_size; pub use crate::cicp::Cicp; pub use crate::cicp::ContentLightLevel; +pub use crate::codecs::avif::{HDR_REFERENCE_WHITE_NITS, HdrTransfer, encode_avif_hdr, encode_avif_with_speed}; pub use crate::codecs::exr_cryptomatte::{cryptomatte_id, cryptomatte_key, cryptomatte_preview_color}; pub use crate::codecs::gif_frames::{AnimationFrame, MAX_GIF_FRAMES}; pub use crate::codecs::png::{ diff --git a/crates/stream-codecs/src/ccitt.rs b/crates/stream-codecs/src/ccitt.rs index 2733cf98b..c5a213cea 100644 --- a/crates/stream-codecs/src/ccitt.rs +++ b/crates/stream-codecs/src/ccitt.rs @@ -152,6 +152,76 @@ fn starts_with_end_of_line(data: &[u8]) -> bool { /// Rows of 1-bit samples filled from a decoder's pixels: `columns` a row, each row on a new byte, at /// most `rows` rows. Pixels past a row's end and rows past the last are dropped. +/// Reason a raw CCITT fax decode stopped or failed. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum CcittErrorKind { + UnexpectedEof, + Overflow, + InvalidData, +} + +impl std::fmt::Display for CcittErrorKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::UnexpectedEof => f.write_str("unexpected end of CCITT stream"), + Self::Overflow => f.write_str("CCITT stream overflow"), + Self::InvalidData => f.write_str("invalid CCITT data"), + } + } +} + +/// Result of [`decode_ccitt_raw`], including any partial rows decoded before an error. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct CcittRawOutput { + pub data: Vec, + pub rows_decoded: u32, + pub bytes_consumed: usize, + pub error: Option, +} + +/// Decodes CCITT Group 3 or Group 4 fax `data` with caller-specified `params` and `max_rows`, +/// returning packed MSB-first rows, rows decoded, input bytes consumed, and any decode error. +pub fn decode_ccitt_raw(data: &[u8], p: &CcittParams, max_rows: u32) -> Result { + if p.columns <= 0 || p.columns > i64::from(MAX_CCITT_COLUMNS) { + return Err(CodecError::Corrupt { filter: "CCITTFaxDecode", detail: format!("CCITT columns {} outside 1..={MAX_CCITT_COLUMNS}", p.columns) }); + } + let columns = u32::try_from(p.columns).map_err(|_| CodecError::Corrupt { filter: "CCITTFaxDecode", detail: "invalid CCITT columns".into() })?; + let rows = if p.rows > 0 { u32::try_from(p.rows).unwrap_or(max_rows).min(max_rows) } else { max_rows }; + let settings = hayro_ccitt::DecodeSettings { + columns, + rows, + end_of_block: p.end_of_block, + end_of_line: p.end_of_line, + rows_are_byte_aligned: p.encoded_byte_align && (p.k < 0 || !starts_with_end_of_line(data)), + encoding: match p.k { + ..0 => hayro_ccitt::EncodingMode::Group4, + 0 => hayro_ccitt::EncodingMode::Group3_1D, + k => hayro_ccitt::EncodingMode::Group3_2D { k: u32::try_from(k).unwrap_or(u32::MAX) }, + }, + invert_black: false, + }; + let mut out = Rows::new(columns as usize, rows as usize, p.black_is_1); + let (bytes_consumed, error) = match hayro_ccitt::decode(data, &mut out, &mut hayro_ccitt::DecoderContext::new(settings)) { + Ok(used) => (used, None), + Err(e) => { + let kind = match e { + hayro_ccitt::DecodeError::UnexpectedEof => CcittErrorKind::UnexpectedEof, + hayro_ccitt::DecodeError::Overflow => CcittErrorKind::Overflow, + _ => CcittErrorKind::InvalidData, + }; + let used = if out.done > 0 { + let mut drop_rows = Rows::new(columns as usize, out.done, p.black_is_1); + let retry = hayro_ccitt::DecodeSettings { rows: u32::try_from(out.done).unwrap_or(u32::MAX), ..settings }; + hayro_ccitt::decode(data, &mut drop_rows, &mut hayro_ccitt::DecoderContext::new(retry)).unwrap_or(data.len()) + } else { + 0 + }; + (used, Some(kind)) + } + }; + Ok(CcittRawOutput { data: out.out, rows_decoded: u32::try_from(out.done).unwrap_or(u32::MAX), bytes_consumed, error }) +} + pub(crate) struct Rows { pub(crate) out: Vec, row: Vec, diff --git a/crates/stream-codecs/src/lib.rs b/crates/stream-codecs/src/lib.rs index d129cacaa..ee275b2ce 100644 --- a/crates/stream-codecs/src/lib.rs +++ b/crates/stream-codecs/src/lib.rs @@ -42,7 +42,7 @@ mod mq; pub mod predictor; pub mod runlength; -pub use ccitt::{CcittParams, MAX_CCITT_COLUMNS, MAX_G4_COLUMNS, decode_ccitt_within, encode_g4}; +pub use ccitt::{CcittErrorKind, CcittParams, CcittRawOutput, MAX_CCITT_COLUMNS, MAX_G4_COLUMNS, decode_ccitt_raw, decode_ccitt_within, encode_g4}; pub use jbig2::{MAX_JBIG2_COLUMNS, MAX_JBIG2_PIXELS, SharedJbig2, decode_jbig2_within, encode_jbig2, encode_jbig2_generic, encode_jbig2_shared}; /// Parameters of the predictors and of LZW, as a PDF `/DecodeParms` dictionary gives them diff --git a/crates/video/src/av1/encode/mod.rs b/crates/video/src/av1/encode/mod.rs index 0d8e2b04f..10e88d0ad 100644 --- a/crates/video/src/av1/encode/mod.rs +++ b/crates/video/src/av1/encode/mod.rs @@ -600,8 +600,13 @@ impl Encoder { /// Encodes one frame. pub fn encode(&mut self, frame: &Frame) -> Result { + self.encode_with_key(frame, false) + } + + /// Encodes one frame, optionally forcing a keyframe regardless of `keyint`. + pub fn encode_with_key(&mut self, frame: &Frame, force_key: bool) -> Result { self.check_frame(frame)?; - let key = self.frame_num.is_multiple_of(self.cfg.keyint.max(1) as u64); + let key = force_key || self.frame_num.is_multiple_of(self.cfg.keyint.max(1) as u64); let src = self.load_source(frame); let (frame_obu, rf) = match self.cfg.rate { RateControl::ConstantQ(q) => self.encode_frame(&src, key, q.max(1)), From 2942da9a3569a809cee50bb7d6c3632793fd994a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 21:16:28 +0000 Subject: [PATCH 20/29] Model shared XFL publish settings --- crates/xfl/src/document.rs | 65 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/crates/xfl/src/document.rs b/crates/xfl/src/document.rs index f3f3194d8..32a76db40 100644 --- a/crates/xfl/src/document.rs +++ b/crates/xfl/src/document.rs @@ -4,6 +4,7 @@ //! It does not construct a consumer's animation model. More detailed payloads remain available //! through [`Document::root`] for the application adapter. use craft_xml::{Doctype, El, Error as XmlError, Limits, Options, UnknownEntity}; +use std::borrow::Cow; /// Maximum bytes of XML in one XFL document or library item. pub const MAX_XML_BYTES: usize = 256 * 1024 * 1024; @@ -27,6 +28,14 @@ pub struct Stage<'a> { pub background_color: Option<&'a str>, } +/// The script-language version and document class selected by an XFL `PublishSettings.xml`. +/// Values stay in their native string form so each consumer can apply its own domain rules. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct PublishSettings<'a> { + pub action_script_version: Option>, + pub document_class: Option>, +} + /// Bounds used when projecting native schema fields into a compact model. #[derive(Clone, Copy, Debug)] pub struct ModelLimits { @@ -128,6 +137,19 @@ fn boolean(element: &El, name: &str) -> Option { attr_last(element, name).map(|value| value.trim() == "true") } +fn trimmed_text<'a>(element: &'a El) -> Option> { + match element.text() { + Cow::Borrowed(text) => { + let trimmed = text.trim(); + (!trimmed.is_empty()).then_some(Cow::Borrowed(trimmed)) + } + Cow::Owned(text) => { + let trimmed = text.trim(); + (!trimmed.is_empty()).then(|| Cow::Owned(trimmed.to_string())) + } + } +} + impl Document { /// Parse XFL XML with AniCraft's existing size, depth, element, attribute, entity, doctype /// and malformed-end-tag behavior. The shared XML parser owns byte parsing; this crate owns @@ -158,6 +180,21 @@ impl Document { &self.root.name } + /// Reads the current publish profile, falling back to the first profile and then the root. + /// This is also used for `PublishSettings.xml` files, whose schema is separate from the main + /// `DOMDocument` tree. + pub fn publish_settings(&self) -> PublishSettings<'_> { + let profile = self + .root + .elements() + .find(|element| element.name == "flash_profile" && boolean(element, "current") == Some(true)) + .or_else(|| self.root.elements().find(|element| element.name == "flash_profile")) + .unwrap_or(&self.root); + let properties = profile.child("PublishFlashProperties").unwrap_or(profile); + let value = |name: &str| properties.child(name).and_then(trimmed_text); + PublishSettings { action_script_version: value("ActionScriptVersion"), document_class: value("DocumentClass") } + } + /// The XFL stage metadata, applying finite-number checks but leaving product defaults and /// clamping to the consumer. pub fn stage(&self) -> Stage<'_> { @@ -411,4 +448,32 @@ mod tests { assert!(model.timelines.is_empty()); assert_eq!(model.symbol_timeline.unwrap().layers[0].frames[0].index, Some(1)); } + + #[test] + fn selects_current_publish_profile_and_uses_native_value_strings() { + let document = Document::parse( + br#"2OldClass 3 com.example.Main "#, + ) + .unwrap(); + let settings = document.publish_settings(); + assert_eq!(settings.action_script_version.as_deref(), Some("3")); + assert_eq!(settings.document_class.as_deref(), Some("com.example.Main")); + } + + #[test] + fn publish_settings_fall_back_to_first_profile_then_root() { + let first = Document::parse( + br#"23"#, + ) + .unwrap(); + assert_eq!(first.publish_settings().action_script_version.as_deref(), Some("2")); + + let root = Document::parse( + br#" 3 RootClass"#, + ) + .unwrap(); + let settings = root.publish_settings(); + assert_eq!(settings.action_script_version.as_deref(), Some("3")); + assert_eq!(settings.document_class.as_deref(), Some("RootClass")); + } } From 4801a54e3c6082af7063e9f451da50b2db22a6e2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 21:23:28 +0000 Subject: [PATCH 21/29] Select XFL package roots in craft-xfl --- crates/xfl/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ crates/xfl/src/tests.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/crates/xfl/src/lib.rs b/crates/xfl/src/lib.rs index 9fce43eac..4a957111f 100644 --- a/crates/xfl/src/lib.rs +++ b/crates/xfl/src/lib.rs @@ -89,6 +89,19 @@ pub struct Archive<'a> { budget: Cell, } +/// The package root selected from an XFL archive's `DOMDocument.xml` paths. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum XflPackageRoot<'a> { + /// A document XML file is directly at the archive root. + Document, + /// The sole eligible top-level folder containing a document. + Folder(&'a str), + /// No document path was found. + Missing, + /// More than one eligible top-level folder contains a document, sorted by name. + Multiple(Vec<&'a str>), +} + const END: u32 = 0x0605_4b50; const END64: u32 = 0x0606_4b50; const LOCATOR64: u32 = 0x0706_4b50; @@ -529,6 +542,25 @@ impl<'a> Archive<'a> { self.entries.keys().map(String::as_str) } + /// Selects the package root from XFL document paths. A root-level document takes precedence; + /// otherwise only a single-level folder is eligible, with macOS metadata excluded. + pub fn xfl_package_root(&self) -> XflPackageRoot<'_> { + if self.entries.contains_key("DOMDocument.xml") { + return XflPackageRoot::Document; + } + let mut folders: Vec<&str> = self + .names() + .filter_map(|name| name.strip_suffix("/DOMDocument.xml")) + .filter(|folder| !folder.contains('/') && *folder != "__MACOSX") + .collect(); + folders.sort_unstable(); + match folders.as_slice() { + [] => XflPackageRoot::Missing, + [folder] => XflPackageRoot::Folder(folder), + _ => XflPackageRoot::Multiple(folders), + } + } + /// How many files and folder entries the archive lists. pub fn counts(&self) -> (usize, usize) { (self.entries.len(), self.folders) diff --git a/crates/xfl/src/tests.rs b/crates/xfl/src/tests.rs index ee4b2cf33..5ae0713ba 100644 --- a/crates/xfl/src/tests.rs +++ b/crates/xfl/src/tests.rs @@ -28,6 +28,32 @@ fn reads_stored_and_deflated_entries() { assert_eq!(a.read("missing").unwrap(), None); } +#[test] +fn xfl_package_root_prefers_root_then_selects_one_top_level_folder() { + let root = zip(&[("DOMDocument.xml", b"", true), ("Walk/DOMDocument.xml", b"", true)]); + let archive = Archive::open(&root).unwrap(); + assert_eq!(archive.xfl_package_root(), XflPackageRoot::Document); + + let folder = zip(&[("Walk/DOMDocument.xml", b"", true), ("Walk/LIBRARY/Clip.xml", b"", true)]); + let archive = Archive::open(&folder).unwrap(); + assert_eq!(archive.xfl_package_root(), XflPackageRoot::Folder("Walk")); +} + +#[test] +fn xfl_package_root_reports_sorted_candidates_and_ignores_metadata_and_nested_paths() { + let multiple = zip(&[ + ("Zebra/DOMDocument.xml", b"", true), + ("Apple/DOMDocument.xml", b"", true), + ("__MACOSX/DOMDocument.xml", b"not a project", true), + ]); + let archive = Archive::open(&multiple).unwrap(); + assert_eq!(archive.xfl_package_root(), XflPackageRoot::Multiple(vec!["Apple", "Zebra"])); + + let nested = zip(&[("One/Two/DOMDocument.xml", b"", true), ("__MACOSX/One/DOMDocument.xml", b"metadata", true)]); + let archive = Archive::open(&nested).unwrap(); + assert_eq!(archive.xfl_package_root(), XflPackageRoot::Missing); +} + #[test] fn the_checksum_matches_the_published_check_value() { assert_eq!(crc32(b"123456789"), 0xCBF4_3926); From 7d6e2bf38577539b30232c1b57b7734e530f7ab1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 21:59:22 +0000 Subject: [PATCH 22/29] Match prefixed XFL schema elements --- crates/xfl/src/document.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/xfl/src/document.rs b/crates/xfl/src/document.rs index 32a76db40..409c1dd4b 100644 --- a/crates/xfl/src/document.rs +++ b/crates/xfl/src/document.rs @@ -3,7 +3,7 @@ //! This layer recognizes the document, stage, scene, layer and keyframe fields defined by XFL. //! It does not construct a consumer's animation model. More detailed payloads remain available //! through [`Document::root`] for the application adapter. -use craft_xml::{Doctype, El, Error as XmlError, Limits, Options, UnknownEntity}; +use craft_xml::{Doctype, El, Error as XmlError, Limits, Local, Options, Pat, UnknownEntity}; use std::borrow::Cow; /// Maximum bytes of XML in one XFL document or library item. @@ -187,8 +187,8 @@ impl Document { let profile = self .root .elements() - .find(|element| element.name == "flash_profile" && boolean(element, "current") == Some(true)) - .or_else(|| self.root.elements().find(|element| element.name == "flash_profile")) + .find(|element| Local("flash_profile").matches(&element.name) && boolean(element, "current") == Some(true)) + .or_else(|| self.root.elements().find(|element| Local("flash_profile").matches(&element.name))) .unwrap_or(&self.root); let properties = profile.child("PublishFlashProperties").unwrap_or(profile); let value = |name: &str| properties.child(name).and_then(trimmed_text); @@ -209,16 +209,16 @@ impl Document { /// Timelines in document order. Non-`DOMTimeline` extension nodes are skipped. pub fn timelines(&self) -> impl Iterator> { self.root - .child("timelines") + .child(Local("timelines")) .into_iter() .flat_map(El::elements) - .filter(|element| element.name == "DOMTimeline") + .filter(|element| Local("DOMTimeline").matches(&element.name)) .map(|element| Timeline { element }) } /// The timeline nested under `DOMSymbolItem/timeline`, if this is library symbol XML. pub fn symbol_timeline(&self) -> Option> { - self.root.child("timeline")?.child("DOMTimeline").map(|element| Timeline { element }) + self.root.child(Local("timeline"))?.child(Local("DOMTimeline")).map(|element| Timeline { element }) } /// Projects bounded schema metadata and returns the XML tree by ownership. Consumers can @@ -249,7 +249,7 @@ impl<'a> Timeline<'a> { /// Layers in native order. Non-`DOMLayer` extension nodes are skipped. pub fn layers(self) -> impl Iterator> { - self.element.child("layers").into_iter().flat_map(El::elements).filter(|element| element.name == "DOMLayer").map(|element| Layer { element }) + self.element.child(Local("layers")).into_iter().flat_map(El::elements).filter(|element| Local("DOMLayer").matches(&element.name)).map(|element| Layer { element }) } fn into_model(self, max_layers: usize) -> TimelineModel { @@ -449,6 +449,21 @@ mod tests { assert_eq!(model.symbol_timeline.unwrap().layers[0].frames[0].index, Some(1)); } + #[test] + fn recognizes_prefixed_xfl_timeline_elements_by_local_name() { + let document = Document::parse( + b"", + ) + .unwrap(); + assert_eq!(document.timelines().next().unwrap().layers().count(), 1); + + let symbol = Document::parse( + b"", + ) + .unwrap(); + assert!(symbol.symbol_timeline().is_some()); + } + #[test] fn selects_current_publish_profile_and_uses_native_value_strings() { let document = Document::parse( From 620775e30bf4a80ff1e03989d4c1ba101a47f634 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 22:16:51 +0000 Subject: [PATCH 23/29] Format shared XFL local-name lookup --- crates/xfl/src/document.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/xfl/src/document.rs b/crates/xfl/src/document.rs index 409c1dd4b..9cf130351 100644 --- a/crates/xfl/src/document.rs +++ b/crates/xfl/src/document.rs @@ -249,7 +249,12 @@ impl<'a> Timeline<'a> { /// Layers in native order. Non-`DOMLayer` extension nodes are skipped. pub fn layers(self) -> impl Iterator> { - self.element.child(Local("layers")).into_iter().flat_map(El::elements).filter(|element| Local("DOMLayer").matches(&element.name)).map(|element| Layer { element }) + self.element + .child(Local("layers")) + .into_iter() + .flat_map(El::elements) + .filter(|element| Local("DOMLayer").matches(&element.name)) + .map(|element| Layer { element }) } fn into_model(self, max_layers: usize) -> TimelineModel { @@ -457,10 +462,8 @@ mod tests { .unwrap(); assert_eq!(document.timelines().next().unwrap().layers().count(), 1); - let symbol = Document::parse( - b"", - ) - .unwrap(); + let symbol = + Document::parse(b"").unwrap(); assert!(symbol.symbol_timeline().is_some()); } From 9a33bc229a18d02677d192e85a1ba3de8558c668 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 22:20:54 +0000 Subject: [PATCH 24/29] Cover prefixed XFL settings and frames --- crates/xfl/src/document.rs | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/crates/xfl/src/document.rs b/crates/xfl/src/document.rs index 9cf130351..8cde65513 100644 --- a/crates/xfl/src/document.rs +++ b/crates/xfl/src/document.rs @@ -190,8 +190,8 @@ impl Document { .find(|element| Local("flash_profile").matches(&element.name) && boolean(element, "current") == Some(true)) .or_else(|| self.root.elements().find(|element| Local("flash_profile").matches(&element.name))) .unwrap_or(&self.root); - let properties = profile.child("PublishFlashProperties").unwrap_or(profile); - let value = |name: &str| properties.child(name).and_then(trimmed_text); + let properties = profile.child(Local("PublishFlashProperties")).unwrap_or(profile); + let value = |name: &str| properties.child(Local(name)).and_then(trimmed_text); PublishSettings { action_script_version: value("ActionScriptVersion"), document_class: value("DocumentClass") } } @@ -297,7 +297,12 @@ impl<'a> Layer<'a> { /// Frames in native order. Non-`DOMFrame` extension nodes are skipped. pub fn frames(self) -> impl Iterator> { - self.element.child("frames").into_iter().flat_map(El::elements).filter(|element| element.name == "DOMFrame").map(|element| Frame { element }) + self.element + .child(Local("frames")) + .into_iter() + .flat_map(El::elements) + .filter(|element| Local("DOMFrame").matches(&element.name)) + .map(|element| Frame { element }) } fn into_model(self) -> LayerModel { @@ -457,16 +462,38 @@ mod tests { #[test] fn recognizes_prefixed_xfl_timeline_elements_by_local_name() { let document = Document::parse( - b"", + b"", ) .unwrap(); - assert_eq!(document.timelines().next().unwrap().layers().count(), 1); + { + let mut timelines = document.timelines(); + let timeline = timelines.next().unwrap(); + assert!(timelines.next().is_none()); + let mut layers = timeline.layers(); + let layer = layers.next().unwrap(); + assert!(layers.next().is_none()); + let frame = layer.frames().next().unwrap(); + assert_eq!((frame.index(), frame.duration()), (Some(4), Some(2))); + } + let (model, _) = document.into_parts(ModelLimits { max_timelines: 1, max_layers_per_timeline: 1 }); + assert_eq!(model.timelines[0].layers[0].frames[0].index, Some(4)); let symbol = Document::parse(b"").unwrap(); assert!(symbol.symbol_timeline().is_some()); } + #[test] + fn reads_prefixed_publish_settings_children_by_local_name() { + let document = Document::parse( + b"3com.example.Main", + ) + .unwrap(); + let settings = document.publish_settings(); + assert_eq!(settings.action_script_version.as_deref(), Some("3")); + assert_eq!(settings.document_class.as_deref(), Some("com.example.Main")); + } + #[test] fn selects_current_publish_profile_and_uses_native_value_strings() { let document = Document::parse( From c83e8ba250df957c7e12377689ae074f3ee19727 Mon Sep 17 00:00:00 2001 From: alehel Date: Sun, 15 Nov 2026 00:07:33 +0100 Subject: [PATCH 25/29] craft-video: MPEG-2 encoder: flush and timestamp fixes from review (#1133) - flush ends the GOP for average bitrate too: the budget of the pictures the GOP did not get is taken back, so flushed short GOPs no longer raise the bitrate. - A lone picture flushed before a second sets the step gets dts = pts - 1, so decoding times still increase if encoding goes on. - Presentation times whose decoding time does not fit in an i64 are refused with BadTimestamp before any state changes. - Tests: the skip range test uses an f_code that can carry its vector, and the reconstruction count is checked before comparing pictures. --- crates/video/src/mpeg2/README.md | 2 + crates/video/src/mpeg2/encode/mod.rs | 31 +++++++++-- crates/video/src/mpeg2/encode/picture.rs | 7 ++- crates/video/src/mpeg2/encode/rate.rs | 24 ++++++++ crates/video/src/mpeg2/encode/tests.rs | 71 ++++++++++++++++++++++++ crates/video/tests/mpeg2/encode.rs | 1 + 6 files changed, 128 insertions(+), 8 deletions(-) diff --git a/crates/video/src/mpeg2/README.md b/crates/video/src/mpeg2/README.md index a8502f696..069fd67bd 100644 --- a/crates/video/src/mpeg2/README.md +++ b/crates/video/src/mpeg2/README.md @@ -109,6 +109,8 @@ What it does: come out in decoding order; a B picture is held until the anchor after it is coded, and `flush` codes the last held picture as a P picture. With B pictures, `dts` is the presentation times in decoding order one picture interval earlier, so presentation times must step evenly. + A single picture flushed before a second one gets `dts = pts - 1`. With average bitrate, a + GOP that `flush` cuts short gives back the budget of the pictures it did not get. `low_delay` is set only without B pictures. - GOPs are closed or open. The B pictures shown before a GOP's I picture (coded after it) predict backward only in a closed GOP, and from the previous GOP's last anchor too in an open one. diff --git a/crates/video/src/mpeg2/encode/mod.rs b/crates/video/src/mpeg2/encode/mod.rs index 073847f8c..af32b271d 100644 --- a/crates/video/src/mpeg2/encode/mod.rs +++ b/crates/video/src/mpeg2/encode/mod.rs @@ -171,7 +171,8 @@ pub struct Packet { pub data: Vec, pub pts: i64, /// Decoding timestamp: `pts` without B pictures; with them, the presentation times in - /// decoding order one picture interval earlier, so `dts <= pts`. + /// decoding order one picture interval earlier, so `dts <= pts`. A stream of one picture has + /// no interval: its `dts` is `pts - 1`. pub dts: i64, /// An I picture (random access point; in an open GOP the B pictures after it in decoding /// order also need the previous GOP). @@ -449,6 +450,14 @@ impl Encoder { if let Some(last) = self.held.pop() { self.code_anchor(last, PictureType::P, &mut out); } + // the GOP ends here: its budget covered `gop_size` pictures, so take back the share of + // those that never came, or every flushed GOP would raise the bitrate of the next + if self.latest.is_some() { + let missing = u64::from(self.cfg.gop_size).saturating_sub(self.count - self.gop_start); + if let Some(rc) = self.rc.as_mut() { + rc.end_gop(missing as u32); + } + } self.older = None; self.latest = None; self.anchor_mvs.clear(); @@ -596,16 +605,22 @@ impl Encoder { acts.iter().map(|&a| qcode_for(nl, base * (2.0 * a + avg) / (a + 2.0 * avg))).collect() } - /// With B pictures, presentation times must step by a constant amount. Checked before any - /// state changes. + /// With B pictures, presentation times must step by a constant amount, and the decoding + /// times (one step earlier) must fit in an `i64`. Checked before any state changes. fn check_pts(&self, pts: i64) -> Result, EncodeError> { if self.cfg.b_pictures == 0 { return Ok(None); } - let Some(last) = self.last_pts else { return Ok(None) }; + let no_dts = |pts: i64, step: i64| EncodeError::BadTimestamp(format!("{pts}: no decoding time {step} earlier")); + let Some(last) = self.last_pts else { + // flushed before a second picture, this one decodes at `pts - 1` (see `release`) + return if pts == i64::MIN { Err(no_dts(pts, 1)) } else { Ok(None) }; + }; let step = pts.checked_sub(last).filter(|&d| d > 0).ok_or_else(|| EncodeError::BadTimestamp(format!("{pts} after {last}")))?; match self.pts_step { Some(s) if s != step => Err(EncodeError::BadTimestamp(format!("{pts} after {last}: step {step}, earlier {s}"))), + // the earliest decoding time is the earlier picture's time less the step + None if last.checked_sub(step).is_none() => Err(no_dts(last, step)), _ => Ok(Some(step)), } } @@ -756,10 +771,14 @@ impl Encoder { if self.pts_step.is_none() && !flushing { return Vec::new(); } - let off = self.pts_step.unwrap_or(0); + // A lone picture flushed before a second one set the step decodes one unit early, the + // smallest step there is: if encoding goes on, the next picture decodes at this one's + // presentation time whatever the step turns out to be, and that must be later. + let off = self.pts_step.unwrap_or(1); let mut res = std::mem::take(&mut self.out); for p in &mut res { - p.dts -= off; + // `check_pts` refused the times this would not fit for + p.dts = p.dts.saturating_sub(off); } res } diff --git a/crates/video/src/mpeg2/encode/picture.rs b/crates/video/src/mpeg2/encode/picture.rs index b6c177eec..0be5dbafd 100644 --- a/crates/video/src/mpeg2/encode/picture.rs +++ b/crates/video/src/mpeg2/encode/picture.rs @@ -614,11 +614,14 @@ mod tests { inter_matrix: &[16; 64], nonlinear: true, dc_prec: 1, - f_code: 2, - search: 15, + f_code: 3, + search: 31, mb_qcode: &[4; 3], }; let bad = BPred { dirs: MB_FWD, mvs: [[34, 0], [0, 0]] }; + // the bitstream can carry the vector (f_code 3: -64..64): only the position rules it out + let at = |mbx| Search { reference: &f, src: &[0; 256], mbx, mby: 0, f_code: ctx.f_code, range: ctx.search, pmv: bad.mvs[0], lambda: 1 }; + assert!(at(0).valid(bad.mvs[0]) && !at(1).valid(bad.mvs[0])); // a source that the out-of-range (edge-clamped) prediction would match exactly let src = predict_b(&ctx, bad, 1, 0); let st = SliceSt { qcode: 4, dc: [256; 3], pmv: bad.mvs, skipped: 0, left_mv: bad.mvs, last_dirs: MB_FWD }; diff --git a/crates/video/src/mpeg2/encode/rate.rs b/crates/video/src/mpeg2/encode/rate.rs index cd7de9680..96c6f2aa1 100644 --- a/crates/video/src/mpeg2/encode/rate.rs +++ b/crates/video/src/mpeg2/encode/rate.rs @@ -50,6 +50,14 @@ impl RateCtl { self.nb = nb; } + /// The GOP ends `missing` pictures short of what `start_gop` budgeted (the encoder was + /// flushed): their bits are not carried into the next GOP. + pub fn end_gop(&mut self, missing: u32) { + self.remaining -= self.bps * f64::from(missing) / self.fps; + self.np = 0; + self.nb = 0; + } + /// quantiser_scale for the next picture of type `ptype`. pub fn qscale(&self, ptype: PictureType) -> f64 { let floor = self.bps / (8.0 * self.fps); @@ -119,6 +127,22 @@ mod tests { } } + #[test] + fn a_gop_cut_short_gives_its_budget_back() { + let mut rc = RateCtl::new(4e6, 25.0, (1.0, 112.0)); + for _ in 0..20 { + // a GOP of 12 that ends after its I picture and two P pictures + rc.start_gop(11, 0); + for t in [PictureType::I, PictureType::P, PictureType::P] { + let q = rc.qscale(t); + rc.update(t, (1e7 / q) as u64, q); + } + rc.end_gop(9); + } + // what is left is the three pictures' rounding, not nine pictures' budget per GOP + assert!(rc.remaining.abs() < 3.0 * 4e6 / 25.0, "{}", rc.remaining); + } + #[test] fn the_quantiser_stays_in_range() { let mut rc = RateCtl::new(1e6, 25.0, (2.0, 62.0)); diff --git a/crates/video/src/mpeg2/encode/tests.rs b/crates/video/src/mpeg2/encode/tests.rs index c8dfc1205..01aac2b32 100644 --- a/crates/video/src/mpeg2/encode/tests.rs +++ b/crates/video/src/mpeg2/encode/tests.rs @@ -80,6 +80,7 @@ fn assert_decodes_to_recon(name: &str, cfg: EncoderConfig, frames: usize) -> Vec assert!(packets.windows(2).all(|w| w[0].dts < w[1].dts) && packets.iter().all(|p| p.dts <= p.pts), "{name}: decoding times"); let pics = decode(&packets); assert_eq!(pics.len(), frames, "{name}: picture count"); + assert_eq!(recon.len(), frames, "{name}: reconstruction count"); for (i, (p, r)) in pics.iter().zip(&recon).enumerate() { assert_eq!(p.pts, i as i64, "{name}: display order"); for (plane, (a, b)) in [("Y", (&p.y, &r.y)), ("Cb", (&p.cb, &r.cb)), ("Cr", (&p.cr, &r.cr))] { @@ -262,6 +263,76 @@ fn presentation_times_must_step_evenly_with_b_pictures() { } } +#[test] +fn decoding_times_increase_after_a_lone_picture_is_flushed() { + let mut enc = Encoder::new(EncoderConfig::new(32, 32, 25, 1)).unwrap(); + let (y, c) = (vec![0u8; 32 * 32], vec![0u8; 16 * 16]); + let f = YuvFrame { y: &y, cb: &c, cr: &c, y_stride: 32, c_stride: 16 }; + // one picture, flushed before a second sets the step: it decodes one unit early + assert!(enc.encode(&f, 100).unwrap().is_empty()); + let mut packets = enc.flush(); + assert_eq!(packets.iter().map(|p| (p.pts, p.dts)).collect::>(), [(100, 99)]); + // encoding goes on: the next picture decodes at the first one's presentation time + for pts in [105, 110] { + packets.extend(enc.encode(&f, pts).unwrap()); + } + packets.extend(enc.flush()); + assert_eq!(packets.iter().map(|p| (p.pts, p.dts)).collect::>(), [(100, 99), (105, 100), (110, 105)]); + assert!(packets[1].keyframe); +} + +#[test] +fn presentation_times_without_a_decoding_time_are_refused() { + let (y, c) = (vec![0u8; 32 * 32], vec![0u8; 16 * 16]); + let f = YuvFrame { y: &y, cb: &c, cr: &c, y_stride: 32, c_stride: 16 }; + // the decoding times are one step earlier: they must fit in an i64 + for (first, second) in [(i64::MIN + 1, i64::MIN + 3), (i64::MIN + 5, i64::MIN + 20)] { + let mut enc = Encoder::new(EncoderConfig::new(32, 32, 25, 1)).unwrap(); + assert!(matches!(enc.encode(&f, i64::MIN), Err(EncodeError::BadTimestamp(_)))); + assert!(enc.encode(&f, first).unwrap().is_empty()); + assert!(matches!(enc.encode(&f, second), Err(EncodeError::BadTimestamp(_))), "{second} after {first}"); + // a refused time leaves the encoder usable + assert_eq!(enc.flush().iter().map(|p| (p.pts, p.dts)).collect::>(), [(first, first - 1)]); + } + // the lowest times that have one + let mut enc = Encoder::new(EncoderConfig::new(32, 32, 25, 1)).unwrap(); + let mut packets = enc.encode(&f, i64::MIN + 1).unwrap(); + packets.extend(enc.encode(&f, i64::MIN + 2).unwrap()); + packets.extend(enc.flush()); + assert_eq!(packets.iter().map(|p| p.dts).collect::>(), [i64::MIN, i64::MIN + 1]); + // without B pictures every time passes through + let mut enc = Encoder::new(EncoderConfig { b_pictures: 0, ..EncoderConfig::new(32, 32, 25, 1) }).unwrap(); + assert_eq!(enc.encode(&f, i64::MIN).unwrap()[0].dts, i64::MIN); +} + +/// Average bitrate (bit/s) of the scene coded in groups of `group` pictures, flushed after each. +fn flushed_bitrate(cfg: EncoderConfig, group: usize, groups: usize) -> f64 { + let (w, h) = (cfg.width as usize, cfg.height as usize); + let fps = f64::from(cfg.fps_num) / f64::from(cfg.fps_den); + let mut enc = Encoder::new(cfg).unwrap(); + let mut bytes = 0; + for t in 0..group * groups { + let (y, cb, cr) = scene(w, h, w / 2, h / 2, t); + let f = YuvFrame { y: &y, cb: &cb, cr: &cr, y_stride: w, c_stride: w / 2 }; + bytes += enc.encode(&f, t as i64).unwrap().iter().map(|p| p.data.len()).sum::(); + if (t + 1) % group == 0 { + bytes += enc.flush().iter().map(|p| p.data.len()).sum::(); + } + } + bytes as f64 * 8.0 * fps / (group * groups) as f64 +} + +#[test] +fn flushing_short_gops_keeps_the_average_bitrate() { + // every flush ends a GOP of 12 after 4 pictures: the 8 that never came must not leave their + // bits to the next GOP + for b in [0, 2] { + let cfg = EncoderConfig { gop_size: 12, b_pictures: b, rate: RateControl::Abr { kbps: 600 }, ..EncoderConfig::new(176, 144, 25, 1) }; + let got = flushed_bitrate(cfg, 4, 15); + assert!((got / 600e3 - 1.0).abs() < 0.15, "b {b}: {got:.0} bit/s for 600000"); + } +} + #[cfg(feature = "threads")] #[test] fn threaded_slices_give_the_same_stream() { diff --git a/crates/video/tests/mpeg2/encode.rs b/crates/video/tests/mpeg2/encode.rs index eb4ec947a..122fe2d71 100644 --- a/crates/video/tests/mpeg2/encode.rs +++ b/crates/video/tests/mpeg2/encode.rs @@ -123,6 +123,7 @@ fn check(ff: &Path, c: &Case) -> (Vec, f64) { let path = write(c.name, &es); let decoded = ffmpeg_decode(ff, &path, pix_fmt); assert_eq!(decoded.len(), c.frames * fsize, "{}: ffmpeg's frame count", c.name); + assert_eq!(recon.len(), c.frames, "{}: reconstruction count", c.name); let mut all = Stats::default(); for (i, (r, f)) in recon.iter().zip(decoded.chunks_exact(fsize)).enumerate() { let mut s = Stats::default(); From 9efb0a1575814ec7e159f332853f547c7a094832 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 18:12:40 -0500 Subject: [PATCH 26/29] Complete standalone Vorbis feature and option validation --- crates/audio/Cargo.toml | 2 +- crates/audio/src/write/mod.rs | 15 +++++++++++++++ crates/audio/src/write/vorbis.rs | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/audio/Cargo.toml b/crates/audio/Cargo.toml index 6e673b39d..8798272b3 100644 --- a/crates/audio/Cargo.toml +++ b/crates/audio/Cargo.toml @@ -24,7 +24,7 @@ decode-native = ["aac-decode", "alac-decode", "opus-decode", "ac3-decode", "mpa- symphonia = ["decode-native", "dep:symphonia"] # 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"] +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. diff --git a/crates/audio/src/write/mod.rs b/crates/audio/src/write/mod.rs index b8372be1c..597a02e07 100644 --- a/crates/audio/src/write/mod.rs +++ b/crates/audio/src/write/mod.rs @@ -232,6 +232,15 @@ impl std::fmt::Debug for FileEncoder { // The writers differ in size with the features built; the coded one is boxed for its encoder. #[allow(clippy::large_enum_variant)] +#[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" +))] enum Writer { #[cfg(feature = "pcm-encode")] Wav(wav::Writer), @@ -390,6 +399,12 @@ impl EncodeOptions { /// The constant bit rate such a file would be written at ([`FileEncoder::bitrate`]), or the /// error [`EncodeOptions::check`] returns. pub fn bitrate(&self, sample_rate: u32, channels: u16) -> Result> { + #[cfg(feature = "vorbis-encode")] + if self.format == EncodeFormat::Ogg { + // Vorbis is whole-buffer only and VBR has no constant output bitrate. + encode(&AudioBuffer::new(sample_rate, channels, 0), self)?; + return Ok(None); + } // The encoder a file would get, on a sink that is thrown away: what it accepts and the // rate it settles on are then the writer's by construction. Ok(FileEncoder::new(std::io::Cursor::new(Vec::new()), self, sample_rate, channels)?.bitrate()) diff --git a/crates/audio/src/write/vorbis.rs b/crates/audio/src/write/vorbis.rs index c264e1a54..a5e09ad88 100644 --- a/crates/audio/src/write/vorbis.rs +++ b/crates/audio/src/write/vorbis.rs @@ -71,6 +71,7 @@ pub fn encode(_buf: &AudioBuffer, _kbps: u32) -> Result> { #[cfg(all(test, not(target_arch = "wasm32")))] mod tests { + #![allow(clippy::unwrap_used)] use super::*; #[test] From dc2b83d7f426cb3286b3a1fa67229f27c1ca4d40 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 18:13:31 -0500 Subject: [PATCH 27/29] Document Vorbis rate semantics and validate standalone options --- crates/audio/src/lib.rs | 3 ++- crates/audio/src/write/mod.rs | 35 +++++++++++++++++++++++--------- crates/audio/src/write/vorbis.rs | 3 +++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/audio/src/lib.rs b/crates/audio/src/lib.rs index 697568201..33dcdee3b 100644 --- a/crates/audio/src/lib.rs +++ b/crates/audio/src/lib.rs @@ -472,7 +472,8 @@ pub enum RateControl { /// 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. + /// codes silence. Vorbis treats this as a VBR target (64–500 kbit/s), falling back to + /// a quality preset when that target is unsupported; it does not promise a constant bitrate. Bitrate(u32), /// A constant quality from 0.0 (smallest) to 1.0 (best), at whatever bit rate each frame /// needs. AAC only; values outside the range are clamped, and NaN is an error. diff --git a/crates/audio/src/write/mod.rs b/crates/audio/src/write/mod.rs index 597a02e07..ce4b90773 100644 --- a/crates/audio/src/write/mod.rs +++ b/crates/audio/src/write/mod.rs @@ -367,15 +367,6 @@ impl FileEncoder { } } -#[cfg(any( - feature = "pcm-encode", - feature = "flac-encode", - feature = "aac-encode", - feature = "alac-encode", - feature = "mp3-encode", - feature = "ac3-encode", - feature = "eac3-encode" -))] impl EncodeOptions { /// Whether a file of `channels` channels at `sample_rate` can be written with these options: /// `Ok`, or the error [`FileEncoder::new`] and [`encode`] would return, with nothing written. @@ -407,7 +398,31 @@ impl EncodeOptions { } // The encoder a file would get, on a sink that is thrown away: what it accepts and the // rate it settles on are then the writer's by construction. - Ok(FileEncoder::new(std::io::Cursor::new(Vec::new()), self, sample_rate, channels)?.bitrate()) + #[cfg(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" + ))] + { + Ok(FileEncoder::new(std::io::Cursor::new(Vec::new()), self, sample_rate, channels)?.bitrate()) + } + #[cfg(not(any( + feature = "pcm-encode", + feature = "flac-encode", + feature = "aac-encode", + feature = "alac-encode", + feature = "mp3-encode", + feature = "ac3-encode", + feature = "eac3-encode" + )))] + { + let _ = (sample_rate, channels); + Err(AudioError::Unsupported("streaming encoders are not built".into())) + } } } diff --git a/crates/audio/src/write/vorbis.rs b/crates/audio/src/write/vorbis.rs index a5e09ad88..f0e267d50 100644 --- a/crates/audio/src/write/vorbis.rs +++ b/crates/audio/src/write/vorbis.rs @@ -64,6 +64,7 @@ pub fn encode(buf: &AudioBuffer, kbps: u32) -> Result> { } } +/// Ogg Vorbis encoding is unavailable on WASM; returns an unsupported-format error. #[cfg(target_arch = "wasm32")] pub fn encode(_buf: &AudioBuffer, _kbps: u32) -> Result> { Err(AudioError::Unsupported("Ogg Vorbis files are exported by the desktop app".into())) @@ -91,6 +92,8 @@ mod tests { for channels in [1, 2] { let audio = AudioBuffer::new(48_000, channels, 512); let opts = crate::EncodeOptions::new(crate::EncodeFormat::Ogg); + assert!(opts.check(48_000, channels).is_ok()); + assert_eq!(opts.bitrate(48_000, channels).unwrap(), None); let direct = encode(&audio, 192).unwrap(); let generic = crate::encode(&audio, &opts).unwrap(); assert!(direct.starts_with(b"OggS")); From 354acd6a54407a449eade4ccde9e58f6518da38b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 18:15:14 -0500 Subject: [PATCH 28/29] Cover Vorbis in format contract tests and assert streaming refusal --- crates/audio/tests/codecs/file_formats.rs | 26 ++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/audio/tests/codecs/file_formats.rs b/crates/audio/tests/codecs/file_formats.rs index 53947df12..84169f665 100644 --- a/crates/audio/tests/codecs/file_formats.rs +++ b/crates/audio/tests/codecs/file_formats.rs @@ -397,12 +397,15 @@ fn the_formats_are_named() { for name in ENCODE_EXTENSIONS { assert!(encode_format_for(name).is_some(), "{name}"); } - assert_eq!(ENCODE_EXTENSIONS.len(), 12); + assert_eq!(ENCODE_EXTENSIONS.len(), 13); for format in CODED { assert!(EncodeFormat::ALL.contains(&format) && format.is_built()); } let features: Vec<_> = EncodeFormat::ALL.iter().map(|f| f.feature()).collect(); - assert_eq!(features, ["pcm-encode", "pcm-encode", "flac-encode", "alac-encode", "aac-encode", "mp3-encode", "ac3-encode", "eac3-encode"]); + assert_eq!( + features, + ["pcm-encode", "pcm-encode", "flac-encode", "alac-encode", "aac-encode", "mp3-encode", "ac3-encode", "eac3-encode", "vorbis-encode"] + ); } /// ffmpeg's decode of `file` as interleaved `f32`, split into `channels` channels. @@ -495,6 +498,10 @@ fn the_format_queries_agree_with_the_writer() { let layouts = masks.iter().map(|&mask| ChannelLayout::from_mask(mask)).chain((1..=9).map(ChannelLayout::unordered)); for layout in layouts { for &format in EncodeFormat::ALL { + // Vorbis is whole-buffer-only; its direct/generic API and options are tested separately. + if format == EncodeFormat::Ogg { + continue; + } // 24 bits is a depth every format has. let opts = EncodeOptions::new(format).with_layout(layout); for &sample_rate in &rates { @@ -520,7 +527,7 @@ fn the_format_queries_agree_with_the_writer() { // The most channels is the most: one more is refused, by the layouts of a format that has // a fixed set and by the writer of one that has not. let most: Vec = EncodeFormat::ALL.iter().map(|f| f.max_channels()).collect(); - assert_eq!(most, [32_767, 32_767, 8, 8, 8, 2, 6, 8]); + assert_eq!(most, [32_767, 32_767, 8, 8, 8, 2, 6, 8, 2]); for &format in EncodeFormat::ALL { let most = format.max_channels(); match format.fixed_layouts(most) { @@ -569,3 +576,16 @@ fn the_bit_rate_used_can_be_asked_for() { assert_eq!(encode(&buf, &opts).unwrap().len() as u32, 43 * 1152 / 8 * opts.bitrate(48_000, 2).unwrap().unwrap() / 48_000); assert!(is_unsupported(EncodeOptions::new(EncodeFormat::Mp3).bitrate(48_000, 6))); } + +#[test] +fn vorbis_is_whole_buffer_only_and_validates_layouts() { + let opts = EncodeOptions::new(EncodeFormat::Ogg).with_layout(ChannelLayout::STEREO); + assert!(opts.check(48_000, 2).is_ok()); + assert_eq!(opts.bitrate(48_000, 2).unwrap(), None); + assert!(FileEncoder::new(Cursor::new(Vec::new()), &opts, 48_000, 2).is_err()); + let buf = craft_audio::AudioBuffer::new(48_000, 2, 512); + assert!(craft_audio::encode(&buf, &opts).unwrap().starts_with(b"OggS")); + let wrong = opts.with_layout(ChannelLayout::from_mask(0xC)); + assert!(wrong.check(48_000, 2).is_err()); + assert!(craft_audio::encode(&buf, &wrong).is_err()); +} From 92e665a5e8e12576095ca2addb681d2e3a0fac69 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 18:33:24 -0500 Subject: [PATCH 29/29] Exercise Vorbis block boundaries with distinct channel signals --- crates/audio/src/write/vorbis.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/audio/src/write/vorbis.rs b/crates/audio/src/write/vorbis.rs index f0e267d50..f56d01148 100644 --- a/crates/audio/src/write/vorbis.rs +++ b/crates/audio/src/write/vorbis.rs @@ -107,4 +107,24 @@ mod tests { } } } + #[cfg(feature = "decode")] + #[test] + fn distinct_signals_survive_full_and_partial_blocks() { + for frames in [4096, 4097, 8193] { + let channels: Vec> = [440.0f32, 880.0] + .into_iter() + .map(|hz| (0..frames).map(|i| (std::f32::consts::TAU * hz * i as f32 / 48_000.0).sin() * 0.4).collect()) + .collect(); + let audio = AudioBuffer { sample_rate: 48_000, channels }; + let bytes = encode(&audio, 192).unwrap(); + let (_, decoded) = crate::decode(&bytes, Some("ogg")).unwrap(); + assert_eq!(decoded.frames(), frames); + assert_eq!(decoded.channels.len(), 2); + for (expected, actual) in audio.channels.iter().zip(&decoded.channels) { + let mse = expected.iter().zip(actual).map(|(a, b)| (a - b).powi(2)).sum::() / frames as f32; + // Vorbis is lossy; this tolerates codec error but rejects silence or swapped channels. + assert!(mse.sqrt() < 0.08, "{frames} frames: RMS error {}", mse.sqrt()); + } + } + } }