Skip to content

libobs: Show every field with 2x deinterlacing at another canvas rate - #395

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/3402-deinterlace-rate-mismatch
Oct 12, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/3402-deinterlace-rate-mismatch

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Description

Double-rate deinterlacing (Yadif 2x, Linear 2x, Discard 2x) now shows every field when the field rate is not the canvas rate. Before, a 50i source on a 60 fps canvas skipped about a quarter of its fields and looked choppy. At 50 fps it was smooth.

  • The second field of a frame is shown from frame_ts + deinterlace_offset + half_duration (libobs/obs-source-deinterlace.c lines 396-398). deinterlace_get_closest_frames() set the offset from the canvas tick that picked the frame (line 188), which lags the frame by anything up to a canvas interval.
  • Buffered sources re-anchored the offset whenever that lag moved by more than half an interval (line 194). At 50i into 60 fps the lag moves every frame, so the second field often came due only after the next frame had replaced it: 19-20 of 72 fields skipped (1 3 4 5 7 8 9 11 13 ...).
  • Unbuffered sources reset the offset on every frame (line 52), so each frame was held for its duration rounded up to whole ticks (50 ms instead of 40 ms at 50i into 60 fps). The source fell behind, and whole frames were dropped. This is the default for most Windows capture cards: DirectShow's "Auto" buffering only buffers delayed devices.

The fix, in obs-source-deinterlace.c only:

  • Buffered sources take the offset from the source clock that picks the frames (last_frame_ts), plus the 2 ms the frame picker already waits (now named FRAME_TOLERANCE). The second field comes due by the same rule that brings the next frame, half a frame earlier.
  • Unbuffered sources keep their offset. They re-anchor it only when a frame comes earlier than it, or so late that it missed its tick and its first field (a late delivery or a new clock).
  • Matched rates show the same fields as before. Shaders, decoding and encoding are unchanged.
  1. test/cmocka: Add failing test for 2x deinterlacing at another canvas rate: test_deinterlace_2x_field_order (added in libobs: Fix Yadif 2x field order for bottom field first #317) now runs at five rate pairs: 60i into 60, 50i into 50, 50i into 60, 50i into 30 and 60i into 50 fps. All three double-rate modes run in both field orders, buffered, and Yadif 2x also runs unbuffered: 40 cases. Every field carries its capture index, so each output frame is classified exactly. The checks:

    • No field may go back in time.
    • With a canvas at least as fast as the field rate, no field may be skipped. Buffered sources may hold a field for at most canvas / field rate frames, rounded up (50i into 60 fps: five fields once and one twice, like 50p).
    • With a slower canvas, at most one field may be dropped between two shown ones.
    • The first two frames are not counted: the first has no previous frame to time its fields by.

    12 of 40 fail, the same 12 in every run and in both lanes:

    • buffered, 50i into 60 fps, all 6: 19-20 fields skipped;
    • unbuffered, 50i into 60 fps: 10-12 skipped, two in a row;
    • unbuffered, 50i into 30 fps and 60i into 50 fps: 24-26 and 8 skipped, two in a row.
  2. libobs: Show every field with 2x deinterlacing at another canvas rate: 40 of 40 pass. At 50i into 60 fps every field is shown. At 50i into 30 fps three fields in five are shown, evenly spaced (2 3 5 7 8 10 ...).

Motivation and Context

Fixes #394 (upstream obsproject/obs-studio#3402, reported by zoton2; also seen by mufunyo and crannofonix).

Why this one: anyone who captures PAL consoles, cameras or broadcast feeds (50i) for a 60 fps stream gets visible stutter with the recommended deinterlacer. That includes the news stream in the upstream thread that mixes 50i and 60p feeds. The upstream issue was closed as stale with "If there is an actual bug here, PRs are welcome". No upstream PR exists. The open obsproject/obs-studio#12431 reworks async timestamps but leaves deinterlaced sources for later. The change is small, libobs only, and follows the fix for the Yadif 2x field order (#317, upstream obsproject/obs-studio#6061).

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON; obs-source-deinterlace.c is C in both), on current master:

  • test_deinterlace_2x_field_order: 12 of 40 cases fail at the RED commit and 40 of 40 pass at GREEN, in each lane. ctest -V shows all 40 cases run with no skips, about 75 s.
  • It loads the GL module from the build tree (GRAPHICS_MODULE_PATH). All 40 cases also ran with the system libobs-opengl masked.
  • Without a display, obs_startup() fails and all 40 cases skip, as before. This is what CI's Tier 2 Linux lane does: there it only proves the test builds and skips cleanly.
  • Repeatability at GREEN: 3 sequential runs and 12 runs in parallel (each on its own Xvfb) all passed 40 of 40.
  • Full ctest: 124 of 125 at RED (only test_deinterlace_2x_field_order fails) and 125 of 125 at GREEN, in each lane (130 of 130 rebased on current master).
  • clang-format, gersemi and ci/test_registration.py are clean.

Not tested:

  • A real capture card or console. The test drives libobs with a synthetic async source, with timestamps at exact frame intervals. Real devices have timestamp jitter. The new unbuffered rule re-anchors on a frame that misses its tick, and the test covers that path only by chance (when a frame arrives late under load).
  • Media Source playback of an interlaced file.
  • The start of a stream: the first frame's field duration is computed against no previous frame, so its second field can be skipped. This is unchanged by this PR, and the test leaves the first two frames out.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

…rate

A 50i source (25 interlaced frames, 50 fields per second) deinterlaced
with Yadif 2x on a 60 fps canvas looks choppy, and smooth again when the
canvas is set to 50 fps (reported upstream by zoton2 with a PAL console
and a capture card; mufunyo and crannofonix saw the same, the latter
mixing 50i and 60p feeds on a news stream). Played at 60 fps, 50p shows
five frames once and one twice; 50i with Yadif 2x should do the same.

test_deinterlace_2x_field_order (added for the Yadif 2x field order fix)
now runs every double-rate mode and field order at five rate pairs:
- 60i into 60 fps and 50i into 50 fps (matched rates);
- 50i into 60 fps (canvas faster than the field rate): every field must
  be shown, and none for more than two frames;
- 50i into 30 fps and 60i into 50 fps (canvas slower): at most one field
  dropped between two shown ones.
Every field carries its capture index, so the output is classified
exactly. The first two frames are left out of the counts: the first has
no previous frame to time its fields by.

Unbuffered sources are timed by other code, and capture cards often run
unbuffered (DirectShow's "Auto" buffering), so Yadif 2x also runs
unbuffered at the same five rate pairs. An unbuffered frame that arrives
just after a tick repeats the field before it, so only buffered sources
are held to the repeat limit.

On master 12 of the 40 cases fail, the same 12 in every run and in both
libobs lanes:
- buffered, 50i into 60 fps, all 6: 50-51 of the 72 fields shown, 19-20
  skipped (1 3 4 5 7 8 9 11 13 ...);
- unbuffered, 50i into 60 fps: 10-12 skipped, two in a row (... 16 17
  20 21 ...), a whole frame dropped every 0.3 s;
- unbuffered, 50i into 30 fps and 60i into 50 fps: 24-26 and 8
  skipped, two in a row.

Refs obs-rust#394 (upstream obsproject/obs-studio#3402).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 17:09
With Yadif 2x, Linear 2x or Discard 2x, a 50i source on a 60 fps canvas
skipped about a quarter of its fields and looked choppy; at 50 fps it was
smooth (reported upstream by zoton2; mufunyo and crannofonix saw the
same). As mufunyo put it in the upstream thread, deinterlacing only
worked when the field rate matched the canvas.

The second field of a frame is shown from frame_ts + deinterlace_offset
+ half the frame duration. deinterlace_get_closest_frames set the
offset from the canvas tick that picked the frame, which lags the frame
by anything up to a canvas interval:

- Buffered sources re-anchored it whenever that lag moved by more than
  half an interval. At 50i into 60 fps the lag moves every frame, so the
  second field often came due only after the next frame had replaced it:
  19-20 of 72 fields skipped (1 3 4 5 7 8 9 11 13 ...).
- Unbuffered sources re-anchored it on every frame, so each frame was
  held for its duration rounded up to whole ticks (50 ms instead of
  40 ms at 50i into 60 fps). The source fell behind and whole frames
  were dropped, two fields at once, also at 50i into 30 fps and 60i into
  50 fps.

Buffered sources now take the offset from the source clock that picks
the frames (last_frame_ts), plus the same 2 ms the frame picker waits,
so the second field comes due by the rule that brings the next frame,
half a frame earlier. Unbuffered sources keep their offset, and
re-anchor it only when a frame comes earlier than it, or so late that it
missed its tick and its first field (a late delivery or a new clock;
this also keeps matched rates from losing a field when a frame arrives
just after its tick). Matched rates show the same fields as before.

test_deinterlace_2x_field_order: 40 of 40 pass (12 failed before). At 50i
into 60 fps every field is shown, one in five of them twice, like 50p;
50i into 30 fps shows three fields in five, evenly spaced. Decoding,
encoding and the deinterlacing shaders are unchanged.

Fixes obs-rust#394 (upstream obsproject/obs-studio#3402).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=6c915c51f7a29e22203a9141cff7744cc56baf66 secs=186 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791825714,"gate":"rust/all/fmt","host":"linux-x86_64","key":"5f457cf3d3f2631ff2ee2c24c2783d3efa61de503530fbd930eeea4a3a56203d","lane":"lint","parents":["53a6ff27489ee08740cc6d179ece0562fd281e9a"],"secs":71,"stamp":"218209986e2e8fd420c0a3a3a0c1304a","tree":"6c915c51f7a29e22203a9141cff7744cc56baf66","v":1,"via":"run"}
Ci-Attestation: {"at":1791825714,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"5f457cf3d3f2631ff2ee2c24c2783d3efa61de503530fbd930eeea4a3a56203d","lane":"lint","parents":["53a6ff27489ee08740cc6d179ece0562fd281e9a"],"secs":71,"stamp":"b001133f6f36fc12a5e2a43d7ab254c5","tree":"6c915c51f7a29e22203a9141cff7744cc56baf66","v":1,"via":"run"}
Ci-Attestation: {"at":1791825714,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"7e7260ea06d6de0d7dcae3ff599041bcf00f4b0d6d94f93a1dbdccc604e51450","lane":"rust-test","parents":["53a6ff27489ee08740cc6d179ece0562fd281e9a"],"secs":53,"stamp":"27d39101b6b0b0566cd07589ab070525","tree":"6c915c51f7a29e22203a9141cff7744cc56baf66","v":1,"via":"run"}
Ci-Attestation: {"at":1791825714,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"eff1f5726ba03375afd40d821c03a243b85dbccbc67610593f65784b8facdf08","lane":"tier2-linux","parents":["53a6ff27489ee08740cc6d179ece0562fd281e9a"],"secs":62,"stamp":"c6686b443a7fd62ff3352abce64f675f","tree":"6c915c51f7a29e22203a9141cff7744cc56baf66","v":1,"via":"run"}
@zackees
zackees force-pushed the fix/3402-deinterlace-rate-mismatch branch from 407fb3b to e825545 Compare October 12, 2026 17:21
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

Verified locally under xvfb-run (Tier 2 image + mesa):

  • master's original test: 6/6, twice
  • RED 53a6ff2: 13 of 40 fail, and the same set on repeat runs (every mode at 50i→60; the unbuffered 50i→30, 50i→60 and 60i→50 cases; and yadif_2x_top_first_60i_into_60, which skips 13 fields in a row at startup, the matched-rate loss your commit message mentions)
  • GREEN 407fb3b: 40/40

The test rewrite keeps the original 6 cases as the 60i_into_60 set and adds rates; I saw no weakened assertions. It needs a display, so plain CI skips it. Rebased on master, local gate passed; pushed as e825545.

@rdoupe

rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator Author

The red "Rust Tests 🦀 (windows-2025-vs2026)" job is unrelated to this change: it's windows_process::dead_pipe_write_matches_oracle in rust/obs-util/tests/pipe_parity.rs:874 (a dead-pipe write returned 64 bytes instead of 0), and this PR only touches libobs/obs-source-deinterlace.c and its C test. The same job passes on the last three master runs, so it looks timing-dependent; a re-run should clear it. Logged it as a possible flaky test on our side.

@zackees
zackees merged commit 09d6d8e into obs-rust:master Oct 12, 2026
45 of 46 checks passed
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 23, 2026
…ace-pacing.c

Behaviour-neutral extraction before the Rust port. The pacing decisions of
double-rate deinterlacing (Yadif 2x, Linear 2x, Discard 2x) in
obs-source-deinterlace.c.h move to libobs/obs-source-deinterlace-pacing.c.h
as six pure functions on plain integers, declared in the new
libobs/obs-source-deinterlace-pacing.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_source_deinterlace_pacing_hold(): an unbuffered source keeps its
  frame while the frame's end (frame_ts + offset + 2 * half_duration -
  TWOX_TOLERANCE) is ahead of the clock by less than TS_JUMP_THRESHOLD;
- obs_source_deinterlace_pacing_wait_for_next() and
  obs_source_deinterlace_pacing_waiting_frame_ts(): the "don't skip
  ahead" guard of ready_deinterlace_frames() for an unbuffered source with
  two frames queued, and the timestamp it then stores;
- obs_source_deinterlace_pacing_half_duration(): half the time from the
  frame before, truncated to uint32_t;
- obs_source_deinterlace_pacing_buffered_offset(): the offset of a
  buffered source, from the source clock that picks frames (obs-rust#395);
- obs_source_deinterlace_pacing_reanchor(): the offset of an unbuffered
  source, kept unless the frame comes early or misses its tick and its
  first field (obs-rust#395).

obs-source-deinterlace.c.h keeps everything else: the async_frames queue,
the buffered walk, first_frame(), deinterlace_process_last_frame(), the
textures and the render. It still tests async_unbuffered and
deinterlace_offset before the hold and the guard, reads the same fields
and obs->video values, and stores deinterlace_frame_ts,
deinterlace_half_duration and deinterlace_offset in the same order. The
re-anchor now stores the offset it returns every time; when it keeps the
offset, that is the value already there. All arithmetic is the same
uint64_t arithmetic, wrapping where it wrapped, the (int64_t) cast of
the re-anchor's lateness included; TWOX_TOLERANCE is now 1000000ULL,
which every use already converted to uint64_t.

The thresholds are named in the header. obs-source-deinterlace.c.h keeps
FRAME_TOLERANCE (the buffered walk) and TWOX_TOLERANCE (the render) and
checks them against the header at compile time; TS_JUMP_THRESHOLD was
used only by the hold and moves. The obs-rust#395 comments move with the code.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header. The exported symbol sets are unchanged (1766).

Refs obs-rust#1950
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 23, 2026
test_deinterlace_pacing_steps compiles
libobs/obs-source-deinterlace-pacing.c.h (ASan and UBSan, no libobs
core) and calls only its six hidden functions, so it runs on every
platform. 20 cases:

- obs_source_deinterlace_pacing_hold(): held until the frame's end and
  not at it; TS_JUMP_THRESHOLD - 1 (held), TS_JUMP_THRESHOLD and
  TS_JUMP_THRESHOLD + 1 (moves on, the clock jumped: strict); the half
  duration widened before it is doubled (UINT32_MAX gives 8.6 s, no
  32-bit wrap); a half duration of 0; the end wrapping past UINT64_MAX
  and below 0.
- obs_source_deinterlace_pacing_wait_for_next() and _waiting_frame_ts():
  wait until timestamp + offset + duration (no tolerance, no threshold),
  the waiting timestamp one duration back, so its half duration is half
  the duration; a next frame before the first.
- obs_source_deinterlace_pacing_half_duration(): 50i and 59.94i, rounding
  down, the largest that fits.
- obs_source_deinterlace_pacing_buffered_offset(): video_time -
  last_frame_ts + FRAME_TOLERANCE + TWOX_TOLERANCE (obs-rust#395).
- obs_source_deinterlace_pacing_reanchor(): no offset yet; a frame early
  by 1 ns; on time and later by less than the larger of the field and the
  tick, less TWOX_TOLERANCE (kept); at it and past it (re-anchored), for
  50i into 60 fps (the field is longer) and into 30 fps (the tick is),
  ties, a field and a tick of 0, a tick over UINT32_MAX.
- Unbuffered sequences of 100 frames through a model of
  deinterlace_get_closest_frames() (hold, newest arrived frame, half
  duration, re-anchor), on exact rational clocks: 25 and 50 frames a
  second into 30 and 60 fps never re-anchor and never pick early (obs-rust#395),
  50 into 30 drops 76 frames; a frame a field late re-anchors once and
  an early one re-anchors back.

Characterized, not endorsed:
- half a duration of 2^32 ns or more is truncated to 32 bits (a 10 s gap
  gives 705 ms);
- a timestamp going backwards wraps: the half duration of a frame 40 ms
  back is 2^32 - 20 ms, the guard's end is the next frame's timestamp
  plus the offset, the waiting timestamp lands after the first frame;
- the first frame with no second one queued takes its half duration
  against deinterlace_frame_ts, which is 0 for a source that never showed
  a frame: half its own timestamp truncated (1.78 s for a capture
  timestamp 1000 s after boot), or against the last frame shown before a
  queue reset. An unbuffered source then moves on at the next tick as if
  the clock jumped, or, when the truncation leaves 30 ms, holds the
  frame to 59 ms where a 50i frame ends at 39 ms;
- the buffered offset and the re-anchored offset wrap when the frame's
  clock is ahead of the canvas, and the re-anchor's lateness is an
  int64_t, so 2^63 late or more counts as early;
- 29.97 frames a second into 30 and 60 fps, and 59.94 into 60, re-anchor
  on every frame: the canvas ticks a little faster than the frames, so a
  tick lands within TWOX_TOLERANCE before the frame's time and the frame
  counts as early; the offset follows the canvas, 33 or 17 us a frame.

It is in the native test graph, in the core suite. The real-libobs
test_deinterlace_2x_field_order is unchanged.

Refs obs-rust#1950
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 23, 2026
Ports libobs/obs-source-deinterlace-pacing.c.h, the frame pacing of
double-rate deinterlacing, to the deinterlace_pacing module of
rust/obs-source. deinterlace_get_closest_frames() and
ready_deinterlace_frames() stay C in obs-source-deinterlace.c.h: the
async_unbuffered and deinterlace_offset tests before the hold and the
guard, the field reads and the stores in their order, the async_frames
queue, the buffered walk, first_frame(), the textures and the render.
The hybrid libobs drops the C file and links the Rust shims in its place.

Safe core (src/deinterlace_pacing.rs): hold() keeps an unbuffered frame
while its end (frame_ts + offset + twice the widened half duration, less
TWOX_TOLERANCE) is ahead of the clock by less than TS_JUMP_THRESHOLD;
wait_for_next() waits while the clock is before timestamp + offset + the
frame's duration, and waiting_frame_ts() is the timestamp one duration
back; half_duration() is half the difference, its low 32 bits as the C's
(uint32_t); buffered_offset() is video_time - last_frame_ts +
FRAME_TOLERANCE + TWOX_TOLERANCE (obs-rust#395); reanchor() returns video_time -
frame_ts when there is no offset, when the frame is early (the lateness,
cast_signed() of the wrapped difference as the C's (int64_t), is
negative) or when the lateness plus TWOX_TOLERANCE reaches the larger of
the half duration and the canvas interval, and the old offset otherwise
(obs-rust#395). Every sum and difference is wrapping_* where the C's uint64_t
wraps. The module denies clippy's arithmetic, indexing, as, unwrap,
expect, panic, unimplemented and unreachable lints, keeps no state and
takes no lock.

Shim (src/ffi/source_deinterlace_pacing.rs): the six functions keep the C
signatures, plain integers in and out, so nothing is mirrored and no
pointer is checked. No intentional differences.

Kept, characterized, not endorsed (test_deinterlace_pacing_steps): half a
duration of 2^32 ns or more is truncated; a timestamp going backwards
wraps the half duration, the guard's end and the waiting timestamp; the
first frame with no second one queued takes its half duration against a
deinterlace_frame_ts of 0; the offsets wrap when the frame's clock is
ahead of the canvas; a lateness of 2^63 or more counts as early; at 29.97
and 59.94 frames a second into a slightly faster canvas the offset
re-anchors on every frame and follows the canvas.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-deinterlace-pacing (LIBOBS_PORTED_SOURCES), whose PCH
is only obs-source-deinterlace-pacing.h; libobs.pch.h now includes the
header for obs-source-deinterlace.c. The six functions are not EXPORT, so
the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map
and rust-unexports-macos.txt. libobs-rust already links the crate's ffi.
The exported symbol sets are unchanged and equal: 1766 on the C backend,
the hybrid and libobs-c-reference; the six are local in all three, and the
hybrid's come from the Rust archive. ci/test_ported_sources.py checks the
group on every host.

test_deinterlace_pacing_steps still compiles the C file into itself,
unchanged. The new test_deinterlace_pacing_steps_rust builds the same
test source against libobs_rust-static instead, as
test_source_audio_timing_steps_rust does, and links: the test includes
only the header and calls only the six functions. test_deinterlace_2x_field_order
sees the swap through the hybrid.

Tier 1 source::deinterlace_pacing passes 30/30.

Refs obs-rust#1950
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 23, 2026
Oracle (oracle/source_deinterlace_pacing.c): the original
obs-source-deinterlace-pacing.c.h, unmodified, with its six functions
renamed to oracle_*, plus the three thresholds. The bool results are
declared as u8 on the Rust side, the byte the C returns.

Tier 3 (tests/source/deinterlace_pacing_parity.rs). Every function takes
and returns plain integers, so each run compares the bytes of the two
results: the C's bool as its byte, the uint32_t and uint64_t results as
their bytes. Runs:
- the three thresholds against the C header;
- each function over the special values of the u64 range (0, 1, the
  thresholds +-1, 50i and 60 fps durations, 2^32, 2^63 +-1, the end of
  the range) and ten half durations, every combination: 138240 holds,
  331776 guards, 576 half durations and buffered offsets, 3317760
  re-anchors;
- proptests of each function over full-range u64 and u32 values, with
  the clock drawn near the frame's end and the lateness near the
  thresholds as often as anywhere (20000 cases each);
- 3000 random sequences of 2 to 300 canvas ticks through a model of
  deinterlace_get_closest_frames(), ready_deinterlace_frames() (the
  unbuffered queue trim and guard, and the buffered walk with
  frame_out_of_bounds() and the MAX_TS_VAR jump) and first_frame(), with
  the stores in the C's order, driven once by the C functions and once
  by the shims: sources of 25, 29.97, 50 and 59.94 frames a second into
  30 and 60 fps canvases, buffered and unbuffered, with timestamp and
  arrival jitter, late deliveries, late ticks, timestamps that jump or go
  backwards or land anywhere, queue resets (last_frame_ts 0, the
  first-frame path), a full queue and the buffering toggled. The bytes
  of every call's result and the model's state after every tick must be
  equal;
- a check that 2000 sequences reach every branch of every function more
  than 100 times (held, moved on, the clock jumped; waited or not; the
  half duration from a pair and from deinterlace_frame_ts, truncated;
  the buffered offset; the re-anchor with no offset, early, late and
  kept; the first frame) and every rate pair.

The parity module passed 20 runs in a row with fresh random seeds; no
regression file came up.

Mutation check (done once, not committed), with the two test modules
built into a scratch test binary: 53 mutants. The 45 non-equivalent
core mutants fail Tier 1 (2 to 10 of 30) and Tier 3 (1 to 4 of 11):
each comparison at its threshold (< and <=, >= and >), the hold without
its jump threshold or its "before the end" test, one half duration
instead of two, the half duration doubled in 32 bits, TWOX_TOLERANCE or
FRAME_TOLERANCE dropped, the guard without its duration or offset, the
waiting timestamp ahead or unchanged, the half duration not halved,
rounded up, saturated to UINT32_MAX or from the high bits, the buffered
offset reversed, the re-anchor without its no-offset, early or late rule,
early only past TWOX_TOLERANCE, the smaller of field and tick or only
one of them, the pre-obs-rust#395 re-anchor at every pick, the lateness clamped
or computed without the modulo, and every wrapping operation made
saturating. Two mutants survive and are equivalent: end - sys_time is
computed only when sys_time is before the end, and the lateness plus
TWOX_TOLERANCE is reached only for a lateness of at most INT64_MAX, so
neither can wrap. The 6 shim mutants (a result negated, arguments
swapped) fail Tier 3; Tier 1 does not reach the shims.

No intentional differences: there is no pointer to check.

Refs obs-rust#1950
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 23, 2026
* libobs: Move the 2x deinterlace frame pacing into obs-source-deinterlace-pacing.c

Behaviour-neutral extraction before the Rust port. The pacing decisions of
double-rate deinterlacing (Yadif 2x, Linear 2x, Discard 2x) in
obs-source-deinterlace.c.h move to libobs/obs-source-deinterlace-pacing.c.h
as six pure functions on plain integers, declared in the new
libobs/obs-source-deinterlace-pacing.h without EXPORT (hidden, as
-fvisibility=hidden keeps them):

- obs_source_deinterlace_pacing_hold(): an unbuffered source keeps its
  frame while the frame's end (frame_ts + offset + 2 * half_duration -
  TWOX_TOLERANCE) is ahead of the clock by less than TS_JUMP_THRESHOLD;
- obs_source_deinterlace_pacing_wait_for_next() and
  obs_source_deinterlace_pacing_waiting_frame_ts(): the "don't skip
  ahead" guard of ready_deinterlace_frames() for an unbuffered source with
  two frames queued, and the timestamp it then stores;
- obs_source_deinterlace_pacing_half_duration(): half the time from the
  frame before, truncated to uint32_t;
- obs_source_deinterlace_pacing_buffered_offset(): the offset of a
  buffered source, from the source clock that picks frames (obs-rust#395);
- obs_source_deinterlace_pacing_reanchor(): the offset of an unbuffered
  source, kept unless the frame comes early or misses its tick and its
  first field (obs-rust#395).

obs-source-deinterlace.c.h keeps everything else: the async_frames queue,
the buffered walk, first_frame(), deinterlace_process_last_frame(), the
textures and the render. It still tests async_unbuffered and
deinterlace_offset before the hold and the guard, reads the same fields
and obs->video values, and stores deinterlace_frame_ts,
deinterlace_half_duration and deinterlace_offset in the same order. The
re-anchor now stores the offset it returns every time; when it keeps the
offset, that is the value already there. All arithmetic is the same
uint64_t arithmetic, wrapping where it wrapped, the (int64_t) cast of
the re-anchor's lateness included; TWOX_TOLERANCE is now 1000000ULL,
which every use already converted to uint64_t.

The thresholds are named in the header. obs-source-deinterlace.c.h keeps
FRAME_TOLERANCE (the buffered walk) and TWOX_TOLERANCE (the render) and
checks them against the header at compile time; TS_JUMP_THRESHOLD was
used only by the hold and moves. The obs-rust#395 comments move with the code.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header. The exported symbol sets are unchanged (1766).

Refs obs-rust#1950

* test: Characterize the 2x deinterlace frame pacing

test_deinterlace_pacing_steps compiles
libobs/obs-source-deinterlace-pacing.c.h (ASan and UBSan, no libobs
core) and calls only its six hidden functions, so it runs on every
platform. 20 cases:

- obs_source_deinterlace_pacing_hold(): held until the frame's end and
  not at it; TS_JUMP_THRESHOLD - 1 (held), TS_JUMP_THRESHOLD and
  TS_JUMP_THRESHOLD + 1 (moves on, the clock jumped: strict); the half
  duration widened before it is doubled (UINT32_MAX gives 8.6 s, no
  32-bit wrap); a half duration of 0; the end wrapping past UINT64_MAX
  and below 0.
- obs_source_deinterlace_pacing_wait_for_next() and _waiting_frame_ts():
  wait until timestamp + offset + duration (no tolerance, no threshold),
  the waiting timestamp one duration back, so its half duration is half
  the duration; a next frame before the first.
- obs_source_deinterlace_pacing_half_duration(): 50i and 59.94i, rounding
  down, the largest that fits.
- obs_source_deinterlace_pacing_buffered_offset(): video_time -
  last_frame_ts + FRAME_TOLERANCE + TWOX_TOLERANCE (obs-rust#395).
- obs_source_deinterlace_pacing_reanchor(): no offset yet; a frame early
  by 1 ns; on time and later by less than the larger of the field and the
  tick, less TWOX_TOLERANCE (kept); at it and past it (re-anchored), for
  50i into 60 fps (the field is longer) and into 30 fps (the tick is),
  ties, a field and a tick of 0, a tick over UINT32_MAX.
- Unbuffered sequences of 100 frames through a model of
  deinterlace_get_closest_frames() (hold, newest arrived frame, half
  duration, re-anchor), on exact rational clocks: 25 and 50 frames a
  second into 30 and 60 fps never re-anchor and never pick early (obs-rust#395),
  50 into 30 drops 76 frames; a frame a field late re-anchors once and
  an early one re-anchors back.

Characterized, not endorsed:
- half a duration of 2^32 ns or more is truncated to 32 bits (a 10 s gap
  gives 705 ms);
- a timestamp going backwards wraps: the half duration of a frame 40 ms
  back is 2^32 - 20 ms, the guard's end is the next frame's timestamp
  plus the offset, the waiting timestamp lands after the first frame;
- the first frame with no second one queued takes its half duration
  against deinterlace_frame_ts, which is 0 for a source that never showed
  a frame: half its own timestamp truncated (1.78 s for a capture
  timestamp 1000 s after boot), or against the last frame shown before a
  queue reset. An unbuffered source then moves on at the next tick as if
  the clock jumped, or, when the truncation leaves 30 ms, holds the
  frame to 59 ms where a 50i frame ends at 39 ms;
- the buffered offset and the re-anchored offset wrap when the frame's
  clock is ahead of the canvas, and the re-anchor's lateness is an
  int64_t, so 2^63 late or more counts as early;
- 29.97 frames a second into 30 and 60 fps, and 59.94 into 60, re-anchor
  on every frame: the canvas ticks a little faster than the frames, so a
  tick lands within TWOX_TOLERANCE before the frame's time and the frame
  counts as early; the offset follows the canvas, 33 or 17 us a frame.

It is in the native test graph, in the core suite. The real-libobs
test_deinterlace_2x_field_order is unchanged.

Refs obs-rust#1950

* obs-source: Add Tier 1 tests for the 2x deinterlace frame pacing port

RED for the port of libobs/obs-source-deinterlace-pacing.c.h to the
deinterlace_pacing module of rust/obs-source, next to audio_timing (obs-rust#1936).
The safe core is a stub (todo!()): hold(), wait_for_next(),
waiting_frame_ts(), half_duration(), buffered_offset() and reanchor(), one
per hidden C function, on plain u64 and u32 values, and FRAME_TOLERANCE,
TWOX_TOLERANCE and TS_JUMP_THRESHOLD.

tests/source/deinterlace_pacing.rs ports test_deinterlace_pacing_steps
1:1: its 20 cases under the same names (steps_*), in the same order, with
the same inputs and expectations, the characterized-not-endorsed wraps, the
first frame's half duration and the model of the unbuffered sequences
included.

Edge cases (edge_*) check each function against a model of the C's
uint64_t arithmetic written in i128 with the modulo explicit (the
guard's end as next + offset, the waiting timestamp as 2 * timestamp -
next, the lateness negative from 2^63): over the special values of the
u64 range and the thresholds +-1 (every combination), the hold at
TS_JUMP_THRESHOLD +-1 before every end, the re-anchor at its threshold and
at the int64_t sign from every offset, and four proptests over
full-range values with the clock drawn near the end and the lateness near
the thresholds (4000 cases each).

The tests are a module of obs-tests-core (rust/obs-data/tests/core.rs),
as audio_timing's are.

soldr cargo test -p obs-data --test obs-tests-core
source::deinterlace_pacing fails 30 of 30 with "not yet implemented".

Refs obs-rust#1950

* obs-source: Port the 2x deinterlace frame pacing

Ports libobs/obs-source-deinterlace-pacing.c.h, the frame pacing of
double-rate deinterlacing, to the deinterlace_pacing module of
rust/obs-source. deinterlace_get_closest_frames() and
ready_deinterlace_frames() stay C in obs-source-deinterlace.c.h: the
async_unbuffered and deinterlace_offset tests before the hold and the
guard, the field reads and the stores in their order, the async_frames
queue, the buffered walk, first_frame(), the textures and the render.
The hybrid libobs drops the C file and links the Rust shims in its place.

Safe core (src/deinterlace_pacing.rs): hold() keeps an unbuffered frame
while its end (frame_ts + offset + twice the widened half duration, less
TWOX_TOLERANCE) is ahead of the clock by less than TS_JUMP_THRESHOLD;
wait_for_next() waits while the clock is before timestamp + offset + the
frame's duration, and waiting_frame_ts() is the timestamp one duration
back; half_duration() is half the difference, its low 32 bits as the C's
(uint32_t); buffered_offset() is video_time - last_frame_ts +
FRAME_TOLERANCE + TWOX_TOLERANCE (obs-rust#395); reanchor() returns video_time -
frame_ts when there is no offset, when the frame is early (the lateness,
cast_signed() of the wrapped difference as the C's (int64_t), is
negative) or when the lateness plus TWOX_TOLERANCE reaches the larger of
the half duration and the canvas interval, and the old offset otherwise
(obs-rust#395). Every sum and difference is wrapping_* where the C's uint64_t
wraps. The module denies clippy's arithmetic, indexing, as, unwrap,
expect, panic, unimplemented and unreachable lints, keeps no state and
takes no lock.

Shim (src/ffi/source_deinterlace_pacing.rs): the six functions keep the C
signatures, plain integers in and out, so nothing is mirrored and no
pointer is checked. No intentional differences.

Kept, characterized, not endorsed (test_deinterlace_pacing_steps): half a
duration of 2^32 ns or more is truncated; a timestamp going backwards
wraps the half duration, the guard's end and the waiting timestamp; the
first frame with no second one queued takes its half duration against a
deinterlace_frame_ts of 0; the offsets wrap when the frame's clock is
ahead of the canvas; a lateness of 2^63 or more counts as early; at 29.97
and 59.94 frames a second into a slightly faster canvas the offset
re-anchors on every frame and follows the canvas.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-deinterlace-pacing (LIBOBS_PORTED_SOURCES), whose PCH
is only obs-source-deinterlace-pacing.h; libobs.pch.h now includes the
header for obs-source-deinterlace.c. The six functions are not EXPORT, so
the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map
and rust-unexports-macos.txt. libobs-rust already links the crate's ffi.
The exported symbol sets are unchanged and equal: 1766 on the C backend,
the hybrid and libobs-c-reference; the six are local in all three, and the
hybrid's come from the Rust archive. ci/test_ported_sources.py checks the
group on every host.

test_deinterlace_pacing_steps still compiles the C file into itself,
unchanged. The new test_deinterlace_pacing_steps_rust builds the same
test source against libobs_rust-static instead, as
test_source_audio_timing_steps_rust does, and links: the test includes
only the header and calls only the six functions. test_deinterlace_2x_field_order
sees the swap through the hybrid.

Tier 1 source::deinterlace_pacing passes 30/30.

Refs obs-rust#1950

* obs-source: Check the 2x deinterlace frame pacing port against the C

Oracle (oracle/source_deinterlace_pacing.c): the original
obs-source-deinterlace-pacing.c.h, unmodified, with its six functions
renamed to oracle_*, plus the three thresholds. The bool results are
declared as u8 on the Rust side, the byte the C returns.

Tier 3 (tests/source/deinterlace_pacing_parity.rs). Every function takes
and returns plain integers, so each run compares the bytes of the two
results: the C's bool as its byte, the uint32_t and uint64_t results as
their bytes. Runs:
- the three thresholds against the C header;
- each function over the special values of the u64 range (0, 1, the
  thresholds +-1, 50i and 60 fps durations, 2^32, 2^63 +-1, the end of
  the range) and ten half durations, every combination: 138240 holds,
  331776 guards, 576 half durations and buffered offsets, 3317760
  re-anchors;
- proptests of each function over full-range u64 and u32 values, with
  the clock drawn near the frame's end and the lateness near the
  thresholds as often as anywhere (20000 cases each);
- 3000 random sequences of 2 to 300 canvas ticks through a model of
  deinterlace_get_closest_frames(), ready_deinterlace_frames() (the
  unbuffered queue trim and guard, and the buffered walk with
  frame_out_of_bounds() and the MAX_TS_VAR jump) and first_frame(), with
  the stores in the C's order, driven once by the C functions and once
  by the shims: sources of 25, 29.97, 50 and 59.94 frames a second into
  30 and 60 fps canvases, buffered and unbuffered, with timestamp and
  arrival jitter, late deliveries, late ticks, timestamps that jump or go
  backwards or land anywhere, queue resets (last_frame_ts 0, the
  first-frame path), a full queue and the buffering toggled. The bytes
  of every call's result and the model's state after every tick must be
  equal;
- a check that 2000 sequences reach every branch of every function more
  than 100 times (held, moved on, the clock jumped; waited or not; the
  half duration from a pair and from deinterlace_frame_ts, truncated;
  the buffered offset; the re-anchor with no offset, early, late and
  kept; the first frame) and every rate pair.

The parity module passed 20 runs in a row with fresh random seeds; no
regression file came up.

Mutation check (done once, not committed), with the two test modules
built into a scratch test binary: 53 mutants. The 45 non-equivalent
core mutants fail Tier 1 (2 to 10 of 30) and Tier 3 (1 to 4 of 11):
each comparison at its threshold (< and <=, >= and >), the hold without
its jump threshold or its "before the end" test, one half duration
instead of two, the half duration doubled in 32 bits, TWOX_TOLERANCE or
FRAME_TOLERANCE dropped, the guard without its duration or offset, the
waiting timestamp ahead or unchanged, the half duration not halved,
rounded up, saturated to UINT32_MAX or from the high bits, the buffered
offset reversed, the re-anchor without its no-offset, early or late rule,
early only past TWOX_TOLERANCE, the smaller of field and tick or only
one of them, the pre-obs-rust#395 re-anchor at every pick, the lateness clamped
or computed without the modulo, and every wrapping operation made
saturating. Two mutants survive and are equivalent: end - sys_time is
computed only when sys_time is before the end, and the lateness plus
TWOX_TOLERANCE is reached only for a lateness of at most INT64_MAX, so
neither can wrap. The 6 shim mutants (a result negated, arguments
swapped) fail Tier 3; Tier 1 does not reach the shims.

No intentional differences: there is no pointer to check.

Refs obs-rust#1950

* docs: Record the 2x deinterlace frame pacing port

The rust/README.md crate table now says rust/obs-source also holds the
units extracted from libobs/obs-source-deinterlace.c, and its port table
has a row for libobs/obs-source-deinterlace-pacing.c.h: the six functions,
what stays C, the seam (the async_unbuffered and deinterlace_offset tests,
the field and obs->video reads and the stores in their order), the
wrapping arithmetic and the (int64_t) lateness, what the characterization
pins and the port keeps, no intentional differences, the _rust variant of
the steps test, test_deinterlace_2x_field_order and what Tier 3 compares.
The crate's description and lib.rs say what it holds. docs/rust-port/naming.md
already maps libobs/obs-source*.c.h to rust/obs-source.

port_comparison gets its own bench file,
benches/port_comparison/source_deinterlace_pacing.rs: 1024 ticks of 50i
and 59.94i sources into 60 fps that reach every branch, as an unbuffered
source runs the hold, the guard, the half duration and the re-anchor, and
as a buffered one runs the half duration and its offset, the results
folded into a checksum both sides must match. On x86_64 Linux (GCC, not
pinned; median per 1024 ticks): unbuffered_tick 8.67 us Rust and 7.59 us
C (1.14x), buffered_pick 3.20 us Rust and 3.13 us C (1.02x), about 1 ns
more per tick.

Fixes obs-rust#1950
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 23, 2026
With Yadif 2x, Linear 2x or Discard 2x, the first frame a source showed,
and the first after a restart, a full queue or a pause, lost its second
field, or was held too long. When that frame was picked with no second
frame queued, its half duration was half the gap to deinterlace_frame_ts:
0 for a new source, so half its own timestamp truncated to 32 bits (1.78 s
for a capture timestamp 1000 s after boot), or the last frame shown, so
705 ms after a 10 s pause. A buffered source then showed the second field
only after the next frame had replaced the first; an unbuffered one held
the frame past TS_JUMP_THRESHOLD, moved on as if the clock had jumped, or,
when the truncation left 0.5 to 35.5 ms, held it too long.

obs_source_deinterlace_pacing_half_duration() no longer takes a half
duration from a gap that is no frame duration: no previous timestamp (0),
one at or after the frame (a timestamp going backwards), or one more than
twice TS_JUMP_THRESHOLD before it, a half duration past the threshold (a
pause or a new clock). It keeps the half duration before when that is set
and at most TS_JUMP_THRESHOLD, and else takes half the canvas interval, so
the second field comes at the next tick. A regular stream with frames up
to 140 ms apart keeps its half durations, so a source below 14.3 frames a
second does not change. The C, the Rust port and the C oracle (which compiles the libobs
file) change alike; every rule of obs-rust#395 and of the unbuffered hold is
unchanged.

- Tier 3: the sequences now count the fallbacks (no previous timestamp,
  backwards, past the threshold; the half duration kept or half the
  interval) instead of the truncation, which can no longer happen.
  first_frame_cadences runs 25, 29.97, 50 and 59.94 frames a second into
  30 and 60 fps, buffered and unbuffered, for a new source and after a
  pause, through the model of the caller: the C and the shims agree, the
  first frame shows its fields as a source timed from a frame before it
  would (buffered always; unbuffered unless its next frame is there at
  the next tick), and from the next pick on every state is the one of
  before. low_rate_cadences does the same at 12.5 frames a second: every
  state after the first frame, and up to a pause, is the one of before.
- docs/features.md: a line under fixed upstream bugs. rust/README.md and
  the module docs drop the characterized truncation and first-frame
  behaviour.

test_deinterlace_pacing_steps (and _rust) 23/23, Tier 1 33/33, Tier 3
13/13, and test_deinterlace_2x_field_order 55/55 on the C backend, the
hybrid and libobs-c-reference.

Fixes obs-rust#1965
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 23, 2026
With Yadif 2x, Linear 2x or Discard 2x, the first frame a source showed,
and the first after a restart, a full queue or a pause, lost its second
field, or was held too long. When that frame was picked with no second
frame queued, its half duration was half the gap to deinterlace_frame_ts:
0 for a new source, so half its own timestamp truncated to 32 bits (1.78 s
for a capture timestamp 1000 s after boot), or the last frame shown, so
705 ms after a 10 s pause. A buffered source then showed the second field
only after the next frame had replaced the first; an unbuffered one held
the frame past TS_JUMP_THRESHOLD, moved on as if the clock had jumped, or,
when the truncation left 0.5 to 35.5 ms, held it too long.

obs_source_deinterlace_pacing_half_duration() no longer takes a half
duration from a gap that is no frame duration: no previous timestamp (0),
one at or after the frame (a timestamp going backwards), or one more than
twice TS_JUMP_THRESHOLD before it, a half duration past the threshold (a
pause or a new clock). It keeps the half duration before when that is set
and at most TS_JUMP_THRESHOLD, and else takes half the canvas interval, so
the second field comes at the next tick. A regular stream with frames up
to 140 ms apart keeps its half durations, so a source below 14.3 frames a
second does not change. The C, the Rust port and the C oracle (which compiles the libobs
file) change alike; every rule of obs-rust#395 and of the unbuffered hold is
unchanged.

- Tier 3: the sequences now count the fallbacks (no previous timestamp,
  backwards, past the threshold; the half duration kept or half the
  interval) instead of the truncation, which can no longer happen.
  first_frame_cadences runs 25, 29.97, 50 and 59.94 frames a second into
  30 and 60 fps, buffered and unbuffered, for a new source and after a
  pause, through the model of the caller: the C and the shims agree, the
  first frame shows its fields as a source timed from a frame before it
  would (buffered always; unbuffered unless its next frame is there at
  the next tick), and from the next pick on every state is the one of
  before. low_rate_cadences does the same at 12.5 frames a second: every
  state after the first frame, and up to a pause, is the one of before.
- docs/features.md: a line under fixed upstream bugs. rust/README.md and
  the module docs drop the characterized truncation and first-frame
  behaviour.

test_deinterlace_pacing_steps (and _rust) 23/23, Tier 1 33/33, Tier 3
13/13, and test_deinterlace_2x_field_order 55/55 on the C backend, the
hybrid and libobs-c-reference.

Fixes obs-rust#1965
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 23, 2026
…#1995)

* test: Expect both fields of the first 2x deinterlaced frame

With a double-rate deinterlace mode (Yadif 2x, Linear 2x, Discard 2x), the
first frame a source shows, or the first after a pause, has its second
field skipped, or less often held too long. Its half duration is taken from
the gap to deinterlace_frame_ts, which is 0 for a source that never showed
a frame (half its own timestamp, truncated: 1.78 s for a capture timestamp
1000 s after boot) and the last frame shown after a pause (a 10 s pause
gives 705 ms).

obs_source_deinterlace_pacing_half_duration() gets two more arguments, the
half duration before and the canvas interval, so the tests can state the
fallback. They are not used yet: the C, the Rust port and the C oracle
behave as before.

- test_deinterlace_pacing_steps (and its _rust variant) and the Rust
  Tier 1 port: half_duration_first_frame and
  first_frame_half_duration_hold now expect the fixed values (half the
  canvas interval for a new source, the half duration before after a
  restart or a pause). half_duration_over_2_32_truncated and
  half_duration_timestamp_backwards_wraps become
  half_duration_after_a_pause and half_duration_timestamp_backwards, and
  half_duration_fallback is new. seq_unbuffered_first_frame runs the
  unbuffered model from a source that never showed a frame, and
  seq_unbuffered_12_5 pins 12.5 frames a second (80 ms gaps), which must
  keep its half durations from the gap.
- The Tier 1 edge model and proptests take the new rule.
- test_deinterlace_2x_field_order gets 15 cases that count the first
  frame's fields, field 0 included: first_frame (a source that never
  showed a frame, buffered and unbuffered) and after_pause (an unbuffered
  source that pauses for 150 ms, past TS_JUMP_THRESHOLD), with Linear 2x
  at the five rate pairs. The 40 existing cases still leave out their
  first two frames: their first measured frame follows the last warm-up
  frame back to back, so it has no frame duration either way.

Fails as expected on all three builds (C backend, hybrid,
libobs-c-reference): the steps test 7 of 23, Tier 1 9 of 33, and
test_deinterlace_2x_field_order 10 of 55: the three first-frame cases at
60i into 60, 50i into 50 and 50i into 60 fps, and the buffered first_frame
case at 50i into 30 fps (Linear 2x at 60i into 60 fps shows 0 2 3 ...,
field 1 skipped). The 40 existing cases pass.

Refs obs-rust#1965

* libobs: Show both fields of the first 2x deinterlaced frame

With Yadif 2x, Linear 2x or Discard 2x, the first frame a source showed,
and the first after a restart, a full queue or a pause, lost its second
field, or was held too long. When that frame was picked with no second
frame queued, its half duration was half the gap to deinterlace_frame_ts:
0 for a new source, so half its own timestamp truncated to 32 bits (1.78 s
for a capture timestamp 1000 s after boot), or the last frame shown, so
705 ms after a 10 s pause. A buffered source then showed the second field
only after the next frame had replaced the first; an unbuffered one held
the frame past TS_JUMP_THRESHOLD, moved on as if the clock had jumped, or,
when the truncation left 0.5 to 35.5 ms, held it too long.

obs_source_deinterlace_pacing_half_duration() no longer takes a half
duration from a gap that is no frame duration: no previous timestamp (0),
one at or after the frame (a timestamp going backwards), or one more than
twice TS_JUMP_THRESHOLD before it, a half duration past the threshold (a
pause or a new clock). It keeps the half duration before when that is set
and at most TS_JUMP_THRESHOLD, and else takes half the canvas interval, so
the second field comes at the next tick. A regular stream with frames up
to 140 ms apart keeps its half durations, so a source below 14.3 frames a
second does not change. The C, the Rust port and the C oracle (which compiles the libobs
file) change alike; every rule of obs-rust#395 and of the unbuffered hold is
unchanged.

- Tier 3: the sequences now count the fallbacks (no previous timestamp,
  backwards, past the threshold; the half duration kept or half the
  interval) instead of the truncation, which can no longer happen.
  first_frame_cadences runs 25, 29.97, 50 and 59.94 frames a second into
  30 and 60 fps, buffered and unbuffered, for a new source and after a
  pause, through the model of the caller: the C and the shims agree, the
  first frame shows its fields as a source timed from a frame before it
  would (buffered always; unbuffered unless its next frame is there at
  the next tick), and from the next pick on every state is the one of
  before. low_rate_cadences does the same at 12.5 frames a second: every
  state after the first frame, and up to a pause, is the one of before.
- docs/features.md: a line under fixed upstream bugs. rust/README.md and
  the module docs drop the characterized truncation and first-frame
  behaviour.

test_deinterlace_pacing_steps (and _rust) 23/23, Tier 1 33/33, Tier 3
13/13, and test_deinterlace_2x_field_order 55/55 on the C backend, the
hybrid and libobs-c-reference.

Fixes obs-rust#1965
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…s behind

With Yadif 2x, Linear 2x or Discard 2x, a buffered source that fell
behind could skip a frame later on. When ready_deinterlace_frames()
found three frames queued and all of them overdue, it erased the first
(prev_frame), returned it to the async cache with remove_async_frame(),
found two frames left, and then set prev_frame->prev_frame on the frame
it had just returned. remove_async_frame() clears the flag, but this
write came after it, and cache_video() did not clear the flag when it
reused the entry. So the next frame to reuse the entry was queued already
flagged. When it was picked with a frame behind it,
deinterlace_get_closest_frames() took it as the previous frame of a pair
and showed the frame behind it instead: one frame early, with the
reused frame's fields skipped (with Yadif 2x, its first field).

The walk's exit no longer flags anything. The line came with the walk
(07c644c, 2016) and was copied from the walk's other exit, where
prev_frame is the front frame and is paired with frame. In this exit,
the frame shown is the front one alone, whether prev_frame is set or
not. Its previous frame has already been erased, so there is nothing to
pair it with. The pick at the exit is unchanged: the flag was never on a
queued frame. (Flagging the front frame instead would pair it with the
frame behind it and drop one more frame at every such exit.)
prev_frame implies frame, so "else if (!frame && num == 2)" becomes a
plain if. cache_video() also clears prev_frame when it reuses a cache
entry, so a frame from the cache always starts unpaired. Each change
alone fixes the RED cases. Every rule of obs-rust#395 in the walk is unchanged:
FRAME_TOLERANCE, deinterlace_offset = 0 on every buffered exit, and the
unbuffered path. The pacing functions, the Rust port and the C oracle are
unchanged.

- Tier 3: the caller model keeps the async cache (cache_video() with
  clean_cache(), remove_async_frame(), free_async_cache() and the
  render's release of the previous frame), so a reused frame starts with
  the flag its entry was left with. A full queue now also empties the
  queue in the model, as free_async_cache() does. The walk and the cache
  can each run as before obs-rust#2009 or as now:
  - behind_cadence runs the behind cases. Before, frames 0 2 3 5 6 7 8 9
    are picked (4 is paired with 5). Now 0 2 3 4 5 6 7 8 9 are picked,
    and each change alone gives the same.
  - buffered_walk_cadences runs the cadences of first_frame_cadences and
    low_rate_cadences, with pauses of 150 and 500 ms (140 runs). None
    changes: one run reaches the exit, and no frame is reused flagged.
  - buffered_walk_sequences runs the 2000 sequences of
    sequences_reach_every_branch. 461 reach the exit and 170 change, the
    170 in which a frame reused with the flag left on was paired.
    Flagging the front frame instead would change all 461.
- docs/features.md: a line under fixed upstream bugs.

test_deinterlace_2x_field_order 73/73 on the C backend, the hybrid and
libobs-c-reference.

Fixes obs-rust#2009
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 24, 2026
…s behind (obs-rust#2048)

* test: Expect every field after a buffered 2x source falls behind

With a double-rate deinterlace mode (Yadif 2x, Linear 2x, Discard 2x), a
buffered source that falls behind can skip a whole frame later on. When
the buffered walk in ready_deinterlace_frames() finds three frames queued
and all of them overdue, it erases the first, returns it to the async
cache with remove_async_frame(), and then sets prev_frame on it.
cache_video() does not clear the flag when it reuses the entry, so the next
frame to reuse it is queued already flagged. When that frame is picked
with a frame behind it, deinterlace_get_closest_frames() takes it as the
previous frame of a pair and shows the frame behind it instead.

test_deinterlace_2x_field_order gets 18 cases:
- behind (Yadif 2x and Linear 2x; 60i into 60, 50i into 50, 50i into 60
  and 60i into 50 fps): a source that never showed a frame shows frame 0.
  Right after a tick it then gets frames 1 to 3 at once, stamped 4, 8 and
  12 ms after frame 0, so all three are overdue at the next tick. Right
  after that tick, frames 4 to 9 come at once at the source's rate after
  frame 3. Frame 4 reuses frame 1's cache entry. From frame 4's first
  field on, every field must be shown in order (no two in a row dropped
  at 60i into 50 fps).
- after_pause, buffered (Yadif 2x and Linear 2x, the five rate pairs): the
  first_frame cases' 150 ms pause, for a buffered source. A pause does not
  reach the walk's exit (the source's clock stands still while nothing is
  queued), so these pass already and guard the pause. The comment that
  called this a separate issue is dropped.

Fails as expected on the C backend: 7 of 73. All the behind cases fail
except Yadif 2x at 60i into 50 fps, which may drop one field in a row.
Linear 2x shows 0 1 5 7 10 11 ...: frame 4 (fields 8 and 9) is never
shown. Yadif 2x shows 0 4 6 9 10 ...: field 8 is skipped. The 10
after_pause cases and the 55 existing cases pass.

Refs obs-rust#2009

* libobs: Show every 2x deinterlaced frame after a buffered source falls behind

With Yadif 2x, Linear 2x or Discard 2x, a buffered source that fell
behind could skip a frame later on. When ready_deinterlace_frames()
found three frames queued and all of them overdue, it erased the first
(prev_frame), returned it to the async cache with remove_async_frame(),
found two frames left, and then set prev_frame->prev_frame on the frame
it had just returned. remove_async_frame() clears the flag, but this
write came after it, and cache_video() did not clear the flag when it
reused the entry. So the next frame to reuse the entry was queued already
flagged. When it was picked with a frame behind it,
deinterlace_get_closest_frames() took it as the previous frame of a pair
and showed the frame behind it instead: one frame early, with the
reused frame's fields skipped (with Yadif 2x, its first field).

The walk's exit no longer flags anything. The line came with the walk
(07c644c, 2016) and was copied from the walk's other exit, where
prev_frame is the front frame and is paired with frame. In this exit,
the frame shown is the front one alone, whether prev_frame is set or
not. Its previous frame has already been erased, so there is nothing to
pair it with. The pick at the exit is unchanged: the flag was never on a
queued frame. (Flagging the front frame instead would pair it with the
frame behind it and drop one more frame at every such exit.)
prev_frame implies frame, so "else if (!frame && num == 2)" becomes a
plain if. cache_video() also clears prev_frame when it reuses a cache
entry, so a frame from the cache always starts unpaired. Each change
alone fixes the RED cases. Every rule of obs-rust#395 in the walk is unchanged:
FRAME_TOLERANCE, deinterlace_offset = 0 on every buffered exit, and the
unbuffered path. The pacing functions, the Rust port and the C oracle are
unchanged.

- Tier 3: the caller model keeps the async cache (cache_video() with
  clean_cache(), remove_async_frame(), free_async_cache() and the
  render's release of the previous frame), so a reused frame starts with
  the flag its entry was left with. A full queue now also empties the
  queue in the model, as free_async_cache() does. The walk and the cache
  can each run as before obs-rust#2009 or as now:
  - behind_cadence runs the behind cases. Before, frames 0 2 3 5 6 7 8 9
    are picked (4 is paired with 5). Now 0 2 3 4 5 6 7 8 9 are picked,
    and each change alone gives the same.
  - buffered_walk_cadences runs the cadences of first_frame_cadences and
    low_rate_cadences, with pauses of 150 and 500 ms (140 runs). None
    changes: one run reaches the exit, and no frame is reused flagged.
  - buffered_walk_sequences runs the 2000 sequences of
    sequences_reach_every_branch. 461 reach the exit and 170 change, the
    170 in which a frame reused with the flag left on was paired.
    Flagging the front frame instead would change all 461.
- docs/features.md: a line under fixed upstream bugs.

test_deinterlace_2x_field_order 73/73 on the C backend, the hybrid and
libobs-c-reference.

Fixes obs-rust#2009
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
…ce-draw.c

Behaviour-neutral extraction before the Rust port. The decisions of
get_effect(), deinterlace_linear_required(), deinterlace_render() and
obs_source_set_deinterlace_mode() in obs-source-deinterlace.c.h move to
libobs/obs-source-deinterlace-draw.c.h as ten functions on plain values,
declared in the new libobs/obs-source-deinterlace-draw.h without EXPORT
(hidden, as -fvisibility=hidden keeps them):

- obs_source_deinterlace_draw_effect(): which effect a mode draws with,
  an enum C maps to the effect slot and file (NONE for DISABLE and for a
  mode outside the enum);
- obs_source_deinterlace_draw_linear_required(): the modes that need
  linear sRGB input;
- obs_source_deinterlace_draw_skip(): the early return of
  deinterlace_render();
- obs_source_deinterlace_draw_reads_linear_srgb() and
  obs_source_deinterlace_draw_linear_srgb(): the linear sRGB flag, and
  whether gs_get_linear_srgb() is read for it;
- obs_source_deinterlace_draw_technique(),
  obs_source_deinterlace_draw_reads_white_level() and
  obs_source_deinterlace_draw_multiplier(): the technique (an enum C maps
  to "Draw", "DrawMultiply", "DrawTonemap" or "DrawMultiplyTonemap") and
  the multiplier for each pair of source and current colour spaces;
- obs_source_deinterlace_draw_frame2(): whether the second field is due,
  video_time >= frame_ts + offset + half_duration - TWOX_TOLERANCE in
  uint64_t (obs-rust#395, obs-rust#1995, obs-rust#2048);
- obs_source_deinterlace_draw_mode_change(): same, enable, disable or
  switch, a mode outside the enum being DISABLE (obs-rust#2120, which moves here
  with deinterlace_mode_known()).

obs-source-deinterlace.c.h keeps every gs_* call in its order and under
the same conditions, obs_load_effect() and the effect slots,
convert_video_space(), obs_get_video_sdr_white_level(),
enable_deinterlacing(), disable_deinterlacing() and obs_enter_graphics().
The early return still comes before convert_video_space() and
gs_get_color_space(), so the decisions are separate calls rather than one
plan: gs_get_linear_srgb() is still read only for a GS_CS_SRGB source (the
short circuit of the old expression), and obs_get_video_sdr_white_level()
only for a pair that multiplies, after gs_get_color_space(). The
multiplier is the same single float division, sdr_white_level / 80.0f or
80.0f / sdr_white_level. The frame2 sum is the same uint64_t arithmetic,
wrapping where it wrapped; TWOX_TOLERANCE, used only there, is now the
pacing header's OBS_SOURCE_DEINTERLACE_PACING_TWOX_TOLERANCE (1000000ULL,
which the old int constant converted to). Modes and colour spaces cross
as int, so a value outside either enum takes the paths it took before;
obs-source-deinterlace.c.h checks that both enums are int-sized.

Checked against the unchanged functions of origin/master (outside the
tree, not committed): both versions compiled against stubs that log
every gs_* and obs_* call with its arguments (floats as their bits), and
the traces compared byte for byte, under GCC -O2 and clang -O0. 100,601
effect lookups (modes -300 to 300 and random int), 585,120 renders (every
texture and size combination of the early return; 9 source x 9 current
spaces, in the enum and out, x the graphics linear flag x 16 modes x 20
white levels, NaNs, infinities, signed zeros and subnormals included;
200,000 random white level bit patterns; every combination of 16 edge
u64 values for video_time, frame_ts and offset with 8 half durations;
300,000 random and near-threshold frame2 inputs) and 100,512 mode changes
(valid or not x 16 current x 16 new modes, and a random sequence). The
traces are identical; a planted change to the linear sRGB read or to the
frame2 compare is caught.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header. The exported symbol sets are unchanged (1766).

Refs obs-rust#2130
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
Ports libobs/obs-source-deinterlace-draw.c.h, the deinterlace draw
decisions of obs-source-deinterlace.c, to the deinterlace_draw module of
rust/obs-source. obs-source-deinterlace.c.h stays C: every gs_* call in
its order and under its conditions, obs_load_effect() and the effect
slots, the technique names, convert_video_space(),
obs_get_video_sdr_white_level(), enable_deinterlacing(),
disable_deinterlacing() and obs_enter_graphics(). The hybrid libobs drops
the C file and links the Rust shims in its place.

Safe core (src/deinterlace_draw.rs):
- effect() and effect_index(): a match over the mode's int, the C's
  numbers (DISCARD 0 to YADIF_2X 7, RETRO drawing with discard 2x), None
  for DISABLE and any value outside the enum; mode_known() is DISABLE or
  an effect;
- linear_required(): BLEND to YADIF_2X;
- skip(): no current or previous texture, or a width or height of 0;
- reads_linear_srgb() (GS_CS_SRGB only) and linear_srgb() (not
  GS_CS_SRGB, the graphics flag, or linear_required());
- technique(), Technique with the C's numbers: DrawMultiply for SDR or
  709_EXTENDED into scRGB, DrawTonemap for 709_EXTENDED into SDR,
  DrawMultiplyTonemap for scRGB into SDR, DrawMultiply for scRGB into
  709_EXTENDED, Draw otherwise (spaces outside the enum included);
  reads_white_level() when it multiplies;
- multiplier(): sdr_white_level / 80.0 into scRGB, 80.0 / sdr_white_level
  out of it, 1.0 otherwise: one f32 division, the same IEEE operation as
  the C's, so the bits match, NaN payloads and infinities included;
- frame2(): video_time >= frame_ts + offset + half_duration -
  TWOX_TOLERANCE (deinterlace_pacing's), each step wrapping_* as the C's
  uint64_t (obs-rust#395, obs-rust#1995, obs-rust#2048);
- mode_change(): a mode outside the enum is DISABLE (obs-rust#2120), then same,
  enable from DISABLE, disable to DISABLE, else switch.
The module denies clippy's arithmetic, indexing, as, unwrap, expect,
panic, todo, unimplemented and unreachable lints, keeps no state and takes
no lock.

Shim (src/ffi/source_deinterlace_draw.rs): the ten functions keep the C
signatures, plain values in and out. The effect comes back as int (-1 for
none), the technique and the mode change as the C's enum values. No
intentional differences: there is no pointer to check.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-deinterlace-draw (LIBOBS_PORTED_SOURCES), whose PCH
is obs.h (the two enums), obs-source-deinterlace-draw.h and
obs-source-deinterlace-pacing.h (TWOX_TOLERANCE). libobs.pch.h keeps the
header for obs-source-deinterlace.c. The functions are not EXPORT, so the
shims are hidden: rust-hidden.txt, the local: list of rust-exports.map
and rust-unexports-macos.txt. libobs-rust already links the crate's ffi.
The exported symbol sets are unchanged and equal: 1766 on the C backend,
the hybrid and libobs-c-reference; the ten are local in all three, and the
hybrid's come from the Rust archive. ci/test_ported_sources.py checks the
group on every host.

test_deinterlace_draw_plan_steps still compiles the C file into itself,
unchanged. The new test_deinterlace_draw_plan_steps_rust builds the same
test source against libobs_rust-static (and libm except on MSVC): the test
calls only the ten functions and includes their header and obs.h for the
enum values, so it takes libobs's include directories (SIMDe,
obsconfig.h) and links the archive alone. It passes 17 of 17 against the
shims. test_deinterlace_2x_field_order, test_deinterlace_mode_load and
test_media_restart_first_frame see the swap through the hybrid.

Tier 1 source::deinterlace_draw passes 27/27.

Refs obs-rust#2130
rdoupe added a commit to rdoupe/obs-studio that referenced this pull request Oct 24, 2026
* libobs: Move the deinterlace draw decisions into obs-source-deinterlace-draw.c

Behaviour-neutral extraction before the Rust port. The decisions of
get_effect(), deinterlace_linear_required(), deinterlace_render() and
obs_source_set_deinterlace_mode() in obs-source-deinterlace.c.h move to
libobs/obs-source-deinterlace-draw.c.h as ten functions on plain values,
declared in the new libobs/obs-source-deinterlace-draw.h without EXPORT
(hidden, as -fvisibility=hidden keeps them):

- obs_source_deinterlace_draw_effect(): which effect a mode draws with,
  an enum C maps to the effect slot and file (NONE for DISABLE and for a
  mode outside the enum);
- obs_source_deinterlace_draw_linear_required(): the modes that need
  linear sRGB input;
- obs_source_deinterlace_draw_skip(): the early return of
  deinterlace_render();
- obs_source_deinterlace_draw_reads_linear_srgb() and
  obs_source_deinterlace_draw_linear_srgb(): the linear sRGB flag, and
  whether gs_get_linear_srgb() is read for it;
- obs_source_deinterlace_draw_technique(),
  obs_source_deinterlace_draw_reads_white_level() and
  obs_source_deinterlace_draw_multiplier(): the technique (an enum C maps
  to "Draw", "DrawMultiply", "DrawTonemap" or "DrawMultiplyTonemap") and
  the multiplier for each pair of source and current colour spaces;
- obs_source_deinterlace_draw_frame2(): whether the second field is due,
  video_time >= frame_ts + offset + half_duration - TWOX_TOLERANCE in
  uint64_t (obs-rust#395, obs-rust#1995, obs-rust#2048);
- obs_source_deinterlace_draw_mode_change(): same, enable, disable or
  switch, a mode outside the enum being DISABLE (obs-rust#2120, which moves here
  with deinterlace_mode_known()).

obs-source-deinterlace.c.h keeps every gs_* call in its order and under
the same conditions, obs_load_effect() and the effect slots,
convert_video_space(), obs_get_video_sdr_white_level(),
enable_deinterlacing(), disable_deinterlacing() and obs_enter_graphics().
The early return still comes before convert_video_space() and
gs_get_color_space(), so the decisions are separate calls rather than one
plan: gs_get_linear_srgb() is still read only for a GS_CS_SRGB source (the
short circuit of the old expression), and obs_get_video_sdr_white_level()
only for a pair that multiplies, after gs_get_color_space(). The
multiplier is the same single float division, sdr_white_level / 80.0f or
80.0f / sdr_white_level. The frame2 sum is the same uint64_t arithmetic,
wrapping where it wrapped; TWOX_TOLERANCE, used only there, is now the
pacing header's OBS_SOURCE_DEINTERLACE_PACING_TWOX_TOLERANCE (1000000ULL,
which the old int constant converted to). Modes and colour spaces cross
as int, so a value outside either enum takes the paths it took before;
obs-source-deinterlace.c.h checks that both enums are int-sized.

Checked against the unchanged functions of origin/master (outside the
tree, not committed): both versions compiled against stubs that log
every gs_* and obs_* call with its arguments (floats as their bits), and
the traces compared byte for byte, under GCC -O2 and clang -O0. 100,601
effect lookups (modes -300 to 300 and random int), 585,120 renders (every
texture and size combination of the early return; 9 source x 9 current
spaces, in the enum and out, x the graphics linear flag x 16 modes x 20
white levels, NaNs, infinities, signed zeros and subnormals included;
200,000 random white level bit patterns; every combination of 16 edge
u64 values for video_time, frame_ts and offset with 8 half durations;
300,000 random and near-threshold frame2 inputs) and 100,512 mode changes
(valid or not x 16 current x 16 new modes, and a random sequence). The
traces are identical; a planted change to the linear sRGB read or to the
frame2 compare is caught.

The new file joins libobs.core-source with libobs.pch.h, which includes
the new header. The exported symbol sets are unchanged (1766).

Refs obs-rust#2130

* test: Characterize the deinterlace draw decisions

test_deinterlace_draw_plan_steps compiles
libobs/obs-source-deinterlace-draw.c.h (ASan and UBSan) and calls only
its ten hidden functions. 17 cases:

- enum_values: the effect, technique and mode change numbers the C maps
  (and the port will return), and that each enum is int-sized;
- effect_every_mode, effect_outside_enum: each mode's effect, RETRO
  drawing with discard 2x; none for DISABLE, -1, 99, 9 (one past
  YADIF_2X) and the ends of int;
- linear_required_every_mode: blend, linear and yadif (and their 2x)
  need linear input; disable, discard, retro and modes outside the enum
  do not;
- skip_without_texture_or_size: the early return for every combination
  of the two textures and widths and heights of 0, 1, 1080 and
  UINT32_MAX;
- reads_linear_srgb_every_space, linear_srgb_every_input:
  gs_get_linear_srgb() is read for a GS_CS_SRGB source only; the flag is
  set for any other space (outside the enum included), and for SDR by the
  graphics flag or the mode;
- technique_space_pairs, multiplier_space_pairs: the 4 x 4 pairs of
  source and current spaces, the technique, whether the white level is
  read, and the multiplier as float bits at SDR white levels of 80, 300
  and 0 (300 / 80 = 3.75, 80 / 300 = 0x3e888889);
- technique_outside_enum: a space outside the enum, as source or current,
  draws with Draw at 1 and reads no white level;
- multiplier_any_level: -0, infinity, -80, 480, the smallest subnormal and
  NaN, each one IEEE division;
- frame2_at_threshold: due at frame_ts + offset + half_duration -
  TWOX_TOLERANCE (>=), not 1 ns before; the half duration counted in full
  up to UINT32_MAX;
- frame2_sum_wraps, frame2_below_tolerance_wraps: the uint64_t wraps;
- mode_change_every_pair, mode_change_outside_enum,
  mode_change_current_outside_enum: same, enable, disable or switch for
  every pair of modes; a mode outside the enum disables, or does nothing
  when deinterlacing is off (obs-rust#2120).

Characterized, not endorsed:
- obs_get_video_sdr_white_level() is not checked: a level of 0 gives a
  multiplier of +inf from scRGB (80.0f / 0), -0 gives -inf, a negative
  level a negative multiplier and a NaN a NaN. OBS sets levels from 80 to
  480 nits;
- the frame2 threshold wraps: when frame_ts + offset + half_duration
  passes UINT64_MAX it lands near 0 and the second field is due at once;
  when half_duration is under TWOX_TOLERANCE and frame_ts + offset is
  smaller than the difference, it lands near UINT64_MAX and the second
  field is never due (a source has frame_ts and offset 0 only before its
  first frame, when nothing draws);
- a current mode outside the enum, which obs_source_set_deinterlace_mode()
  no longer stores since obs-rust#2120, is not DISABLE: a mode in the enum
  switches, and one outside it disables, the same value included.

The test is in the native test graph in name order, with libm except on
MSVC, in the core suite (no suite size is pinned in
rust/obs-build/tests/native.rs). On the C backend it passes 17 of 17, and
test_deinterlace_2x_field_order (all five rates), test_deinterlace_pacing_steps,
test_deinterlace_mode_load and test_media_restart_first_frame are
unchanged and pass.

Refs obs-rust#2130

* obs-source: Add Tier 1 tests for the deinterlace draw port

Tests for the port of libobs/obs-source-deinterlace-draw.c.h to the
deinterlace_draw module of rust/obs-source, next to deinterlace_pacing.
The safe core is a stub (todo!()):

- effect() and effect_index() (obs_source_deinterlace_draw_effect(), an
  Effect or its index, None for no effect), mode_known(),
  linear_required(), skip(), reads_linear_srgb(), linear_srgb(),
  technique() (a Technique), reads_white_level(), multiplier() (f32),
  frame2() and mode_change() (a ModeChange), on plain values: modes and
  colour spaces as the C's int, sizes as u32, times as u64;
- the enum obs_deinterlace_mode and enum gs_color_space values, and
  Effect, Technique and ModeChange with the C's numbers.

tests/source/deinterlace_draw.rs ports test_deinterlace_draw_plan_steps
1:1: its 17 cases under the same names (steps_*), in the same order,
with the same inputs and expectations, the multipliers compared as bits.

Edge cases (edge_*): the mode functions for every mode from -1000 to 1000
and near the ends of i32; the technique, the white level read and the
multiplier for every pair of 260 spaces around the enum and the ends of
i32; the multiplier at 26 special f32 bit patterns (zeros, subnormals,
the normal ends, OBS's levels, infinities, quiet and signalling NaNs of
both signs with payloads) for every pair of 9 spaces, against the one
division written out; frame2 over every combination of 16 special u64
values and 8 half durations, and at the threshold -2 to +2, against a
model in i128 with the modulo explicit; every mode change for 20 modes.
Proptests (4000 cases each): frame2 over full-range u64 times and u32
half durations, half of them at the threshold +-3; the multiplier over
any space and any white level bit pattern; the mode functions and the
linear flag over any i32.

soldr cargo test -p obs-data --test obs-tests-core source::deinterlace_draw
fails 27 of 27 with "not yet implemented".

Refs obs-rust#2130

* obs-source: Port the deinterlace draw decisions

Ports libobs/obs-source-deinterlace-draw.c.h, the deinterlace draw
decisions of obs-source-deinterlace.c, to the deinterlace_draw module of
rust/obs-source. obs-source-deinterlace.c.h stays C: every gs_* call in
its order and under its conditions, obs_load_effect() and the effect
slots, the technique names, convert_video_space(),
obs_get_video_sdr_white_level(), enable_deinterlacing(),
disable_deinterlacing() and obs_enter_graphics(). The hybrid libobs drops
the C file and links the Rust shims in its place.

Safe core (src/deinterlace_draw.rs):
- effect() and effect_index(): a match over the mode's int, the C's
  numbers (DISCARD 0 to YADIF_2X 7, RETRO drawing with discard 2x), None
  for DISABLE and any value outside the enum; mode_known() is DISABLE or
  an effect;
- linear_required(): BLEND to YADIF_2X;
- skip(): no current or previous texture, or a width or height of 0;
- reads_linear_srgb() (GS_CS_SRGB only) and linear_srgb() (not
  GS_CS_SRGB, the graphics flag, or linear_required());
- technique(), Technique with the C's numbers: DrawMultiply for SDR or
  709_EXTENDED into scRGB, DrawTonemap for 709_EXTENDED into SDR,
  DrawMultiplyTonemap for scRGB into SDR, DrawMultiply for scRGB into
  709_EXTENDED, Draw otherwise (spaces outside the enum included);
  reads_white_level() when it multiplies;
- multiplier(): sdr_white_level / 80.0 into scRGB, 80.0 / sdr_white_level
  out of it, 1.0 otherwise: one f32 division, the same IEEE operation as
  the C's, so the bits match, NaN payloads and infinities included;
- frame2(): video_time >= frame_ts + offset + half_duration -
  TWOX_TOLERANCE (deinterlace_pacing's), each step wrapping_* as the C's
  uint64_t (obs-rust#395, obs-rust#1995, obs-rust#2048);
- mode_change(): a mode outside the enum is DISABLE (obs-rust#2120), then same,
  enable from DISABLE, disable to DISABLE, else switch.
The module denies clippy's arithmetic, indexing, as, unwrap, expect,
panic, todo, unimplemented and unreachable lints, keeps no state and takes
no lock.

Shim (src/ffi/source_deinterlace_draw.rs): the ten functions keep the C
signatures, plain values in and out. The effect comes back as int (-1 for
none), the technique and the mode change as the C's enum values. No
intentional differences: there is no pointer to check.

C side: the file leaves libobs.core-source for its own group,
libobs.ported-source-deinterlace-draw (LIBOBS_PORTED_SOURCES), whose PCH
is obs.h (the two enums), obs-source-deinterlace-draw.h and
obs-source-deinterlace-pacing.h (TWOX_TOLERANCE). libobs.pch.h keeps the
header for obs-source-deinterlace.c. The functions are not EXPORT, so the
shims are hidden: rust-hidden.txt, the local: list of rust-exports.map
and rust-unexports-macos.txt. libobs-rust already links the crate's ffi.
The exported symbol sets are unchanged and equal: 1766 on the C backend,
the hybrid and libobs-c-reference; the ten are local in all three, and the
hybrid's come from the Rust archive. ci/test_ported_sources.py checks the
group on every host.

test_deinterlace_draw_plan_steps still compiles the C file into itself,
unchanged. The new test_deinterlace_draw_plan_steps_rust builds the same
test source against libobs_rust-static (and libm except on MSVC): the test
calls only the ten functions and includes their header and obs.h for the
enum values, so it takes libobs's include directories (SIMDe,
obsconfig.h) and links the archive alone. It passes 17 of 17 against the
shims. test_deinterlace_2x_field_order, test_deinterlace_mode_load and
test_media_restart_first_frame see the swap through the hybrid.

Tier 1 source::deinterlace_draw passes 27/27.

Refs obs-rust#2130

* obs-source: Check the deinterlace draw port against the C

Oracle (oracle/source_deinterlace_draw.c): the original
obs-source-deinterlace-draw.c.h, unmodified, with its ten functions
renamed to oracle_*, plus every enum value the port uses (the nine modes,
the four colour spaces, the effect, technique and mode change values),
TWOX_TOLERANCE and the enum, int, bool and float sizes. The rename macros
also rename the three enum tags that share their function's name, so the
size table names the renamed tags. The bool results are declared as u8 on
the Rust side, the byte the C returns.

Tier 3 (tests/source/deinterlace_draw_parity.rs, 12 tests). Every
function takes and returns plain values, so each run compares the bytes
of the two results: the bools as bytes, the enums as values, the
multiplier as its f32 bits.
- the values and sizes against the C headers;
- modes: every value from -70000 to 70000, every 65521st i32 over the
  whole range and the ends: 205,551 modes, the effect and the linear
  rule; every mode change between 41 modes (1,681 pairs);
- space pairs: the technique, the white level read and the multiplier for
  every pair of 649 spaces (-320 to 320 and four near each end of i32):
  421,201 pairs; every pair of -3 to 6 at 26 special white levels (zeros,
  subnormals, the normal ends, 80 to 480 nits, infinities, quiet and
  signalling NaNs of both signs with payloads);
- the linear flag for every space, both graphics flags and the 41 modes
  (53,218); the early return for both flags each and 18 widths and
  heights (1,296);
- frame2 for every combination of 18 special u64 values and 10 half
  durations (58,320), and at the threshold -2 to +2 (16,200);
- deinterlace_render()'s decisions in its order (the early return, the
  linear sRGB read and flag, the technique, the white level read, the
  multiplier only when it is read, frame2), one copy on each side: 51,840
  renders;
- proptests (20,000 cases each): frame2 over full-range u64 times and u32
  half durations, half of them at the threshold +-3; the multiplier and
  technique over any space and any white level bit pattern (never a
  float range); the mode functions over any i32; the render decisions
  over all of it at once.

Tier 1 and the parity module passed 10 runs in a row with fresh seeds
(39/39 each). No regression file came up; the ones the todo!() stub and
the mutants left were deleted.

Mutation check (once, not committed): 40 mutants, Tier 1 (27 tests) and
Tier 3 (12), failures per mutant.

| Mutant | Tier 1 | Tier 3 |
|---|---|---|
| effect: RETRO as discard / YADIF_2X as yadif / DISABLE as discard | 3 / 3 / 3 | 2 / 2 / 2 |
| effect_index: blend 2x is 2 | 2 | 3 |
| linear_required: RETRO too / not YADIF_2X | 4 / 4 | 5 / 5 |
| skip: height ignored / both textures missing only | 1 / 1 | 3 / 3 |
| reads_linear_srgb: SRGB_16F too | 2 | 3 |
| linear_srgb: graphics flag ignored / mode ignored | 2 / 2 | 3 / 3 |
| SRGB_16F not SDR | 4 | 4 |
| technique: 709_EXTENDED into SDR not tonemapped / scRGB into SDR not tonemapped / scRGB into 709_EXTENDED drawn | 2 / 2 / 4 | 4 / 4 / 4 |
| reads_white_level: DrawMultiply only | 2 | 3 |
| multiplier: into scRGB inverted / 80.00001 / times 1/80 / scRGB into scRGB divides | 4 / 4 / 2 / 5 | 4 / 4 / 4 / 2 |
| frame2: > / saturating sum / saturating TWOX / no TWOX / half doubled | 6 / 4 / 3 / 6 / 6 | 2 / 3 / 3 / 3 / 3 |
| mode_change: unknown kept (pre-obs-rust#2120) / unknown is same / same before normalizing | 4 / 4 / 3 | 2 / 2 / 2 |
| shim: no effect is 0 / tonemap numbers swapped / enable and disable swapped | 0 / 0 / 0 | 3 / 4 / 2 |
| shim: graphics flag negated / spaces swapped in multiplier / video_time and frame_ts swapped / modes swapped | 0 / 0 / 0 / 0 | 3 / 4 / 3 / 2 |
| mode_known: DISABLE unknown | 2 | 0 |
| equivalent: multiplier divided in f64 and rounded / disable tested before enable / shim frame_ts and offset swapped / shim width and height swapped | 0 / 0 / 0 / 0 | 0 / 0 / 0 / 0 |

35 are killed by Tier 3. The other five are equivalent through the C
ABI: mode_known() is used only by mode_change(), which maps an unknown
mode to DISABLE, so DISABLE counting as unknown changes nothing (Tier 1
kills it, as it tests mode_known() itself); a single f32 division done in
f64 and rounded is correctly rounded the same; the disable test can only
come first when the current mode is DISABLE and the new one is too,
which is "same" before either; the sum and the size test are symmetric.
Tier 1 does not reach the shim; test_deinterlace_draw_plan_steps_rust and
Tier 3 do.

No intentional differences: there is no pointer to check.

Refs obs-rust#2130

* docs: Record the deinterlace draw port

The rust/README.md crate table now lists
libobs/obs-source-deinterlace-draw.c.h among the rust/obs-source units,
and its port table has a row for it: the ten functions and what they
decide, what stays C (every gs_* call in its order and under its
conditions, obs_load_effect() and the effect slots, the technique names,
convert_video_space(), obs_get_video_sdr_white_level(),
enable_deinterlacing(), disable_deinterlacing() and obs_enter_graphics()),
why the decisions are separate calls rather than one plan (the early
return comes before convert_video_space() and gs_get_color_space()), the
modes and spaces as int, the multiplier as one f32 division matching bit
for bit, the frame2 wraps it keeps, no intentional differences, the _rust
variant of the characterization, the real-libobs tests and what Tier 3
compares. The crate's description and lib.rs say what it holds.
docs/rust-port/naming.md already maps libobs/obs-source*.c.h to
rust/obs-source.

port_comparison gets its own bench file,
benches/port_comparison/source_deinterlace_draw.rs: 1024 renders (the
decisions of deinterlace_render() in its order, over every pair of
colour spaces, the nine modes, white levels of 80 to 480 nits and field
times either side of the threshold, one in 64 returning early) and 1024
mode changes with the new mode's effect, folded into checksums both sides
must match. On x86_64 Linux (GCC, not pinned, load average 13 to 17;
median, two runs): render_frame 25.2 and 25.8 us Rust, 13.9 and 13.3 us
C per 1024 renders (1.8x to 1.9x), about 12 ns more per render, which
runs once a frame for each deinterlaced source; mode_change 4.1 and 5.8
us Rust, 6.0 and 4.2 us C, within the noise.

Fixes obs-rust#2130
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: libobs: 2x deinterlacing skips fields when the source's field rate differs from the canvas (50i into 60 fps) (upstream #3402)

2 participants