Repository navigation
libobs: Copy the packet in add_caption() only for codecs with captions - #1778
Merged
Merged
Conversation
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
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
force-pushed
the
fix/add-caption-leak
branch
from
October 22, 2026 10:42
468928b to
793ec40
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1777
What and why
add_caption()(libobs/obs-output.c.h) built the new packet inout_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 without_datastill allocated: one packet-sized leak per caption page and per CEA-708 batch. Details on #1777.out_datais now declared and built inside theif (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.add_caption()is still C. The interleaver port (libobs: Port the output interleaver to obs-output #1603) calls it throughobs_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.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-websocketSendStreamCaptionand decklink-captions send to the streaming output only, whose codecs are all h264, hevc or av1.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, liketest_output_interleave_restart, and is wired the same way intests.d/). It drives a real AV output through the public API, with a registered video encoder that reportsprores, one that reportsh264as 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 leavebnum_allocs()where it found it. On h264 the captions must also reach the delivered packets, which shows that the capture reachesadd_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.
text_captions_on_prores_free_the_copy3 != 0(3 pages, 3 allocations left)cea708_captions_on_prores_free_the_copy1 != 0(one batch, 1 allocation left)test_output_caption_codecRED → GREEN
test: Expect captions on a codec without caption support not to leak. On master,test_output_caption_codecfails 2 of 5 against libobs withOBS_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).test_output_caption_codecpasses 5/5 against all three libraries, 8 runs each. The output, caption and encoder tests also pass against all three:test_output_caption_queue2/2,test_output_caption_text32/32,test_caption_unmappable3/3,test_output_interleave30/30,test_output_interleave_e2e8/8,test_output_interleave_restart3/3,test_output_delay20/20,test_output_delay_e2e5/5, and the othertest_output_*,test_encoder_*,test_ffmpeg_output_*andtest_multitrack_*tests. The exported symbols of the three libraries are identical (1766).uv run ci/local_gate.py --lane lintpassed, and theci/test_cmocka_*checks pass.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
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (Fixed upstream bugs, Encoding, streaming and recording)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)