Skip to content

libnsgif: Resume a scan at the frame's start after the end of the data (#2515) - #2516

Merged
zackees merged 4 commits into
masterfrom
fix/2515-gif-scan-resume
Oct 27, 2026
Merged

zackees merged 4 commits into
masterfrom
fix/2515-gif-scan-resume

Conversation

@vpacher

@vpacher vpacher commented Oct 26, 2026

Copy link
Copy Markdown
Member

Fixes #2515

A scan of a GIF in parts lost the GIF when a part ended inside a frame: every later nsgif_data_scan returned NSGIF_ERR_DATA_FRAME, also with all the data. This PR resumes the scan at the frame's start, in the vendored C and in obs_graphics::gif. Stacked on #2514 (it changes obs_graphics::gif); the diff of this PR is the last two commits.

Cause

nsgif__process_frame (scan mode) saved the position where the frame's parse stopped as the resume point, also after NSGIF_ERR_END_OF_DATA. By then the frame's extensions, image descriptor and colour table were behind that position, so the next scan read the frame's image data, or an extension's block terminator, as the next block. Upstream libnsgif has the same code; OBS was not affected, since gs_image_file scans whole files.

Fix

After NSGIF_ERR_END_OF_DATA in a frame, the next scan starts at the frame's start (frame_offset, kept already) and reads the whole frame again:

cleanup:
	if (!decode) {
		if (ret == NSGIF_ERR_END_OF_DATA) {
			gif->buf_pos = frame->frame_offset;
		} else {
			gif->buf_pos = pos - gif->buf;
		}
	}
  • A second read of the frame writes the same fields; the loop count of an application extension is set, not added.
  • Other errors keep their position, as before.
  • The lzw_data_length that a partial scan adds up stays as it was: only nsgif_data_complete reads it, as "greater than 0".

The same change is in libobs/graphics/libnsgif/gif.c.h (still built in libobs-c-reference and the C oracle) and in Gif::process_frame, so Tier 3 (render::gif_parity, which scans in parts) keeps comparing like with like.

RED → GREEN

  1. RED 8b1e38bc6: gif_scan_incremental (cmocka and Tier 1) expects a scan in two parts, split at every byte of the test's three-frame GIF, and a byte-by-byte scan to give the info and frame info of a whole scan. It characterized the bad frame before (rust: Port the libnsgif state machine and public API (graphics/libnsgif/gif.c.h) to obs-graphics (libnsgif slice 2c) #2510). Fails at byte 51 (the last byte of frame 0's graphic control extension), in the C and in Rust.
  2. GREEN cceaecbc6: the fix; gif_scan_incremental passes in both, Tier 3 3/3. The README row of obs-graphics::gif no longer lists the old behaviour as kept.

Tier 2 (Linux x86_64, Debian 13)

  • Lint lane (ci/local_gate.py --lane lint) passes.
  • test_nsgif_decode (39/39 on the C), test_nsgif_decode_rust (39/39 on the shims, with AddressSanitizer and UBSan), test_image_file, test_image_file_version, test_image_file_version_filetime and test_media_gif_last_frame pass on the hybrid libobs and on libobs-c-reference (6/6 each).

Checklist

  • Base is master (after obs-graphics: Port the libnsgif state machine and public API (#2510) #2514)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: not applicable (OBS scans whole GIF files)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes: not applicable
  • Intentional differences from C: none (the fix is in both)

🤖 Generated with Claude Code

@vpacher
vpacher requested a review from zackees as a code owner October 26, 2026 18:23
@zackees

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

Coordinator note: this is stacked on #2514, and #2514's Rust Tier 2 on windows-2025-vs2026 is red right now (run 40375738347). I'll review the last two commits and queue this after #2514 lands and this is rebased onto master.

@zackees

zackees commented Oct 27, 2026

Copy link
Copy Markdown
Member

Coordinator tick (2026-10-27). Two things keep this PR from the queue: it is DIRTY against master, and its Windows Tier 2 job fails in this PR's own code.

1. Windows Tier 2 failure (run 40377242953): the canonical-manifest build panics.

thread 'native_libobs::windows_tests::core_builds_from_the_canonical_manifest' panicked at
rust/obs-build/tests/libobs/windows.rs:34:89:
called `Result::unwrap()` on an `Err` value: "compiler options refer to sources outside this target"
...
test result: FAILED. 0 passed; 1 failed
Exception: rust/tools/tier2-validate/validate.ps1:190: Cargo Windows core contract failed (101)

The message comes from rust/obs-build/src/lib.rs:560: some SourceCompileOptions entry names a source that is not part of the target it is attached to. This PR moves the libnsgif group from LIBOBS_SOURCES to LIBOBS_PORTED_SOURCES (libobs/build/source-sets.json) and drops the matching SourceCompileOptions from rust/obs-build/src/platform/libobs_windows.rs, while libobs/cmake/os-windows.cmake now sets the /wd options on build/libobs.ported-nsgif-gif+.h.c instead. So the Windows manifest and the Rust-side source list disagree about which target owns that file; the C build is fine, the Rust canonical-manifest build is not. This is the PR's to fix - it needs the ported group in the same target the rest of libobs.ported-nsgif-{blocks,frame,lzw}+.h.c live in, and no SourceCompileOptions entry pointing outside its target.

2. Rebase: master generates the bench-unit list (#1997), so port_comparison.rs conflicts.

Take master's rust/obs-c-oracle/benches/port_comparison.rs whole: rust/obs-c-oracle/build.rs now emits the mod lines and the modules![...] call from the directory, so the hand-kept list in this branch (and the gif entry added to it) is obsolete and your port_comparison/gif.rs is discovered automatically.

3. Stacked on #2514. This PR carries #2514's commits, so it should be rebased onto master only after #2514 lands; rebasing now drags the pre-rebase copies of those commits in again.

I did not push a rebase: it needs a force-push and this session cannot rewrite a remote branch.

@zackees

zackees commented Oct 27, 2026

Copy link
Copy Markdown
Member

Windows Tier 2 fails on this head, and it is the manifest move in this PR rather than a flake.

called `Result::unwrap()` on an `Err` value: "compiler options refer to sources outside this target"

The string comes from rust/obs-build/src/lib.rs:560, the check that fails when a SourceCompileOptions entry is left over after the target's source list is built — i.e. a per-source option annotates a file that is not in the target.

The only manifest-side change in this PR is the libnsgif move, and it lines up with exactly that check: libobs/build/source-sets.json takes build/libobs.nsgif+.h.c out of LIBOBS_SOURCES and puts build/libobs.ported-nsgif-gif+.h.c into LIBOBS_PORTED_SOURCES, while rust/obs-build/src/platform/libobs_windows.rs now points the /wd4244 /wd4267 SourceCompileOptions at the ported file. On the Windows C reference target the annotated file is not among the expanded sources (the Linux jobs and the non-canonical Windows jobs all pass), so the annotation is left dangling.

Everything else is green — 36 passed, and the linux/macOS Tier 2 legs, the cmocka test and the oracle benches are fine.

Two other things while I have the pen:

@vpacher

vpacher commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

Thanks, on it. That run (40377242953) is from when this branch was stacked on the pre-fix head of #2514. The fix is in #2514 as a21966652 ("obs-build: Drop the Windows warning options of the ported libnsgif group"): libobs_windows.rs no longer has a SourceCompileOptions entry for libnsgif, and os-windows.cmake keeps the /wd4244;/wd4267 options on all four libobs.ported-nsgif-* groups together. On the new #2514 head (4261f81d2), Windows Rust Tier 2 passes, including the canonical-manifest contract, which runs before the Cargo CTest suite.

I will rebase this PR onto master after #2514 merges (conflicts expected only in rust/README.md, where the gif.c.h row now comes from #2514), retest, and force-push. No action needed from you.

On the design question: the gif group follows the same shape as the blocks, frame and lzw groups that are already on master, so I would keep it like that. If you prefer the LIBOBS_SOURCES + Rust-variant exclusion shape, it should be a change for all four groups together, in a separate PR.

vpacher and others added 3 commits October 27, 2026 04:12
…2515)

gif_scan_incremental (cmocka) and render::gif::gif_scan_incremental
(Tier 1) now expect a scan in two parts, split at every byte of the
writer's three-frame GIF, and a byte-by-byte scan to give the info and
frame info of a whole scan, with the end of the data until the first
frame is complete and a success after. They characterized the bad frame
before (#2510).

RED: a split inside a frame's image data, local colour table or on an
extension's last byte (the first is byte 51) gives NSGIF_ERR_DATA_FRAME
for good, in the C and in Rust.

Refs #2515

Co-Authored-By: Claude Opus 5.5 <[email protected]>
#2515)

nsgif__process_frame kept the position where a frame's parse stopped as
the resume point of the next nsgif_data_scan, also when the parse ended
with NSGIF_ERR_END_OF_DATA. By then the frame's extensions, image
descriptor and colour table were behind that position, so the next scan
read the frame's image data (or an extension's terminator) as the next
block: NSGIF_ERR_DATA_FRAME from then on, also with all the data. A
caller that scans a GIF in parts lost it whenever a part ended inside a
frame. OBS scans whole files; upstream libnsgif has the same code.

Now a scan that ends with the end of the data in a frame resumes at the
frame's start (frame_offset, kept already), and the next scan reads the
whole frame again. A second read writes the same frame fields, and the
loop count of an application extension is set, not added. The
lzw_data_length that a partial scan adds up stays as it was; only
nsgif_data_complete reads it, as "greater than 0".

Fixed in the vendored C (libobs/graphics/libnsgif/gif.c.h, still built in
libobs-c-reference and the C oracle) and in obs_graphics::gif, so the
Tier 3 parity (render::gif_parity, which scans in parts) keeps comparing
like with like.

GREEN: gif_scan_incremental (cmocka and Tier 1) passes; Tier 3 3/3.

Fixes #2515

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zackees
zackees force-pushed the fix/2515-gif-scan-resume branch from cceaecb to a72ef1b Compare October 27, 2026 11:12
@zackees

zackees commented Oct 27, 2026

Copy link
Copy Markdown
Member

Rebased onto master as the coordinator, and added the features-ledger line.

Why the rebase moved two commits: the branch carried the three #2510 port commits, which landed as #2514 (e406c6f3c). They are dropped; only your RED test and the fix are replayed, so git diff origin/master is now just the four files this PR owns:
libobs/graphics/libnsgif/gif.c.h, rust/obs-graphics/src/gif.rs, rust/obs-graphics/tests/render/gif.rs, test/cmocka/test_nsgif_decode.c.

The Windows Tier 2 red was not yours. native_libobs::windows_tests::core_builds_from_the_canonical_manifest failed in run 40377242953 because soldr cook exits 101 in every Rust lane since #2061 (serde_json features); the log line is cook: failed with exit 101 after 14.1s; continuing without cooked deps. That is fixed by #2604, merged as 7140eb3d4, which is in this rebase's base. Your diff does not touch the build.

Added: a docs/features.md line under Fixed upstream bugs → Sources and filters, since this changes what a GIF in a Media Source decodes to. Wording and position are mine; edit it if you want it phrased differently. It is a separate commit so it is easy to drop.

PR CI now runs fresh against 7140eb3d4.

The row still listed the upstream resume (where the frame's parse
stopped) as kept and characterized. With this PR a scan resumes at the
frame's start; the row now says so and names the upstream behaviour.

Refs #2515

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher

vpacher commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

Thanks for the rebase and the ledger line; the wording of the docs/features.md entry is fine for me.

Added 71d518a81 on top: rust/README.md. The rebase dropped the README change of the fix commit, so the gif.c.h row still listed the upstream resume (where the frame's parse stopped) as kept and characterized. The row now says that a scan resumes at the frame's start (#2515) and names the upstream behaviour.

One correction for the record: the Windows Tier 2 failure in run 40377242953 was compiler options refer to sources outside this target (core_builds_from_the_canonical_manifest). That came from this branch being stacked on the pre-fix head of #2514, and a21966652 in #2514 fixed it. The cook: failed with exit 101 line was in every lane at that time and only slowed the builds; #2604 fixed that.

Local checks on the same code (before the README commit): CT104 cmocka (nsgif, image_file, gif, state stacks, sprite, effect parser) 12/12 against libobs-c-reference and the hybrid libobs; mbmax obs-tests-graphics 420/420 and gif_parity 20/20 runs. This PR's CI selects no Windows or macOS Tier 2 lane, so a local Windows Tier 2 run (VM) is in progress; I will post the result here.

@vpacher

vpacher commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

Local Windows Tier 2 (VM, Windows 11, VS 2026 Build Tools, validate.ps1 as in CI, same code as this PR's head without the README and ledger commits):

  • Configure and MSVC build of libobs and libobs-c-reference: pass. Exported symbols C vs Rust: identical.
  • Suite [OFF] against libobs-c-reference\obs.dll: 298/300. All tests of this area pass: test_nsgif_decode (with the new byte-by-byte scan case), test_nsgif_decode_rust, test_image_file, test_image_file_version_filetime, test_graphics_state_stacks(_rust), test_sprite_geometry_steps(_rust), test_effect_parser_lifecycle(_rust).
  • The 2 failures are from the VM environment, not this PR: test_win_capture_obs_windows (lists its own windows; the run has no interactive desktop) and test_media_alpha_network (VP8 alpha through libvpx). Both pass on the CI Windows runners.
  • validate.ps1 stops after the first failing suite, so the hybrid and the two Cargo-core suites did not run here. The Rust side of this change runs on Windows through test_nsgif_decode_rust (the shims) and the Rust Tests lane.

This PR's CI selects no Windows or macOS Tier 2 lane (the ci_scope gap: test .c files and core C start the Linux lanes only).

@zackees
zackees added this pull request to the merge queue Oct 27, 2026
Merged via the queue into master with commit bc62929 Oct 27, 2026
36 checks passed
@zackees
zackees deleted the fix/2515-gif-scan-resume branch October 27, 2026 11:43
@obs-rust-project-manager obs-rust-project-manager Bot moved this from Needs fix to Done in obs-rust Multiview Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: libnsgif: a scan in parts that ends inside a frame never recovers (NSGIF_ERR_DATA_FRAME)

2 participants