Skip to content

rust: Port the libnsgif frame decode and disposal (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2b) #2501

Closed

Description

@vpacher

Unit

The frame pixel decode and disposal of libnsgif, from libobs/graphics/libnsgif/gif.c.h, into rust/obs-graphics as obs_graphics::gif_frame. Slice 2b of libnsgif, after #2488 (the block parsers). These functions write the pixels of one frame into the client's bitmap:

  • nsgif__decode and its two paths: nsgif__decode_simple (a full-width, not interlaced frame at x 0, through lzw_decode_map) and nsgif__decode_complex (any rectangle, interlaced or not, clipped to the image, through lzw_decode and the colour table, with the transparent index), and the rule that makes NSGIF_ERR_END_OF_DATA a success once the data is complete;
  • the helpers they use: nsgif__deinterlace and nsgif__next_row (the four interlace passes), gif__clip, gif__jump_data, nsgif__error_from_lzw;
  • the disposal of the previous frame before a decode: nsgif__restore_bg (the whole image, or the previous frame's clipped rectangle to transparent or to the background colour) and nsgif__recover_frame (the saved frame back).

About 350 lines. The bitmap callbacks (nsgif__initialise_sprite, nsgif__bitmap_*), nsgif__record_frame (it allocates the saved frame), nsgif__update_bitmap (the order of the steps) and the public nsgif_* functions stay C in this slice.

Why

GIFs come from outside OBS (image sources, slideshows, GIF stinger and transition files). This is the code that writes into the bitmap with offsets, rows and strides that come from the file: the frame rectangle, interlacing and clipping decide every write position. #2465 and the parity test of #2466 found bugs in libnsgif; this slice is the part where an out-of-range position would write outside the bitmap.

Phase and position

A library unit like #2466 and #2488, not Phase 6: no libobs core state. Crate obs-graphics. After this slice: 2c, the frame state machine and the public nsgif_* API (nsgif__process_frame, nsgif__update_bitmap, nsgif_data_scan, nsgif_frame_prepare, nsgif_frame_decode and the rest), which moves struct nsgif to Rust.

Seam

The same way as #2488: an extraction in C first, to libobs/graphics/libnsgif/gif-frame.c.h behind gif-frame.h.

  • The decode takes the LZW context, the GIF data and the position of the frame's image data, a plain canvas (the bitmap pointer, the image width and height, the row span, the background colour), the frame's rectangle, interlace flag and transparent index, the colour table and the data-complete flag. gif.c.h builds those from struct nsgif and struct nsgif_frame.
  • The restore takes the canvas and the previous frame's rectangle, display and transparency flags (or none, for the whole image); the recover takes the canvas and the saved frame.
  • No change in behaviour. The old and the new C are compared over generated and mutated GIFs with every frame decoded, as for rust: Port the libnsgif GIF block parsers (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2a) #2488.

The LZW calls stay calls through lzw.h: in the hybrid libobs they reach the Rust decoder of #2466; in the C oracle they reach the C one.

Existing tests

Kept as found, to be characterized: nsgif__restore_bg steps rows by the image width, not the row span; nsgif__update_bitmap sets decoded_frame before it checks it against NSGIF_FRAME_INVALID, so that check is never true there; nsgif__recover_frame does not check the saved frame for NULL (only reachable when nsgif__record_frame could not allocate it).

Plan

One PR:

  1. Extraction to gif-frame.c.h / gif-frame.h, no change in behaviour (old against new C).
  2. Characterization: the cases above against the C, through gif-frame.h, in test_nsgif_decode.
  3. RED: the safe core in obs-graphics with todo!(), Tier 1 tests (the cmocka cases 1:1, plus properties: no panic, no write outside the canvas for any rectangle and data).
  4. GREEN: the core, a thin #[no_mangle] shim for gif-frame.h, the include moved to a ported group, Tier 3 against the unmodified C in rust/obs-c-oracle/ (the result and the whole canvas after each call, for random frames, data and canvases), and port_comparison numbers for the decode of a large frame on both paths.
  5. Tier 2: the GIF tests above with Rust off and on.

Activity

  1. vpacher commented on Oct 26, 2026

    @vpacher
    MemberAuthor

    Claiming: #2501

    Plan as in the description: one PR (extraction to gif-frame.c.h, characterization, RED, GREEN with Tier 3 against the C oracle and port_comparison, Tier 2). It starts on top of #2492 (#2488) and is rebased onto master after that merges.

  2. self-assigned this
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions