Skip to content

rust: Port the libnsgif LZW decoder (graphics/libnsgif/lzw.c.h) to obs-graphics (libnsgif slice 1) #2466

Closed

Description

@vpacher

Unit

The LZW decoder of libnsgif, libobs/graphics/libnsgif/lzw.c.h (611 lines), into rust/obs-graphics as obs_graphics::lzw. Slice 1 of libnsgif. After #2465, so the port starts from the fixed C.

  • lzw_context_create / lzw_context_destroy;
  • lzw_decode_init (minimum code size, the GIF sub-block chain) and lzw_decode_init_map (the variant that writes pixels through a colour table, with the transparent index);
  • lzw_decode and lzw_decode_map: the code reader over sub-blocks (fast and slow path), the code table, clear and end-of-information codes, the output stack, and the results (LZW_OK, LZW_OK_EOD, LZW_NO_MEM, LZW_NO_DATA, LZW_EOI_CODE, LZW_NO_COLOUR, LZW_BAD_ICODE, LZW_BAD_PARAM, LZW_BAD_CODE).

gif.c.h stays C in this slice and calls the same lzw.h API.

Why libnsgif

GIFs come from outside OBS (image sources, slideshows, GIF stinger and transition files, files people share), and libnsgif is a third-party C decoder with dense index arithmetic. The LZW part is self-contained, needs no libobs seam and has a small C API, so a byte-exact differential test against the C is straightforward.

Phase and position

A library unit like #1907 (image-file), not Phase 6: no libobs core state and no seam. Crate obs-graphics (it exists). Next slices: gif.c.h (header, blocks, frame decode and disposal) behind the existing nsgif_* API, possibly in two parts (scan/info, frame decode). Not the gif crate from image-rs: it differs from libnsgif on damaged files, and the port keeps libnsgif's behaviour.

Seam

None: lzw.h is already the boundary. The Rust shim exports the same lzw_* functions; struct lzw_ctx becomes an opaque Rust context behind the pointer (gif.c.h only holds the pointer).

Existing tests

Plan

One PR:

  1. Characterization: the cases above against the C (through lzw.h), in the existing test binary for this category.
  2. RED: the safe core in obs-graphics with todo!(), Tier 1 tests (the cmocka cases 1:1, plus properties: no panic on any input, output length within the frame).
  3. GREEN: the core, a thin #[no_mangle] shim for lzw.h, the include moved to a ported group, Tier 3 against the unmodified C in rust/obs-c-oracle/ (byte-exact output and results for proptest-generated code streams and for the GIFs in the tree), and port_comparison numbers for a large frame.
  4. Tier 2: the GIF tests above with Rust off and on.

Activity

  1. vpacher commented on Oct 26, 2026

    @vpacher
    MemberAuthor

    Claiming: #2466

    Plan as in the description: one PR (characterization, RED, GREEN with Tier 3 against the C oracle and port_comparison, Tier 2). It starts after #2465, so the oracle is the fixed C.

  2. added
    ready
    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

ready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions