Skip to content

rust: Port obs-av1.c to obs-codec - #105

Merged
zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:port/obs-av1
Oct 10, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
b7216309-jpg:port/obs-av1

Conversation

@b7216309-jpg

Copy link
Copy Markdown
Contributor

Stacked draft: builds on #98 (HEVC), #90 (AVC) and #89 (obs-codec), and includes the C fix from #103. Until those merge, this diff also shows their commits. New here: the RED and GREEN commits (rust: Add obs-av1 shims…, rust: Port obs-av1.c…).

Description

Last step of the parser plan in #45: ports libobs/obs-av1.c to obs-codec::av1, all four exports: obs_av1_keyframe, obs_extract_av1_headers, metadata_obu and metadata_obu_itu_t35. With ENABLE_RUST_LIBOBS=ON libobs builds without obs-av1.c.

C fixed first. Reading obs-av1.c turned up out-of-bounds reads on malformed OBUs: a size field larger than the packet, a leb128 shift that overflows int, an off-by-one without a size field, and an underflow on a lone header. These are fixed in C in #103, with test/cmocka/test_av1.c. This branch merges #103, and the port matches the fixed C exactly, so there are no intentional differences. Because the fixed C stays in bounds on any input, the parity test feeds both sides arbitrary bytes.

The port:

  • The OBU walk. leb128 reads at most 8 bytes into 64 bits. A missing extension byte ends the OBU. Without a size field, the OBU is what follows the header (AV1 5.3.1). The size is cast as size_t, like C, then cut to what is left. Every OBU is at least its header byte, so the walk always advances and never leaves the buffer.
  • obs_av1_keyframe: decided by the first frame or frame header OBU with a payload. show_existing_frame is never a key frame; otherwise frame_type == 0 is.
  • obs_extract_av1_headers: every OBU stays in the packet, and sequence header and metadata OBUs are also copied to the header. Empty outputs are NULL.
  • metadata_obu: header byte, leb128 size, type, payload, then the 0x80 trailing bits.

Motivation and Context

Plan: #45 (comment). With this, all four codec helpers (obs-nal, obs-avc, obs-hevc, obs-av1) are ported.

Refs #45

How Has This Been Tested?

Windows 11, MSVC 14.44, soldr 0.9.31.

  • Tier 3 (4096 cases each):

    • OBU streams with random types, extension bytes, size fields that claim more or less than the payload, padded leb128 up to 9 bytes, and truncated tails;
    • well-formed OBUs with 100–400-byte payloads, whose sizes need two leb128 bytes;
    • arbitrary bytes.

    Keyframe results and both extract outputs are compared byte for byte. metadata_obu and the T.35 wrapper are compared on random payloads and types, and at the leb128 size boundaries.

  • C test expectations: cmocka_cases_match_c_oracle checks test_av1.c's expectations against the oracle. Against master's obs-av1.c it fails with a packet of 18 bytes for a 4-byte input; against the fixed file it passes.

  • Mutation check: 11 mutants, 10 caught. The leb128 shift needed the big-payload generator. The survivor is equivalent: size - 1 without a size field, which the final cut reduces to the same value.

  • Other checks: soldr cargo test --workspace --locked (46 binaries), python ci/local_gate.py --lane lint, and cargo clippy -p obs-codec -p libobs-rust --lib -- -D warnings for x86_64/aarch64 Linux and aarch64 macOS.

Types of changes

  • Code cleanup (non-breaking change which makes code smaller or more readable)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

🤖 Generated with Claude Code

@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Heads-up: #103 (now on master) fixed out-of-bounds reads in obs-av1.c's parse_obu_header. OBU sizes are now clamped to the buffer, leb128 shifts in 64 bits, and size - 1 - obu_extension_flag is used when there is no size field. The new test_av1 Tier 2 test pins that behavior, so this port must reproduce it once rebased, and the parity oracle will now compile the fixed C.

@zackees
zackees marked this pull request as ready for review October 10, 2026 16:22
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Rebased onto #89's head (rebased on master), no longer stacked on #90/#98: AV1 only needed the bytes/bmalloc_concat helpers, now in a minimal ffi/packet.rs (it will conflict trivially with #90's fuller version; whichever merges second takes the superset). Added the oracle obsconfig include that obs.h needs.

  • RED: commit 996bdc6, Tier 1 tests/av1.rs 1 passed / 6 failed against the todo!() core. GREEN: f4d405b, all pass (av1 7/7, av1_parity 4/4).
  • The port already matches the libobs: Keep AV1 OBU parsing inside the buffer #103-fixed C (clamped obu_size, 64-bit leb128, sz - 1 - ext, missing extension byte). Tier 1 has a test for each case and the parity oracle compiles the fixed C, so no new bug fix was needed.
  • New cargo-fuzz differential target rust/obs-codec/fuzz (av1_differential): ran 3661 s, 138,890,134 executions, no mismatches.
  • Local gate passed (lint, rust-test, tier2-linux) and stamped 23a82cf. CI is green.

b7216309-jpg and others added 3 commits October 10, 2026 11:22
C ABI shims for the four exports of libobs/obs-av1.c
(obs_av1_keyframe, obs_extract_av1_headers, metadata_obu and
metadata_obu_itu_t35), the oracle, rust-exports.txt entries, and Tier 1
and Tier 3 tests against a todo!() core: Tier 1 fails 6/7 and the
parity binary aborts.

The oracle compiles obs-av1.c with the OBU bounds fix merged from

Co-Authored-By: Claude Opus 5.5 <[email protected]>
obs_av1_keyframe, obs_extract_av1_headers, metadata_obu and
metadata_obu_itu_t35 move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-av1.c.

The OBU walk follows obs-av1.c with the bounds fix from obs-rust#103:
- leb128 reads at most 8 bytes into 64 bits;
- a missing extension byte ends the OBU;
- without a size field the OBU is what follows the header;
- the size is cut to what is left, cast as size_t first, as in C.

Every OBU is at least its header byte, so the walk always advances and
never leaves the buffer. Keyframes come from the first frame or frame
header OBU with a payload. metadata_obu builds the header, leb128 size,
type, payload and 0x80 trailing bits. No intentional differences.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Feeds arbitrary bytes to the Rust obs-av1 shims and the C oracle
(obs-av1.c with the obs-rust#103 bounds fix) and asserts identical results, per
the bitstream-parser fuzz rule in docs/rust-port/testing-policy.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4 secs=56 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791656801,"gate":"rust/all/fmt","host":"linux-x86_64","key":"a462edd75576cc3f895aa589808b34f03df55969110a0b83c50da8acba70e0bd","lane":"lint","parents":["fe63e450129c1eeb4b1e1dc1272b678b7305f7d6"],"secs":13,"stamp":"3b9d19058a18740a8119265176b3d64b","tree":"9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4","v":1,"via":"run"}
Ci-Attestation: {"at":1791656801,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"a462edd75576cc3f895aa589808b34f03df55969110a0b83c50da8acba70e0bd","lane":"lint","parents":["fe63e450129c1eeb4b1e1dc1272b678b7305f7d6"],"secs":13,"stamp":"2e06ead6cace504173cfde20d9d37f9c","tree":"9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4","v":1,"via":"run"}
Ci-Attestation: {"at":1791656801,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"d9f9eb5090c7a44b3f2f23547f7d5ca46a72d7107ef48180b3cd47a692710a8f","lane":"rust-test","parents":["fe63e450129c1eeb4b1e1dc1272b678b7305f7d6"],"secs":19,"stamp":"bdf6d4d57719dca99c2e4fdc58f985c0","tree":"9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4","v":1,"via":"run"}
Ci-Attestation: {"at":1791656801,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"8168c5812db09dd001ecdd524685622f754df9c62269d3d1f0d76dccc610456d","lane":"tier2-linux","parents":["fe63e450129c1eeb4b1e1dc1272b678b7305f7d6"],"secs":24,"stamp":"f4a4bb74e724e8dc8cb2544f8b9651d7","tree":"9ce794709fd0a6ffd4dbdbb73593a7ea5dd67be4","v":1,"via":"run"}
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Rebased onto master after #98 and #88 merged. The cut-down ffi/packet.rs is gone; AV1 now uses #98's shared bytes/bmalloc_concat, and av1_differential is added as a second bin in #98's rust/obs-codec/fuzz crate. Both sides kept in the export list, oracle (lib.rs, build.rs), README and CMake. RED (4b869e8 lineage): Tier 1 av1 fails 6/7; head: av1 7/7, av1_parity 4/4, all obs-codec/obs-util tests pass. Local gate stamped 52dc3a9; no binaries in the diff. CI green. AV1 code unchanged since the 1h fuzz (3661 s, 138.9M execs, no mismatch).

@zackees
zackees merged commit 0a5664f into obs-rust:master Oct 10, 2026
22 checks passed
zackees added a commit that referenced this pull request Oct 10, 2026
…43)

Phase 3 ports of libobs/util into the obs-util crate, each with a safe
core, a C ABI shim, Tier 1 tests, a layout test where a struct crosses
the ABI, and a proptest parity suite against the original C built as an
oracle. ENABLE_RUST_LIBOBS=ON drops the matching C sources from libobs.

- utf8: hidden and non-Windows only (Windows keeps the C utf8.c).
- lexer.c: follows master's C, including the #48 strref ordering.
- text-lookup: no longer crashes on an opening quote at end of line;
  the shim returns false for a NULL lookup.
- dstr: follows master's #46/#47 fixes; strlist_split takes a NUL
  separator. Wide-char and printf helpers stay in dstr-libc.c.
- bmem: Windows _aligned_* branch and the allocation counter.

The oracle vendors uthash 2.3.0 (BSD-1-Clause) for text-lookup.c, since
the Rust test runners have no uthash package.

Refs #43

Rebased onto master after #61 (base), #66 (file-serializer) and #85 (real
utf8.c in the oracle): the oracle's utf8.c/platform_conv_host.c now use
oracle_* utf8 names so they no longer clash with the Rust shims; the
dstr-conversion oracle test moved to obs-util and calls oracle_dstr_*;
duplicate bcrash/os_fopen/bmemdup/os_*_ptr stand-ins were dropped in favour
of master's base-variadic.c, file_serializer_host.c and
platform_conv_host.c; utf8 shims are listed in rust-hidden.txt; the
vendored uthash is excluded from clang-format.

Rebased again onto master after #82/#83/#87/#97/#89/#98/#88/#105: oracle build.rs keeps
video_fourcc.c/task.c and drops the direct util/utf8.c (oracle/utf8.c already
compiles it under oracle_* names, which platform_conv_host.c and #82's
file_serializer_host.c use).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=58f2156a689831720a1e6ad9004041e400917579 secs=135 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791658556,"gate":"rust/all/fmt","host":"linux-x86_64","key":"e533312cff231c50944045005e90c2b48ce1920b317c9037a705bfda5b49ef4c","lane":"lint","parents":["930903b5e8c0cac6cec54edd05abeaacb825637c"],"secs":17,"stamp":"a33efc3cd6c98f3fbd2fa57e9a61a281","tree":"58f2156a689831720a1e6ad9004041e400917579","v":1,"via":"run"}
Ci-Attestation: {"at":1791658556,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"e533312cff231c50944045005e90c2b48ce1920b317c9037a705bfda5b49ef4c","lane":"lint","parents":["930903b5e8c0cac6cec54edd05abeaacb825637c"],"secs":17,"stamp":"fb2d0a58bcefb05085a4b512fc916740","tree":"58f2156a689831720a1e6ad9004041e400917579","v":1,"via":"run"}
Ci-Attestation: {"at":1791658556,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"1a00cb7a8eb7b67b33a108e748711133d604e544b254813435cc1ca5ea24cd26","lane":"rust-test","parents":["930903b5e8c0cac6cec54edd05abeaacb825637c"],"secs":22,"stamp":"d80b75173a27c995da2676193fa36904","tree":"58f2156a689831720a1e6ad9004041e400917579","v":1,"via":"run"}
Ci-Attestation: {"at":1791658556,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"a950373231b455174e5c031fe578e1e48c61e04f0387195a7ddc0e265e371e15","lane":"tier2-linux","parents":["930903b5e8c0cac6cec54edd05abeaacb825637c"],"secs":96,"stamp":"f1c2d81609277e26492a5a3f3553cd9c","tree":"58f2156a689831720a1e6ad9004041e400917579","v":1,"via":"run"}
@b7216309-jpg b7216309-jpg mentioned this pull request Oct 10, 2026
6 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants