Skip to content

rust: Port the libnsgif state machine and public API (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2c) #2510

Closed

Description

@vpacher

Unit

The rest of libnsgif, libobs/graphics/libnsgif/gif.c.h, into rust/obs-graphics as obs_graphics::gif: the GIF state machine and the public nsgif_* API of nsgif.h. Slice 2c, the last one of libnsgif, after #2466 (LZW decoder), #2488 (block parsers) and #2501 (frame decode and disposal). With it, the hybrid libobs has no C left from libnsgif.

About 945 lines of code (with comments 1453).

Why

GIFs come from outside OBS (image sources, slideshows, GIF stinger and transition files). This part holds the frame table, the positions into the data across incremental scans, and the frame order and loop count that decide which frame is decoded next. After 2a and 2b the parsers and the pixel writes are Rust; the state that indexes into them is the last C.

Phase and position

A library unit like #2466, #2488 and #2501. Crate obs-graphics. gs_image_file is the only user of the API: the C libobs/graphics/image-file.c.h, and the Rust port of it (#1907, obs_graphics::image_file), which calls nsgif_* through extern "C". Both keep calling the C ABI in this slice; moving the Rust image_file onto the safe core directly can be a follow-up.

Seam

None needed: nsgif.h is the boundary, and nsgif_t is opaque to its callers. struct nsgif becomes a Rust struct behind the pointer.

  • nsgif_get_info and nsgif_get_frame_info return pointers into the state (nsgif_info_t, nsgif_frame_info_t), so those are #[repr(C)] in the Rust state, and a frame info pointer stays valid until the next scan, as with the C's realloc.
  • The bitmap callbacks (nsgif_bitmap_cb_vt: create, destroy, get_buffer, set_opaque, test_opaque, modified, get_rowspan; each may be NULL except the ones the C asserts) become a trait in the safe core; the shim adapts the C function pointers.
  • The C oracle's libnsgif (obs_c_oracle_nsgif, used by the cargo image_file tests) stops building gif.c.h in the hybrid, so nsgif_* resolves to the Rust shims there too, as for the LZW decoder.

Existing tests

Plan

One PR:

  1. Characterization: the cases above against the C, through nsgif.h, in test_nsgif_decode.
  2. RED: the safe core in obs-graphics with todo!(), Tier 1 tests (the cmocka cases 1:1, plus properties: no panic on any data and any call order, the frame count never shrinks, pointers from nsgif_get_info stay valid).
  3. GREEN: the core, a thin #[no_mangle] shim for nsgif.h, the group libobs.nsgif removed from the hybrid (moved to a ported group), Tier 3 against the unmodified C in rust/obs-c-oracle/ (every result, every info and frame info field, the palettes and every decoded bitmap, for the generated GIFs in whole, incremental and complete scans), and port_comparison numbers for the scan and the full decode of an animated GIF.
  4. Tier 2: the GIF tests above with Rust off and on.

If the PR turns out too large to review, it can split into the scan and info half and the prepare and decode half; both need struct nsgif in Rust, so the split would be by commits in one PR rather than two seams.

Activity

  1. vpacher commented on Oct 26, 2026

    @vpacher
    MemberAuthor

    Claiming: #2510

    Plan as in the description: one PR (characterization, RED, GREEN with Tier 3 against the C oracle and port_comparison, Tier 2). It starts on top of #2503 (#2501) 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