Repository navigation
Description
Unit
The GIF block parsers of libnsgif, from libobs/graphics/libnsgif/gif.c.h, into rust/obs-graphics as obs_graphics::gif_blocks. Slice 2a of libnsgif, after #2466 (the LZW decoder, merged in #2471). These functions read the bytes that nsgif_data_scan and nsgif_frame_decode get from the file, before any pixel is decoded:
nsgif__parse_header: theGIF87a/GIF89asignature, strict and lenient;nsgif__parse_logical_screen_descriptor: the screen size, the global colour table flag and size, the background index, the aspect ratio;nsgif__parse_frame_extensions: the extension loop, withnsgif__parse_extension_graphic_control(disposal, delay, transparency) andnsgif__parse_extension_application/nsgif__app_ext_is_loop_count(NETSCAPE2.0andANIMEXTS1.0loop count); other extensions are skipped by their sub-blocks;nsgif__parse_image_descriptor: the frame rectangle, the interlace and local colour table flags;nsgif__parse_colour_tablewithnsgif__colour_table_decode: the colour table offset and size, and the decode into the caller's pixel layout.
About 520 lines. The frame decode (nsgif__decode_simple, nsgif__decode_complex, disposal and nsgif__restore_bg, nsgif__recover_frame), the frame state machine (nsgif__process_frame, nsgif__next_displayable_frame) and the public nsgif_* functions stay C in this slice.
Why libnsgif, and why these functions first
GIFs come from outside OBS (image sources, slideshows, GIF stinger and transition files, files that people share). These parsers are the first code that reads those bytes: positions, lengths and flags that later code trusts. #2465 showed that this code can hold real bugs, and the parity test of #2466 found a read of uninitialized memory in the C. The vendored gif.c.h is upstream libnsgif release/1.0.0 with no change since; upstream has no later fix to src/gif.c (only upstream commit 22e99eb, an include in nsgif.h), so the port starts from the current C.
Phase and position
A library unit like #1907 (image-file) and #2466, not Phase 6: no libobs core state. Crate obs-graphics. Next slices: 2b, the frame decode and disposal; 2c, the frame state machine and the public nsgif_* API, which moves struct nsgif to Rust. Not the gif crate from image-rs: it differs from libnsgif on damaged files, and the port keeps libnsgif's behaviour.
Seam
These functions are static and take struct nsgif * and struct nsgif_frame *, which stay C until 2c. So the first step is an extraction in C, as #1905 did for the audio mix:
- Move the parsers to
libobs/graphics/libnsgif/gif-blocks.c.hbehind a small header,gif-blocks.h. Each function takes the data as a pointer, a length and a position, and returns its results in a plain struct (for examplestruct nsgif_screenfor the logical screen descriptor,struct nsgif_block_gcefor the graphic control extension) and annsgif_error. gif.c.hcalls them and copies the results intostruct nsgifandstruct nsgif_frame, as it does now inline. No change in behaviour: the existing tests and the characterization of step 2 run before and after the move.- The parsers that change state of
struct nsgif(the loop count, the colour table pointer) return the new value, andgif.c.hwrites it.
The Rust shim exports the gif-blocks.h functions; gif.c.h stays the only user.
Existing tests
test_nsgif_decode(bug: libnsgif: the last LZW code is lost when it ends on a byte boundary at the end of the data (upstream fix 2255b2f74) #2465, rust: Port the libnsgif LZW decoder (graphics/libnsgif/lzw.c.h) to obs-graphics (libnsgif slice 1) #2466): GIF files through thensgif_*API, and the LZW cases.test_image_file,test_media_gif_last_frameand the cargoimage_filetests decode GIFs through libobs: the Tier 2 for this slice.- No test calls the block parsers directly. New cases: both signatures and a bad one (strict and lenient), a short header and screen descriptor, global and local colour tables of every size (2 to 256 entries) and a table that ends early, the background index past the table, each disposal value (0 to 7), a delay below the minimum, a transparent index with the flag set and clear, a graphic control extension that is too short, the two loop count extensions and others with the same length, unknown extensions, a zero-size frame and a frame past the screen, the interlace flag, and data that ends inside each block.
Plan
One PR:
- Extraction: the parsers to
gif-blocks.c.h/gif-blocks.h, no change in behaviour. - Characterization: the cases above against the C, through
gif-blocks.h, intest_nsgif_decode. - RED: the safe core in
obs-graphicswithtodo!(), Tier 1 tests (the cmocka cases 1:1, plus properties: no panic on any input, positions never past the data). - GREEN: the core, a thin
#[no_mangle]shim forgif-blocks.h, the include moved to a ported group, Tier 3 against the unmodified C inrust/obs-c-oracle/(results, positions and decoded colour tables for random bytes and for generated blocks), andport_comparisonnumbers for a scan of a GIF with many frames. - 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: #2488
Plan as in the description: one PR (extraction to
gif-blocks.c.h, characterization, RED, GREEN with Tier 3 against the C oracle andport_comparison, Tier 2).obs-graphics: Port the libnsgif GIF block parsers (obs-rust#2488) (ob…
obs-graphics: Port the libnsgif frame decode and disposal (obs-rust#2501