Repository navigation
obs-graphics: Port the libnsgif LZW decoder (#2466) - #2471
Merged
Merged
Conversation
Cases for lzw.c before its port to obs-graphics, in test_nsgif_decode. An emitter writes exact code streams with the decoder's own width rule (the width grows when an entry is added at table_size == code_max, up to 12; nothing is added once the table has 4096 entries): - lzw_roots_at_every_code_size: minimum code sizes 2 to 8, every root. - lzw_kwkwk_and_clear: a code equal to table_size; a clear resets the table and the width. - lzw_bad_codes: a code above table_size (LZW_BAD_CODE, the values before it written); EOI or a table code first after a clear (LZW_BAD_ICODE); a run of clears; minimum code size 11 works, 12 and 255 are refused. - lzw_end_of_data: the padding bits read as codes, then a zero-length sub-block (LZW_OK_EOD) or no more data (LZW_NO_DATA); the sub-block bounds on init. - lzw_codes_across_sub_blocks: 9- to 12-bit codes in sub-blocks of 1, 2, 3 and 255 bytes decode the same. - lzw_eoi_on_byte_boundary: the EOI code ending on a byte boundary at the end of the data at widths 3 to 12 (#2465). - lzw_full_table: after 4096 entries nothing is added and the width stays. - lzw_output_stack_full: lzw_decode's 4096-value stack, a record split across two calls. - lzw_decode_map_*: the colour table, a transparent index keeps the old pixel, no transparency at 256 and above, outputs of 1 and 0 values, LZW_BAD_PARAM without a table, LZW_NO_COLOUR after a plain init, and (kept, not endorsed) a plain init that fails early leaves an earlier colour table in place. 11/11 on the C (Linux x86_64). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The safe core of lzw.c in obs_graphics::lzw: Decoder with init, init_map, decode, output and decode_map, with todo!() bodies. The input is a slice on every call (the decoder keeps offsets, as the C keeps a pointer); the colour table is the caller's, as None for the C's null. Tier 1 in obs-tests-graphics (render/lzw.rs): the ten LZW cmocka cases of test_nsgif_decode one to one, with the same code emitter; edge cases (minimum code size 1, a start position inside the data); proptests (no input panics, the counts stay within the output; any root sequence at code sizes 2 to 8 in any sub-block size decodes back); a full table for 70 000 codes (a deferred clear). RED: 0 of 15 pass. Refs #2466 Co-Authored-By: Claude Opus 5.5 <[email protected]>
GREEN for obs_graphics::lzw: the safe core (Decoder: init, init_map, decode, decode_map, the code reader over sub-blocks, the table and the output stack, with the C's fixed-width arithmetic) and a thin hidden #[no_mangle] shim for the six functions of lzw.h. lzw.c.h moves from libobs.nsgif to its own group libobs.ported-nsgif-lzw (own PCH), so the hybrid libobs drops the C and libnsgif's gif.c (still C) calls the Rust decoder; the C oracle's libnsgif does the same in the cargo tests. The shim's context is a zeroed allocation (LZW_NO_MEM on failure, as malloc) holding the safe decoder and the C context's two pointers, the input from init and the colour table; init stores the input before it can fail and clears the colour table only on success, as the C does. Tier 1: 15/15. Tier 3 (render::lzw_parity) against the unmodified C in rust/obs-c-oracle/oracle/lzw.c: the enum and LZW_CODE_MAX, every minimum code size 0 to 13 in sub-blocks of 1, 2, 3, 4 and 255 bytes, and 20 000 random call sequences (init, init_map with and without a table, decode, decode_map with 0 to 300 outputs) over random bytes and emitter streams with clears, KwKwK and bad codes and truncation. 10/10 (with the shim-only null pointer test). Found by the parity test: decoding on after LZW_BAD_ICODE from a clear code inside the data, the C follows extends links it never initialized (the clear reset the table, prev_code still points past it); with the C context filled with 0xFF it differs at once, and under AddressSanitizer it is a wild read in lzw__write_fn. gif.c stops at the first error and inits every frame, so libobs never gets there. The parity test clears the C context after creation (oracle_lzw_context_fill), as the Rust one starts; the difference is listed as intentional. test_nsgif_decode_rust runs the eleven cmocka cases against the Rust shims (lzw.h as the boundary). port_comparison (M1 Max, a 1920x1080 frame through lzw_decode_map): 10.54 to 10.59 ms, C 10.19 to 10.28 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Member
|
Coordinator review: ports libnsgif's LZW decoder with the standard layout. A ported-group PCH replaces the C in the hybrid build, and it adds a C reference copy, a parity test against the C, a bench and the export maps. test_nsgif_decode.c, added a few hours ago in #2468, only gains cases, with no lines removed, so the regression image now runs against the Rust decoder too. Queuing; it merges when CI is green. |
zackees
enabled auto-merge
October 26, 2026 10:32
This was referenced Oct 26, 2026
6 of 7 tasks
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 #2466
Slice 1 of libnsgif: the LZW decoder,
libobs/graphics/libnsgif/lzw.c.h, ported toobs_graphics::lzw. libnsgif'sgif.cstays C and calls it throughlzw.h, unchanged. Built on the fixed C from #2468.What changed
rust/obs-graphics/src/lzw.rs(safe core) andsrc/ffi/lzw.rs(shim) for the six functions oflzw.h:lzw_context_create/_destroy,lzw_decode_init,lzw_decode_init_map,lzw_decode,lzw_decode_map. They are notEXPORT, so the shims are hidden (rust-hidden.txt,rust-exports.map,rust-unexports-macos.txt).lzw_context_createreturnsLZW_NO_MEMwhen the allocation fails, asmallocdoes. Init stores the input before it can fail and clears the colour table only on success, as the C does.lzw.c.hmoves fromlibobs.nsgifto its own grouplibobs.ported-nsgif-lzwwith its own PCH (<assert.h>,<stdbool.h>,<stddef.h>,<stdint.h>,<stdlib.h>,lzw.h), 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, built fromlibobs.nsgif+.h.c) now calls the Rust decoder too, so the cargoimage_filetests decode GIFs through it.Kept, characterized, not endorsed
table_size == code_max, up to 12; nothing is added once the table has 4096 entries, for any number of codes (a deferred clear).Intentional differences (C dereferences null or reads memory it never initialized;
gif.cnever gets there)LZW_BAD_PARAM; a null input reads as empty; decoding before any init reads no data.LZW_BAD_ICODEfrom a clear code inside the data. The clear resets the table, butprev_codekeeps a code past it, so later records followextendslinks the C never initialized. The parity test found this with the C context filled with 0xFF; under AddressSanitizer it is a wild read (BUS on unknown address) inlzw__write_fn(lzw.c.h:483).gif.cstops at the first result other thanLZW_OKand starts each frame with an init, so libobs never gets there. The Rust context starts zeroed and every table read is bounds-checked. The parity test clears the C context after creation (oracle_lzw_context_fill), so both sides start from the same memory.RED → GREEN
62fb21255: 10 new LZW cases intest_nsgif_decodewith a code emitter that follows the decoder's width rule (code sizes 2 to 8, KwKwK and clear, bad codes, end of data, 9- to 12-bit codes across sub-blocks of 1 to 3 bytes, EOI on a byte boundary at widths 3 to 12, the full table, the output stack across calls, the colour map and transparency, the colour table rules). 11/11 on the C.54b3f7703: Tier 1render::lzwinobs-tests-graphics, the 10 cmocka cases 1:1 plus edge cases (minimum code size 1, a start position inside the data, a full table for 70 000 codes) and proptests: 0 of 15 pass (todo!()).00b387c02: 15 of 15, and Tier 3render::lzw_parityagainst the C oracle (the file unmodified, functions renamed): the enum andLZW_CODE_MAX, every minimum code size 0 to 13 in sub-blocks of 1, 2, 3, 4 and 255 bytes, and 20 000 random call sequences (init, init_map with and without a table, decode, decode_map with 0 to 300 outputs) over random bytes and over emitter streams with clears, KwKwK and bad codes and truncation; 10 of 10 with the shim-only null pointer test.Mutation check (done once, not committed): 10 of 12 mutants fail Tier 1 or Tier 3, among them the #2465 bug itself (
byte_advancewithout- 1: 9 tests fail). The two survivors are equivalent: the fast path at exactly 24 remaining bits reads the same bytes as the slow path, and the first code after a clear cannot equal the clear code there. A third candidate (<=on the full table) is equivalent too:table_sizestops at 4097, which no 12-bit code reaches; the new deferred-clear test pins the behaviour anyway.port_comparison(M1 Max, a 1920×1080 frame throughlzw_decode_map: 8-bit roots, records of 2 to 40 values, a clear whenever the table is full): 10.54 to 10.59 ms, C 10.19 to 10.28 ms (+3 %). The bounds-checked table reads in the record loop are the difference; two attempts to hoist or batch them measured slower.Tier 2 (Linux x86_64, Debian 13)
ci/local_gate.py --lane lint) passes: clang-format, gersemi, ruff, the 225ci/test_*.pyself-tests, the ratchets, rustfmt, clippy-D warningsfor the workspace and every feature set, the Dylints.test_nsgif_decode,test_image_file,test_image_file_version,test_image_file_version_filetime,test_media_gif_last_frameand the newtest_nsgif_decode_rustpass on the hybrid libobs and onlibobs-c-reference(6/6 each; the loaded libobs checked withLD_DEBUG=libs).test_nsgif_decode_rustruns the eleven cmocka cases against the Rust shims (withoutlzw.c, with AddressSanitizer and UBSan).lzw.cobject.Checklist
master