Repository navigation
Description
Unit
The frame pixel decode and disposal of libnsgif, from libobs/graphics/libnsgif/gif.c.h, into rust/obs-graphics as obs_graphics::gif_frame. Slice 2b of libnsgif, after #2488 (the block parsers). These functions write the pixels of one frame into the client's bitmap:
nsgif__decodeand its two paths:nsgif__decode_simple(a full-width, not interlaced frame at x 0, throughlzw_decode_map) andnsgif__decode_complex(any rectangle, interlaced or not, clipped to the image, throughlzw_decodeand the colour table, with the transparent index), and the rule that makesNSGIF_ERR_END_OF_DATAa success once the data is complete;- the helpers they use:
nsgif__deinterlaceandnsgif__next_row(the four interlace passes),gif__clip,gif__jump_data,nsgif__error_from_lzw; - the disposal of the previous frame before a decode:
nsgif__restore_bg(the whole image, or the previous frame's clipped rectangle to transparent or to the background colour) andnsgif__recover_frame(the saved frame back).
About 350 lines. The bitmap callbacks (nsgif__initialise_sprite, nsgif__bitmap_*), nsgif__record_frame (it allocates the saved frame), nsgif__update_bitmap (the order of the steps) and the public nsgif_* functions stay C in this slice.
Why
GIFs come from outside OBS (image sources, slideshows, GIF stinger and transition files). This is the code that writes into the bitmap with offsets, rows and strides that come from the file: the frame rectangle, interlacing and clipping decide every write position. #2465 and the parity test of #2466 found bugs in libnsgif; this slice is the part where an out-of-range position would write outside the bitmap.
Phase and position
A library unit like #2466 and #2488, not Phase 6: no libobs core state. Crate obs-graphics. After this slice: 2c, the frame state machine and the public nsgif_* API (nsgif__process_frame, nsgif__update_bitmap, nsgif_data_scan, nsgif_frame_prepare, nsgif_frame_decode and the rest), which moves struct nsgif to Rust.
Seam
The same way as #2488: an extraction in C first, to libobs/graphics/libnsgif/gif-frame.c.h behind gif-frame.h.
- The decode takes the LZW context, the GIF data and the position of the frame's image data, a plain canvas (the bitmap pointer, the image width and height, the row span, the background colour), the frame's rectangle, interlace flag and transparent index, the colour table and the data-complete flag.
gif.c.hbuilds those fromstruct nsgifandstruct nsgif_frame. - The restore takes the canvas and the previous frame's rectangle, display and transparency flags (or none, for the whole image); the recover takes the canvas and the saved frame.
- No change in behaviour. The old and the new C are compared over generated and mutated GIFs with every frame decoded, as for rust: Port the libnsgif GIF block parsers (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2a) #2488.
The LZW calls stay calls through lzw.h: in the hybrid libobs they reach the Rust decoder of #2466; in the C oracle they reach the C one.
Existing tests
test_nsgif_decode: GIF files through thensgif_*API, the LZW cases (rust: Port the libnsgif LZW decoder (graphics/libnsgif/lzw.c.h) to obs-graphics (libnsgif slice 1) #2466) and the block parser cases (rust: Port the libnsgif GIF block parsers (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2a) #2488).test_image_file,test_media_gif_last_frameand the cargoimage_filetests decode GIFs through libobs: the Tier 2 for this slice.- New cases through
gif-frame.h: a frame at every position relative to the image (inside, on each edge, past each edge, zero size), clipping on each side, interlaced frames of heights 1 to 9 and larger, the simple and the complex path for the same pixels, transparency on and off, a row span wider than the image, data that ends inside the frame with and without the data-complete flag, every LZW result mapped to itsnsgif_error, and each disposal (restore to background with and without transparency, for a frame partly outside the image; recover).
Kept as found, to be characterized: nsgif__restore_bg steps rows by the image width, not the row span; nsgif__update_bitmap sets decoded_frame before it checks it against NSGIF_FRAME_INVALID, so that check is never true there; nsgif__recover_frame does not check the saved frame for NULL (only reachable when nsgif__record_frame could not allocate it).
Plan
One PR:
- Extraction to
gif-frame.c.h/gif-frame.h, no change in behaviour (old against new C). - Characterization: the cases above against the C, through
gif-frame.h, intest_nsgif_decode. - RED: the safe core in
obs-graphicswithtodo!(), Tier 1 tests (the cmocka cases 1:1, plus properties: no panic, no write outside the canvas for any rectangle and data). - GREEN: the core, a thin
#[no_mangle]shim forgif-frame.h, the include moved to a ported group, Tier 3 against the unmodified C inrust/obs-c-oracle/(the result and the whole canvas after each call, for random frames, data and canvases), andport_comparisonnumbers for the decode of a large frame on both paths. - Tier 2: the GIF tests above with Rust off and on.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
vpacher commentedon Oct 26, 2026
Claiming: #2501
Plan as in the description: one PR (extraction to
gif-frame.c.h, characterization, RED, GREEN with Tier 3 against the C oracle andport_comparison, Tier 2). It starts on top of #2492 (#2488) and is rebased onto master after that merges.obs-c-oracle: Silence MSVC C4701/C4703 in the vendored frame decode
obs-graphics: Speed up the LZW decoder's stack decode (obs-rust#2504) (…
obs-graphics: Port the libnsgif frame decode and disposal (obs-rust#2501