Skip to content

obs-source: Port the 2x deinterlace frame pacing - #1954

Merged
zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/deinterlace-pacing
Oct 23, 2026
Merged

zackees merged 6 commits into
obs-rust:masterfrom
rdoupe:port/deinterlace-pacing

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1950

What and why

Ports the frame pacing of double-rate deinterlacing (Yadif 2x, Linear 2x, Discard 2x) in libobs/obs-source-deinterlace.c.h to rust/obs-source, as a new module deinterlace_pacing next to audio_timing (#1936). With a 2x mode each async frame is shown as two fields, the second one from deinterlace_frame_ts + deinterlace_offset + deinterlace_half_duration - TWOX_TOLERANCE on the canvas clock. These rules decide that offset and half duration, and when an unbuffered source moves on to its next frame. #395 fixed them for real users (upstream issues 3402, where 50i into a 60 fps canvas dropped about a quarter of the fields, and 6061, the field order). Each rule #395 and 3ea98b8 (the unbuffered hold and "don't skip ahead") added is pinned in the characterization and checked by a mutant.

Six commits:

  1. Move (behaviour-neutral): the decisions go to libobs/obs-source-deinterlace-pacing.c.h as six pure functions on plain integers. They are declared in the new obs-source-deinterlace-pacing.h without EXPORT, so they are hidden:

    • obs_source_deinterlace_pacing_hold(): an unbuffered source keeps its frame while the frame's end is ahead of the clock by less than TS_JUMP_THRESHOLD;
    • obs_source_deinterlace_pacing_wait_for_next() and _waiting_frame_ts(): the "don't skip ahead" guard of ready_deinterlace_frames(), 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(): a buffered source's offset, from the source clock that picks frames (libobs: Show every field with 2x deinterlacing at another canvas rate #395);
    • obs_source_deinterlace_pacing_reanchor(): an unbuffered source's offset, kept unless the frame comes early or misses its tick and its first field (libobs: Show every field with 2x deinterlacing at another canvas rate #395).

    obs-source-deinterlace.c.h keeps everything else: the async_unbuffered && deinterlace_offset tests in front of the hold and the guard, the async_frames queue, the buffered walk, first_frame(), deinterlace_process_last_frame(), the textures and the render. It reads the same fields and obs->video values and stores deinterlace_frame_ts, deinterlace_half_duration and deinterlace_offset in the same order. The thresholds are named in the header; FRAME_TOLERANCE (the buffered walk) and TWOX_TOLERANCE (the render) stay in obs-source-deinterlace.c.h and are checked against it with _Static_assert. The file stays in libobs.core-source for this commit.

  2. Characterization: test_deinterlace_pacing_steps, 20 cases, compiles the moved file. It covers:

    • every branch of the re-anchor (no offset, early, late by a field or a tick, kept), for 50i into 60 fps (the field is longer than the tick) and into 30 fps (the tick is longer);
    • the hold at TS_JUMP_THRESHOLD - 1, at it and + 1, and at the frame's end;
    • the guard and its timestamp, the half duration and the buffered offset;
    • the wraps (below), and unbuffered sequences of 100 frames at 25, 29.97, 50 and 59.94 frames a second into 30 and 60 fps through a model of the caller, on exact rational clocks.

    It is in the core suite of the test graph, so no pinned suite count changes.

  3. RED: Tier 1 in the crate against todo!(). 30 of 30 fail.

  4. GREEN: the safe core and #[no_mangle] shims, in a new libobs.ported-source-deinterlace-pacing group with its own PCH (only the new header). The export lists, source-sets.json, the unity lists and ci/test_ported_sources.py are updated, and a _rust variant of the steps test is added (see below).

  5. Tier 3: a C oracle and byte-level parity, with the mutation check.

  6. Docs: the README rows, the crate's description and lib.rs, and a port_comparison bench file. docs/rust-port/naming.md already maps libobs/obs-source*.c.h to rust/obs-source.

Every sum and difference is wrapping_* where the C's uint64_t wraps. The re-anchor's lateness is cast_signed() of the wrapped difference, the C's (int64_t)(offset - deinterlace_offset). The half duration is the low 32 bits, the C's (uint32_t). The crate still depends on no other crate.

  • What changed in the C. The re-anchor now stores the offset it returns on every unbuffered pick. When it keeps the offset, that is the value already there. TWOX_TOLERANCE in the header is 1000000ULL; every use already converted it to uint64_t. TS_JUMP_THRESHOLD was used only by the hold, so it moves to the header.

  • Kept, characterized, not endorsed:

    • half a duration of 2^32 ns or more is truncated (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, and 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 (see below);
    • the buffered and re-anchored offsets wrap when the frame's clock is ahead of the canvas, and a lateness of 2^63 or more counts as early;
    • at 29.97 frames a second into 30 and 60 fps, and 59.94 into 60, the canvas ticks a little faster than the frames come. A tick then lands within TWOX_TOLERANCE before a frame's time, the frame counts as early, and the offset re-anchors on every frame, 33 or 17 µs back each time. The offset follows the canvas. This is the rule working as written: no field is skipped and none is shown early.
  • No intentional differences. Every input is a value, so there is no pointer to check.

  • The _rust variant links. test_deinterlace_pacing_steps_rust builds the unchanged test source against libobs_rust-static, as test_source_audio_timing_steps_rust does. The test includes only the new header and calls only the six functions. It passes 20 of 20 on the hybrid tree.

  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, with identical sets. The six functions are local (t) in all three. In the hybrid they come from liblibobs_rust.a (the group's object is not on the hybrid link line), and the ported group is compiled only into libobs-c-reference.

  • Performance (port_comparison, x86_64, GCC, not pinned; median per 1024 ticks):

    • unbuffered_tick (hold, guard, half duration, re-anchor): 8.67 µs Rust, 7.59 µs C (1.14x);
    • buffered_pick (half duration, buffered offset): 3.20 µs Rust, 3.13 µs C (1.02x).

    That is about 1 ns more per tick, for one tick of each deinterlaced source per canvas frame.

  • No user-visible change, so no docs/features.md line.

Observed, not changed (upstream behaviour; the first is in the moved code, the second is outside it). Neither is fixed here: both fixes would change behaviour, and each can be its own issue.

  1. The first frame's half duration. When a source's first frame (or the first after last_frame_ts is reset by obs_source_output_video(NULL) or a full queue) is picked with no second frame queued, first_frame() cannot mark a pair, so the half duration is (uint32_t)((ts - deinterlace_frame_ts) / 2). For a source that never showed a frame, deinterlace_frame_ts is 0. Example: a capture card's frame stamped 1000 s after boot (os_gettime_ns()) gives 1e12 / 2 mod 2^32 = 1.78 s.

    • Buffered (the default): the offset is video_time - ts + 3 ms, so the second field comes due 1.78 s + 2 ms after the pick. The next frame replaces the first 40 ms later (50i), so the first frame's second field is never shown. That happens unless the truncation leaves less than about a frame, so nearly always.
    • Unbuffered: the first pick re-anchors (the offset is 0), so the frame ends 2 × 1.78 s - 1 ms after the pick. That is past TS_JUMP_THRESHOLD, so the next tick moves on as if the clock had jumped, and the second field is skipped the same way. When the truncation leaves 0.5 to 35.5 ms (about 0.8 % of start times), the first frame is held too long. Example: a timestamp that leaves 30 ms holds it to 59 ms after the pick, where a 50i frame ends at 39 ms, one 60 fps tick more.
    • After a pause, deinterlace_frame_ts is the last frame shown, so a 10 s pause gives 705 ms, with the same effect.

    The cost is one field (20 ms at 50i) at each start, restart or queue overflow. The next frame's half duration is right again, so it does not carry on. test_deinterlace_2x_field_order leaves the first two frames out of its counts for this reason. half_duration_first_frame and first_frame_half_duration_hold pin it.

  2. A stale prev_frame flag in the buffered walk (ready_deinterlace_frames(), not moved). When the walk erases prev_frame (da_erase and remove_async_frame()) and then finds two or fewer frames left, it sets prev_frame->prev_frame = true on the frame it just removed. This was likely meant for frame, the new front. Two effects:

    • The front frame is shown alone, not paired.
    • remove_async_frame() only returns the frame to async_cache, and copy_frame_data() does not reset prev_frame. So the next incoming frame that reuses that cache entry is queued with prev_frame already true. When it is picked with another frame behind it, deinterlace_get_closest_frames() takes it as the previous frame of a pair and shows the next frame instead: one frame early, and the due frame's fields are skipped.

    This happens when a buffered source falls three frames behind, for example after a stall. It is memory-safe: the write happens under async_mutex, to a frame that stays allocated in the cache. This comes from reading the code, not from a reproduction. The Tier 3 model mirrors the C here (the write goes to the erased frame).

RED → GREEN

  • RED commit: 6ed2b2253. soldr cargo test -p obs-data --test obs-tests-core source::deinterlace_pacing fails 30 of 30, every failure "not yet implemented": the 20 ported steps_* cases and 10 edge_* cases, four of them proptests.
  • GREEN: b51e2a419. Tier 1 passes 30/30. Tier 2 ran under a private Xvfb -noreset with dbus-run-session, a fresh HOME and ulimit -c 0, with no TMPDIR set:
Test C backend hybrid libobs-c-reference
test_deinterlace_2x_field_order 40/40 40/40 40/40
test_deinterlace_pacing_steps 20/20 20/20 20/20
test_deinterlace_pacing_steps_rust (not built: needs the Rust archive) 20/20 20/20
full ctest 395/395 399/399 (not run)

These ran on e00f1e328: the branch on 20839ada6 (#1929), with the same tree as the head before the last rebase (41ee56071, on af0273e37) except for one obs-scene Rust test from #1952. No ctest test was skipped. Inside the tests, three cases skip on each lane, as on master: open_crashes_without_obs_core in test_audio_io and test_video_io, and test_hex_float_values_ignore_comma_locale, which needs a comma-decimal locale this host does not have. No PipeWire or PulseAudio case skipped. No rerun was needed: test_deinterlace_2x_field_order passed every time it ran, 4 runs on each lane. Besides the run above, it also passed on all three builds at commit 2 (c922ff3ea, before the rebases), at commit 4 (5e891bbb3, before the rebases) and on 07683bc85 (on dd6a5597e). The full ctest also passed on 07683bc85 (394/394 and 398/398, 0 ctest skips). Each c-ref run checked that the loader used libobs-c-reference/libobs.so.30, which was rebuilt (ninja libobs-c-reference) before every c-ref run.

  • Tier 3, c8e2030c8 (source::deinterlace_pacing_parity, 11 tests). Each run compares the bytes of the two results: the C's bool as the byte it returns, and the integers as their bytes. Runs:

    • the three thresholds against the C header;
    • every combination of the special values of the u64 range (0, 1, the thresholds ±1, 2^32, 2^63 ±1, the end of the range) and ten half durations: 138,240 holds, 331,776 guards, 576 half durations and buffered offsets, and 3,317,760 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 (20,000 cases each);
    • 3,000 random sequences of 2 to 300 canvas ticks through a model of deinterlace_get_closest_frames(), ready_deinterlace_frames() (the unbuffered trim and guard, the buffered walk with frame_out_of_bounds()) and first_frame(), with the stores in the C's order, run once with the C functions and once with the shims. The sequences cover 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 and ticks, timestamps that jump, go backwards or land anywhere, queue resets (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 2,000 sequences reach every branch of every function more than 100 times (the fewest: 948 re-anchors with no offset), and every rate pair.

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

  • Mutation check (once, not committed; the two test modules built into a scratch test binary): 51 of 53 mutants fail, and the 2 that survive are equivalent.

Mutant Tier 1 (of 30) Tier 3 (of 11)
hold: before the end at <= 7 2
hold: jump threshold at <= 5 2
hold: no jump threshold 7 4
hold: no "before the end" test 7 2
hold: one half duration, not two 9 4
hold: no TWOX_TOLERANCE 9 4
hold: half duration doubled in 32 bits 4 4
hold: saturating frame_ts + offset / + 2 * half / - TWOX_TOLERANCE 4 / 3 / 4 4 / 2 / 2
guard: at <= 4 4
guard: no duration / no offset 4 / 4 4 / 4
guard: saturating duration / + offset / + duration 3 / 3 / 3 4 / 4 / 4
waiting timestamp: a duration ahead / unchanged 4 / 4 4 / 4
waiting timestamp: saturating duration / ts - duration 3 / 3 4 / 4
half duration: not halved / rounded up 10 / 5 4 / 4
half duration: saturating difference 3 4
half duration: saturated to UINT32_MAX / the high 32 bits 6 / 9 4 / 4
buffered offset: no FRAME_TOLERANCE / no TWOX_TOLERANCE / reversed 4 / 4 / 4 4 / 4 / 4
buffered offset: saturating difference / + FRAME_TOLERANCE / + TWOX_TOLERANCE 3 / 3 / 3 4 / 2 / 2
re-anchor: no "no offset yet" rule 3 2
re-anchor: early frames kept (#395's early rule reverted) 6 4
re-anchor: early only past TWOX_TOLERANCE 6 4
re-anchor: late frames kept (#395's late rule reverted) 7 4
re-anchor: late at > 4 1
re-anchor: no TWOX_TOLERANCE in the late rule 6 4
re-anchor: the smaller of field and tick / the field only / the tick only 7 / 6 / 5 4 / 4 / 4
re-anchor: at every pick (before #395) 9 4
re-anchor: saturating new offset 3 4
re-anchor: saturating lateness before the (int64_t) 7 4
re-anchor: lateness clamped instead of cast / not modular 2 / 4 2 / 2
shim: hold result negated; guard, waiting timestamp, half duration, buffered offset or re-anchor arguments swapped (6 mutants) 0 (Tier 1 does not reach the shims) 4 each
equivalent: saturating end - sys_time (computed only when sys_time is before the end) 0 0
equivalent: saturating late + TWOX_TOLERANCE (reached only for a lateness of at most INT64_MAX) 0 0
  • Real app: no UI, protocol or logging change.

Other runs:

  • uv run ci/local_gate.py --test-diff origin/master (the CI changed-crates step) on the head 5839d8614: 2877/2877 passed, 1 skipped (an existing ignored test). The disk on the build host was under soldr's 5 GiB guard, so this ran with SOLDR_TARGET_BLOCK_FREE_GB=3. An earlier run, without the override, passed every test and then stopped at the doc-test step because of the disk guard.
  • uv run ci/local_gate.py --lane lint: ok. --lane cross: ok. Both ran on e00f1e328, and before that on 07683bc85 and 39d231d0d. cargo clippy -p obs-source -p obs-c-oracle -p obs-data --all-targets -D warnings is clean.
  • ci/test_build_groups.py, ci/test_ported_sources.py and ci/test_oracle_standins.py: OK on the head.
  • Exported symbols: 1766 on all three builds, identical sets, checked on e00f1e328.
  • soldr cargo test -p obs-data --test obs-tests-core source::: 86/86 (45 audio_timing and 30 + 11 deinterlace_pacing).
  • Rebased four times, onto dd6a5597e, 20839ada6, af0273e37 and 773faa051 (obs-scene: Port the scene item crop arithmetic #1947). The last one conflicted in the bench list and the rust/README.md rows; after it, ci/test_build_groups.py, ci/test_ported_sources.py, uv run ci/local_gate.py --test-diff (2902/2902, 1 skipped) and the lint lane were re-run and pass. Only the port_comparison bench list conflicted (libobs: Port the source registration rules to obs-core #1929 added module_source_rules, and both entries are kept). After each rebase, source-sets.json, test-graph.json, the unity lists and the three export lists were compared by hand against master: only this PR's lines differ.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • 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 (N/A for docs-only; a focused CI fix may push RED and GREEN together)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite (the lint and cross lanes passed locally; nothing is stamped)
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (none)

@rdoupe
rdoupe requested a review from zackees as a code owner October 23, 2026 17:30
…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
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
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
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
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
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
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.

rust: Port the 2x deinterlace frame pacing in obs-source-deinterlace.c to obs-source

2 participants