Skip to content

obs-graphics: Port the libnsgif frame decode and disposal (#2501) - #2503

Merged
zackees merged 5 commits into
masterfrom
port/2501-gif-frame
Oct 26, 2026
Merged

zackees merged 5 commits into
masterfrom
port/2501-gif-frame

Conversation

@vpacher

@vpacher vpacher commented Oct 26, 2026

Copy link
Copy Markdown
Member

Fixes #2501

Slice 2b of libnsgif: the frame pixel decode and disposal of libobs/graphics/libnsgif/gif.c.h, moved to gif-frame.c.h and ported to obs_graphics::gif_frame. gif.c stays C and keeps struct nsgif, the bitmap callbacks, the saved frame's allocation and the order of the steps. After #2488 (#2492).

What changed

  • Extraction (9e738350f): nsgif__decode with nsgif__decode_simple and nsgif__decode_complex and their helpers (deinterlace, next row, clip, jump over clipped data, the LZW result map), nsgif__restore_bg and nsgif__recover_frame move from gif.c.h to gif-frame.c.h. They take a plain canvas (struct nsgif_canvas: bitmap, image size, row span, background) and frame (struct nsgif_frame_area: rectangle, interlace, display, transparency, transparent index) in place of struct nsgif and struct nsgif_frame; gif.c.h keeps the three names as wrappers. The LZW calls stay calls through lzw.h. No change in behaviour: a differential run of the old (rust: Port the libnsgif GIF block parsers (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2a) #2488) and the new C over 40 000 generated and mutated GIFs, each scanned whole, in 7-byte steps, with nsgif_data_complete and into a bitmap with a row span wider than the image (95 076 frame decodes), gives the same results, frame info and decoded bitmaps for all 160 000 runs, also under AddressSanitizer and UBSan.
  • Port: rust/obs-graphics/src/gif_frame.rs (safe core) and src/ffi/gif_frame.rs (shim) for the three functions of gif-frame.h. The shim decodes on the safe decoder inside the LZW context of lzw.h (LzwCtx::decoder_mut, crate-internal). Not EXPORT, so the shims are hidden (rust-hidden.txt, rust-exports.map, rust-unexports-macos.txt).
  • gif-frame.c.h is in its own group libobs.ported-nsgif-frame with its own PCH, so the hybrid libobs drops the C. The MSVC /wd4244;/wd4267 covers the new group. ci/test_ported_sources.py, rust/README.md.
  • The C oracle's libnsgif now calls the Rust frame decode too, so the cargo image_file tests decode GIFs through it.

Kept, characterized, not endorsed

  • The restore and the recover work on width * height pixels from the start of the bitmap, and the restore steps rows by the image width, not the row span.
  • A frame that starts past the image or is clipped to nothing is a success and reads no data.
  • Noted in rust: Port the libnsgif frame decode and disposal (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2b) #2501, outside this slice (still C in gif.c.h): nsgif__update_bitmap sets decoded_frame before it compares it with NSGIF_FRAME_INVALID, so that check is never true; nsgif__recover_frame gets no NULL check for the saved frame, which is NULL only when nsgif__record_frame could not allocate it.

Intentional differences (C dereferences null or writes past the bitmap; gif.c never gets there)

  • Null pointers return NSGIF_ERR_DATA (the restore and the recover do nothing); a null bitmap or saved frame is empty; a data position past the data is NSGIF_ERR_END_OF_DATA.
  • Every write is checked against max(width, rowspan) * height pixels, the bitmap the client makes for the image.

RED → GREEN

  1. Extraction 9e738350f (above).
  2. Characterization d93cd4d67: nine frame_* cases in test_nsgif_decode through gif-frame.h, on streams from the LZW emitter and a canvas of sentinel pixels (the simple path, the complex path with clipping and a wider row span, the interlaced row order for 14 heights against GIF89a appendix E, transparency on both paths, frames past the image, the end of the data with and without the data-complete flag, bad codes, the restore and the recover). 29/29 on the C.
  3. RED 42ffaeabb: Tier 1 render::gif_frame, the nine cases 1:1 plus edge cases and proptests (no write outside the frame's clipped rectangle, the restore writes exactly that rectangle): 0 of 13 pass (todo!()).
  4. GREEN a5de475b0: 13 of 13, and Tier 3 render::gif_frame_parity against the C oracle (the file unmodified, functions renamed, its LZW calls on the C LZW oracle, so the Rust side is Rust on Rust and the C side all C): the struct layouts, and 20 000 random cases each for the decode and for the restore and recover, on canvases of 1×1 to 24×24 with row spans up to 3 wider, frames inside, across and past every edge, interlaced or not, with and without a transparent index, over emitter streams (with and without EOI, cut, behind random bytes) and random bytes, with and without complete data: the result and the whole bitmap match. 4 of 4 with the shim-only null pointer test.

Mutation check (done once, not committed): 11 of 12 applied mutants fail Tier 1 or Tier 3. One first survived (the simple path without the rowspan == width condition); a case for a full-width frame in a wider bitmap now catches it in all three tiers. The survivor is equivalent: decode_map returns LZW_OK when the output is full, so the C's final if (pixels == 0) ret = NSGIF_OK never changes a result.

port_comparison (M1 Max, one 1920×1080 frame; Rust frame code on the Rust LZW decoder, C on C): simple path 10.47 to 10.63 ms, C 10.20 to 10.29 ms; interlaced (complex) path 11.37 to 11.53 ms, C 9.74 to 9.81 ms. Most of the complex-path gap is the LZW stack decode of #2466, which its PR did not measure (only lzw_decode_map): lzw_decode alone on the same stream is 10.66 ms against 9.42 ms (measured once, not committed). The frame code adds about 4 %. We will look at the Decoder::decode regression next, with its own issue and PR (a port_comparison bench for lzw_decode first, then the fix), so this PR stays a port without changes to the LZW decoder.

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). test_nsgif_decode_rust runs the twenty-nine cmocka cases against the Rust shims (NSGIF_TEST_RUST: without lzw.c, gif-blocks.c and gif-frame.c, with AddressSanitizer and UBSan).
  • Exported symbols: 1766 on both; the three shims are local. The hybrid build has no gif-frame.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 17:30
The frame pixel decode of gif.c moves to gif-frame.c behind gif-frame.h
for the port (#2501): nsgif__decode with nsgif__decode_simple and
nsgif__decode_complex and their helpers (the deinterlace row order, the
clip, the jump over clipped data, the LZW result map), and the disposal
steps nsgif__restore_bg and nsgif__recover_frame. They take a plain
canvas (the bitmap, the image size, the row span, the background) and
the frame's rectangle and flags in place of struct nsgif and struct
nsgif_frame, which stay in gif.c with the bitmap callbacks, the saved
frame's allocation and the order of the steps; gif.c keeps the three
names as wrappers. The LZW calls stay calls through lzw.h. The helpers
get an nsgif_frame__ prefix, so they do not clash with gif.c's in the
unity group; gif.c keeps its own nsgif__error_from_lzw for
nsgif_data_scan.

Behaviour-neutral: the same code on the canvas fields. A differential
run of the old (#2488) and the new C over the 40 000 generated and
mutated GIFs of #2488, each scanned whole, in 7-byte steps, with
nsgif_data_complete and into a bitmap with a row span wider than the
image (95 076 frame decodes), gives the same results, frame info and
decoded bitmaps for all 160 000 runs, also under AddressSanitizer and
UBSan.

Refs #2501

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-frame.h (#2501)

Nine cases for gif-frame.c before its port, on code streams from the
LZW emitter and a canvas with sentinel pixels: a full-width frame on the
simple path (also lower down and clipped at the bottom); a frame with an
offset on the complex path, clipped on the right and at the bottom, in a
bitmap with a row span wider than the image; the interlaced row order for
heights 1 to 9, 15, 16, 17, 31 and 64 against GIF89a appendix E;
transparency on both paths and no transparent index above 0xFF; frames
past the image or clipped to nothing (a success that reads no data) and
the same bad data inside the image (a bad frame); data that ends with an
EOI, with a zero-length sub-block, and inside a sub-block (the end of the
data, a success once the data is complete); a code past the table and a
bad first code (a bad frame, also with complete data); the restore of the
whole image and of a frame's clipped rectangle to the background and to
transparent, and not for a frame that is not displayed or starts past the
image; the recover. 29/29 on the C.

Kept, characterized, not endorsed: the restore and the recover work on
width * height pixels from the start of the bitmap and step rows by the
image width, not the row span.

Refs #2501

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The safe core of libobs/graphics/libnsgif/gif-frame.c.h as
obs_graphics::gif_frame, with todo!() bodies: decode (the simple and the
complex path on the LZW Decoder of #2466), restore_bg and recover, on a
Canvas (the image size, the row span, the background) and a separate
pixel slice, and the #[repr(C)] FrameArea of gif-frame.h.

Tier 1 (render::gif_frame in obs-tests-graphics): the nine cmocka cases
one to one, edge cases (a pixel slice shorter than the canvas takes what
fits, a data position past the data reads nothing) and proptests (no
panic; the decode writes only inside the frame's rectangle clipped to the
image, or whole rows from the frame's row on the simple path; the
restore writes exactly the clipped rectangle of a displayed frame).

RED: 0 of 13 pass.

Refs #2501

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for obs_graphics::gif_frame: the safe core (decode on its two
paths, the simple one through Decoder::decode_map and the complex one
through Decoder::decode and the colour table with the clip, the jump
over clipped values and the interlaced row order; restore_bg and
recover; the LZW result map; the C's uint32_t arithmetic as wrapping
operations) and a thin hidden #[no_mangle] shim for the three functions
of gif-frame.h. The shim decodes on the safe decoder inside the LZW
context of lzw.h (LzwCtx::decoder_mut). gif-frame.c.h moves from
libobs.nsgif to its own group libobs.ported-nsgif-frame (own PCH), so the
hybrid libobs drops the C and libnsgif's gif.c (still C) calls the Rust
frame decode; the C oracle's libnsgif does the same in the cargo tests.

Tier 1: 13/13. Tier 3 (render::gif_frame_parity) against the unmodified
C in rust/obs-c-oracle/oracle/gif_frame.c, whose LZW calls go to the C
LZW oracle: the struct layouts, and 20 000 random cases each for the
decode and for the restore and recover, on canvases of 1 to 24 by 1 to 24
pixels with row spans up to 3 wider, frames inside, across and past
every edge, interlaced or not, with and without a transparent index, over
emitter streams (with and without EOI, cut, behind random bytes) and
random bytes, with and without complete data: the result and the whole
bitmap match. 4/4 with the shim-only null pointer test.
test_nsgif_decode_rust runs the twenty-nine cmocka cases against the
shims (NSGIF_TEST_RUST: without lzw.c, gif-blocks.c and gif-frame.c).

port_comparison (M1 Max, one 1920x1080 frame; the Rust side on the Rust
LZW decoder, the C side on the C one): simple path 10.47 to 10.63 ms,
C 10.20 to 10.29 ms; interlaced (complex) path 11.37 to 11.53 ms, C 9.74
to 9.81 ms. Of that gap, the LZW stack decode alone (lzw_decode on the
same stream, measured once and not committed) is 10.66 ms against
9.42 ms: the frame code adds about 4 %. The complex path branches on the
transparent index once per run of values, as the C.

Refs #2501

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

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

Coordinator review: the third libnsgif port, in the same shape as #2471 and #2492. The test_nsgif_decode edits only extend the Rust variant's list of left-out C units and add frame-decode and disposal cases; no assertion is removed. Queuing; it merges when CI is green.

The Windows Rust tests build the oracle with -W4 -WX. MSVC reports
'uncompressed' in nsgif_frame__decode_complex (gif-frame.c.h:218) as a
potentially uninitialized pointer. It is a false positive: available
starts at 0, so lzw_decode sets uncompressed before the first read. The
vendored code stays as it is; the oracle's warning pragma for it, which
already disables C4244 and C4267, now disables C4701 and C4703 too.

Refs #2501

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher

vpacher commented Oct 26, 2026

Copy link
Copy Markdown
Member Author

Follow-up from the benchmark note above: with #2505 (#2504) merged, the same bench gives simple 9.68 to 9.74 ms (C 10.38 to 10.50), interlaced 9.90 to 9.96 ms (C 9.85 to 10.06). The LZW stack decode was the gap; the frame path is now at or below the C.

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 frame decode and disposal (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2b)

2 participants