Skip to content

obs-graphics: Port the libnsgif LZW decoder (#2466) - #2471

Merged
zackees merged 3 commits into
masterfrom
port/2466-lzw
Oct 26, 2026
Merged

zackees merged 3 commits into
masterfrom
port/2466-lzw

Conversation

@vpacher

@vpacher vpacher commented Oct 26, 2026

Copy link
Copy Markdown
Member

Fixes #2466

Slice 1 of libnsgif: the LZW decoder, libobs/graphics/libnsgif/lzw.c.h, ported to obs_graphics::lzw. libnsgif's gif.c stays C and calls it through lzw.h, unchanged. Built on the fixed C from #2468.

What changed

  • rust/obs-graphics/src/lzw.rs (safe core) and src/ffi/lzw.rs (shim) for the six functions of lzw.h: lzw_context_create/_destroy, lzw_decode_init, lzw_decode_init_map, lzw_decode, lzw_decode_map. They are not EXPORT, so the shims are hidden (rust-hidden.txt, rust-exports.map, rust-unexports-macos.txt).
  • The safe core keeps offsets into the input, which every call passes as a slice; the C keeps a pointer. The shim's context is a zeroed allocation with the safe decoder and the C context's two pointers (the input from init, the colour table). lzw_context_create returns LZW_NO_MEM when the allocation fails, as malloc does. Init stores the input before it can fail and clears the colour table only on success, as the C does.
  • lzw.c.h moves from libobs.nsgif to its own group libobs.ported-nsgif-lzw with its own PCH (<assert.h>, <stdbool.h>, <stddef.h>, <stdint.h>, <stdlib.h>, lzw.h), so the hybrid libobs drops the C. The MSVC /wd4244;/wd4267 of the vendored code covers the new group. ci/test_ported_sources.py, rust/README.md.
  • The C oracle's libnsgif (obs_c_oracle_nsgif, built from libobs.nsgif+.h.c) now calls the Rust decoder too, so the cargo image_file tests decode GIFs through it.

Kept, characterized, not endorsed

  • The code width grows when an entry is added at table_size == code_max, up to 12; nothing is added once the table has 4096 entries, for any number of codes (a deferred clear).
  • A sub-block is refused when its size byte plus its length reach the end of the data.
  • The padding bits after the last code read as codes.
  • A failed init changes only what the C changed before it returned: a plain init that fails early keeps an earlier colour table.
  • A record that does not fit the output continues on the next call.

Intentional differences (C dereferences null or reads memory it never initialized; gif.c never gets there)

  • Null pointers return LZW_BAD_PARAM; a null input reads as empty; decoding before any init reads no data.
  • Decoding on after LZW_BAD_ICODE from a clear code inside the data. The clear resets the table, but prev_code keeps a code past it, so later records follow extends links the C never initialized. The parity test found this with the C context filled with 0xFF; under AddressSanitizer it is a wild read (BUS on unknown address) in lzw__write_fn (lzw.c.h:483). gif.c stops at the first result other than LZW_OK and starts each frame with an init, so libobs never gets there. The Rust context starts zeroed and every table read is bounds-checked. The parity test clears the C context after creation (oracle_lzw_context_fill), so both sides start from the same memory.

RED → GREEN

  1. Characterization 62fb21255: 10 new LZW cases in test_nsgif_decode with a code emitter that follows the decoder's width rule (code sizes 2 to 8, KwKwK and clear, bad codes, end of data, 9- to 12-bit codes across sub-blocks of 1 to 3 bytes, EOI on a byte boundary at widths 3 to 12, the full table, the output stack across calls, the colour map and transparency, the colour table rules). 11/11 on the C.
  2. RED 54b3f7703: Tier 1 render::lzw in obs-tests-graphics, the 10 cmocka cases 1:1 plus edge cases (minimum code size 1, a start position inside the data, a full table for 70 000 codes) and proptests: 0 of 15 pass (todo!()).
  3. GREEN 00b387c02: 15 of 15, and Tier 3 render::lzw_parity against the C oracle (the file unmodified, functions renamed): the enum and LZW_CODE_MAX, every minimum code size 0 to 13 in sub-blocks of 1, 2, 3, 4 and 255 bytes, and 20 000 random call sequences (init, init_map with and without a table, decode, decode_map with 0 to 300 outputs) over random bytes and over emitter streams with clears, KwKwK and bad codes and truncation; 10 of 10 with the shim-only null pointer test.

Mutation check (done once, not committed): 10 of 12 mutants fail Tier 1 or Tier 3, among them the #2465 bug itself (byte_advance without - 1: 9 tests fail). The two survivors are equivalent: the fast path at exactly 24 remaining bits reads the same bytes as the slow path, and the first code after a clear cannot equal the clear code there. A third candidate (<= on the full table) is equivalent too: table_size stops at 4097, which no 12-bit code reaches; the new deferred-clear test pins the behaviour anyway.

port_comparison (M1 Max, a 1920×1080 frame through lzw_decode_map: 8-bit roots, records of 2 to 40 values, a clear whenever the table is full): 10.54 to 10.59 ms, C 10.19 to 10.28 ms (+3 %). The bounds-checked table reads in the record loop are the difference; two attempts to hoist or batch them measured slower.

Tier 2 (Linux x86_64, Debian 13)

  • Lint lane (ci/local_gate.py --lane lint) passes: clang-format, gersemi, ruff, the 225 ci/test_*.py self-tests, the ratchets, rustfmt, clippy -D warnings for the workspace and every feature set, the Dylints.
  • test_nsgif_decode, test_image_file, test_image_file_version, test_image_file_version_filetime, test_media_gif_last_frame and the new test_nsgif_decode_rust pass on the hybrid libobs and on libobs-c-reference (6/6 each; the loaded libobs checked with LD_DEBUG=libs). test_nsgif_decode_rust runs the eleven cmocka cases against the Rust shims (without lzw.c, with AddressSanitizer and UBSan).
  • Exported symbols: 1766 on both; the six shims are local. The hybrid build has no lzw.c object.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: not applicable (a port with no change in behaviour for GIF decoding)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes: not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports)

vpacher and others added 3 commits October 26, 2026 10:45
Cases for lzw.c before its port to obs-graphics, in test_nsgif_decode. An
emitter writes exact code streams with the decoder's own width rule (the
width grows when an entry is added at table_size == code_max, up to 12;
nothing is added once the table has 4096 entries):

- lzw_roots_at_every_code_size: minimum code sizes 2 to 8, every root.
- lzw_kwkwk_and_clear: a code equal to table_size; a clear resets the
  table and the width.
- lzw_bad_codes: a code above table_size (LZW_BAD_CODE, the values before
  it written); EOI or a table code first after a clear (LZW_BAD_ICODE); a
  run of clears; minimum code size 11 works, 12 and 255 are refused.
- lzw_end_of_data: the padding bits read as codes, then a zero-length
  sub-block (LZW_OK_EOD) or no more data (LZW_NO_DATA); the sub-block
  bounds on init.
- lzw_codes_across_sub_blocks: 9- to 12-bit codes in sub-blocks of 1, 2,
  3 and 255 bytes decode the same.
- lzw_eoi_on_byte_boundary: the EOI code ending on a byte boundary at the
  end of the data at widths 3 to 12 (#2465).
- lzw_full_table: after 4096 entries nothing is added and the width stays.
- lzw_output_stack_full: lzw_decode's 4096-value stack, a record split
  across two calls.
- lzw_decode_map_*: the colour table, a transparent index keeps the old
  pixel, no transparency at 256 and above, outputs of 1 and 0 values,
  LZW_BAD_PARAM without a table, LZW_NO_COLOUR after a plain init, and
  (kept, not endorsed) a plain init that fails early leaves an earlier
  colour table in place.

11/11 on the C (Linux x86_64).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The safe core of lzw.c in obs_graphics::lzw: Decoder with init,
init_map, decode, output and decode_map, with todo!() bodies. The input
is a slice on every call (the decoder keeps offsets, as the C keeps a
pointer); the colour table is the caller's, as None for the C's null.

Tier 1 in obs-tests-graphics (render/lzw.rs): the ten LZW cmocka cases of
test_nsgif_decode one to one, with the same code emitter; edge cases
(minimum code size 1, a start position inside the data); proptests (no
input panics, the counts stay within the output; any root sequence at
code sizes 2 to 8 in any sub-block size decodes back); a full table for 70 000 codes (a deferred clear).

RED: 0 of 15 pass.

Refs #2466

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for obs_graphics::lzw: the safe core (Decoder: init, init_map,
decode, decode_map, the code reader over sub-blocks, the table and the
output stack, with the C's fixed-width arithmetic) and a thin hidden
#[no_mangle] shim for the six functions of lzw.h. lzw.c.h moves from
libobs.nsgif to its own group libobs.ported-nsgif-lzw (own PCH), so the
hybrid libobs drops the C and libnsgif's gif.c (still C) calls the Rust
decoder; the C oracle's libnsgif does the same in the cargo tests.

The shim's context is a zeroed allocation (LZW_NO_MEM on failure, as
malloc) holding the safe decoder and the C context's two pointers, the
input from init and the colour table; init stores the input before it can
fail and clears the colour table only on success, as the C does.

Tier 1: 15/15. Tier 3 (render::lzw_parity) against the unmodified C in
rust/obs-c-oracle/oracle/lzw.c: the enum and LZW_CODE_MAX, every minimum
code size 0 to 13 in sub-blocks of 1, 2, 3, 4 and 255 bytes, and 20 000
random call sequences (init, init_map with and without a table, decode,
decode_map with 0 to 300 outputs) over random bytes and emitter streams
with clears, KwKwK and bad codes and truncation. 10/10 (with the shim-only
null pointer test).

Found by the parity test: decoding on after LZW_BAD_ICODE from a clear
code inside the data, the C follows extends links it never initialized
(the clear reset the table, prev_code still points past it); with the C
context filled with 0xFF it differs at once, and under AddressSanitizer
it is a wild read in lzw__write_fn. gif.c stops at the first error and
inits every frame, so libobs never gets there. The parity test clears
the C context after creation (oracle_lzw_context_fill), as the Rust one
starts; the difference is listed as intentional.

test_nsgif_decode_rust runs the eleven cmocka cases against the Rust
shims (lzw.h as the boundary). port_comparison (M1 Max, a 1920x1080
frame through lzw_decode_map): 10.54 to 10.59 ms, C 10.19 to 10.28 ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher requested a review from zackees as a code owner October 26, 2026 10:27
@zackees

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

Coordinator review: ports libnsgif's LZW decoder with the standard layout. A ported-group PCH replaces the C in the hybrid build, and it adds a C reference copy, a parity test against the C, a bench and the export maps. test_nsgif_decode.c, added a few hours ago in #2468, only gains cases, with no lines removed, so the regression image now runs against the Rust decoder too. Queuing; it merges when CI is green.

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

2 participants