Skip to content

libobs: Copy the packet in add_caption() only for codecs with captions - #1778

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/add-caption-leak
Oct 22, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/add-caption-leak

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1777

What and why

add_caption() (libobs/obs-output.c.h) built the new packet in out_data (a reference count, then the whole packet's data) before it checked the codec. For a codec other than h264, hevc and av1 it then took the caption off the queue, as intended, and returned false with out_data still allocated: one packet-sized leak per caption page and per CEA-708 batch. Details on #1777.

  • The fix. out_data is now declared and built inside the if (avc || hevc || av1) branch, the only place that uses it, where it replaces the packet. For any other codec nothing is allocated and the packet is left as it is. Everything else is unchanged: the caption is still taken off the queue on every codec, and h264, hevc and av1 packets get the same bytes as before.
  • C only. add_caption() is still C. The interleaver port (libobs: Port the output interleaver to obs-output #1603) calls it through obs_output_interleave_add_caption(), and the caption text port (libobs: Move the caption text wrap into obs-output-caption-text.c #1618) moved only the page wrap, so there is no Rust or oracle copy to keep in step.
  • Who can reach it. A plugin or script that sends captions (obs_output_output_caption_text1/2(), obs_output_caption()) to an output whose codec cannot carry them, for example a MOV recording with ProRes, or a plugin's own codec. The Captions tool, obs-websocket SendStreamCaption and decklink-captions send to the streaming output only, whose codecs are all h264, hevc or av1.
  • Compatibility. No format, public API, header or exported-symbol change (1766 in each library, identical).
  • Upstream. OBS master (95d5223b6) has the same code in libobs/obs-output.c (da_init(out_data) at line 1561, before the codec check). I found no upstream report.

New test: test/cmocka/test_output_caption_codec.c (Linux; it needs a display, like test_output_interleave_restart, and is wired the same way in tests.d/). It drives a real AV output through the public API, with a registered video encoder that reports prores, one that reports h264 as a control, and one audio encoder, with media fed by hand. Each case runs a warm-up capture and then a measured one: start, queue captions, 12 frames, stop. The measured capture must take every caption off the queue and leave bnum_allocs() where it found it. On h264 the captions must also reach the delivered packets, which shows that the capture reaches add_caption(). bnum_allocs() counts every thread, so the test waits for the count to settle (up to 3 s) before and after a capture: a leak never comes back, a late free does.

Headless (private Xvfb, fresh HOME), OpenGL renderer.

Build Case Result
master + RED, libobs (C backend), hybrid libobs, libobs-c-reference text_captions_on_prores_free_the_copy fails in each, 3 runs: 3 != 0 (3 pages, 3 allocations left)
the same cea708_captions_on_prores_free_the_copy fails in each, 3 runs: 1 != 0 (one batch, 1 allocation left)
the same no captions, and the two h264 cases pass
This branch, the three libraries test_output_caption_codec 5/5, 8 runs each

RED → GREEN

  • RED commit: 5570ea8 test: Expect captions on a codec without caption support not to leak. On master, test_output_caption_codec fails 2 of 5 against libobs with OBS_LIBOBS_BACKEND=C, and against the hybrid libobs and libobs-c-reference (HYBRID, OBS_BUILD_C_REFERENCE=ON): both ProRes cases leave the copies behind (3 for 3 text pages, 1 for one CEA-708 batch).
  • GREEN: 793ec40. test_output_caption_codec passes 5/5 against all three libraries, 8 runs each. The output, caption and encoder tests also pass against all three: test_output_caption_queue 2/2, test_output_caption_text 32/32, test_caption_unmappable 3/3, test_output_interleave 30/30, test_output_interleave_e2e 8/8, test_output_interleave_restart 3/3, test_output_delay 20/20, test_output_delay_e2e 5/5, and the other test_output_*, test_encoder_*, test_ffmpeg_output_* and test_multitrack_* tests. The exported symbols of the three libraries are identical (1766).
  • Also run: uv run ci/local_gate.py --lane lint passed, and the ci/test_cmocka_* checks pass.
  • Real app: not applicable. The app's own caption senders only reach h264, hevc and av1 outputs; the test drives the API path that plugins and scripts use.

Windows/macOS: the change is portable C in shared libobs code. The test is Linux only (it needs a display) and uses no fail_msg() or cmocka range assertions.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • 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: its line in docs/features.md is added or updated (Fixed upstream bugs, Encoding, streaming and recording)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (not applicable: no port here)

add_caption() copies the whole video packet into a new buffer before it
checks the codec. For a codec other than h264, hevc and av1 (ProRes, or
a plugin's own codec) it then takes the caption off the queue and
returns false without freeing the copy: one packet-sized allocation is
left behind for each caption page and each CEA-708 batch.

test_output_caption_codec drives a real AV output through the public
API, with a registered video encoder that reports "prores" and one that
reports "h264" as a control, queues captions with
obs_output_output_caption_text2() and obs_output_caption(), and expects
a capture to leave bnum_allocs() where it found it. On h264 the captions
must also reach the delivered packets, which shows the capture reaches
add_caption().

Refs obs-rust#1777
@rdoupe
rdoupe requested a review from zackees as a code owner October 22, 2026 10:42
add_caption() built the new packet (a reference count, then the old
packet's data) before it knew whether the codec can carry a caption.
For a codec other than h264, hevc and av1 it then took the caption off
the queue and returned false with the copy still allocated, so every
caption page and every CEA-708 batch on such a track leaked one
packet-sized buffer.

The copy is now made inside the h264/hevc/av1 branch, where it replaces
the packet. For any other codec the packet is left as it is and nothing
is allocated; the caption is still taken off the queue, as before.

Fixes obs-rust#1777
@rdoupe
rdoupe force-pushed the fix/add-caption-leak branch from 468928b to 793ec40 Compare October 22, 2026 10:42
@zackees
zackees added this pull request to the merge queue Oct 22, 2026
Merged via the queue into obs-rust:master with commit 6b27ac9 Oct 22, 2026
26 checks passed
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.

bug: libobs: add_caption() leaks a packet copy for each caption on a codec other than h264, hevc or av1

2 participants