Skip to content

libobs: Reject an image too large for a texture before decoding it - #1474

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/image-size-precheck
Oct 20, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/image-size-precheck

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1473

What and why

An Image source given a picture larger than the GPU's texture size limit now fails at once, with a log line that gives the size and the limit. Before, OBS decoded the whole picture, held up to three full-size copies (2.5 GB for a 20480x11520 PNG) and blocked the loading thread for seconds, and only then did the texture creation fail (upstream obsproject/obs-studio#3427, reported by micolous, closed as stale).

  • Nothing compared the picture's size with the device's limit before device_texture_create(). ffmpeg_image_init() called avformat_find_stream_info(), which decodes the whole picture to learn its size and pixel format. ffmpeg_image_decode() then decoded it again, and ffmpeg_image_reformat_frame() made more full-size copies.
  • gs_create() now records the device's texture size limit, and any thread can read it. The OpenGL module reports GL_MAX_TEXTURE_SIZE through a new optional export, device_get_max_texture_size. For Direct3D 11 and Metal, which don't have the export, libobs uses 16384, their API upper bound. Upstream maintainers pointed out that the limit differs between devices, so it is read from the device, not hard-coded.
  • The image decoder checks the size in its get_buffer2 callback, which the decoder calls after it has read the header and before it decodes any pixels. It also checks container dimensions (for example AVIF) before the decoder opens. An oversized picture is logged once: Image '<file>' is 20480x11520, larger than the maximum texture size of the graphics device (16384x16384); not loading it. On Windows, the WIC fallback isn't tried for such a picture.
  • For a single-stream picture whose codec the demuxer already knows (PNG, JPEG, WebP, BMP, TGA, TIFF, GIF, QOI, JPEG XL, JPEG 2000, PNM...), avformat_find_stream_info() is skipped. The size and pixel format now come from the decoded frame. Multi-stream files and unknown codecs still get the probe.
  • No change for pictures within the limit. The same 17 format/pixel-format cases decode to byte-identical data, size and format on master and this branch. With no graphics device (CPU-only callers), there is no check.
  • Not covered: Direct3D 11 at feature level 10.x has a limit of 8192. The 16384 bound doesn't catch pictures between 8192 and 16384 there, and they fail late, as they do today. An exact D3D11 value would need the export in libobs-d3d11, which can't be built here.

Headless (private Xvfb, private D-Bus, fresh HOME), OpenGL (llvmpipe, limit 16384). Image source added with obs-websocket CreateInput, peak RSS read from VmHWM:

Build Case Result
v0.1.0-beta.20261018 AppImage 20480x11520 PNG CreateInput blocks 9.3 s, peak RSS +2484 MiB, device_texture_create (GL) failed
v0.1.0-beta.20261018 AppImage 8000x8000 PNG loads, CreateInput 1.31 s, peak RSS +767 MiB
This branch 20480x11520 PNG fails in 0.01 s, peak RSS +4 MiB, logs the size and the limit
This branch 8000x8000 PNG loads, CreateInput 0.85 s, peak RSS +714 MiB
This branch 640x360 PNG loads, 0.05 s

RED → GREEN

  • RED commit: cd89b5e test: Fail an oversized image before decoding it. test_image_size_precheck (cmocka, Linux, core video with libobs-opengl under xvfb-run; it skips without a display) finds the device limit by creating 1-pixel-high textures. It writes PNGs row by row with zlib, so making them costs no memory. A 16385x8192 picture and a 16x16385 one must fail to load, with peak RSS growth of at most 32 MiB and a log line naming the size and "texture size". Pictures at the limit (16384x16, 16x16384) and a small one must load and get a texture. On master, 2 of 4 fail: too_wide_fails_before_decoding (peak RSS +1420 MiB, 0.74 s) and too_tall_fails_before_decoding (no log line).
  • GREEN: 52511a3. 4 of 4 pass, in 2 runs (16385x8192: 0.00 s, peak RSS +8 MiB). ctest -j4 (C lane, under xvfb-run): 318/318 passed, none skipped, including test_image_exif_orientation (JPEG/PNG/WebP decode without the probe). Format check against master with a small harness over gs_create_texture_file_data3(): 17/17 files are byte-identical. The changed code (graphics-ffmpeg, graphics.c, libobs-opengl) has no Rust port, so only the C lane was built.
  • Real app: the table above (Image sources added over obs-websocket, release AppImage and this branch).

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: its line in docs/features.md is added (Fixed upstream bugs, Sources and filters)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not applicable

@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 01:22
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 20, 2026
An Image source given a picture larger than the graphics device's
texture size limit decodes all of it, holds up to three full-size
copies, stalls the loading thread for seconds, and only then fails to
create the texture. The test finds the device limit, loads PNGs one
pixel wider and one pixel taller than it, and expects them to fail with
a log line naming the size and the limit, without the peak resident
memory growing. Pictures at the limit and small ones must still load.

Refs obs-rust#1473
An image larger than the graphics device's texture size limit was
decoded in full, with up to three full-size copies (2.5 GB for a
20480x11520 PNG), and stalled the loading thread for seconds before
the texture creation failed.

gs_create() now records the device's limit: GL_MAX_TEXTURE_SIZE from
the OpenGL module through a new optional device export, and the API
limit of 16384 for Direct3D 11 and Metal. The image decoder checks the
picture's size once the header has been read (in its get_buffer2
callback) and gives up before decoding pixels, logging the size and
the limit. With no graphics device the check is off.

For a single-stream picture the demuxer already names the codec, so
avformat_find_stream_info() is skipped: it decoded the whole picture
once more, without any size check, only to learn its size and pixel
format, which now come from the decoded frame.

Fixes obs-rust#1473
@zackees
zackees force-pushed the fix/image-size-precheck branch from 52511a3 to afac6ed Compare October 20, 2026 02:12
@zackees
zackees enabled auto-merge October 20, 2026 02:12
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
Merged via the queue into obs-rust:master with commit 9502f2e Oct 20, 2026
23 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: an image too large for a texture is fully decoded before it fails, using GBs and freezing OBS (upstream #3427)

2 participants