Repository navigation
obs-graphics: Port the libnsgif frame decode and disposal (#2501) - #2503
Merged
Merged
Conversation
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]>
Member
zackees
enabled auto-merge
October 26, 2026 16:43
This was referenced Oct 26, 2026
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]>
Member
Author
6 of 7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2501
Slice 2b of libnsgif: the frame pixel decode and disposal of
libobs/graphics/libnsgif/gif.c.h, moved togif-frame.c.hand ported toobs_graphics::gif_frame.gif.cstays C and keepsstruct nsgif, the bitmap callbacks, the saved frame's allocation and the order of the steps. After #2488 (#2492).What changed
9e738350f):nsgif__decodewithnsgif__decode_simpleandnsgif__decode_complexand their helpers (deinterlace, next row, clip, jump over clipped data, the LZW result map),nsgif__restore_bgandnsgif__recover_framemove fromgif.c.htogif-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 ofstruct nsgifandstruct nsgif_frame;gif.c.hkeeps the three names as wrappers. The LZW calls stay calls throughlzw.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, withnsgif_data_completeand 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.rust/obs-graphics/src/gif_frame.rs(safe core) andsrc/ffi/gif_frame.rs(shim) for the three functions ofgif-frame.h. The shim decodes on the safe decoder inside the LZW context oflzw.h(LzwCtx::decoder_mut, crate-internal). NotEXPORT, so the shims are hidden (rust-hidden.txt,rust-exports.map,rust-unexports-macos.txt).gif-frame.c.his in its own grouplibobs.ported-nsgif-framewith its own PCH, so the hybrid libobs drops the C. The MSVC/wd4244;/wd4267covers the new group.ci/test_ported_sources.py,rust/README.md.image_filetests decode GIFs through it.Kept, characterized, not endorsed
width * heightpixels from the start of the bitmap, and the restore steps rows by the image width, not the row span.gif.c.h):nsgif__update_bitmapsetsdecoded_framebefore it compares it withNSGIF_FRAME_INVALID, so that check is never true;nsgif__recover_framegets no NULL check for the saved frame, which is NULL only whennsgif__record_framecould not allocate it.Intentional differences (C dereferences null or writes past the bitmap;
gif.cnever gets there)NSGIF_ERR_DATA(the restore and the recover do nothing); a null bitmap or saved frame is empty; a data position past the data isNSGIF_ERR_END_OF_DATA.max(width, rowspan) * heightpixels, the bitmap the client makes for the image.RED → GREEN
9e738350f(above).d93cd4d67: nineframe_*cases intest_nsgif_decodethroughgif-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.42ffaeabb: Tier 1render::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!()).a5de475b0: 13 of 13, and Tier 3render::gif_frame_parityagainst 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 == widthcondition); a case for a full-width frame in a wider bitmap now catches it in all three tiers. The survivor is equivalent:decode_mapreturnsLZW_OKwhen the output is full, so the C's finalif (pixels == 0) ret = NSGIF_OKnever 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 (onlylzw_decode_map):lzw_decodealone 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 theDecoder::decoderegression next, with its own issue and PR (aport_comparisonbench forlzw_decodefirst, then the fix), so this PR stays a port without changes to the LZW decoder.Tier 2 (Linux x86_64, Debian 13)
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_filetimeandtest_media_gif_last_framepass on the hybrid libobs and onlibobs-c-reference(6/6 each).test_nsgif_decode_rustruns the twenty-nine cmocka cases against the Rust shims (NSGIF_TEST_RUST: withoutlzw.c,gif-blocks.candgif-frame.c, with AddressSanitizer and UBSan).gif-frame.cobject.Checklist
master🤖 Generated with Claude Code