Skip to content

rust: Port the libnsgif GIF block parsers (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2a) #2488

Closed

Description

@vpacher

Unit

The GIF block parsers of libnsgif, from libobs/graphics/libnsgif/gif.c.h, into rust/obs-graphics as obs_graphics::gif_blocks. Slice 2a of libnsgif, after #2466 (the LZW decoder, merged in #2471). These functions read the bytes that nsgif_data_scan and nsgif_frame_decode get from the file, before any pixel is decoded:

  • nsgif__parse_header: the GIF87a / GIF89a signature, strict and lenient;
  • nsgif__parse_logical_screen_descriptor: the screen size, the global colour table flag and size, the background index, the aspect ratio;
  • nsgif__parse_frame_extensions: the extension loop, with nsgif__parse_extension_graphic_control (disposal, delay, transparency) and nsgif__parse_extension_application / nsgif__app_ext_is_loop_count (NETSCAPE2.0 and ANIMEXTS1.0 loop count); other extensions are skipped by their sub-blocks;
  • nsgif__parse_image_descriptor: the frame rectangle, the interlace and local colour table flags;
  • nsgif__parse_colour_table with nsgif__colour_table_decode: the colour table offset and size, and the decode into the caller's pixel layout.

About 520 lines. The frame decode (nsgif__decode_simple, nsgif__decode_complex, disposal and nsgif__restore_bg, nsgif__recover_frame), the frame state machine (nsgif__process_frame, nsgif__next_displayable_frame) and the public nsgif_* functions stay C in this slice.

Why libnsgif, and why these functions first

GIFs come from outside OBS (image sources, slideshows, GIF stinger and transition files, files that people share). These parsers are the first code that reads those bytes: positions, lengths and flags that later code trusts. #2465 showed that this code can hold real bugs, and the parity test of #2466 found a read of uninitialized memory in the C. The vendored gif.c.h is upstream libnsgif release/1.0.0 with no change since; upstream has no later fix to src/gif.c (only upstream commit 22e99eb, an include in nsgif.h), so the port starts from the current C.

Phase and position

A library unit like #1907 (image-file) and #2466, not Phase 6: no libobs core state. Crate obs-graphics. Next slices: 2b, the frame decode and disposal; 2c, the frame state machine and the public nsgif_* API, which moves struct nsgif to Rust. Not the gif crate from image-rs: it differs from libnsgif on damaged files, and the port keeps libnsgif's behaviour.

Seam

These functions are static and take struct nsgif * and struct nsgif_frame *, which stay C until 2c. So the first step is an extraction in C, as #1905 did for the audio mix:

  • Move the parsers to libobs/graphics/libnsgif/gif-blocks.c.h behind a small header, gif-blocks.h. Each function takes the data as a pointer, a length and a position, and returns its results in a plain struct (for example struct nsgif_screen for the logical screen descriptor, struct nsgif_block_gce for the graphic control extension) and an nsgif_error.
  • gif.c.h calls them and copies the results into struct nsgif and struct nsgif_frame, as it does now inline. No change in behaviour: the existing tests and the characterization of step 2 run before and after the move.
  • The parsers that change state of struct nsgif (the loop count, the colour table pointer) return the new value, and gif.c.h writes it.

The Rust shim exports the gif-blocks.h functions; gif.c.h stays the only user.

Existing tests

Plan

One PR:

  1. Extraction: the parsers to gif-blocks.c.h / gif-blocks.h, no change in behaviour.
  2. Characterization: the cases above against the C, through gif-blocks.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 on any input, positions never past the data).
  4. GREEN: the core, a thin #[no_mangle] shim for gif-blocks.h, the include moved to a ported group, Tier 3 against the unmodified C in rust/obs-c-oracle/ (results, positions and decoded colour tables for random bytes and for generated blocks), and port_comparison numbers for a scan of a GIF with many frames.
  5. Tier 2: the GIF tests above with Rust off and on.

Activity

  1. vpacher commented on Oct 26, 2026

    @vpacher
    MemberAuthor

    Claiming: #2488

    Plan as in the description: one PR (extraction to gif-blocks.c.h, characterization, RED, GREEN with Tier 3 against the C oracle and port_comparison, Tier 2).

  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