Repository navigation
obs-graphics: Port the libnsgif GIF block parsers (#2488) - #2492
Merged
Merged
Conversation
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]>
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. |
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 #2488
Slice 2a of libnsgif: the GIF block parsers of
libobs/graphics/libnsgif/gif.c.h, moved togif-blocks.c.hand ported toobs_graphics::gif_blocks.gif.cstays C, keepsstruct nsgifandstruct nsgif_frame, and calls the parsers through the newgif-blocks.h. After #2466 (the LZW decoder, #2471).What changed
51c7017c0): the header, the logical screen descriptor (with the screen size quirk ofnsgif_data_scan), the extensions before a frame (graphic control, theNETSCAPE2.0/ANIMEXTS1.0loop count, the others skipped by their sub-blocks), the image descriptor and the colour table extract and decode move fromgif.c.htogif-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.hkeeps its function names as wrappers that copy the values intostruct nsgifandstruct nsgif_frame.struct nsgif_colour_layoutmoves togif-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 withnsgif_data_complete, gives the same results, frame info, palettes, rectangles and decoded bitmaps for all 120 000 runs, also under AddressSanitizer and UBSan.rust/obs-graphics/src/gif_blocks.rs(safe core) andsrc/ffi/gif_blocks.rs(shim) for the seven functions ofgif-blocks.h. The result structs are the core's#[repr(C)]types. NotEXPORT, so the shims are hidden (rust-hidden.txt,rust-exports.map,rust-unexports-macos.txt).gif-blocks.c.his in its own grouplibobs.ported-nsgif-blockswith its own PCH, so the hybrid libobs drops the C. The MSVC/wd4244;/wd4267of the vendored code covers the new group.ci/test_ported_sources.py,rust/README.md.obs_c_oracle_nsgif) now calls the Rust parsers too, so the cargoimage_filetests scan GIFs through them.Kept, characterized, not endorsed
nsgif_data_scan) takes any version afterGIF.NSGIF_ERR_END_OF_DATA.Intentional differences (C dereferences null, wraps a length or writes past the table;
gif.cnever gets there)NSGIF_ERR_DATA(the enum has no parameter error) and write nothing; a null data pointer reads as empty.NSGIF_ERR_END_OF_DATA; the extension loop returns the end of the data). The C computes the remaining length insize_tand wraps.gif.cmakes only layouts with offsets 0 to 3.RED → GREEN
51c7017c0(above).c9abe5b9f: nineblocks_*cases intest_nsgif_decodethroughgif-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.cf14cbe02: Tier 1render::gif_blocks, the nine cases 1:1 plus edge cases and proptests: 0 of 14 pass (todo!()).7d080ddfb: 14 of 14, and Tier 3render::gif_blocks_parityagainst 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 == 0check after an introducer at the end of the data, the next check (bytes < 2) returns the sameNSGIF_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)
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; the loaded libobs checked withLD_DEBUG=libs).test_nsgif_decode_rustruns the twenty cmocka cases against the Rust shims (NSGIF_TEST_RUST: withoutlzw.candgif-blocks.c, with AddressSanitizer and UBSan).gif-blocks.cobject.Checklist
master🤖 Generated with Claude Code