Skip to content

Loading large PNG images temporarily consumes large amounts of RAM and causes OBS to be non-responsive #3427

Closed as not planned

Description

@micolous

Adding an Image or Image Slide Show source with an extremely large PNG image file (eg: 20480 x 11520px) temporarily consumes a large amount of RAM and causes OBS to become non-responsive, until a later stage rejects the image for being too large.

The non-responsiveness then triggers audio-video de-synchronisation for the remainder of the broadcast with an active RTMP (and probably other) source, as OBS' stream output continues to send audio but not new video frames.

Platform

Operating system and version: Windows 10 Pro 64-bit (Build 1903)
OBS Studio version: 25.0.8, 64-bit, installed from obsproject.com

Expected Behavior

Excessively large images should be rejected early, when parsing the image metadata, without loading the entire image as a bitmap into RAM. An ideal upper limit would be something like the larger of 2x the size of the output frame size, or 4K.

Current Behavior

Based on observed memory usage patterns, it appears that OBS still decompresses the entire PNG image into a buffer, then allocates 2 or 3 copies of this buffer (RAM usage for 1 copy of the image would be about 900MiB, but we saw more than 2.6GiB), before deciding that the image is way too large, and replacing it with a translucent frame.

The amount of time OBS locks up for varies depending on the complexity of the image. Using an image with a solid colour and basic text decompresses very quickly, and releases memory quickly. Using an image consisting entirely of random noise takes much longer to decompress.

The log file reports:

17:22:58.055: User switched to scene 'Scene 2'
17:23:05.782: device_texture_create (D3D11): Failed to create 2D texture (80070057)

The error appears to be thrown from

hr = device->device->CreateTexture2D(&td, data ? srd.data() : NULL,
texture.Assign());
if (FAILED(hr))
throw HRError("Failed to create 2D texture", hr);

E_INVALIDARG (0x80070057) | An invalid parameter was passed to the returning function.

This suggests that the loading process goes very far along without any checks, and D3D is simply rejecting it out of hand.

During the "loading" time, even if the image source is removed or the scene is changed again, the image is still loading in the background and consuming RAM and CPU, blocking scene changes and further source manipulation.

Steps to Reproduce

  1. In your favourite image editor, create a new image of 20480 x 11520px

  2. Add some content to this image, such that it can be seen on a black background (eg: draw some text and coloured lines).

    To see memory usage for longest, use your image editor's Hurl Noise function with maximum randomness of colour values.

  3. Save this image as a PNG file. In my tests, simple content came out at about 1.7MiB, whereas random noise came out at 459MiB.

  4. Start OBS, and start Task Manager to track the process' memory usage.

  5. In a new OBS scene, add a new Image or Image Slideshow source.

  6. Fit the source to the frame (right-click -> Transform -> Fit to screen), and then lock it by clicking the padlock 🔒 .

  7. Edit the properties of the Image or Image Slideshow and set the Image File to the large PNG created in step 3.

For Image sources, enabling Unload image when not showing allows you to more easily measure the amount of time it takes for images to load, and causes OBS to retry loading the image after a scene change, without needing to manually change the image source.

Additional information

Switching between a scene with a large, random image source takes about 10 seconds.

Switching between two scenes with image sources, with Unload image when not showing enabled, and one of the image sources are large, takes about 21 seconds to change.

This issue is easy to unintentionally trigger, and occurred in production for a live event I was involved with. For example, if an image intended for an overlay was created at 1920x1080 millimetres (=22677x12756 px at 300 PPI), instead of 1920x1080 pixels.

Even Windows "open file" dialog renders the thumbnails of such images correctly, so there's no way for the operator to know they're about to break OBS, especially if the file was supplied by a third party.

Activity

  1. changed the title [-]Loading large PNG images temporarily consumes large amounts of RAM[/-] [+]Loading large PNG images temporarily consumes large amounts of RAM and causes OBS to be non-responsive[/+] on Sep 7, 2020
  2. devjack commented on Sep 7, 2020

    @devjack

    I would also second this - it occurred during an event I was running recently. Third party graphics provided last minute caused the issue as they skipped a pre-production QA process. Having checks prior to loading would be extremely valuable in ensuring the stability of OBS during live events.

  3. micolous commented on Sep 8, 2020

    @micolous
    Author

    I've been trying to understand the failure modes of this issue better.

    It looks like gs_image_file_init_internal outright rejects some very large images much faster, in particular, the 1920x1080mm (22677x12756px) one:

    13:06:43.736: Failed to decode frame for 'test slide 1920mm.png': Invalid data found when processing input
    13:06:43.737: gs_image_file_init_internal: Failed to load file 'test slide 1920mm.png'
    13:06:43.737: [image_source: 'Image'] failed to load texture 'test slide 1920mm.png'
    

    Looking at gs_image_file_init_internal, for non-GIF files this calls out to gs_create_texture_file_data, and then later checks the result (which is where our later two error comes from):

    image->texture_data = gs_create_texture_file_data(
    file, &image->format, &image->cx, &image->cy);
    if (mem_usage) {
    *mem_usage += image->cx * image->cy *
    gs_get_format_bpp(image->format) / 8;
    }
    image->loaded = !!image->texture_data;
    if (!image->loaded) {
    blog(LOG_WARNING, "Failed to load file '%s'", file);
    gs_image_file_free(image);
    }

    gs_create_texture_file_data appears to have two platform-specific implementations, and I'm presuming on Windows it uses ffmpeg. The first error message comes from ffmpeg_image_decode:

    if (ret < 0) {
    blog(LOG_WARNING, "Failed to decode frame for '%s': %s",
    info->file, av_err2str(ret));
    goto fail;
    }

    Unwinding the stack a little, this would indicate ffmpeg_image_init successfully completed, and we tried to allocate some memory for the full image size here:

    data = bmalloc(image.cx * image.cy * 4);

    Using the ffprobe utility, I can see ffmpeg rejects this image very early:

    [IMGUTILS @ 0x7fffc96df3e0] Picture size 12756x22677 is invalid
    [png @ 0xc7d020] [IMGUTILS @ 0x7fffc96df050] Picture size 22677x12756 is invalid
    [png @ 0xc7d020] Invalid image size
    [png_pipe @ 0xc7bc80] decoding for stream 0 failed
    [png_pipe @ 0xc7bc80] Could not find codec parameters for stream 0 (Video: png, none(pc)): unspecified size
    Consider increasing the value for the 'analyzeduration' and 'probesize' options
    test slide 1920mm.png: End of file
    

    This appears to be thrown from av_image_check_size2, which limits the image to < 2^31 bytes total:

    https://github.com/FFmpeg/FFmpeg/blob/9faae05ddf27369838fbd500468484af31f1219c/libavutil/imgutils.c#L300-L303

    However, using ffprobe against the 20480x11520px random image works fine (although it uses about 1.8GB of RAM for about 6 seconds):

    Input #0, png_pipe, from '20480px-hurl.png':
      Duration: N/A, bitrate: N/A
        Stream #0:0: Video: png, rgba(pc), 20480x11520 [SAR 11811:11811 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
    

    Another limit here is in D3D11_TEXTURE2D_DESC, which has a cap of 16384x16384 pixels on all textures, which is where device_texture_create (D3D11): Failed to create 2D texture (80070057) came from.

    I created some other test images, and observed different results:

    Both of those I'd expect OBS to reject somewhere in ffmpeg_image_init.

  4. micolous commented on Sep 8, 2020

    @micolous
    Author

    tl;dr on above, there are multiple limits:

    1. PNG has an image dimension limit of (2^32)-1 pixels, according to the spec. I haven't been able to make an image that big yet to check for crashes there.

    2. ffmpeg has an (approximately) 2^31 byte image buffer limit.

      If it goes over, av_image_check_size2 says no (and the image won't display at all in OBS), but you still have OBS allocating a width * height * 4 byte buffer in gs_create_texture_file_data temporarily. If that bmalloc fails, OBS will crash with "out of memory".

    3. I think there's additional format conversion happening inside of OBS which I haven't really dug in to. Those temporarily allocate more memory, which could also cause OBS to crash with "out of memory".

    4. D3D has a limit of 16384x16384 pixels on textures.

      If it's larger than that, you'll get device_texture_create (D3D11): Failed to create 2D texture (80070057). OBS may become non-responsive and spike RAM usage for several seconds (from the earlier steps), and you may get A/V desynchronisation once it unlocks, which matches the behaviour we observed in our event.

      However, large images which are smaller than this limit can result in a crash, deep within in D3D. The scene can also be saved by OBS, causing it to be permanently broken without renaming PNG files or hacking scene configuration files manually.

    We got very lucky when this issue hit us on a 20480x11520px image:

    • our image was small enough that ffmpeg would process it
    • we had enough RAM in our OBS machine that it could allocate several buffers for the format conversion steps
    • the image was large enough that it was rejected by D3D early with "invalid argument", instead of crashing in D3D or GPU vendor driver code

    This could be mitigated in OBS with some simple checks in ffmpeg_image_init.

    PS: it also looks like any bmalloc() failure will result in a crash:

    void *bmalloc(size_t size)
    {
    void *ptr = alloc.malloc(size);
    if (!ptr && !size)
    ptr = alloc.malloc(1);
    if (!ptr) {
    os_breakpoint();
    bcrash("Out of memory while trying to allocate %lu bytes",
    (unsigned long)size);
    }
    os_atomic_inc_long(&num_allocs);
    return ptr;
    }

    ...and there's no way for OBS code handle an out-of-memory condition gracefully.

  5. RytoEX commented on Sep 8, 2020

    @RytoEX
    Member

    Another limit here is in D3D11_TEXTURE2D_DESC, which has a cap of 16384x16384 pixels on all textures, which is where device_texture_create (D3D11): Failed to create 2D texture (80070057) came from.

    Minor clarification:
    The texture size limit is actually determined by the D3D level used on the rendering device created by OBS. 16384 is the maximum dimension for textures using 11_0. The maximum value for 10_1 or 10_0 is 8192.
    https://docs.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-devices-downlevel-intro

  6. kkartaltepe commented on Apr 1, 2022

    @kkartaltepe
    Collaborator

    Leaving this open but marking it as not a bug generally. I dont think we will ever attempt to make OBS not crash when memory allocations fail, so those cases are all working as intended. Large video memory allocations failing and causing drivers to crash is also not fixable by us.

    Finally large images causing long hangs and delays are unlikely to change, the image source is intended to stall rendering while it loads right now. Possibly a change to make it load async, but there are more issues to resolve in such an implementation.

  7. micolous commented on Apr 1, 2022

    @micolous
    Author

    I dont think we will ever attempt to make OBS not crash when memory allocations fail, so those cases are all working as intended. Large video memory allocations failing and causing drivers to crash is also not fixable by us.

    Sure, that's reasonable, but OBS can avoid getting to that unsafe position. As detailed in my earlier comments, the size validation checks OBS does are far too late to prevent this class of issue. That is a bug.

    By checking the image metadata before sending it off to the graphics stack, OBS could check if the image is larger than some arbitrary "very large" amount (say, 8192x4096px), pop up a warning:

    The image source (name) is very large (width x height pixels), and loading it may crash OBS, or cause it to become unresponsive.

    Many graphics drivers crash loading large images, or your computer may run out of RAM. OBS has no way to know what will happen, and there is nothing OBS can do to fix this.

    Do you want to load this image anyway, potentially crashing OBS?

    Load anyway Cancel (default is cancel)

    Have an option in the settings somewhere if someone wants to skip this prompt, potentially shooting themselves in the foot. 😄

    As you've noticed, there's multiple crash reports that are definitely caused by loading large images.

    The incident that prompted this issue report started as a regular occurrence in live events: an innocent, last minute change. Unfortunately, the author of the graphics made an unintentional error that nearly took the whole event offline – and nobody involved had any warning. We got very lucky that it merely locked up the mixer for a minute or so, but it caused a huge panic for everyone involved.

    Processing asynchronously would be nice to have (but complicated), but the bare minimum OBS could do is warn about these sorts of landmines (and that part is easier).

  8. Fenrirthviti commented on Apr 1, 2022

    @Fenrirthviti
    Member

    We already have similar sanity checks in place for the actual canvas size, I don't think it's unreasonable to include them for sources.

    Just to confirm, these limits are universal across all systems? Or is there variance that adds complexity to that kind of programmatic check?

  9. RytoEX commented on Apr 1, 2022

    @RytoEX
    Member

    We already have similar sanity checks in place for the actual canvas size, I don't think it's unreasonable to include them for sources.

    Just to confirm, these limits are universal across all systems? Or is there variance that adds complexity to that kind of programmatic check?

    I mentioned this previously, but D3D texture size limits vary. Feature Level 10_0 and 10_1 are limited to 8192x8192 for 2D textures, while Feature Level 11_0 through 12_2 are limited to 16384x16384. If I recall correctly, we still currently support Feature Level 10_0 in the codebase, though we generally advertise that DX 10.1 is the minimum supported version. I'm not clear on if our minimum supported feature level is 10_0 or 10_1, but in either case, we still support 8192x8192 2D textures as possible maximum.

    OpenGL texture size limits seem to vary between 1024, 2048, 4096, 8192, and 16384, depending on GL_MAX_TEXTURE_SIZE. It seems that 8192 and 16384 are most common these days. I'll defer to @kkartaltepe on the subject though.

  10. kkartaltepe commented on Apr 2, 2022

    @kkartaltepe
    Collaborator

    I think last we checked all hardware that supports opengl 3.2/3.3 (on mesa) supported 8k textures, but 16k support is rare for older hardware.

  11. Fenrirthviti commented on Oct 14, 2024

    @Fenrirthviti
    Member

    Closing as stale. If this is still an issue please open a new issue with updated reproduction steps on the latest version of OBS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions