Skip to content

obs-graphics: Port the libnsgif GIF block parsers (#2488) - #2492

Merged
zackees merged 4 commits into
masterfrom
port/2488-gif-blocks
Oct 26, 2026
Merged

zackees merged 4 commits into
masterfrom
port/2488-gif-blocks

Conversation

@vpacher

@vpacher vpacher commented Oct 26, 2026

Copy link
Copy Markdown
Member

Fixes #2488

Slice 2a of libnsgif: the GIF block parsers of libobs/graphics/libnsgif/gif.c.h, moved to gif-blocks.c.h and ported to obs_graphics::gif_blocks. gif.c stays C, keeps struct nsgif and struct nsgif_frame, and calls the parsers through the new gif-blocks.h. After #2466 (the LZW decoder, #2471).

What changed

  • Extraction (51c7017c0): the header, the logical screen descriptor (with the screen size quirk of nsgif_data_scan), the extensions before a frame (graphic control, the NETSCAPE2.0/ANIMEXTS1.0 loop count, the others skipped by their sub-blocks), the image descriptor and the colour table extract and decode move from gif.c.h to gif-blocks.c.h. They take the data, its length and a byte position, and return plain values (struct nsgif_screen, struct nsgif_blocks_frame_ext, struct nsgif_blocks_image). gif.c.h keeps its function names as wrappers that copy the values into struct nsgif and struct nsgif_frame. struct nsgif_colour_layout moves to gif-blocks.h. No change in behaviour: a differential run of the old and the new C over 40 000 generated and mutated GIFs, each scanned whole, in 7-byte steps and with nsgif_data_complete, gives the same results, frame info, palettes, rectangles and decoded bitmaps for all 120 000 runs, also under AddressSanitizer and UBSan.
  • Port: rust/obs-graphics/src/gif_blocks.rs (safe core) and src/ffi/gif_blocks.rs (shim) for the seven functions of gif-blocks.h. The result structs are the core's #[repr(C)] types. Not EXPORT, so the shims are hidden (rust-hidden.txt, rust-exports.map, rust-unexports-macos.txt).
  • gif-blocks.c.h is in its own group libobs.ported-nsgif-blocks with its own PCH, 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) now calls the Rust parsers too, so the cargo image_file tests scan GIFs through them.

Kept, characterized, not endorsed

  • The lenient header (nsgif_data_scan) takes any version after GIF.
  • The graphic control extension does not check its size byte, and the skip after it starts from that byte. So with size 0 the fields are set and the result can be NSGIF_ERR_END_OF_DATA.
  • The extension fields and the loop count are written as each extension is parsed, also when a later extension then fails.
  • An extension size that skips past the end of the data is not an error: the position stops at the end.
  • Without decode (the frame decode pass), the image descriptor's separator is not checked.

Intentional differences (C dereferences null, wraps a length or writes past the table; gif.c never gets there)

  • Null pointers return NSGIF_ERR_DATA (the enum has no parameter error) and write nothing; a null data pointer reads as empty.
  • A position past the data reads nothing (NSGIF_ERR_END_OF_DATA; the extension loop returns the end of the data). The C computes the remaining length in size_t and wraps.
  • A colour layout offset that reaches past the table writes nothing. gif.c makes only layouts with offsets 0 to 3.

RED → GREEN

  1. Extraction 51c7017c0 (above).
  2. Characterization c9abe5b9f: nine blocks_* cases in test_nsgif_decode through gif-blocks.h, on hand-written bytes (header, screen descriptor, screen size quirk, colour tables of every size and two layouts, every disposal value, transparency, the loop count extensions, skipping comments, plain text and unknown labels, the image descriptor, and data that ends inside each block). 20/20 on the C.
  3. RED cf14cbe02: Tier 1 render::gif_blocks, the nine cases 1:1 plus edge cases and proptests: 0 of 14 pass (todo!()).
  4. GREEN 7d080ddfb: 14 of 14, and Tier 3 render::gif_blocks_parity against the C oracle (the file unmodified, functions renamed): the enum and every size and offset of the result structs, the screen size rule around every listed size, and 20 000 random cases per parser over random bytes and generated blocks (headers, screen descriptors, every extension kind with right and wrong sizes, image descriptors, colour tables with random layouts), cut at random lengths and read from random positions, with decode on and off; 8 of 8 with the two shim-only tests.

Mutation check (done once, not committed): 13 of 14 mutants fail Tier 1 or Tier 3. The survivor is equivalent: without the bytes == 0 check after an introducer at the end of the data, the next check (bytes < 2) returns the same NSGIF_ERR_END_OF_DATA; the Rust reads 0 past the data where the C would read out of bounds.

port_comparison (M1 Max, the blocks of a 1000-frame GIF: graphic control, comment, image descriptor and a 256-entry local colour table per frame): 138 to 140 µs, C 272 µs. The colour table decode writes each component as a shift of the native-endian word when the layout stays inside the entry, and byte by byte otherwise.

Tier 2 (Linux x86_64, Debian 13)

  • Lint lane (ci/local_gate.py --lane lint) passes.
  • test_nsgif_decode, test_nsgif_decode_rust, test_image_file, test_image_file_version, test_image_file_version_filetime and test_media_gif_last_frame 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 twenty cmocka cases against the Rust shims (NSGIF_TEST_RUST: without lzw.c and gif-blocks.c, with AddressSanitizer and UBSan).
  • Exported symbols: 1766 on both; the seven shims are local. The hybrid build has no gif-blocks.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 (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)

🤖 Generated with Claude Code

vpacher and others added 4 commits October 26, 2026 16:20
The block parsers of gif.c move to gif-blocks.c behind gif-blocks.h for
the port (#2488): the header, the logical screen descriptor (and the
screen size quirk of nsgif_data_scan), the extensions before a frame
(graphic control, the loop count application extensions, skipping the
others by their sub-blocks), the image descriptor and the colour table
extract and decode. They take the GIF data, its length and a byte
position in place of struct nsgif and a pointer, and return plain
values; gif.c keeps its function names as wrappers that copy the values
into struct nsgif and struct nsgif_frame, which stay in gif.c.
struct nsgif_colour_layout moves to gif-blocks.h.

Behaviour-neutral: the same code on offsets, with gif.c's pointer
difference kept as an int in the extension loop, and the extension
fields copied back also when a later extension returns an error, as gif.c
wrote them in place. A differential run of the old and the new C over
40 000 generated and mutated GIFs (header, screen descriptor, every
extension kind, image descriptors, colour tables, truncation), each
scanned whole, in 7-byte steps and with nsgif_data_complete, gives the
same results, frame info, palettes, frame rectangles and decoded bitmaps
for all 120 000 runs, also under AddressSanitizer and UBSan.

Refs #2488

Co-Authored-By: Claude Opus 5.5 <[email protected]>
#2488)

Nine cases for gif-blocks.c before its port, on hand-written bytes: the
header (strict and lenient, short, from a position inside the data), the
logical screen descriptor (every field, the size bits without the flag,
short), the screen size quirk (each reported size, the 0 and over-2048
rules and sizes next to them), the colour tables (every size from 2 to
256 entries, two layouts, one byte short, without decode, the plain
decode), the graphic control extension (every disposal value with the
4-as-3 quirk and the redraw rule, the delay, transparency set once and
kept, without decode, short), the loop count extensions (NETSCAPE2.0 and
ANIMEXTS1.0, 0 as forever and N as N+1, other identifiers, a wrong
sub-block, short), skipping comments, plain text and unknown labels, and
the image descriptor (the rectangle, interlace, the first frame growing
the image, 16-bit sums, a bad separator, short). 20/20 on the C.

Kept, characterized, not endorsed: the lenient header takes any version;
the graphic control extension does not check its size byte and the skip
starts from it, so its fields can be set with an error result;
extensions write the frame fields before a later extension fails; an
extension size that skips past the end of the data is not an error (the
position stops at the end); without decode the image descriptor's
separator is not checked.

Refs #2488

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The safe core of libobs/graphics/libnsgif/gif-blocks.c.h as
obs_graphics::gif_blocks, with todo!() bodies: header, screen,
screen_size_quirk, extensions, image_descriptor, colour_table and
colour_table_decode on the whole data and a byte position, with the
plain values of gif-blocks.h (Screen, FrameExt, Image, ColourLayout,
NsgifError in the C's order).

Tier 1 (render::gif_blocks in obs-tests-graphics): the nine cmocka cases
one to one, edge cases (a position past the data reads nothing, layout
offsets past the entry write into the next entry and nothing past the
table, the plain decode stops at the data) and proptests (no panic on any
input; positions move by the block size or not at all, extensions stay in
the data and change nothing without decode; the colour table length rule).

RED: 0 of 14 pass.

Refs #2488

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for obs_graphics::gif_blocks: the safe core (header, screen and
the screen size quirk, the extension loop with the graphic control and
loop count extensions, the image descriptor, the colour table and its
plain decode, with the C's position arithmetic, including its int
difference in the extension loop) and a thin hidden #[no_mangle] shim
for the seven functions of gif-blocks.h; the result structs are the
core's #[repr(C)] types. gif-blocks.c.h moves from libobs.nsgif to its
own group libobs.ported-nsgif-blocks (own PCH), so the hybrid libobs
drops the C and libnsgif's gif.c (still C) calls the Rust parsers; the
C oracle's libnsgif does the same in the cargo tests.
struct nsgif_screen keeps the screen descriptor's name apart from the
function, so the oracle's rename macro does not rename the struct.

Tier 1: 14/14. Tier 3 (render::gif_blocks_parity) against the unmodified
C in rust/obs-c-oracle/oracle/gif_blocks.c: the enum and every size and
offset of the result structs, the screen size rule around every listed
size, and 20 000 random cases per parser over random bytes and generated
blocks (headers, screen descriptors, every extension kind with right and
wrong sizes, image descriptors, colour tables with random layouts), cut
at random lengths and read from random positions, decode on and off:
8/8 with the two shim-only tests. test_nsgif_decode_rust runs the twenty
cmocka cases against the shims (NSGIF_TEST_RUST: without lzw.c and
gif-blocks.c).

port_comparison (M1 Max, the blocks of a 1000-frame GIF: graphic
control, comment, image descriptor, 256-entry local colour table):
138 to 140 us, C 272 us. The colour table decode writes each component
as a shift of the native-endian word when the layout stays inside the
entry (every layout gif.c makes), and byte by byte otherwise.

Refs #2488

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

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

Coordinator review: ports the GIF block parsers in the same shape as #2471, with a C reference copy, a parity test, a bench and the export maps. The edits to test_nsgif_decode.c (an obs-rust test from #2468) rename the build macro NSGIF_TEST_RUST_LZW to NSGIF_TEST_RUST, so the Rust variant also leaves out gif-blocks.c, and add new block-parser cases. No existing assertion is removed or changed. Queuing; it merges when CI is green.

@zackees
zackees enabled auto-merge October 26, 2026 15:42
@zackees
zackees added this pull request to the merge queue Oct 26, 2026
Merged via the queue into master with commit d4c276a Oct 26, 2026
36 checks passed
@zackees
zackees deleted the port/2488-gif-blocks branch October 26, 2026 16:29
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 GIF block parsers (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2a)

2 participants