Skip to content

rust: Port obs-avc.c to obs-codec - #90

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

zackees merged 4 commits into
obs-rust:masterfrom
b7216309-jpg:port/obs-avc

Conversation

@b7216309-jpg

Copy link
Copy Markdown
Contributor

Stacked draft: builds on #89 (obs-nal.c and the obs-codec crate). Until #89 merges, this diff also shows its commits. The last three commits are new here.

Description

Second step of the parser plan in #45: ports libobs/obs-avc.c to obs-codec::avc, all six exports: obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet, obs_parse_avc_packet_priority, obs_parse_avc_header and obs_extract_avc_headers. With ENABLE_RUST_LIBOBS=ON libobs builds without obs-avc.c.

Kept exactly as in C:

  • The NAL walk. Every function repeats while (nal_start < end && !*(nal_start++));. That skips zeros and then one more byte, so units start after the first nonzero byte. nal_units reproduces this, including garbage before the first start code and truncated tails.

  • Keyframes and priority. The first slice decides obs_avc_keyframe. In obs_parse_avc_packet, an IDR unit sets keyframe but nothing clears it, and priority is the maximum of header >> 5, so values up to 7 are possible.

  • The AVCC packet has a long reference count of 1 in front of the data: 4 bytes on Windows, 8 elsewhere. data points past it, as libobs's packet refcounting expects.

  • The header record:

    • built from the last SPS and the last PPS;
    • SPS/PPS sizes are written as uint16_t, so they wrap above 65535;
    • High profiles (100/110/122/244) append chroma format and bit depths.

    The SPS reader drops emulation-prevention bytes, keeps the Exp-Golomb leading-zero cap from fix(libobs): get_ue_golomb UB on a truncated SPS (1 << 32) #17, and truncates to uint8_t.

  • Allocation and outputs. Outputs are bmalloced like the C serializer and DARRAY buffers. Empty outputs of obs_extract_avc_headers are NULL; obs_parse_avc_header returns 0 without touching *header.

Intentional difference, inherited from #89: on NULL data with size 0 these functions give empty results. C reaches the start-code search's end - 3 wrap and reads address 0. I found it here: the first parity run crashed in the C oracle on an empty input, whose pointer is 0x1. #89 now documents it.

Oracle. obs-avc.c is compiled with its calls pointed at the oracle copies of obs-nal.c, array-serializer.c and bitstream.c, so the whole call tree is the original C. It includes obs.h, which pulls in the CMake-generated obsconfig.h, so the oracle gets a test-only stand-in (oracle/obsconfig/obsconfig.h) with placeholder values the AVC code never uses.

Motivation and Context

Plan: #45 (comment). HEVC and AV1 follow.

Refs #45

How Has This Been Tested?

Windows 11, MSVC 14.44, soldr 0.9.31.

  • Commits:

    1. test/cmocka/test_avc_parse.c (C characterization through libobs only). The existing test_avc.c compiles its own obs-avc.c for UBSan, so with ON it would still test C on Linux and macOS.
    2. RED (todo!() core: Tier 1 fails 7/8, the parity binary aborts, layout passes).
    3. GREEN.
  • Tier 2: layout of struct encoder_packet, plus sizeof(long).

  • Tier 3 (4096 cases): generated Annex B streams with every NAL type, both start-code lengths, garbage prefixes, truncated tails and emulation-prevention bytes; High-profile SPS/PPS pairs with sparse bits that reach the Exp-Golomb cap; arbitrary bytes. Every output is compared byte for byte:

    • all encoder_packet fields, plus the refcount-prefixed data block, also with the packet parsed in place;
    • whether *header was written;
    • NULL-ness of the extract outputs.

    Everything is then freed with bfree. Separate tests cover the fix(libobs): get_ue_golomb UB on a truncated SPS (1 << 32) #17 truncated-SPS case and SPS/PPS over 65535 bytes.

  • Mutation check: 15 mutants. 14 are caught, including the 16-bit size truncation, which needed the oversized test. One survives, and it is equivalent: "skip until 01" instead of "until nonzero", because the walk always lands on a start code.

  • Checks: soldr cargo test --workspace --locked (42 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 added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs-hevc.c and obs-avc.c share the same NAL walk, AVCC-style
conversion, priority scan and header split, differing only in how a
NAL header byte is classified.

- obs-codec::nal holds the walk, to_length_prefixed, packet_priority
  and split_headers, which take that classification as a function.
- obs-codec::ffi::packet holds struct encoder_packet, the bmalloc
  helpers, the long-refcounted packet output and the three
  extract-headers outputs.

Extracted from b7216309-jpg's AVC refactor so the HEVC port does not
depend on the AVC port (obs-rust#90).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zackees
zackees marked this pull request as ready for review October 10, 2026 17:17
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Rebased onto #89's head (port/obs-nal, itself on master); depends on #89 and should merge after it. Added rust/obs-codec/fuzz with a cargo-fuzz differential target avc_differential (same comparison as tests/avc_parity.rs: every C ABI entry point + safe core vs the C oracle, output buffers byte-for-byte). Ran 3661 s, 49.1M execs, no mismatch. Exclusion: NULL/near-NULL data with size 0 (C wraps end - 3), never generated by the harness; get_ue_golomb UB already fixed in C. RED commit verified (4 Tier 1 failures on todo!() core), head GREEN; ./lint, Rust tests and Tier 2 OFF/ON (test_avc, test_avc_parse) pass; local gate stamped.

zackees added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs-hevc.c and obs-avc.c share the same NAL walk, AVCC-style
conversion, priority scan and header split, differing only in how a
NAL header byte is classified.

- obs-codec::nal holds the walk, to_length_prefixed, packet_priority
  and split_headers, which take that classification as a function.
- obs-codec::ffi::packet holds struct encoder_packet, the bmalloc
  helpers, the long-refcounted packet output and the three
  extract-headers outputs.

Extracted from b7216309-jpg's AVC refactor so the HEVC port does not
depend on the AVC port (obs-rust#90).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees added a commit to b7216309-jpg/obs-studio that referenced this pull request Oct 10, 2026
obs-hevc.c and obs-avc.c share the same NAL walk, AVCC-style
conversion, priority scan and header split, differing only in how a
NAL header byte is classified.

- obs-codec::nal holds the walk, to_length_prefixed, packet_priority
  and split_headers, which take that classification as a function.
- obs-codec::ffi::packet holds struct encoder_packet, the bmalloc
  helpers, the long-refcounted packet output and the three
  extract-headers outputs.

Extracted from b7216309-jpg's AVC refactor so the HEVC port does not
depend on the AVC port (obs-rust#90).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zackees
zackees force-pushed the port/obs-avc branch 2 times, most recently from 921585b to f608b9d Compare October 10, 2026 18:25
@b7216309-jpg

Copy link
Copy Markdown
Contributor Author

Rebased onto current master, after #98 and #105, and pushed with --force-with-lease against your f608b9d.

#98 already landed the shared NAL walk and packet plumbing, so AVC is now built on it instead of carrying its own copy:

  • avc.rs keeps only AVC's classification (low 5 bits, IDR = keyframe, header >> 5 priority) plus the SPS reader and the header record;
  • ffi/avc.rs uses ffi::packet (parse_packet, write_split, bytes, bmalloc_concat) and re-exports encoder_packet;
  • oracle/avc.c drops its encoder_packet layout functions, which oracle/encoder_packet.c now provides, and obs_c_oracle::avc re-exports OracleEncoderPacket from there;
  • tests/avc_layout.rs is gone (covered by tests/packet_layout.rs).

Your avc_diff fuzz target is carried over unchanged with you as author, added as a third bin in the fuzz crate.

Commits: C test, RED (Tier 1 fails 7/8 at the todo!() core; the one passing test is master's NAL walk), GREEN, fuzz target. soldr cargo test --workspace --locked (80 binaries) and ./lint pass. The AVC logic is unchanged from what you fuzzed, only the plumbing moved, so I didn't re-run the hour.

Unrelated, found while running clippy for aarch64 Linux: obs-util/src/ffi/dstr.rs on master has 7 casts that clippy rejects on targets where c_char is u8 (unnecessary_cast and ptr_as_ptr-style same-type casts, lines 83, 132, 187, 206, 288, 341, 389). No CI job runs clippy for that target, so it's latent. I can send a small fix if you'd like.

@b7216309-jpg
b7216309-jpg force-pushed the port/obs-avc branch 2 times, most recently from 2f2c32a to 44cda6f Compare October 10, 2026 20:31
b7216309-jpg and others added 4 commits October 10, 2026 13:35
test_avc.c compiles its own obs-avc.c for UBSan, so with
ENABLE_RUST_LIBOBS=ON it would not reach a port. test_avc_parse.c links
libobs only and pins the six exports: keyframe detection by the first
slice, Annex B to AVCC conversion with the long reference count in
front, priority, the AVCDecoderConfigurationRecord for a High-profile
SPS, the 0 / copy cases of obs_parse_avc_header, and the header/SEI/
packet split with NULL outputs when empty.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
C ABI shims for the six exports of libobs/obs-avc.c over the shared
obs-codec packet helpers (encoder_packet, bytes, bmalloc_concat,
parse_packet, write_split), the oracle (obs-avc.c over the oracle copies
of obs-nal.c, array-serializer.c and bitstream.c, reusing the
encoder_packet oracle), rust-exports entries, and Tier 1 and Tier 3
tests, against a todo!() core: Tier 1 fails 7/8, the parity binary
aborts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
obs_avc_keyframe, obs_avc_find_startcode, obs_parse_avc_packet,
obs_parse_avc_packet_priority, obs_parse_avc_header and
obs_extract_avc_headers move to Rust. With ENABLE_RUST_LIBOBS=ON libobs
builds without obs-avc.c.

The NAL walk, AVCC conversion, priority scan and header split use the
shared helpers in obs-codec::nal; AVC adds its unit rating (IDR is a
keyframe, priority header >> 5) and the AVCDecoderConfigurationRecord
builder. The SPS reader keeps the C details: emulation-prevention bytes
dropped, the Exp-Golomb leading-zero cap from obs-rust#17, uint8_t results, and
SPS/PPS sizes written as uint16_t.

The one intentional difference is inherited from the obs-nal port: on
NULL data with size 0 these functions give empty results, where C reads
address 0 in the start-code search.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
avc_diff compares every obs-avc C ABI entry point and the safe core with
the C oracle, output buffers byte for byte, as tests/avc_parity.rs does.
NULL/near-0 ranges (C UB) are excluded and never generated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=7cb32b754174e9516723dce1bba46e08123dfbbb secs=266 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791664929,"gate":"rust/all/fmt","host":"linux-x86_64","key":"59c2e4d9640d08a8a1cf720efde260f91e322e6a898bb30d3ebcc1f47a8d9793","lane":"lint","parents":["db1ba2d9cf11e2a8728cb6073ae91377834753cd"],"secs":86,"stamp":"4e3871acb6985d6bf26b55d687b3fe81","tree":"7cb32b754174e9516723dce1bba46e08123dfbbb","v":1,"via":"run"}
Ci-Attestation: {"at":1791664929,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"59c2e4d9640d08a8a1cf720efde260f91e322e6a898bb30d3ebcc1f47a8d9793","lane":"lint","parents":["db1ba2d9cf11e2a8728cb6073ae91377834753cd"],"secs":86,"stamp":"c3c8a03a7e6702158c60a3efa9c10dd3","tree":"7cb32b754174e9516723dce1bba46e08123dfbbb","v":1,"via":"run"}
Ci-Attestation: {"at":1791664930,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"e7c176de28b46793d2ca84a7b8b3fbbd14f846ef47e62d78cb56c677277ae25c","lane":"rust-test","parents":["db1ba2d9cf11e2a8728cb6073ae91377834753cd"],"secs":44,"stamp":"051d237c4eb1c148afb7d60cd0439d9d","tree":"7cb32b754174e9516723dce1bba46e08123dfbbb","v":1,"via":"run"}
Ci-Attestation: {"at":1791664930,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"b8bda407b3e19d406e9a2eafbeab5856756797649c3272970f0590494ecf8f77","lane":"tier2-linux","parents":["db1ba2d9cf11e2a8728cb6073ae91377834753cd"],"secs":136,"stamp":"78b39a9fdf34cd2badc41a832ab41245","tree":"7cb32b754174e9516723dce1bba46e08123dfbbb","v":1,"via":"run"}
@zackees

zackees commented Oct 10, 2026

Copy link
Copy Markdown
Member

Rebased onto master (after #98, #105 and the later ports). It now reuses #98's shared obs-codec helpers (nal walk, packet bytes/bmalloc_concat, the encoder_packet oracle), so the diff is AVC-only: 16 files, no binaries. RED→GREEN, checked with soldr's cache disabled: avc fails 7/8 at the RED commit and the full suite passes at head, with clippy clean. Fuzz: avc.rs and the shared helpers are byte-identical to the version fuzzed for 3661 s (49.1M execs, no mismatch), so I didn't re-run it. The local gate passes and is stamped.

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