Skip to content

obs-output: Port the output caption plan (#3074) - #3077

Merged
zackees merged 6 commits into
masterfrom
rust/3074-output-caption-plan
Oct 30, 2026
Merged

zackees merged 6 commits into
masterfrom
rust/3074-output-caption-plan

Conversation

@vpacher

@vpacher vpacher commented Oct 30, 2026

Copy link
Copy Markdown
Member

Fixes #3074

What

The caption plan of add_caption() in libobs/obs-output.c.h moves to libobs/obs-output-caption.c.h and is ported to rust/obs-output (module output_caption). The hybrid libobs links the Rust shims in place of the C (group libobs.ported-output-caption). The plan is three hidden functions on plain values:

  • obs_output_caption_begin(): the carrier for the encoder's codec (h264 AVC SEI, av1 AV1 metadata OBU, hevc HEVC suffix SEI with ENABLE_HEVC, else none) and the priority > 1 gate;
  • obs_output_caption_hevc_sei_header(): the 3-byte Annex B start code tried before the 4-byte one, and the suffix SEI's NAL unit header (type 40, the other bits of the packet's own header);
  • obs_output_caption_cc_keep(): the CEA-608 triplet filter (cc_type 0, no 0x8080 padding, no 0, odd parity in both bytes).

add_caption() keeps everything else: the caption track lookup and its log, the HEVC log, the deque pops, the allocations, every libcaption call, the new packet's DARRAY and obs_encoder_packet_release(), in their order.

The caption text that obs-websocket's SendStreamCaption reaches goes through caption_frame_from_text(), which stays in C. The ported filter sees only the CEA-608 triplets that sources queue with obs_output_caption().

No intentional differences. Kept, characterized, not endorsed:

  • a codec with no carrier still takes the caption off the queue;
  • negative priorities take a caption;
  • cc_valid is not looked at;
  • the AVC SEI goes after the whole packet (the C's TODO);
  • the HEVC start code check and header read do not look at the packet size. A packet under 6 bytes is read past its end. The shim reads what the C reads; the safe core takes a slice and gives None.

The last point is the possible intentional difference raised in #3074. It needs a size parameter in the C function as well, so it is not done here.

Commits

  1. Extraction d103f4b: behaviour-neutral. A scratch harness (not committed) compared master's inline code with the moved functions, with 0 differences on clang -O0 and -O2, with and without ASan/UBSan. It covered:

    • all 2^24 triplets;
    • 13 codec strings with HEVC on and off at 9 priorities;
    • 2,000,000 random HEVC packet heads.
  2. Characterization 8a5f1b3: test_output_caption_steps, 10 cases, the moved file on its own, every platform, ASan/UBSan. 13 of 14 planted mutants of the C are caught. The one missed drops the C's test for 0, which is equivalent: 0x00 0x00 fails the parity test too.

  3. RED 4143a7c: the core with todo!() bodies and 16 Tier 1 tests:

    • steps_*: the cmocka cases 1:1;
    • edge_*: the short packet, proptests against a model of the C, no panic on any input.

    All 16 fail.

  4. GREEN 4fe9b85: the core, three C ABI shims and the ported group. The symbols are in rust-exports.map, rust-hidden.txt and rust-unexports-macos.txt and stay hidden.

  5. Tier 3 6e73c73: the oracle build of the original C, and 8 parity tests comparing the shim, the C and the core exactly:

    • 22 codec strings with HEVC on and off at 17 priorities;
    • every 6-byte packet head over a 6-value alphabet, plus every header byte pair after each start code;
    • every pair under 12 first bytes;
    • proptests over any input.

    All 16 planted mutants of the Rust core are caught by Tier 1 and by Tier 3.

  6. Docs d75f657: the rust/README.md rows and naming.md. No docs/features.md line: no user-visible change.

Tests

  • macOS (aarch64): output_caption Tier 1 and Tier 3, 24/24. The cmocka test passes 10/10 with ASan/UBSan.
  • Linux gate host, stamped head d75f657: lint, rust-test, cross (Windows and macOS clippy and Dylint) and tier2-linux passed (test_output_caption_steps against the C, and the caption tests against the hybrid libobs and the C reference).
  • Real app: N/A. This is a behaviour-neutral port. test_output_caption_codec and test_output_caption_queue run the real libobs caption path on the hybrid libobs in Tier 2.
  • Test cost: Tier 1 0.5 s (the every-triplet case); Tier 3 under 1 s; the cmocka case under 0.2 s. No own threads.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: not applicable (behaviour-neutral port)
  • (Optional) Local gate passed and the pushed head is stamped
  • Warning or flag changes: not applicable
  • Intentional differences from C: none

🤖 Generated with Claude Code

vpacher and others added 6 commits October 30, 2026 15:26
Behaviour-neutral extraction before the Rust port (#3074). The caption
decisions of add_caption() in obs-output.c move to
libobs/obs-output-caption.c.h, as three functions on plain values
declared in the new libobs/obs-output-caption.h without EXPORT (hidden,
as -fvisibility=hidden keeps them):

- obs_output_caption_begin(codec, hevc, priority, &carrier): the carrier
  the encoder's codec gets (h264 AVC, av1 AV1, hevc HEVC when built with
  ENABLE_HEVC, anything else none) and the priority > 1 gate, which
  returns before the caption track is read;
- obs_output_caption_hevc_sei_header(data, header): the 3- or 4-byte
  Annex B start code and the suffix SEI's NAL unit header (type 40, the
  other bits of the packet's own header), read where the C read it;
- obs_output_caption_cc_keep(triplet, &cc_data): the CEA-608 triplet
  filter (cc_type 0 only, no 0x8080 padding, no 0, odd parity).

add_caption() keeps the caption track lookup and its log, the HEVC log,
every deque pop, bzalloc() and bfree(), every libcaption call, the new
packet's DARRAY, obs_encoder_packet_release() and the order of all of
them. ENABLE_HEVC reaches the plan as CAPTION_HEVC, undefined at the end
of the file.

Checked in a scratch harness (not committed): master's inline code
against the moved functions, every one of the 2^24 triplets, 13 codec
strings (the four names and near misses) with HEVC on and off at 9
priorities from INT_MIN to INT_MAX, and 2,000,000 random 6-byte HEVC
packet heads biased to both start codes, a partial one and none; clang
-O0 and -O2, with and without ASan/UBSan: 0 differences.

Refs #3074.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Characterization before the Rust port, against the C moved to
libobs/obs-output-caption.c: test_output_caption_steps (every platform,
the moved file on its own, ASan and UBSan), 10 cases:

- the carriers: h264 AVC, av1 AV1, hevc HEVC with ENABLE_HEVC;
- no carrier for hevc without ENABLE_HEVC and for 15 other names (empty,
  other codecs, other case, a trailing space, a prefix, a longer name);
- the priority gate from INT_MIN to INT_MAX, *carrier set either way
  (negative priorities take a caption: characterized, not endorsed);
- the suffix SEI header after a 3-byte and a 4-byte start code: type 40,
  the first byte's last bit and the second byte kept, a
  forbidden_zero_bit of 1 not carried over;
- the 3-byte start code tried first (00 00 01 00 01);
- no start code: 6 packet heads, header left as it was;
- the triplet filter: cc_type (cc_valid not looked at: characterized, not
  endorsed), padding and zero, parity of each byte, and all 2^24
  triplets against the rule, 64 * 16383 kept.

Not covered: an HEVC packet shorter than its start code and 2 header
bytes, which the C reads past (#3074).

Deliberate breaks of the moved C, 14 mutants (each codec name and the
HEVC switch, both priority bounds, the start code length, its order,
the SEI type, the header's bit mask and second byte, the cc_type mask,
padding, the zero test, parity, the cc_data word): 13 caught. The one
missed drops the test for 0, which is equivalent: 0x00 0x00 fails the
parity test too (noted in the test).

Refs #3074.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RED for #3074. obs_output::output_caption has its API and docs, with
todo!() bodies: begin() (the Carrier for the codec, and whether a packet
of that priority takes a caption), hevc_sei_header() (the start code and
the suffix SEI's header, None for no start code or a packet too short)
and cc_keep() (the CEA-608 triplet filter).

tests/output/output_caption.rs:
- steps_*: test_output_caption_steps.c 1:1 (10 cases, same names, values
  and expectations, every triplet included);
- edge_*: a packet that ends before its start code and header (None from
  the slice, where the C reads past it), and proptests of the three
  decisions against a model of the C (codecs from the four names, near
  misses and any bytes, any priority; packet heads with each start code,
  a partial one and none; any triplet), the header's NAL type, and any
  input without a panic.

All 16 fail on the stubs (obs-tests-core, output::output_caption).

Refs #3074.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for #3074. obs_output::output_caption implements the caption plan
of add_caption():

- begin(): h264 Avc, av1 Av1, hevc Hevc when hevc (ENABLE_HEVC) is set,
  any other name None, compared exactly; takes for priority 1 and below;
- hevc_sei_header(): 00 00 01 tried before 00 00 00 01, then type 40
  with the first header byte's last bit and the second byte; None
  without a start code or for a slice too short for the header;
- cc_keep(): cc_type 0, not 0x8080, not 0, odd parity in both bytes
  (libcaption's eia608_parity_varify(): a byte equals its parity-table
  entry exactly when it has an odd number of bits set).

The module denies clippy::todo now too. No intentional differences.

C ABI (ffi::output_caption): three shims on plain values.
obs_output_caption_hevc_sei_header() reads the first 3 bytes, the 4th
only after 00 00 00, then the start code and 2 header bytes, as the
C's memcmp() and reads may: like the C, it reads past a packet too short
for them.

libobs: obs-output-caption.c.h moves from libobs.core-output to its own
group libobs.ported-output-caption (registered in
build/ported-source-sets/output-caption.json), whose precompiled header
includes <string.h>, libcaption's <caption/eia608.h> (the parity
inline) and the unit's header. The hybrid libobs leaves the group out
and links the shims; the three symbols are listed in rust-exports.map,
rust-hidden.txt and rust-unexports-macos.txt and stay hidden.
ci/test_ported_sources.py lists the object.

Tier 1: 16/16 (obs-tests-core, output::output_caption).

Refs #3074.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 3 for #3074. obs-c-oracle builds the original
libobs/obs-output-caption.c.h as oracle_obs_output_caption_*
(oracle/output_caption.c).

tests/output/output_caption_parity.rs runs every call on the shim, the
C and the core and compares the results exactly, the outputs a false
return leaves alone included:

- the carrier and the gate: 22 codec strings (the four names, other
  codecs, other case, spaces, prefixes, longer names, a non-ASCII byte)
  with HEVC on and off at 17 priorities from INT_MIN to INT_MAX; every
  carrier occurs;
- the HEVC header: every 6-byte packet head over 00 01 02 40 41 FF
  (46,656, both start codes among them) and every header byte pair
  after each start code (2 x 65,536);
- the triplet filter: every pair under 12 first bytes (each cc_type,
  cc_valid set and clear, other bits);
- proptests over any codec string, priority, packet head and triplet.

No intentional differences; the shims are never given an HEVC packet
shorter than what the C reads (the core's slice case is in Tier 1).
8 tests.

Deliberate breaks of the core (16: each codec name and the HEVC switch,
both priority bounds, each start code, the header's bit mask, second
byte and type, the cc_type mask, padding, each byte's parity, the byte
order of the word and the parity test itself): all 16 caught here and
by Tier 1.

Refs #3074.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
rust/README.md: the obs-output crate row lists obs-output-caption.c.h,
and its own row says what moved, what obs-output.c keeps, the cases
kept and characterized (a codec with no carrier still drains the queue,
negative priorities, cc_valid unread, the AVC SEI after the packet, the
HEVC read past a packet under 6 bytes) and what Tier 3 covers.
docs/rust-port/naming.md maps the file to rust/obs-output.

No docs/features.md line: no user-visible change.

Refs #3074.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=dc311d0731fc8bc3f49e4f5dfbed93e3162c5ac3 secs=418 lanes=lint:run,rust-test:run,cross:run,tier2-linux:reused@f3e6236dcfe4,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793372580,"gate":"rust/all/fmt","host":"linux-x86_64","key":"759e6178ce66b610e837e7b129ad8c53d4272fe0413bdbdee8e348116b80b7b0","lane":"lint","parents":["6e73c73cbdd77a32239f9b6493d193c431ebcd56"],"secs":77,"stamp":"ec41dee1873b320dc2e1aa7c2cba2b49","tree":"dc311d0731fc8bc3f49e4f5dfbed93e3162c5ac3","v":1,"via":"run"}
Ci-Attestation: {"at":1793372580,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"759e6178ce66b610e837e7b129ad8c53d4272fe0413bdbdee8e348116b80b7b0","lane":"lint","parents":["6e73c73cbdd77a32239f9b6493d193c431ebcd56"],"secs":77,"stamp":"407e52ab9d3f0cc2dfd21ac2da62f035","tree":"dc311d0731fc8bc3f49e4f5dfbed93e3162c5ac3","v":1,"via":"run"}
Ci-Attestation: {"at":1793372580,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"154cf63845abd86b7f1807e35ccf9d354aa504bab17f02af4560650624624688","lane":"rust-test","parents":["6e73c73cbdd77a32239f9b6493d193c431ebcd56"],"secs":197,"stamp":"e525be825dac56fe56a3854c6879ce47","tree":"dc311d0731fc8bc3f49e4f5dfbed93e3162c5ac3","v":1,"via":"run"}
Ci-Attestation: {"at":1793372580,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"35798de8b25f216c9124186eaf145bd5ab7e37df8fbd704ba6b09d8fca6e1b8b","lane":"cross","parents":["6e73c73cbdd77a32239f9b6493d193c431ebcd56"],"secs":144,"stamp":"36ead7baa895ae60dcb63fbde45d1d28","tree":"dc311d0731fc8bc3f49e4f5dfbed93e3162c5ac3","v":1,"via":"run"}
Ci-Attestation: {"at":1793372580,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"35798de8b25f216c9124186eaf145bd5ab7e37df8fbd704ba6b09d8fca6e1b8b","lane":"cross","parents":["6e73c73cbdd77a32239f9b6493d193c431ebcd56"],"secs":144,"stamp":"a970b57f56fc39cd3f6db10ca5f52614","tree":"dc311d0731fc8bc3f49e4f5dfbed93e3162c5ac3","v":1,"via":"run"}
Ci-Attestation: {"at":1793372580,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"35798de8b25f216c9124186eaf145bd5ab7e37df8fbd704ba6b09d8fca6e1b8b","lane":"cross","parents":["6e73c73cbdd77a32239f9b6493d193c431ebcd56"],"secs":144,"stamp":"f5f36cbe72a46b98cf54440b04f9a6be","tree":"dc311d0731fc8bc3f49e4f5dfbed93e3162c5ac3","v":1,"via":"run"}
Ci-Attestation: {"at":1793372580,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"35798de8b25f216c9124186eaf145bd5ab7e37df8fbd704ba6b09d8fca6e1b8b","lane":"cross","parents":["6e73c73cbdd77a32239f9b6493d193c431ebcd56"],"secs":144,"stamp":"a854469f489b38b427beae3bb4dad2ca","tree":"dc311d0731fc8bc3f49e4f5dfbed93e3162c5ac3","v":1,"via":"run"}
Ci-Attestation: {"at":1793372108,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"f3e6236dcfe477a7e4f078bf8bd6ca5e206ab854cb1bc0323cae4845d1bd69ca","lane":"tier2-linux","parents":["6e73c73cbdd77a32239f9b6493d193c431ebcd56"],"secs":null,"stamp":"8aa3c3b4f9040c10d0f37b75f8cd94cf","tree":"dc311d0731fc8bc3f49e4f5dfbed93e3162c5ac3","v":1,"via":"reused"}
@vpacher
vpacher requested a review from zackees as a code owner October 30, 2026 15:04
@zackees
zackees added this pull request to the merge queue Oct 30, 2026
Merged via the queue into master with commit 89e5b4b Oct 30, 2026
37 checks passed
@zackees
zackees deleted the rust/3074-output-caption-plan branch October 30, 2026 17:09
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.

rust: Port output caption plan

2 participants