Skip to content

libobs: Show both fields of the first 2x deinterlaced frame - #1995

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/deinterlace-first-frame
Oct 23, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/deinterlace-first-frame

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1965

What and why

With a double-rate deinterlace mode (Yadif 2x, Linear 2x, Discard 2x), the first frame a source shows lost its second field, or, less often, was held too long. The same happened to the first frame after a restart, a full queue or a pause. Now the first field pair at each start is shown correctly: the first field at the tick that picks the frame, the second at the next tick, before the next frame.

The cause is in libobs/obs-source-deinterlace-pacing.c.h (moved and ported to rust/obs-source deinterlace_pacing by #1954; upstream has the same code). When the first frame is picked with no second frame queued, its half duration was half the gap to deinterlace_frame_ts:

  • for a source that never showed a frame, deinterlace_frame_ts is 0, so the half duration was half the frame's own timestamp, truncated to 32 bits: 1.78 s for a capture timestamp 1000 s after boot;
  • after a pause, it is the last frame shown: a 10 s pause gave 705 ms.

A buffered source (the default) then showed the second field only after the next frame had replaced the first. An unbuffered one held the frame past TS_JUMP_THRESHOLD, so it moved on as if the clock had jumped, without the second field. In about 0.8 % of starts the truncation left 0.5 to 35.5 ms instead, and the frame was held too long.

The fix. obs_source_deinterlace_pacing_half_duration() takes two more arguments, the half duration before and the canvas interval (obs->video.video_frame_interval_ns). It no longer takes a half duration from a gap that is no frame duration:

  • no previous timestamp (prev_ts 0);
  • a timestamp at or before the previous one (it went backwards, or repeated);
  • a gap of more than twice TS_JUMP_THRESHOLD (140 ms), which would give a half duration past the threshold: a pause or a new clock.

Then it keeps the half duration before, if that is set and at most TS_JUMP_THRESHOLD (the same limit), and otherwise takes half the canvas interval. The gap limit is twice the threshold so that a regular stream below 14.3 frames a second (frames more than 70 ms apart) keeps timing each frame from the one before, exactly as before; with a 70 ms limit, a 12.5 fps stream fell back on every frame and changed every later state. A new source therefore shows its second field at the next tick, and a source after a restart or a pause keeps the field duration it had. Both call sites in deinterlace_get_closest_frames() pass the same values, so the pair path follows the same rule. The C, the Rust port and the C oracle (which compiles the libobs file itself) change in the same way. Every rule from #395 and from the unbuffered hold (3ea98b8: the hold, its jump threshold, "don't skip ahead", the buffered offset, the re-anchor) is unchanged, and each is still pinned by its characterization case. Nothing else in obs-source-deinterlace.c.h changes.

Two commits:

  1. Tests (Refs #1965). The two new arguments are added to the signature, unused, so that the tests can state the rule. The C, the Rust port and the oracle still behave as before.
    • test_deinterlace_pacing_steps (and its _rust variant) and its Tier 1 port in rust/obs-source: the half duration cases, seq_unbuffered_first_frame, which runs the unbuffered model from a source that never showed a frame, and seq_unbuffered_12_5, which pins a 12.5 fps stream (80 ms gaps) to its half durations from the gap. That case passes before and after the fix, by design.
    • Tier 1's edge model and its proptests take the new rule.
    • test_deinterlace_2x_field_order gets 15 cases that count the first frame's fields from field 0, with Linear 2x at the five rate pairs:
      • first_frame_*: a source that never showed a frame, buffered and unbuffered;
      • after_pause_*: an unbuffered source that pauses for 150 ms.
  2. Fix (Fixes #1965): the rule in C and Rust. Tier 3 gets the cadence evidence (below) and counts the fallbacks, where it used to count the truncation, which can no longer happen. Also the docs/features.md line, and the README row and module docs, which drop the characterized first-frame and truncation behaviour.

Test expectation change (maintainer sign-off): this changes what the characterization pins. In test_deinterlace_pacing_steps and its Tier 1 port:

  • half_duration_first_frame and first_frame_half_duration_hold now expect the fixed values, where they used to expect 1.78 s, a 3.57 s hold and the 59 ms hold;
  • half_duration_over_2_32_truncated and half_duration_timestamp_backwards_wraps become half_duration_after_a_pause and half_duration_timestamp_backwards: a 10 s pause or a frame 40 ms back now keeps the half duration before, where it used to give 705 ms or 2^32 - 20 ms;
  • half_duration_of_a_frame loses its 8.6 s case, now past the limit, and gains 12.5 fps (80 ms gives 40 ms) and the limit itself (140 ms gives 70 ms);
  • half_duration_fallback is new.

No case outside the first frame changes: seq_unbuffered_25_and_50 and seq_unbuffered_ntsc keep their expectations. The 40 existing test_deinterlace_2x_field_order cases are unchanged. They still leave out their first two frames, because their first measured frame comes back to back with the last warm-up frame, so it has no frame duration either way.

Cadence evidence (Tier 3 first_frame_cadences, through #1954's model of the caller: the queue, the buffered walk, first_frame() and the stores in the C's order). It covers 25, 29.97, 50 and 59.94 frames a second into 30 and 60 fps canvases, buffered and unbuffered. Each pair runs five starts: a new source whose first timestamp is 1000 s after boot, one at 5 s, and one that left 30 ms (the held-too-long case), then a 500 ms pause with and without a queue reset. That is 80 runs. Each run is compared with two references: the half duration of before, and an "ideal" one that knows the source's field duration.

  • The C oracle and the Rust shims agree, call for call and tick for tick.
  • The measured frame shows, at its pick, the field the ideal run shows: for a new source, its first field.
    • It shows its second field before the next frame whenever the ideal run does, and never later than the ideal run.
    • The second field is now shown in 40 of the 80 runs where it was skipped before.
    • At 25 fps into 60 fps the second field comes one tick earlier than in the ideal run, at the next tick. One frame cannot tell the source's rate.
  • From the next pick on, every state (the queue, last_frame_ts, the offset, the half duration, the frame timestamps) is byte-equal to before in all 64 runs where the old code did not hold the first frame too long. In the other 16 runs the old code picked the next frame later, which is the bug, and the fix picks it when the ideal run does.
  • Where the ideal run shows only one field of the first frame, the fix does the same. This is the case when the field rate is above the canvas rate (50 and 59.94 frames a second into 30 fps), or when a buffered source catches up through its queue after a pause. Every later frame of those runs shows one field as well.
  • Not fixed: an unbuffered new source at 25 into 30 fps or 50 into 60 fps, whose next frame is already waiting at the next tick, still moves straight on without the second field, as before (6 of the 80 runs, all of them for a new source). Showing that field means holding the frame one more tick, and an unbuffered source keeps its offset, so its anchor would move one tick later for the rest of the stream. In the model, a fallback of frame_interval_ns - FRAME_TOLERANCE shows those 6 fields. But at 29.97 into 30 and 59.94 into 60 it then changes every later state: about a tick of extra latency for the whole stream. This PR keeps the latency.

12.5 fps (Tier 3 low_rate_cadences): 12.5 frames a second (25 fields, 80 ms apart) into 30 and 60 fps, buffered and unbuffered, from the same five starts, 20 runs. The C and the shims agree. After a pause, every state from the stream's second frame up to the pause is byte-equal to before. From the measured frame's next pick on, every state is byte-equal to before (all 16 runs without the held-too-long start), and a new source shows its first field at its pick. With the 70 ms limit this test fails.

The steps test's seq_unbuffered_first_frame checks the same thing for the unbuffered model at the eight rate pairs and at 12.5 fps into 30 and 60:

  • the first field shows at the pick and the second at the next tick, before the next frame, whenever a frame lasts longer than a tick;
  • every pick from the second frame on (its tick, timestamp, offset and half duration) equals that of a source whose first frame was timed from a frame before it.

Observed, not changed:

  • A buffered source after a pause catches up through its queue, and its walk can skip the due frame. I tried giving every existing test_deinterlace_2x_field_order case a 150 ms pause before its measured frames. Buffered Yadif 2x at 60i into 60 fps then showed 0 1 2 5 5 5 6 ...: frame 1's fields were skipped and field 5 was held for three frames. It did the same before and after this fix. This looks like the stale prev_frame flag in the buffered walk that obs-source: Port the 2x deinterlace frame pacing #1954 described (its "Observed, not changed" item 2). So this PR adds pause cases for unbuffered sources only, and that one can be its own issue.
  • Sources whose frames are more than 140 ms apart (under 7.1 frames a second) take the fallback on every frame. No interlaced format runs that slowly.
  • A source that stamps its first frame 0 (a clock that starts at 0) now gets its second frame's half duration from the fallback, because a previous timestamp of 0 counts as none.
  • Yadif 2x on a new source's first frame. Yadif has no previous frame to interpolate from, so its first frame is classified oddly, as before. The first-frame cases therefore use Linear 2x, which shows the pacing directly: a new buffered source at 60i into 60 fps shows 0 1 1 1 2 3 .... Its first frame is held for one or two extra ticks, because the buffered clock starts from it; the next frame comes when it did before.

RED → GREEN

  • RED commit: 7bb85f397. On all three builds (C backend, hybrid, libobs-c-reference):

    • test_deinterlace_pacing_steps and _rust fail 7 of 23. The 12.5 fps case seq_unbuffered_12_5 passes, as it should: that behaviour does not change;
    • Tier 1 (soldr cargo test -p obs-data --test obs-tests-core source::deinterlace_pacing) fails 9 of 33;
    • test_deinterlace_2x_field_order fails 10 of 55. These are the three new cases at each of 60i into 60, 50i into 50 and 50i into 60 fps, and the buffered first-frame case at 50i into 30 fps. Every one fails on its first frame: Linear 2x at 60i into 60 fps shows 0 2 3 4 ..., with field 1 skipped. The 40 existing cases pass.

    test_deinterlace_2x_field_order is unchanged since the RED run, which was on 59577ac08 (before the gap-limit change and the rebases). The steps and Tier 1 counts are from the RED code with the current tests. Against the RED code, Tier 3's first_frame_cadences fails at its first run (25 into 30 fps, buffered, new source: the second field is not shown), and low_rate_cadences passes.

  • GREEN: f1c31cd0e. Steps tests 23/23 and Tier 1 33/33. Tier 3 13/13, including both cadence tests and sequences_reach_every_branch, which reaches each fallback more than 100 times. Tier 2 results are in the table below.

  • Real app: no UI, protocol or logging change. The change is in the fields shown, which test_deinterlace_2x_field_order checks through the real libobs.

Tier 2 ran under a private Xvfb -noreset with dbus-run-session, a fresh HOME, ulimit -c 0, no audio server and no TMPDIR set. Before every c-ref run, libobs-c-reference was rebuilt with ninja libobs-c-reference, and the run checked that the loader used libobs-c-reference/libobs.so.30 and printed its mtime.

Test C backend hybrid libobs-c-reference
test_deinterlace_pacing_steps 23/23 23/23 23/23
test_deinterlace_pacing_steps_rust (not built: needs the Rust archive) 23/23 23/23
test_deinterlace_2x_field_order (55 cases), final rule 4/4 runs 4/4 runs 4/4 runs
test_deinterlace_2x_field_order, first version (70 ms gap limit) 10/10 runs 10/10 runs 10/10 runs
full ctest, first version 396/396 401/401 (not run)
  • Which commit each run was on:
    • With the final rule: the steps tests and 3 field-order runs per build ran on 23eec77a3 (on 01843e7c5). After the rebase onto c1351a665, the steps tests and 1 more field-order run per build ran at the head f1c31cd0e.
    • The 10 field-order runs per build and the full ctest ran on the first version, a1a6cbee9, whose gap limit was TS_JUMP_THRESHOLD. On the field-order test the two limits behave the same: its frames are 33 to 40 ms apart, and its pause of 150 ms is past both limits.
  • No field-order run failed or needed a rerun (test_deinterlace_2x_field_order is timing-sensitive (yadif 2x 60i into 60): startup underrun and load-induced hold #456). In the full ctest no 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.
  • Tier 3 fallback counts in sequences_reach_every_branch, over 2,000 sequences:
    • no previous timestamp 1,765;
    • backwards 14,585;
    • past the limit 6,542;
    • the half duration kept 21,261;
    • half the interval 1,631.
  • Mutation check (not committed). These are mutants of the Rust core, run against Tier 1 and Tier 3:
    • First version, 16 mutants of the rule: Tier 1 catches every one, failing 3 to 10 of its tests, and Tier 3 catches 15.
    • Final rule, 6 mutants of the doubled gap limit:
Mutant of the gap limit Tier 1 (of 33) Tier 3 (of 13)
TS_JUMP_THRESHOLD (70 ms, the first version) 4 4 (low_rate_cadences among them)
at < 1 2
one more ns 1 0
3 × TS_JUMP_THRESHOLD 2 2
no limit 5 6
the kept half's limit doubled as well 3 1

Other runs:

  • uv run ci/local_gate.py --test-diff origin/master: 2940/2940 passed, 1 skipped (an existing ignored test), at the head f1c31cd0e. It also passed on 23eec77a3 and on the first version.
  • uv run ci/local_gate.py --lane lint: ok at the head. --lane cross: ok on 23eec77a3. The only change since then is the rebase onto three frontend/CI commits.
  • Exported symbols: 1766 on the C backend, the hybrid and libobs-c-reference, with identical sets, at the head. obs_source_deinterlace_pacing_half_duration is local (t) in all three.
  • Rebased onto c1351a665, then onto d33f07fb4 right before posting (no conflict), then onto 4ec76eda9 after obs-source: Port the source colour space query routing #1990 (the rust/README.md rows conflicted); after that source:: (115/115) and the lint lane were re-run and pass. After each rebase, the libobs/build unity lists, source-sets.json, test-graph.json and the export lists were compared by hand against master: this PR touches none of them.

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 local gate and the lint and cross lanes passed; 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: the C and the port change alike)

@rdoupe
rdoupe requested a review from zackees as a code owner October 23, 2026 22:45
@rdoupe
rdoupe force-pushed the fix/deinterlace-first-frame branch from 86b3b6a to bf32f98 Compare October 23, 2026 22:45
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
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
rdoupe force-pushed the fix/deinterlace-first-frame branch from bf32f98 to f1c31cd Compare October 23, 2026 23:08
@zackees
zackees added this pull request to the merge queue Oct 23, 2026
Merged via the queue into obs-rust:master with commit 4a57322 Oct 23, 2026
31 checks passed
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 the first frame's second field after every start

2 participants