Skip to content

Native AVIF: enforce the pixel budget in rav1d and before RGBA conversion #1719

Done

Description

@pascalandr

Could Rune enforce its native AVIF pixel budget in the decoder as well as in preflight?

#1516 added container and AV1 header checks for #888. Current dev still uses avif-decode 3.0.0, whose rav1d wrapper leaves Dav1dSettings::frame_size_limit at its default. In crates/rune-image/src/avif.rs, Rune also collects the RGBA buffer before checking the dimensions returned by the decoder.

Please keep preflight, enforce the frame budget in the codec, and check returned dimensions and checked RGBA byte size before allocating the conversion buffer. A decoder-side limit would also let callers with smaller preview surfaces enforce their own pixel budget.

Activity

  1. Hona commented on Nov 24, 2026

    @Hona
    Member

    The post-decode RGBA conversion hardening is now merged on dev in #1891 (bbaf6c8ab):

    • What landed: crates/rune-image/src/avif.rs now validates decoded dimensions and checked RGBA8 byte size (w * h * 4) against Rune's shared raster limits (MAX_SIDE = 8192, MAX_ALLOC = 256 MiB, matching image::Limits in crates/rune-image/src/lib.rs) before allocating any conversion buffer, and pre-allocates exact capacity with Vec::try_reserve_exact across all six avif_decode::Image variants. Preflight check_limits and public ImageCache APIs are unchanged.
    • Verified: cargo test -p rune-image --features avif on macOS arm64 (60 passed, 2 ignored), cargo check -p rune-image --features avif --tests on Linux aarch64, and cargo test -p rune-image (57 passed, 2 ignored), rune image/texture-limit/integration tests, and wasm32 checks on Windows x86_64.
    • Not run: --features avif on Windows x86_64 (requires NASM for rav1d x86 assembly), and no live window (internal decoder hardening only).

    Leaving this issue open for the two remaining parts:

    1. Enforcing Dav1dSettings::frame_size_limit inside rav1d (avif-decode 3.0.0 leaves it at 0 and exposes no setter, so this requires vendoring/patching avif-decode or replacing it with direct avif-parse/rav1d/yuv dependencies).
    2. Caller-configurable per-load pixel budgets for smaller preview surfaces.
  2. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    Thanks for the detailed write-up! Part of this is already on dev. #1891 (bbaf6c8a) changed crates/rune-image/src/avif.rs so that it checks the decoded dimensions and the RGBA8 byte size (w * h * 4, overflow-checked, against MAX_SIDE/MAX_ALLOC), then reserves the conversion buffer with try_reserve_exact before converting. The preflight in limits::inspect is unchanged.

    Two parts are still open, and this issue now tracks them:

    1. Codec-side limit: rgba() calls avif_decode::Decoder::from_avif(..).to_image() without passing any decoder settings. As noted above, avif-decode 3.0.0 has no way to set Dav1dSettings::frame_size_limit. Setting it means patching or vendoring avif-decode, or calling avif-parse and rav1d directly.
    2. Caller-supplied budget: an optional per-load pixel budget below the global limit, for callers that only show small previews.
  3. opencodejr commented on Nov 25, 2026

    @opencodejr
    Contributor

    I looked into the codec-side limit and haven't opened a change for it yet. On docs.rs, avif-decode 3.0.0's Decoder::from_reader creates its rav1d decoder with only a thread count, and its public API has no way to set Dav1dSettings::frame_size_limit. Setting it means either vendoring avif-decode with a patch, following the repo's other vendor/* crates, or replacing it with direct avif-parse/rav1d/yuv calls. Either way it's a decoder change that has to be built and tested against real AVIF files with --features avif, and I couldn't do that this time.

    Until then, native AVIF still goes through the container and AV1 header preflight (#1516) and the post-decode dimension and byte-budget checks before the RGBA allocation (#1891). This issue stays open for the in-codec limit and for a caller-supplied per-load pixel budget.

  4. Hona commented on Nov 27, 2026

    @Hona
    Member

    Both remaining parts are now merged on dev in #2641 (70f7a5553):

    • Codec-side rav1d pixel budget: Replaced the avif-decode 3.0.0 wrapper with direct avif-parse, pure-Rust rav1d, and yuv decoding in crates/rune-image/src/avif/codec.rs. Dav1dSettings::frame_size_limit is now set to the caller's effective pixel budget (with zero-pixel budgets rejected before opening rav1d, since 0 disables the limit in rav1d) so oversized primary and alpha frames fail with ERANGE before frame-plane allocation. Decoded dimensions, pixel count, and checked RGBA8 byte size are validated before calling Vec::try_reserve_exact, converting straight into the final RGBA8 buffer with no intermediate full-frame RGB/RGBA16 allocation.
    • Per-load caller pixel budget: Added ImageLimits (with_max_side, with_max_pixels, with_max_alloc, clamped to Rune's global 8192 px / 67,108,864 px / 256 MiB ceiling) with ImageCache::load_with_limits and ImageCache::load_scaled_with_limits (re-exported as rune::ImageLimits). Default limits share cache entries with load/load_scaled; tighter limits use separate cache keys and are enforced across native AVIF, still rasters, animated GIF/WebP/APNG, SVG, and web header preflight (header::check_with_limits).
    • Verified locally (Windows x86_64): cargo test -p rune-image --features avif (106 passed, 2 ignored; 12 doc-tests passed, including direct rav1d frame_size_limit rejection bypassing preflight on hello-320.avif, real AV1 alpha-plane decoding, and ImageCache::load_with_limits tests), cargo test -p rune-image (102 passed, 2 ignored), cargo check -p rune-image --no-default-features, cargo check -p rune --features avif, cargo check -p rune-image --target wasm32-unknown-unknown (default and --no-default-features), and scripts/file-size.sh.
    • Not run on real hardware: macOS, Linux, and live browser runtimes were not exercised directly.

    Closing as completed — please reopen if anything is still off.

  5. added a commit that references this issue on Nov 27, 2026
    70f7a55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions